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());