sunspec/models/
model10.rs

1//! Communication Interface Header
2/// Communication Interface Header
3///
4/// To be included first for a complete interface description
5#[derive(Debug)]
6#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
7pub struct Model10 {
8    /// Interface Status
9    ///
10    /// Overall interface status
11    pub st: St,
12    /// Interface Control
13    ///
14    /// Overall interface control (TBD)
15    pub ctl: Option<u16>,
16    /// Physical Access Type
17    ///
18    /// Enumerated value.  Type of physical media
19    pub typ: Option<Typ>,
20}
21#[allow(missing_docs)]
22impl Model10 {
23    pub const ST: crate::Point<Self, St> = crate::Point::new(0, 1, false);
24    pub const CTL: crate::Point<Self, Option<u16>> = crate::Point::new(1, 1, true);
25    pub const TYP: crate::Point<Self, Option<Typ>> = crate::Point::new(2, 1, false);
26}
27impl crate::Model for Model10 {
28    const ID: u16 = 10;
29    fn from_data(data: &[u16]) -> Result<Self, crate::DecodeError> {
30        Ok(Self {
31            st: Self::ST.from_data(data)?,
32            ctl: Self::CTL.from_data(data)?,
33            typ: Self::TYP.from_data(data)?,
34        })
35    }
36    fn addr(models: &crate::Models) -> crate::ModelAddr<Self> {
37        models.m10
38    }
39}
40/// Interface Status
41///
42/// Overall interface status
43#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)]
44#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
45#[repr(u16)]
46pub enum St {
47    #[allow(missing_docs)]
48    Down = 0,
49    #[allow(missing_docs)]
50    Up = 1,
51    #[allow(missing_docs)]
52    Fault = 2,
53}
54impl crate::Value for St {
55    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
56        let value = u16::decode(data)?;
57        Self::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)
58    }
59    fn encode(self) -> Box<[u16]> {
60        (self as u16).encode()
61    }
62}
63impl crate::Value for Option<St> {
64    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
65        let value = u16::decode(data)?;
66        if value != 65535 {
67            Ok(Some(
68                St::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)?,
69            ))
70        } else {
71            Ok(None)
72        }
73    }
74    fn encode(self) -> Box<[u16]> {
75        if let Some(value) = self {
76            value.encode()
77        } else {
78            65535.encode()
79        }
80    }
81}
82/// Physical Access Type
83///
84/// Enumerated value.  Type of physical media
85#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)]
86#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
87#[repr(u16)]
88pub enum Typ {
89    #[allow(missing_docs)]
90    Unknown = 0,
91    #[allow(missing_docs)]
92    Internal = 1,
93    #[allow(missing_docs)]
94    TwistedPair = 2,
95    #[allow(missing_docs)]
96    Fiber = 3,
97    #[allow(missing_docs)]
98    Wireless = 4,
99}
100impl crate::Value for Typ {
101    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
102        let value = u16::decode(data)?;
103        Self::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)
104    }
105    fn encode(self) -> Box<[u16]> {
106        (self as u16).encode()
107    }
108}
109impl crate::Value for Option<Typ> {
110    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
111        let value = u16::decode(data)?;
112        if value != 65535 {
113            Ok(Some(
114                Typ::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)?,
115            ))
116        } else {
117            Ok(None)
118        }
119    }
120    fn encode(self) -> Box<[u16]> {
121        if let Some(value) = self {
122            value.encode()
123        } else {
124            65535.encode()
125        }
126    }
127}