use core::fmt;
use embedded_io::ErrorKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
Io(ErrorKind),
Other,
}
impl core::error::Error for Error {}
impl embedded_io::Error for Error {
fn kind(&self) -> ErrorKind {
match self {
Error::Io(kind) => *kind,
Error::Other => ErrorKind::Other,
}
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Error::Io(kind)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for Error {
fn format(&self, f: defmt::Formatter<'_>) {
match self {
Error::Io(_) => defmt::write!(f, "io error"),
Error::Other => defmt::write!(f, "shell error"),
}
}
}
impl Error {
pub fn message(&self) -> &'static str {
match self {
Error::Other => "shell error",
Error::Io(kind) => match *kind {
ErrorKind::NotFound => "not found",
ErrorKind::PermissionDenied => "permission denied",
ErrorKind::ConnectionRefused => "connection refused",
ErrorKind::ConnectionReset => "connection reset",
ErrorKind::ConnectionAborted => "connection aborted",
ErrorKind::NotConnected => "not connected",
ErrorKind::AddrInUse => "address in use",
ErrorKind::AddrNotAvailable => "address not available",
ErrorKind::BrokenPipe => "broken pipe",
ErrorKind::AlreadyExists => "already exists",
ErrorKind::InvalidInput => "invalid input",
ErrorKind::InvalidData => "invalid data",
ErrorKind::TimedOut => "timed out",
ErrorKind::Interrupted => "interrupted",
ErrorKind::Unsupported => "unsupported",
ErrorKind::OutOfMemory => "out of memory",
ErrorKind::WriteZero => "write zero",
_ => "io error",
},
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(kind) => write!(f, "io error: {kind:?}"),
Error::Other => write!(f, "shell error"),
}
}
}
pub type Result<T> = core::result::Result<T, Error>;