1use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct FtStat {
14 pub status: u8,
16 pub delay_ms: u16,
18 pub preferred_size: u16,
20 pub flags: u16,
22}
23
24impl FtStat {
25 pub fn encode(&self) -> Result<Vec<u8>, Error> {
27 let mut out = Vec::with_capacity(7);
28 out.push(self.status);
29 out.extend_from_slice(&self.delay_ms.to_le_bytes());
30 out.extend_from_slice(&self.preferred_size.to_le_bytes());
31 out.extend_from_slice(&self.flags.to_le_bytes());
32 Ok(out)
33 }
34
35 pub fn decode(data: &[u8]) -> Result<Self, Error> {
37 require_exact_len(data, 7, 0x7A)?;
38 Ok(Self {
39 status: data[0],
40 delay_ms: u16::from_le_bytes([data[1], data[2]]),
41 preferred_size: u16::from_le_bytes([data[3], data[4]]),
42 flags: u16::from_le_bytes([data[5], data[6]]),
43 })
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn roundtrip() {
53 let body = FtStat {
54 status: 0x01,
55 delay_ms: 0x0064,
56 preferred_size: 0x0100,
57 flags: 0x0003,
58 };
59 let bytes = body.encode().unwrap();
60 assert_eq!(bytes, [0x01, 0x64, 0x00, 0x00, 0x01, 0x03, 0x00]);
61 assert_eq!(FtStat::decode(&bytes).unwrap(), body);
62 }
63
64 #[test]
65 fn decode_rejects_wrong_length() {
66 assert!(matches!(
67 FtStat::decode(&[0; 6]),
68 Err(Error::PayloadLength { code: 0x7A, .. })
69 ));
70 }
71}