feat: tests and fixed some minor bugs

This commit is contained in:
Strix 2023-10-15 18:06:21 +02:00
parent 3b6b5d7548
commit 174a3739b5
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
3 changed files with 78 additions and 24 deletions

View file

@ -1,3 +1,51 @@
pub mod commands; pub mod commands;
pub mod packet; pub mod packet;
pub mod prelude; 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<u8> -> Vec<DataType>
assert_eq!(
DataType::get_data(vec![0x00, 0x01, 0x00]),
vec![
DataType::Nothing,
DataType::Switch(false),
]);
// test DataType -> Vec<u8>
assert_eq!(
Into::<Vec<u8>>::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
]
);
}
}

View file

@ -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));
}

View file

@ -1,6 +1,7 @@
use crate::packet::identifier::Identifier;
use crate::prelude::{as_u32_be, as_u64_be}; use crate::prelude::{as_u32_be, as_u64_be};
#[derive(Debug)] #[derive(Debug, Eq, PartialEq)]
pub enum DataType { pub enum DataType {
// Basic types // Basic types
Nothing, Nothing,
@ -15,7 +16,7 @@ pub enum DataType {
Seconds(u64), Seconds(u64),
// Domo Types // Domo Types
NodeRef(u32), NodeRef(Identifier),
} }
impl DataType { impl DataType {
@ -91,8 +92,34 @@ impl From<Vec<u8>> for DataType {
), ),
0x10 => impl_data_type!(value, 3, DataType::RGB(value[1], value[2], value[3])), 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]))), 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, _ => DataType::Nothing,
} }
} }
} }
impl Into<Vec<u8>> for DataType {
fn into(self) -> Vec<u8> {
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
},
}
}
}