From 76f8611ee3345268e2091a9d2e81b53a8b05889c Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Wed, 13 Oct 2021 23:22:16 +0200 Subject: ebus parser in rust --- ebus-rust/src/model/mod.rs | 126 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 ebus-rust/src/model/mod.rs (limited to 'ebus-rust/src/model/mod.rs') diff --git a/ebus-rust/src/model/mod.rs b/ebus-rust/src/model/mod.rs new file mode 100644 index 0000000..fd738fa --- /dev/null +++ b/ebus-rust/src/model/mod.rs @@ -0,0 +1,126 @@ +use serde::Deserialize; + +use quick_xml::de::{from_reader}; + +pub fn read_config(xml: &[u8]) -> Result { + let ebus_configuration = from_reader(xml).map_err(|e| format!("failed to read xml: {}", e))?; + Ok(ebus_configuration) +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct Description { + pub lang: String, + #[serde(rename = "$value")] + pub text: String, +} + +#[derive(Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum DeviceType { + Master, + Slave, + Broadcast, +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct Device { + pub address: u8, + #[serde(rename = "type", default)] + pub device_type: Option, + pub name: String, + #[serde(rename = "description", default)] + pub descriptions: Vec, +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct Devices { + #[serde(rename = "device", default)] + pub devices: Vec, +} + +#[derive(Debug, Deserialize, PartialEq, Clone)] +pub struct ByteEnumOption { + pub value: u8, + pub name: String, +} + +#[derive(Debug, Deserialize, PartialEq, Clone)] +pub struct ByteEnumList(pub Vec); + +impl ByteEnumList { + pub fn get(&self, value: u8) -> Option { + self.0 + .iter() + .find(|v| v.value == value) + .map(|v| v.name.clone()) + } +} + +#[derive(Debug, Deserialize, PartialEq, Clone)] +pub enum PacketField { + #[serde(rename = "byte")] + Byte { offset: u8, name: String }, + #[serde(rename = "data1b")] + Data1b { offset: u8, name: String }, + #[serde(rename = "data1c")] + Data1c { offset: u8, name: String }, + #[serde(rename = "data2b")] + Data2b { offset: u8, name: String }, + #[serde(rename = "data2c")] + Data2c { offset: u8, name: String }, + #[serde(rename = "byteEnum")] + ByteEnum { + offset: u8, + name: String, + #[serde(rename = "option")] + options: ByteEnumList, + }, + #[serde(rename = "bcd")] + Bcd { offset: u8, name: String }, + #[serde(rename = "bit")] + Bit { offset: u8, name: String }, + #[serde(rename = "word")] + Word { offset: u8, name: String }, + #[serde(rename = "string")] + String { + offset: u8, + name: String, + length: u8, + }, +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct PacketFields { + #[serde(rename = "$value")] + pub fields: Option>, +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct Packet { + pub primary: u8, + pub secondary: u8, + pub name: String, + #[serde(rename = "description", default)] + pub descriptions: Vec, + pub fields: PacketFields, +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct Packets { + #[serde(rename = "packet", default)] + pub packets: Vec, +} + +impl Packets { + pub fn get(&self, primary: u8, secondary: u8) -> Option<&Packet> { + self.packets + .iter() + .find(|p| p.primary == primary && p.secondary == secondary) + } +} + +#[derive(Debug, Deserialize, PartialEq)] +pub struct Ebus { + pub devices: Devices, + pub packets: Packets, +} -- cgit v1.2.1