use std::fmt;
#[derive(Debug)]
pub enum Error {
NoAdapter,
Timeout(String),
NotConnected,
NotFound(String),
Btleplug(String),
Json(serde_json::Error),
InvalidUuid(String),
Internal(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NoAdapter => write!(f, "no Bluetooth adapter found"),
Error::Timeout(msg) => write!(f, "{msg}"),
Error::NotConnected => write!(f, "peripheral is not connected"),
Error::NotFound(what) => write!(f, "{what}"),
Error::Btleplug(msg) => write!(f, "{msg}"),
Error::Json(e) => write!(f, "JSON error: {e}"),
Error::InvalidUuid(msg) => write!(f, "invalid UUID: {msg}"),
Error::Internal(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Json(e) => Some(e),
_ => None,
}
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}