diff --git a/domo_proto/src/lib.rs b/domo_proto/src/lib.rs index 574db87..f0d2756 100644 --- a/domo_proto/src/lib.rs +++ b/domo_proto/src/lib.rs @@ -1,3 +1,51 @@ pub mod commands; pub mod packet; pub mod prelude; + +#[cfg(test)] +mod tests { + use crate::packet; + use crate::packet::data_types::DataType; + use crate::packet::identifier::Identifier; + use crate::packet::packet_data::PacketData; + + #[test] + pub fn data_types() { + // test Vec -> Vec + assert_eq!( + DataType::get_data(vec![0x00, 0x01, 0x00]), + vec![ + DataType::Nothing, + DataType::Switch(false), + ]); + + // test DataType -> Vec + assert_eq!( + Into::>::into(DataType::Switch(true)), + vec![0x01, 0x01] + ) + } + + #[test] + pub fn packets() { + assert_eq!(packet::Packet::V1 { + src: Identifier::from(0xAABBCCDD), + dest: Identifier::from(0xAABBCCDD), + packet_id: Identifier::from(0x00000001), + reply_to: Identifier::from(0x00000000), + command: 0x00, + data: PacketData::default(), + }.build_base_packet(), + vec![ + 0x01, // version + 0xAA, 0xBB, 0xCC, 0xDD, // src + 0xAA, 0xBB, 0xCC, 0xDD, // dest + 0x00, 0x00, 0x00, 0x01, // packet_id + 0x00, 0x00, 0x00, 0x00, // reply_to + 0x00, // command + 0x00, 0x00, // data length + // no packet data + ] + ); + } +} diff --git a/domo_proto/src/main.rs b/domo_proto/src/main.rs deleted file mode 100644 index a426d16..0000000 --- a/domo_proto/src/main.rs +++ /dev/null @@ -1,21 +0,0 @@ -use domo_proto::packet::data_types::DataType; -use domo_proto::packet::identifier::Identifier; -use domo_proto::packet::packet_data::PacketData; - -fn main() { - let _ = domo_proto::packet::Packet::V1 { - src: Identifier::from(0xAABBCCDD), - dest: Identifier::from(0xAABBCCDD), - packet_id: Identifier::from(0x00000001), - reply_to: Identifier::from(0x00000000), - command: 0x00, - data: PacketData::default(), - }; - - let buf = vec![ - 0x00, 0x01, 0x00, 0x10, 0x00, 0xFF, 0x20, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0xF0, 0xAA, 0xBB, 0xCC, 0xDD, - ]; - - println!("{:?}", DataType::get_data(buf)); -} diff --git a/domo_proto/src/packet/data_types.rs b/domo_proto/src/packet/data_types.rs index 01ab825..6304f9d 100644 --- a/domo_proto/src/packet/data_types.rs +++ b/domo_proto/src/packet/data_types.rs @@ -1,6 +1,7 @@ +use crate::packet::identifier::Identifier; use crate::prelude::{as_u32_be, as_u64_be}; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum DataType { // Basic types Nothing, @@ -15,7 +16,7 @@ pub enum DataType { Seconds(u64), // Domo Types - NodeRef(u32), + NodeRef(Identifier), } impl DataType { @@ -91,8 +92,34 @@ impl From> for DataType { ), 0x10 => impl_data_type!(value, 3, DataType::RGB(value[1], value[2], value[3])), 0x90 => impl_data_type!(value, 8, DataType::Seconds(as_u64_be(&value[1..9]))), - 0xF0 => impl_data_type!(value, 4, DataType::NodeRef(as_u32_be(&value[1..5]))), + 0xF0 => impl_data_type!(value, 4, DataType::NodeRef(Identifier::from(&value[1..5]))), _ => DataType::Nothing, } } } + +impl Into> for DataType { + fn into(self) -> Vec { + match self { + DataType::Nothing => vec![0x00], + DataType::Switch(b) => vec![0x01, b as u8], + DataType::Slider(v) => vec![0x02, v], + DataType::Text(s) => { + let mut bytes = vec![0x03]; + bytes.extend(s.into_bytes()); + bytes + }, + DataType::RGB(r, g, b) => vec![0x10, r, g, b], + DataType::Seconds(s) => { + let mut bytes = vec![0x90]; + bytes.extend_from_slice(s.to_be_bytes().as_ref()); + bytes + }, + DataType::NodeRef(id) => { + let mut bytes = vec![0x90]; + bytes.extend_from_slice(id.bytes.as_ref()); + bytes + }, + } + } +} \ No newline at end of file