#[derive(Debug, thiserror::Error)]
pub enum SdkError {
#[error("hal: {0}")]
Hal(#[from] cr1140_hal::HalError),
#[error("io: {0}")]
Io(#[from] std::io::Error),
#[cfg(feature = "config")]
#[error("config decode: {0}")]
Decode(#[from] toml::de::Error),
#[cfg(feature = "config")]
#[error("config encode: {0}")]
Encode(#[from] toml::ser::Error),
#[cfg(feature = "retain")]
#[error("retain: {0}")]
Retain(String),
#[cfg(feature = "net")]
#[error("net: {0}")]
Net(String),
}
pub type SdkResult<T> = Result<T, SdkError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_io_error_displays_with_prefix() {
let io = std::io::Error::new(std::io::ErrorKind::NotFound, "nope");
let e: SdkError = io.into();
assert!(e.to_string().starts_with("io: "), "got {e}");
}
#[test]
fn from_hal_error_is_wrapped() {
let hal = cr1140_hal::HalError::DeviceNotFound("kbd".into());
let e: SdkError = hal.into();
assert!(matches!(e, SdkError::Hal(_)));
assert!(e.to_string().starts_with("hal: "), "got {e}");
}
#[cfg(feature = "config")]
#[test]
fn from_toml_decode_error_is_wrapped() {
let err = toml::from_str::<toml::Table>("= 1").unwrap_err();
let e: SdkError = err.into();
assert!(matches!(e, SdkError::Decode(_)));
}
}