use bytes::{Buf, Bytes}; #[derive(PartialEq, Debug, Clone)] pub struct Ack { checksum: u32, } impl From 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"); } }