1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8pub enum NodeId {
9 Unconfigured,
11 Configured(ConfiguredNodeId),
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[cfg_attr(feature = "defmt", derive(defmt::Format))]
18pub struct ConfiguredNodeId(u8);
19impl ConfiguredNodeId {
20 pub fn new(value: u8) -> Result<Self, InvalidNodeIdError> {
24 if (value > 0 && value < 128) || value == 255 {
25 Ok(ConfiguredNodeId(value))
26 } else {
27 Err(InvalidNodeIdError)
28 }
29 }
30
31 pub fn raw(&self) -> u8 {
33 self.0
34 }
35}
36
37impl core::fmt::Display for ConfiguredNodeId {
38 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39 write!(f, "{}", self.0)
40 }
41}
42
43impl From<ConfiguredNodeId> for u8 {
44 fn from(value: ConfiguredNodeId) -> Self {
45 value.raw()
46 }
47}
48
49impl NodeId {
50 pub fn new(value: u8) -> Result<Self, InvalidNodeIdError> {
54 if value == 255 {
55 Ok(NodeId::Unconfigured)
56 } else {
57 ConfiguredNodeId::new(value).map(NodeId::Configured)
58 }
59 }
60
61 pub fn raw(&self) -> u8 {
63 match self {
64 NodeId::Unconfigured => 255,
65 NodeId::Configured(node_id_num) => node_id_num.0,
66 }
67 }
68
69 pub fn as_configured(&self) -> Option<ConfiguredNodeId> {
73 match self {
74 NodeId::Unconfigured => None,
75 NodeId::Configured(configured_node_id) => Some(*configured_node_id),
76 }
77 }
78
79 pub fn is_configured(&self) -> bool {
81 match self {
82 Self::Configured(_) => true,
83 Self::Unconfigured => false,
84 }
85 }
86 pub fn is_unconfigured(&self) -> bool {
88 match self {
89 Self::Configured(_) => false,
90 Self::Unconfigured => true,
91 }
92 }
93}
94
95#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97pub struct InvalidNodeIdError;
98
99impl core::fmt::Display for InvalidNodeIdError {
100 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
101 write!(f, "Invalid node ID")
102 }
103}
104impl core::error::Error for InvalidNodeIdError {}
105
106impl TryFrom<u8> for NodeId {
107 type Error = InvalidNodeIdError;
108
109 fn try_from(value: u8) -> Result<Self, Self::Error> {
110 if value == 255 {
111 Ok(NodeId::Unconfigured)
112 } else {
113 Ok(NodeId::Configured(ConfiguredNodeId(value)))
114 }
115 }
116}
117
118impl From<NodeId> for u8 {
119 fn from(value: NodeId) -> Self {
120 value.raw()
121 }
122}