umsh_ulcp/
status.rs

1//! Status codes carried in `PROP_LAST_STATUS`.
2
3/// A status code, encoded on the wire as a packed unsigned integer.
4///
5/// Represented as a newtype rather than an enum so receivers can carry
6/// codes defined by future protocol versions without loss.
7#[derive(Clone, Copy, PartialEq, Eq)]
8pub struct Status(pub u32);
9
10impl Status {
11    pub const OK: Self = Self(0);
12    pub const FAILURE: Self = Self(1);
13    pub const UNIMPLEMENTED: Self = Self(2);
14    pub const INVALID_ARGUMENT: Self = Self(3);
15    pub const INVALID_STATE: Self = Self(4);
16    pub const INVALID_COMMAND: Self = Self(5);
17    pub const INTERNAL_ERROR: Self = Self(7);
18    pub const PARSE_ERROR: Self = Self(9);
19    pub const IN_PROGRESS: Self = Self(10);
20    pub const NOMEM: Self = Self(11);
21    pub const BUSY: Self = Self(12);
22    pub const PROP_NOT_FOUND: Self = Self(13);
23    pub const CCA_FAILURE: Self = Self(18);
24    pub const ALREADY: Self = Self(19);
25    pub const ITEM_NOT_FOUND: Self = Self(20);
26    pub const CURSOR_INVALID: Self = Self(21);
27    pub const DUTY_LIMIT: Self = Self(32);
28
29    pub const RESET_POWER_ON: Self = Self(112);
30    pub const RESET_EXTERNAL: Self = Self(113);
31    pub const RESET_SOFTWARE: Self = Self(114);
32    pub const RESET_RESTORED: Self = Self(115);
33    pub const RESET_CRASH: Self = Self(116);
34    pub const RESET_ASSERT: Self = Self(117);
35    pub const RESET_OTHER: Self = Self(118);
36    pub const RESET_UNKNOWN: Self = Self(119);
37    pub const RESET_WATCHDOG: Self = Self(120);
38
39    /// Whether this code falls in the reset-code range (112-127).
40    pub const fn is_reset(self) -> bool {
41        self.0 >= 112 && self.0 <= 127
42    }
43
44    const fn name(self) -> Option<&'static str> {
45        Some(match self {
46            Self::OK => "OK",
47            Self::FAILURE => "FAILURE",
48            Self::UNIMPLEMENTED => "UNIMPLEMENTED",
49            Self::INVALID_ARGUMENT => "INVALID_ARGUMENT",
50            Self::INVALID_STATE => "INVALID_STATE",
51            Self::INVALID_COMMAND => "INVALID_COMMAND",
52            Self::INTERNAL_ERROR => "INTERNAL_ERROR",
53            Self::PARSE_ERROR => "PARSE_ERROR",
54            Self::IN_PROGRESS => "IN_PROGRESS",
55            Self::NOMEM => "NOMEM",
56            Self::BUSY => "BUSY",
57            Self::PROP_NOT_FOUND => "PROP_NOT_FOUND",
58            Self::CCA_FAILURE => "CCA_FAILURE",
59            Self::ALREADY => "ALREADY",
60            Self::ITEM_NOT_FOUND => "ITEM_NOT_FOUND",
61            Self::CURSOR_INVALID => "CURSOR_INVALID",
62            Self::DUTY_LIMIT => "DUTY_LIMIT",
63            Self::RESET_POWER_ON => "RESET_POWER_ON",
64            Self::RESET_EXTERNAL => "RESET_EXTERNAL",
65            Self::RESET_SOFTWARE => "RESET_SOFTWARE",
66            Self::RESET_RESTORED => "RESET_RESTORED",
67            Self::RESET_CRASH => "RESET_CRASH",
68            Self::RESET_ASSERT => "RESET_ASSERT",
69            Self::RESET_OTHER => "RESET_OTHER",
70            Self::RESET_UNKNOWN => "RESET_UNKNOWN",
71            Self::RESET_WATCHDOG => "RESET_WATCHDOG",
72            _ => return None,
73        })
74    }
75}
76
77impl core::fmt::Debug for Status {
78    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
79        match self.name() {
80            Some(name) => write!(formatter, "Status::{name}"),
81            None => write!(formatter, "Status({})", self.0),
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn reset_range() {
92        assert!(!Status::OK.is_reset());
93        assert!(!Status::DUTY_LIMIT.is_reset());
94        assert!(Status::RESET_POWER_ON.is_reset());
95        assert!(Status::RESET_WATCHDOG.is_reset());
96        assert!(Status(127).is_reset());
97        assert!(!Status(128).is_reset());
98    }
99
100    #[test]
101    fn debug_names() {
102        assert_eq!(std::format!("{:?}", Status::OK), "Status::OK");
103        assert_eq!(std::format!("{:?}", Status(42)), "Status(42)");
104    }
105}