Skip to main content

osdp/command/
id.rs

1//! `osdp_ID` (`0x61`) — request PD identification.
2//!
3//! # Spec: §6.2
4//!
5//! Body is a single `Reserved` byte (always `0x00`).
6
7use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11/// `osdp_ID` body.
12#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
13pub struct Id {
14    /// Reserved byte; spec mandates `0x00`.
15    pub reserved: u8,
16}
17
18impl Id {
19    /// Standard request: `reserved = 0x00`.
20    pub const fn standard() -> Self {
21        Self { reserved: 0 }
22    }
23
24    /// Encode.
25    pub fn encode(&self) -> Result<Vec<u8>, Error> {
26        Ok(alloc::vec![self.reserved])
27    }
28
29    /// Decode.
30    pub fn decode(data: &[u8]) -> Result<Self, Error> {
31        require_exact_len(data, 1, 0x61)?;
32        Ok(Self { reserved: data[0] })
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn standard_encodes_zero() {
42        assert_eq!(Id::standard().encode().unwrap(), [0x00]);
43    }
44
45    #[test]
46    fn roundtrip_preserves_reserved() {
47        let body = Id { reserved: 0x55 };
48        let bytes = body.encode().unwrap();
49        assert_eq!(bytes, [0x55]);
50        assert_eq!(Id::decode(&bytes).unwrap(), body);
51    }
52
53    #[test]
54    fn decode_rejects_wrong_length() {
55        assert!(matches!(
56            Id::decode(&[]),
57            Err(Error::PayloadLength { code: 0x61, .. })
58        ));
59    }
60}