device-envoy-rp 0.1.0

Build Pico applications with LED panels, easy Wi-Fi, and composable device abstractions
Documentation
#[cfg(not(feature = "host"))]
use core::convert::Infallible;

use derive_more::derive::{Display, Error};
use device_envoy_core::lcd_text::LcdTextError;
use device_envoy_core::wifi_auto::WifiAutoError;
use esp_hal_mfrc522::consts::PCDErrorCode;

/// A specialized `Result` where the error is this crate's `Error` type.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Define a unified error type for this crate.
#[expect(missing_docs, reason = "The variants are self-explanatory.")]
#[derive(Debug, Display, Error)]
pub enum Error {
    // `#[error(not(source))]` below tells `derive_more` that `embassy_executor::SpawnError` does
    // not implement Rust's `core::error::Error` trait.  `SpawnError` should, but Rust's `Error`
    // only recently moved from `std` (which is not available in bare-metal development) to `core`
    // (which is). Perhaps a future update of `embassy_executor::SpawnError` will implement
    // `core::error::Error` which will make this unnecessary.
    #[display("{_0:?}")]
    TaskSpawn(#[error(not(source))] embassy_executor::SpawnError),

    #[display("bits_to_indexes does not have enough preallocated space")]
    BitsToIndexesNotEnoughSpace,

    #[display("BitsToIndexes is full")]
    BitsToIndexesFull,

    #[display("Error setting output state")]
    CannotSetOutputState,

    #[display("Index out of bounds")]
    IndexOutOfBounds,

    #[display("MFRC522 initialization failed: {_0:?}")]
    Mfrc522Init(#[error(not(source))] PCDErrorCode),

    #[display("MFRC522 version read failed: {_0:?}")]
    Mfrc522Version(#[error(not(source))] PCDErrorCode),

    #[display("Format error")]
    FormatError,

    #[display("Character LCD operation failed: {_0:?}")]
    LcdText(#[error(not(source))] LcdTextError),

    #[display("Custom WiFi Auto field missing")]
    MissingCustomWifiAutoField,

    #[display("Network Time Protocol (NTP) error: {_0}")]
    Ntp(#[error(not(source))] &'static str),

    #[cfg(not(feature = "host"))]
    #[display("Flash operation failed: {_0:?}")]
    Flash(#[error(not(source))] embassy_rp::flash::Error),

    #[display("Storage is invalid or corrupted")]
    StorageCorrupted,

    #[display("{_0:?}")]
    Core(#[error(not(source))] device_envoy_core::Error),
}

impl From<()> for Error {
    fn from(_: ()) -> Self {
        Self::FormatError
    }
}

#[cfg(not(feature = "host"))]
impl From<Infallible> for Error {
    fn from(value: Infallible) -> Self {
        match value {}
    }
}

impl From<embassy_executor::SpawnError> for Error {
    fn from(err: embassy_executor::SpawnError) -> Self {
        Self::TaskSpawn(err)
    }
}

impl From<device_envoy_core::Error> for Error {
    fn from(error: device_envoy_core::Error) -> Self {
        match error {
            device_envoy_core::Error::TaskSpawn(spawn_error) => Self::TaskSpawn(spawn_error),
            core_error => Self::Core(core_error),
        }
    }
}

impl From<device_envoy_core::led4::Led4BitsToIndexesError> for Error {
    fn from(error: device_envoy_core::led4::Led4BitsToIndexesError) -> Self {
        match error {
            device_envoy_core::led4::Led4BitsToIndexesError::Full => Self::BitsToIndexesFull,
        }
    }
}

impl From<WifiAutoError> for Error {
    fn from(error: WifiAutoError) -> Self {
        match error {
            WifiAutoError::FormatError => Self::FormatError,
            WifiAutoError::StorageCorrupted => Self::StorageCorrupted,
            WifiAutoError::MissingCustomWifiAutoField => Self::MissingCustomWifiAutoField,
        }
    }
}

impl From<LcdTextError> for Error {
    fn from(error: LcdTextError) -> Self {
        Self::LcdText(error)
    }
}