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 const 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 const fn new(value: u8) -> Result<Self, InvalidNodeIdError> {
54 if value == 255 {
55 Ok(NodeId::Unconfigured)
56 } else {
57 match ConfiguredNodeId::new(value) {
58 Ok(id) => Ok(NodeId::Configured(id)),
59 Err(e) => Err(e),
60 }
61 }
62 }
63
64 pub fn raw(&self) -> u8 {
66 match self {
67 NodeId::Unconfigured => 255,
68 NodeId::Configured(node_id_num) => node_id_num.0,
69 }
70 }
71
72 pub fn as_configured(&self) -> Option<ConfiguredNodeId> {
76 match self {
77 NodeId::Unconfigured => None,
78 NodeId::Configured(configured_node_id) => Some(*configured_node_id),
79 }
80 }
81
82 pub fn is_configured(&self) -> bool {
84 match self {
85 Self::Configured(_) => true,
86 Self::Unconfigured => false,
87 }
88 }
89 pub fn is_unconfigured(&self) -> bool {
91 match self {
92 Self::Configured(_) => false,
93 Self::Unconfigured => true,
94 }
95 }
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub struct InvalidNodeIdError;
101
102impl core::fmt::Display for InvalidNodeIdError {
103 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
104 write!(f, "Invalid node ID")
105 }
106}
107impl core::error::Error for InvalidNodeIdError {}
108
109impl TryFrom<u8> for NodeId {
110 type Error = InvalidNodeIdError;
111
112 fn try_from(value: u8) -> Result<Self, Self::Error> {
113 if value == 255 {
114 Ok(NodeId::Unconfigured)
115 } else {
116 Ok(NodeId::Configured(ConfiguredNodeId(value)))
117 }
118 }
119}
120
121impl From<NodeId> for u8 {
122 fn from(value: NodeId) -> Self {
123 value.raw()
124 }
125}