Skip to main content

improv_wifi/
error.rs

1use thiserror::Error;
2
3/// Improv Wi-Fi error codes, as transmitted on the Error State characteristic.
4#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, Hash)]
5#[repr(u8)]
6pub enum Error {
7	/// RPC packet was malformed or had a bad checksum.
8	#[error("invalid RPC packet")]
9	InvalidRPC = 0x01,
10
11	/// The command sent is unknown.
12	#[error("unknown RPC command")]
13	UnknownRPC = 0x02,
14
15	/// Credentials were received but the device couldn't connect to the network.
16	#[error("unable to connect to the requested network")]
17	UnableToConnect = 0x03,
18
19	/// Credentials were sent via RPC but the Improv service is not authorised.
20	#[error("not authorised")]
21	NotAuthorized = 0x04,
22
23	/// A hostname value is not RFC 1123 compliant.
24	#[error("bad hostname")]
25	BadHostname = 0x05,
26
27	/// Catch-all for backend errors.
28	#[error("unknown error")]
29	Unknown = 0xFF,
30}
31
32impl Error {
33	pub fn as_byte(self) -> u8 {
34		self as u8
35	}
36}
37
38#[cfg(test)]
39mod tests {
40	use super::*;
41
42	#[test]
43	fn error_byte_values() {
44		assert_eq!(Error::InvalidRPC.as_byte(), 0x01);
45		assert_eq!(Error::UnknownRPC.as_byte(), 0x02);
46		assert_eq!(Error::UnableToConnect.as_byte(), 0x03);
47		assert_eq!(Error::NotAuthorized.as_byte(), 0x04);
48		assert_eq!(Error::BadHostname.as_byte(), 0x05);
49		assert_eq!(Error::Unknown.as_byte(), 0xFF);
50	}
51}