ads_proto/proto/
ads_state.rs

1use crate::proto::proto_traits::{ReadFrom, WriteTo};
2use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
3use std::io::{self, Read, Write};
4
5#[repr(u16)]
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum AdsState {
8    AdsStateInvalid,
9    AdsStateIdle,
10    AdsStateReset,
11    AdsStateInit,
12    AdsStateStart,
13    AdsStateRun,
14    AdsStateStop,
15    AdsStateSaveCFG,
16    AdsStateLoadCFG,
17    AdsStatePowerFailure,
18    AdsStatePowerGood,
19    AdsStateError,
20    AdsStateShutDown,
21    AdsStateSuspend,
22    AdsStateResume,
23    AdsStateConfig,
24    AdsStateReconfig,
25}
26
27impl From<u16> for AdsState {
28    fn from(state_value: u16) -> Self {
29        match state_value {
30            0 => AdsState::AdsStateInvalid,
31            1 => AdsState::AdsStateIdle,
32            2 => AdsState::AdsStateReset,
33            3 => AdsState::AdsStateInit,
34            4 => AdsState::AdsStateStart,
35            5 => AdsState::AdsStateRun,
36            6 => AdsState::AdsStateStop,
37            7 => AdsState::AdsStateSaveCFG,
38            8 => AdsState::AdsStateLoadCFG,
39            9 => AdsState::AdsStatePowerFailure,
40            10 => AdsState::AdsStatePowerGood,
41            11 => AdsState::AdsStateError,
42            12 => AdsState::AdsStateShutDown,
43            13 => AdsState::AdsStateSuspend,
44            14 => AdsState::AdsStateResume,
45            15 => AdsState::AdsStateConfig,
46            16 => AdsState::AdsStateReconfig,
47            _ => AdsState::AdsStateInvalid,
48        }
49    }
50}
51
52impl WriteTo for AdsState {
53    fn write_to<W: Write>(&self, mut wtr: W) -> io::Result<()> {
54        wtr.write_u16::<LittleEndian>(*self as u16)?;
55        Ok(())
56    }
57}
58
59impl ReadFrom for AdsState {
60    fn read_from<R: Read>(read: &mut R) -> io::Result<Self> {
61        Ok(AdsState::from(read.read_u16::<LittleEndian>()?))
62    }
63}
64
65impl AdsState {
66    pub fn as_u16(&self) -> u16 {
67        *self as u16
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn ads_state_write_to_test() {
77        let compare: [u8; 2] = [5, 0];
78        let mut buffer: Vec<u8> = Vec::new();
79        AdsState::AdsStateRun.write_to(&mut buffer).unwrap();
80        assert_eq!(buffer, compare);
81    }
82
83    #[test]
84    fn ads_state_read_from_test() {
85        let data: Vec<u8> = vec![6, 0, 8, 4];
86        let trans_mode = AdsState::read_from(&mut data.as_slice()).unwrap();
87        assert_eq!(trans_mode, AdsState::AdsStateStop);
88    }
89
90    #[test]
91    fn ads_state_from_test() {
92        assert_eq!(AdsState::from(0), AdsState::AdsStateInvalid);
93        assert_eq!(AdsState::from(1), AdsState::AdsStateIdle);
94        assert_eq!(AdsState::from(2), AdsState::AdsStateReset);
95        assert_eq!(AdsState::from(3), AdsState::AdsStateInit);
96        assert_eq!(AdsState::from(4), AdsState::AdsStateStart);
97        assert_eq!(AdsState::from(5), AdsState::AdsStateRun);
98        assert_eq!(AdsState::from(6), AdsState::AdsStateStop);
99        assert_eq!(AdsState::from(7), AdsState::AdsStateSaveCFG);
100        assert_eq!(AdsState::from(8), AdsState::AdsStateLoadCFG);
101        assert_eq!(AdsState::from(9), AdsState::AdsStatePowerFailure);
102        assert_eq!(AdsState::from(10), AdsState::AdsStatePowerGood);
103        assert_eq!(AdsState::from(11), AdsState::AdsStateError);
104        assert_eq!(AdsState::from(12), AdsState::AdsStateShutDown);
105        assert_eq!(AdsState::from(13), AdsState::AdsStateSuspend);
106        assert_eq!(AdsState::from(14), AdsState::AdsStateResume);
107        assert_eq!(AdsState::from(15), AdsState::AdsStateConfig);
108        assert_eq!(AdsState::from(16), AdsState::AdsStateReconfig);
109        assert_eq!(AdsState::from(999), AdsState::AdsStateInvalid);
110    }
111
112    #[test]
113    fn ads_state_as_u16_test() {
114        assert_eq!(AdsState::AdsStateInvalid.as_u16(), 0);
115        assert_eq!(AdsState::AdsStateIdle.as_u16(), 1);
116        assert_eq!(AdsState::AdsStateReset.as_u16(), 2);
117        assert_eq!(AdsState::AdsStateInit.as_u16(), 3);
118        assert_eq!(AdsState::AdsStateStart.as_u16(), 4);
119        assert_eq!(AdsState::AdsStateRun.as_u16(), 5);
120        assert_eq!(AdsState::AdsStateStop.as_u16(), 6);
121        assert_eq!(AdsState::AdsStateSaveCFG.as_u16(), 7);
122        assert_eq!(AdsState::AdsStateLoadCFG.as_u16(), 8);
123        assert_eq!(AdsState::AdsStatePowerFailure.as_u16(), 9);
124        assert_eq!(AdsState::AdsStatePowerGood.as_u16(), 10);
125        assert_eq!(AdsState::AdsStateError.as_u16(), 11);
126        assert_eq!(AdsState::AdsStateShutDown.as_u16(), 12);
127        assert_eq!(AdsState::AdsStateSuspend.as_u16(), 13);
128        assert_eq!(AdsState::AdsStateResume.as_u16(), 14);
129        assert_eq!(AdsState::AdsStateConfig.as_u16(), 15);
130        assert_eq!(AdsState::AdsStateReconfig.as_u16(), 16);
131    }
132}