Skip to main content

embassy_shell/
error.rs

1use core::fmt;
2
3use embedded_io::ErrorKind;
4
5/// Error type used by the shell and returned by command handlers.
6///
7/// Transport errors are wrapped into [`Error::Io`] so that every command
8/// handler can use the same concrete error type regardless of the underlying
9/// transport.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Error {
12    /// An I/O error occurred on the underlying transport.
13    Io(ErrorKind),
14    /// An operation failed for another reason.
15    Other,
16}
17
18impl core::error::Error for Error {}
19
20impl embedded_io::Error for Error {
21    fn kind(&self) -> ErrorKind {
22        match self {
23            Error::Io(kind) => *kind,
24            Error::Other => ErrorKind::Other,
25        }
26    }
27}
28
29impl From<ErrorKind> for Error {
30    fn from(kind: ErrorKind) -> Self {
31        Error::Io(kind)
32    }
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Error::Io(kind) => write!(f, "io error: {kind:?}"),
39            Error::Other => write!(f, "shell error"),
40        }
41    }
42}
43
44/// Convenience alias used throughout the crate and by command handlers.
45pub type Result<T> = core::result::Result<T, Error>;