dis_rs/common/other/
writer.rs

1use crate::common::other::model::Other;
2use crate::common::{SerializePdu, SupportedVersion};
3use bytes::{BufMut, BytesMut};
4
5impl SerializePdu for Other {
6    /// Serializes the Other PDU into a buffer.
7    /// Assumes there is enough free space in the buffer and relies on the buffer's
8    /// behaviour for what happens if this is not the case (probably panics - `BytesMut` does)
9    fn serialize_pdu(&self, _version: SupportedVersion, buf: &mut BytesMut) -> u16 {
10        buf.put(self.body.as_slice());
11        self.body.len() as u16
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use crate::common::model::{Pdu, PduHeader};
18    use crate::enumerations::PduType;
19    use crate::other::model::Other;
20    use bytes::BytesMut;
21
22    #[test]
23    fn serialize_other_pdu() {
24        let header = PduHeader::new_v6(1, PduType::Other);
25        let body = Other::builder()
26            .with_body(vec![0x01, 0x02, 0x03])
27            .build()
28            .into_pdu_body();
29        let pdu = Pdu::finalize_from_parts(header, body, 10);
30        let pdu_length = pdu.header.pdu_length;
31
32        let mut buf = BytesMut::with_capacity(pdu_length as usize);
33
34        let wire_size = pdu.serialize(&mut buf).unwrap();
35        assert_eq!(wire_size, pdu_length);
36
37        let expected: [u8; 15] = [
38            0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0f, 0x00, 0x00, 0x01, 0x02,
39            0x03,
40        ];
41        assert_eq!(buf.as_ref(), expected.as_ref());
42    }
43}