From c84d69c1643ba62c83ac36e21272fac6bba8d3d7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 10 Sep 2020 17:23:38 +0200 Subject: [PATCH] Expose message method and data field Signed-off-by: trivernis --- src/lib.rs | 3 +-- src/message.rs | 14 +++----------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f78a899..3b1eef3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,8 +16,7 @@ mod tests { fn it_deserializes_messages() { let bytes = vec![0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x10, 0x00, 0x00, 0x11, 0xFF, 0x00, 0x96, 0xA9, 0xB2, 0x2E]; let message = Message::from_bytes(&bytes); - let mut other_message = Message::new([0x01, 0x00, 0x10, 0x00], vec![0x00, 0x11, 0xFF, 0x00]); - other_message.crc = Some( 0x96A9B22E ); + let other_message = Message::new([0x01, 0x00, 0x10, 0x00], vec![0x00, 0x11, 0xFF, 0x00]); assert_eq!(message, Ok(other_message)) } diff --git a/src/message.rs b/src/message.rs index af2a80e..641d16a 100644 --- a/src/message.rs +++ b/src/message.rs @@ -12,20 +12,16 @@ pub enum DeserializeError { #[derive(Clone, Debug, PartialOrd, PartialEq)] pub struct Message { - length: u32, - method: [u8; 4], - data: Vec, - pub crc: Option, + pub method: [u8; 4], + pub data: Vec, } impl Message { /// Creates a new message pub fn new(method: [u8; 4], data: Vec) -> Self { Self { - length: (12 + data.len()) as u32, method, data, - crc: None, } } @@ -35,9 +31,7 @@ impl Message { data.serialize(&mut Serializer::new(&mut buf)).unwrap(); Self { method, - length: (12 + buf.len()) as u32, data: buf, - crc: None, } } @@ -65,10 +59,8 @@ impl Message { let data = bytes[8..(length as usize - 4)].to_vec(); Ok(Self { - length, method, data, - crc: Some(crc) }) } @@ -76,7 +68,7 @@ impl Message { pub fn to_bytes(&self) -> Vec { let mut data = Vec::new(); let mut length_raw = [0u8; 4]; - BigEndian::write_u32(&mut length_raw, self.length); + BigEndian::write_u32(&mut length_raw, self.data.len() as u32 + 12); data.append(&mut length_raw.to_vec()); data.append(&mut self.method.clone().to_vec()); data.append(&mut self.data.clone());