bnr_xfs/capabilities/
recognition_sensor_type.rs

1use std::fmt;
2
3use crate::xfs::{value::XfsValue, xfs_struct::XfsMember};
4use crate::{Error, Result};
5
6/// Recognition sensor type identification. Always ‘B’ in the BNR.
7#[repr(C)]
8#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
9pub struct RecognitionSensorType(u8);
10
11impl RecognitionSensorType {
12    /// Creates a new [RecognitionSensorType].
13    pub const fn new() -> Self {
14        Self(b'B')
15    }
16
17    /// Creates a new [RecognitionSensorType] from the provided parameter.
18    pub const fn create(val: u8) -> Self {
19        Self(val)
20    }
21
22    /// Gets the [XfsMember](crate::xfs::xfs_struct::XfsMember) name.
23    pub const fn xfs_name() -> &'static str {
24        "recognitionSensorType"
25    }
26
27    /// Gets the inner representation of the [RecognitionSensorType].
28    pub const fn inner(&self) -> u8 {
29        self.0
30    }
31
32    /// Sets the inner representation of the [RecognitionSensorType].
33    pub fn set_inner(&mut self, val: u8) {
34        self.0 = val;
35    }
36
37    /// Converts into the inner representation of the [RecognitionSensorType].
38    pub fn into_inner(self) -> u8 {
39        self.0
40    }
41}
42
43impl From<&RecognitionSensorType> for u8 {
44    fn from(val: &RecognitionSensorType) -> Self {
45        val.inner()
46    }
47}
48
49impl From<RecognitionSensorType> for u8 {
50    fn from(val: RecognitionSensorType) -> Self {
51        val.into_inner()
52    }
53}
54
55impl From<u8> for RecognitionSensorType {
56    fn from(val: u8) -> Self {
57        Self::create(val)
58    }
59}
60
61impl From<&u8> for RecognitionSensorType {
62    fn from(val: &u8) -> Self {
63        (*val).into()
64    }
65}
66
67impl From<&RecognitionSensorType> for XfsValue {
68    fn from(val: &RecognitionSensorType) -> Self {
69        Self::new().with_string(std::str::from_utf8(&[val.inner()]).unwrap_or(""))
70    }
71}
72
73impl From<RecognitionSensorType> for XfsValue {
74    fn from(val: RecognitionSensorType) -> Self {
75        (&val).into()
76    }
77}
78
79impl TryFrom<&str> for RecognitionSensorType {
80    type Error = Error;
81
82    fn try_from(val: &str) -> Result<Self> {
83        Ok(val
84            .as_bytes()
85            .first()
86            .ok_or(Error::Xfs("Empty string".into()))?
87            .into())
88    }
89}
90
91impl TryFrom<&XfsValue> for RecognitionSensorType {
92    type Error = Error;
93
94    fn try_from(val: &XfsValue) -> Result<Self> {
95        val.string()
96            .ok_or(Error::Xfs(format!(
97                "Expected RecognitionSensorType XfsValue, have: {val}"
98            )))?
99            .try_into()
100    }
101}
102
103impl From<&RecognitionSensorType> for XfsMember {
104    fn from(val: &RecognitionSensorType) -> Self {
105        Self::create(RecognitionSensorType::xfs_name(), val.into())
106    }
107}
108
109impl From<RecognitionSensorType> for XfsMember {
110    fn from(val: RecognitionSensorType) -> Self {
111        (&val).into()
112    }
113}
114
115impl TryFrom<&XfsMember> for RecognitionSensorType {
116    type Error = Error;
117
118    fn try_from(val: &XfsMember) -> Result<Self> {
119        match (val.name(), val.value().string()) {
120            (n, Some(v)) if n == Self::xfs_name() => v.try_into(),
121            _ => Err(Error::Xfs(format!(
122                "Expected RecognitionSensorType XfsMember, have: {val}"
123            ))),
124        }
125    }
126}
127
128impl TryFrom<XfsMember> for RecognitionSensorType {
129    type Error = Error;
130
131    fn try_from(val: XfsMember) -> Result<Self> {
132        (&val).try_into()
133    }
134}
135
136impl fmt::Display for RecognitionSensorType {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        write!(f, r#""{}""#, self.inner() as char)
139    }
140}