1#[derive(Debug, thiserror::Error)]
9pub enum SdkError {
10 #[error("hal: {0}")]
12 Hal(#[from] cr1140_hal::HalError),
13 #[error("io: {0}")]
15 Io(#[from] std::io::Error),
16 #[cfg(feature = "config")]
18 #[error("config decode: {0}")]
19 Decode(#[from] toml::de::Error),
20 #[cfg(feature = "config")]
22 #[error("config encode: {0}")]
23 Encode(#[from] toml::ser::Error),
24 #[cfg(feature = "retain")]
27 #[error("retain: {0}")]
28 Retain(String),
29 #[cfg(feature = "net")]
32 #[error("net: {0}")]
33 Net(String),
34}
35
36pub type SdkResult<T> = Result<T, SdkError>;
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn from_io_error_displays_with_prefix() {
45 let io = std::io::Error::new(std::io::ErrorKind::NotFound, "nope");
46 let e: SdkError = io.into();
47 assert!(e.to_string().starts_with("io: "), "got {e}");
48 }
49
50 #[test]
51 fn from_hal_error_is_wrapped() {
52 let hal = cr1140_hal::HalError::DeviceNotFound("kbd".into());
53 let e: SdkError = hal.into();
54 assert!(matches!(e, SdkError::Hal(_)));
55 assert!(e.to_string().starts_with("hal: "), "got {e}");
56 }
57
58 #[cfg(feature = "config")]
59 #[test]
60 fn from_toml_decode_error_is_wrapped() {
61 let err = toml::from_str::<toml::Table>("= 1").unwrap_err();
63 let e: SdkError = err.into();
64 assert!(matches!(e, SdkError::Decode(_)));
65 }
66}