mirror of
https://codeberg.org/icewind/taspromto.git
synced 2026-08-02 12:14:45 +02:00
restructure and error logging
This commit is contained in:
parent
a4ff434120
commit
7b8786919c
7 changed files with 527 additions and 217 deletions
63
src/topic.rs
Normal file
63
src/topic.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use crate::device::Device;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Topic {
|
||||
LWT(Device),
|
||||
Power(Device),
|
||||
State(Device),
|
||||
Sensor(Device),
|
||||
Result(Device),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl From<&str> for Topic {
|
||||
fn from(raw: &str) -> Self {
|
||||
let mut parts = raw.split('/');
|
||||
if let (Some(prefix), Some(topic), Some(hostname), Some(cmd)) =
|
||||
(parts.next(), parts.next(), parts.next(), parts.next())
|
||||
{
|
||||
let device = Device {
|
||||
topic: topic.to_string(),
|
||||
hostname: hostname.to_string(),
|
||||
};
|
||||
match (prefix, cmd) {
|
||||
("tele", "LWT") => Topic::LWT(device),
|
||||
("tele", "STATE") => Topic::State(device),
|
||||
("stat", "POWER") => Topic::Power(device),
|
||||
("tele", "SENSOR") => Topic::Sensor(device),
|
||||
("stat", "RESULT") => Topic::Result(device),
|
||||
_ => Topic::Other(raw.to_string()),
|
||||
}
|
||||
} else {
|
||||
Topic::Other(raw.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_topic() {
|
||||
let device = Device {
|
||||
hostname: "hostname".to_string(),
|
||||
topic: "foo".to_string(),
|
||||
};
|
||||
assert_eq!(
|
||||
Topic::LWT(device.clone()),
|
||||
Topic::from("tele/foo/hostname/LWT")
|
||||
);
|
||||
assert_eq!(
|
||||
Topic::Power(device.clone()),
|
||||
Topic::from("stat/foo/hostname/POWER")
|
||||
);
|
||||
assert_eq!(
|
||||
Topic::State(device.clone()),
|
||||
Topic::from("tele/foo/hostname/STATE")
|
||||
);
|
||||
assert_eq!(
|
||||
Topic::Sensor(device.clone()),
|
||||
Topic::from("tele/foo/hostname/SENSOR")
|
||||
);
|
||||
assert_eq!(
|
||||
Topic::Result(device.clone()),
|
||||
Topic::from("stat/foo/hostname/RESULT")
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue