zerodds_rtc/
return_code.rs1use core::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum ReturnCode {
13 Ok,
15 Error,
17 BadParameter,
19 Unsupported,
21 OutOfResources,
23 PreconditionNotMet,
26}
27
28impl ReturnCode {
29 #[must_use]
31 pub const fn is_ok(self) -> bool {
32 matches!(self, Self::Ok)
33 }
34
35 pub const fn into_result(self) -> Result<(), Self> {
41 match self {
42 Self::Ok => Ok(()),
43 other => Err(other),
44 }
45 }
46}
47
48impl fmt::Display for ReturnCode {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 f.write_str(match self {
51 Self::Ok => "OK",
52 Self::Error => "ERROR",
53 Self::BadParameter => "BAD_PARAMETER",
54 Self::Unsupported => "UNSUPPORTED",
55 Self::OutOfResources => "OUT_OF_RESOURCES",
56 Self::PreconditionNotMet => "PRECONDITION_NOT_MET",
57 })
58 }
59}
60
61#[cfg(feature = "std")]
62impl std::error::Error for ReturnCode {}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn ok_is_ok_and_others_are_not() {
70 assert!(ReturnCode::Ok.is_ok());
72 for rc in [
73 ReturnCode::Error,
74 ReturnCode::BadParameter,
75 ReturnCode::Unsupported,
76 ReturnCode::OutOfResources,
77 ReturnCode::PreconditionNotMet,
78 ] {
79 assert!(!rc.is_ok());
80 }
81 }
82
83 #[test]
84 fn into_result_maps_ok_to_unit_and_others_to_err() {
85 assert_eq!(ReturnCode::Ok.into_result(), Ok(()));
86 assert_eq!(
87 ReturnCode::PreconditionNotMet.into_result(),
88 Err(ReturnCode::PreconditionNotMet)
89 );
90 }
91
92 #[test]
93 fn display_reports_spec_token_names() {
94 assert_eq!(alloc::format!("{}", ReturnCode::Ok), "OK");
95 assert_eq!(
96 alloc::format!("{}", ReturnCode::PreconditionNotMet),
97 "PRECONDITION_NOT_MET"
98 );
99 assert_eq!(
100 alloc::format!("{}", ReturnCode::BadParameter),
101 "BAD_PARAMETER"
102 );
103 }
104}