aboutsummaryrefslogtreecommitdiffstats
path: root/src/ack.rs
blob: 0cbef5bf6477fa5c1e4395ef03d86addd6d457c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use bytes::{Buf, Bytes};

#[derive(PartialEq, Debug, Clone)]
pub struct Ack {
    checksum: u32,
}

impl From<Bytes> for Ack {
    fn from(value: Bytes) -> Self {
        let mut bytes = value;
        Ack {
            checksum: bytes.get_u32(),
        }
    }
}

impl core::fmt::Display for Ack {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("Checksum: {:4x?}", self.checksum))
    }
}

#[cfg(test)]
mod tests {
    use crate::{ack::Ack, packet::*, packet_content::PacketContent};
    use bytes::Bytes;
    use core::str::FromStr;
    use hex::decode;
    use tinyvec::array_vec;

    #[test]
    fn ack() {
        let sample = "0D0E78B5561B03CD70DA326066B8A0CF24F3214D";

        let lhs_packet = Packet {
            route_type: RouteType::Flood,
            version: PayloadVersion::VersionOne,
            path: array_vec!([u16; 64] => 0x78, 0xB5, 0x56, 0x1B, 0x03, 0xCD, 0x70, 0xDA, 0x32, 0x60, 0x66, 0xB8, 0xA0, 0xCF),
            transport: [0, 0],
            raw_content: Bytes::copy_from_slice(&decode("24F3214D").unwrap()),
            content: PacketContent::Ack(Ack {
                checksum: 0x24F3214D,
            }),
            incomplete: false,
        };

        let rhs_packet = Packet::from_str(sample).unwrap();
        assert_eq!(lhs_packet, rhs_packet);
    }

    #[test]
    fn display() {
        let ack = Ack {
            checksum: 0x24F3214D,
        };
        println!("{}", ack);
        assert_eq!(format!("{}", ack), "Checksum: 24f3214d");
    }
}