Skip to main content

hap_model/
status.rs

1//! HAP per-characteristic status codes.
2
3/// A HAP per-characteristic status (the negative ids accessories return in
4/// `/characteristics` responses). `0` is success.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum HapStatus {
8    /// `0` — success.
9    Success,
10    /// `-70401` — request denied due to insufficient privileges.
11    InsufficientPrivileges,
12    /// `-70402` — unable to communicate with the requested service/characteristic.
13    CommunicationFailure,
14    /// `-70403` — resource is busy; try again.
15    ResourceBusy,
16    /// `-70404` — cannot write to a read-only characteristic.
17    WriteToReadOnly,
18    /// `-70405` — cannot read from a write-only characteristic.
19    ReadFromWriteOnly,
20    /// `-70406` — notification is not supported for the characteristic.
21    NotificationNotSupported,
22    /// `-70407` — out of resources to process the request.
23    OutOfResources,
24    /// `-70408` — operation timed out.
25    OperationTimedOut,
26    /// `-70409` — resource does not exist.
27    ResourceDoesNotExist,
28    /// `-70410` — accessory received an invalid value in a write request.
29    InvalidValue,
30    /// `-70411` — insufficient authorization.
31    InsufficientAuthorization,
32    /// Any code not in the HAP-defined set.
33    Unknown(i64),
34}
35
36impl HapStatus {
37    /// Map a raw HAP status code to its named variant.
38    #[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    /// The raw HAP status code for this status.
58    #[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}