Skip to main content

osdp/reply/
ostat.rs

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