Skip to main content

osdp/reply/
xrd.rs

1//! `osdp_XRD` (`0xB1`) — extended-read response.
2//!
3//! # Spec: §7.26
4//!
5//! Body's first byte is `XRW_MODE` (matches the request); the remainder is
6//! mode-specific.
7
8use crate::error::Error;
9use alloc::vec::Vec;
10
11/// `osdp_XRD` body — opaque container.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct Xrd {
14    /// XRW_MODE byte.
15    pub mode: u8,
16    /// Mode-specific reply (sub-reply code + data).
17    pub payload: Vec<u8>,
18}
19
20impl Xrd {
21    /// Encode.
22    pub fn encode(&self) -> Result<Vec<u8>, Error> {
23        let mut out = Vec::with_capacity(1 + self.payload.len());
24        out.push(self.mode);
25        out.extend_from_slice(&self.payload);
26        Ok(out)
27    }
28
29    /// Decode.
30    pub fn decode(data: &[u8]) -> Result<Self, Error> {
31        if data.is_empty() {
32            return Err(Error::MalformedPayload {
33                code: 0xB1,
34                reason: "XRD requires XRW_MODE byte",
35            });
36        }
37        Ok(Self {
38            mode: data[0],
39            payload: data[1..].to_vec(),
40        })
41    }
42}