Skip to main content

osdp/command/
acu_rx_size.rs

1//! `osdp_ACURXSIZE` (`0x7B`) — inform the PD of the ACU's max receive size.
2//!
3//! # Spec: §6.19
4//!
5//! Body is a 16-bit little-endian byte count.
6
7use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11/// `osdp_ACURXSIZE` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct AcuRxSize {
14    /// Bytes the ACU can receive in one packet.
15    pub max_size: u16,
16}
17
18impl AcuRxSize {
19    /// Encode.
20    pub fn encode(&self) -> Result<Vec<u8>, Error> {
21        Ok(self.max_size.to_le_bytes().to_vec())
22    }
23
24    /// Decode.
25    pub fn decode(data: &[u8]) -> Result<Self, Error> {
26        require_exact_len(data, 2, 0x7B)?;
27        Ok(Self {
28            max_size: u16::from_le_bytes([data[0], data[1]]),
29        })
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn roundtrip() {
39        let body = AcuRxSize { max_size: 0x0123 };
40        let bytes = body.encode().unwrap();
41        assert_eq!(bytes, [0x23, 0x01]);
42        assert_eq!(AcuRxSize::decode(&bytes).unwrap(), body);
43    }
44
45    #[test]
46    fn decode_rejects_short() {
47        assert!(matches!(
48            AcuRxSize::decode(&[0x10]),
49            Err(Error::PayloadLength { code: 0x7B, .. })
50        ));
51    }
52}