Skip to main content

device_envoy/
error.rs

1#[cfg(not(feature = "host"))]
2use core::convert::Infallible;
3
4use derive_more::derive::{Display, Error};
5use esp_hal_mfrc522::consts::PCDErrorCode;
6
7/// A specialized `Result` where the error is this crate's `Error` type.
8pub type Result<T, E = Error> = core::result::Result<T, E>;
9
10/// Define a unified error type for this crate.
11#[expect(missing_docs, reason = "The variants are self-explanatory.")]
12#[derive(Debug, Display, Error)]
13pub enum Error {
14    // `#[error(not(source))]` below tells `derive_more` that `embassy_executor::SpawnError` does
15    // not implement Rust's `core::error::Error` trait.  `SpawnError` should, but Rust's `Error`
16    // only recently moved from `std` (which is not available in bare-metal development) to `core`
17    // (which is). Perhaps a future update of `embassy_executor::SpawnError` will implement
18    // `core::error::Error` which will make this unnecessary.
19    #[display("{_0:?}")]
20    TaskSpawn(#[error(not(source))] embassy_executor::SpawnError),
21
22    #[display("bits_to_indexes does not have enough preallocated space")]
23    BitsToIndexesNotEnoughSpace,
24
25    #[display("BitsToIndexes is full")]
26    BitsToIndexesFull,
27
28    #[display("Error setting output state")]
29    CannotSetOutputState,
30
31    #[display("Index out of bounds")]
32    IndexOutOfBounds,
33
34    #[display("MFRC522 initialization failed: {_0:?}")]
35    Mfrc522Init(#[error(not(source))] PCDErrorCode),
36
37    #[display("MFRC522 version read failed: {_0:?}")]
38    Mfrc522Version(#[error(not(source))] PCDErrorCode),
39
40    #[display("Format error")]
41    FormatError,
42
43    #[display("Custom WiFi Auto field missing")]
44    MissingCustomWifiAutoField,
45
46    #[display("Network Time Protocol (NTP) error: {_0}")]
47    Ntp(#[error(not(source))] &'static str),
48
49    #[cfg(not(feature = "host"))]
50    #[display("Flash operation failed: {_0:?}")]
51    Flash(#[error(not(source))] embassy_rp::flash::Error),
52
53    #[display("Storage is invalid or corrupted")]
54    StorageCorrupted,
55
56    #[display("animation disabled (max_frames = {_0})")]
57    AnimationDisabled(#[error(not(source))] usize),
58}
59
60impl From<()> for Error {
61    fn from(_: ()) -> Self {
62        Self::FormatError
63    }
64}
65
66#[cfg(not(feature = "host"))]
67impl From<Infallible> for Error {
68    fn from(value: Infallible) -> Self {
69        match value {}
70    }
71}
72
73impl From<embassy_executor::SpawnError> for Error {
74    fn from(err: embassy_executor::SpawnError) -> Self {
75        Self::TaskSpawn(err)
76    }
77}