ps_ecc/long/header/methods/
to_bytes.rs1use crate::long::LongEccHeader;
2
3impl LongEccHeader {
4 #[must_use]
6 pub fn to_bytes(self) -> [u8; 32] {
7 let mut bytes = [0; 32];
8
9 bytes[0..2].copy_from_slice(&self.magic.to_be_bytes());
10 bytes[2] = self.version;
11 bytes[3] = self.parity;
12 bytes[4..8].copy_from_slice(&self.full_length.to_be_bytes());
13 bytes[8..12].copy_from_slice(&self.message_length.to_be_bytes());
14 bytes[12..16].copy_from_slice(&self.header_checksum.to_be_bytes());
15 bytes[16..24].copy_from_slice(&self.checksum.to_be_bytes());
16 bytes[24..32].copy_from_slice(&self.header_parity);
17
18 bytes
19 }
20}
21
22#[cfg(test)]
23mod tests {
24 use crate::long::{LongEccHeader, OverlapFactor, LONG_ECC_HEADER_MAGIC};
25
26 type TestError = Box<dyn std::error::Error>;
27
28 #[test]
29 fn test_to_bytes_layout() -> Result<(), TestError> {
30 let checksum = 0x0123_4567_89AB_CDEF;
31 let header = LongEccHeader::new(3, OverlapFactor::Double, 138, 100, checksum)?;
32
33 let bytes = header.to_bytes();
34
35 assert_eq!(bytes[0..2], LONG_ECC_HEADER_MAGIC.to_be_bytes());
36 assert_eq!(bytes[2], 1);
37 assert_eq!(bytes[3], 3 | OverlapFactor::Double.to_u8());
38 assert_eq!(bytes[4..8], 138u32.to_be_bytes());
39 assert_eq!(bytes[8..12], 100u32.to_be_bytes());
40 assert_eq!(bytes[12..16], header.header_checksum().to_be_bytes());
41 assert_eq!(bytes[16..24], checksum.to_be_bytes());
42 assert_eq!(bytes[24..32], header.header_parity());
43
44 Ok(())
45 }
46
47 #[test]
48 fn test_to_bytes_from_bytes_roundtrip() -> Result<(), TestError> {
49 let header = LongEccHeader::new(5, OverlapFactor::Quadruple, 482, 400, 42)?;
50
51 let reparsed = LongEccHeader::from_bytes(header.to_bytes())?;
52
53 assert_eq!(reparsed, header);
54
55 Ok(())
56 }
57}