Skip to main content

osdp/reply/
istat.rs

1//! `osdp_ISTATR` (`0x49`) — input status report.
2//!
3//! # Spec: §7.7
4//!
5//! One byte per input, where each byte is `0` (inactive) or `1` (active).
6
7use crate::error::Error;
8use alloc::vec::Vec;
9
10/// `osdp_ISTATR` body.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct IStatR {
13    /// Status of each input, ordered by input number.
14    pub inputs: Vec<u8>,
15}
16
17impl IStatR {
18    /// Encode.
19    pub fn encode(&self) -> Result<Vec<u8>, Error> {
20        Ok(self.inputs.clone())
21    }
22
23    /// Decode.
24    pub fn decode(data: &[u8]) -> Result<Self, Error> {
25        Ok(Self {
26            inputs: data.to_vec(),
27        })
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn roundtrip() {
37        let body = IStatR {
38            inputs: alloc::vec![0, 1, 0, 1],
39        };
40        let bytes = body.encode().unwrap();
41        assert_eq!(bytes, [0, 1, 0, 1]);
42        assert_eq!(IStatR::decode(&bytes).unwrap(), body);
43    }
44
45    #[test]
46    fn empty_inputs_is_valid() {
47        assert_eq!(IStatR::decode(&[]).unwrap(), IStatR { inputs: Vec::new() });
48    }
49}