1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum HapStatus {
8 Success,
10 InsufficientPrivileges,
12 CommunicationFailure,
14 ResourceBusy,
16 WriteToReadOnly,
18 ReadFromWriteOnly,
20 NotificationNotSupported,
22 OutOfResources,
24 OperationTimedOut,
26 ResourceDoesNotExist,
28 InvalidValue,
30 InsufficientAuthorization,
32 Unknown(i64),
34}
35
36impl HapStatus {
37 #[must_use]
39 pub fn from_code(code: i64) -> Self {
40 match code {
41 0 => HapStatus::Success,
42 -70401 => HapStatus::InsufficientPrivileges,
43 -70402 => HapStatus::CommunicationFailure,
44 -70403 => HapStatus::ResourceBusy,
45 -70404 => HapStatus::WriteToReadOnly,
46 -70405 => HapStatus::ReadFromWriteOnly,
47 -70406 => HapStatus::NotificationNotSupported,
48 -70407 => HapStatus::OutOfResources,
49 -70408 => HapStatus::OperationTimedOut,
50 -70409 => HapStatus::ResourceDoesNotExist,
51 -70410 => HapStatus::InvalidValue,
52 -70411 => HapStatus::InsufficientAuthorization,
53 other => HapStatus::Unknown(other),
54 }
55 }
56
57 #[must_use]
59 pub fn code(self) -> i64 {
60 match self {
61 HapStatus::Success => 0,
62 HapStatus::InsufficientPrivileges => -70401,
63 HapStatus::CommunicationFailure => -70402,
64 HapStatus::ResourceBusy => -70403,
65 HapStatus::WriteToReadOnly => -70404,
66 HapStatus::ReadFromWriteOnly => -70405,
67 HapStatus::NotificationNotSupported => -70406,
68 HapStatus::OutOfResources => -70407,
69 HapStatus::OperationTimedOut => -70408,
70 HapStatus::ResourceDoesNotExist => -70409,
71 HapStatus::InvalidValue => -70410,
72 HapStatus::InsufficientAuthorization => -70411,
73 HapStatus::Unknown(c) => c,
74 }
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::HapStatus;
81
82 #[test]
83 fn round_trips_every_named_code_and_unknown() {
84 for code in [
85 0, -70401, -70402, -70403, -70404, -70405, -70406, -70407, -70408, -70409, -70410,
86 -70411, -99999,
87 ] {
88 assert_eq!(HapStatus::from_code(code).code(), code);
89 }
90 assert_eq!(HapStatus::from_code(-70405), HapStatus::ReadFromWriteOnly);
91 assert_eq!(HapStatus::from_code(-99999), HapStatus::Unknown(-99999));
92 }
93}