sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
/// Convenience alias for the MMC [`Result`](core::result::Result) type.
pub type Result<T> = core::result::Result<T, Error>;

pub const EINVAL: i32 = 22;
pub const NEINVAL: i32 = -EINVAL;
pub const EILSEQ: i32 = 84;
pub const NEILSEQ: i32 = -EILSEQ;
pub const ETIMEDOUT: i32 = 110;
pub const NETIMEDOUT: i32 = -ETIMEDOUT;
pub const ENOMEDIUM: i32 = 123;
pub const NENOMEDIUM: i32 = -ENOMEDIUM;

/// Represents MMC error types.
#[repr(i32)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Error {
    #[default]
    None = 0,
    /// Invalid argument.
    Invalid = 22,
    /// Illegal byte sequence.
    IllegalSequence = 84,
    /// Connection timed out.
    TimedOut = 110,
    /// No medium found.
    NoMedium = 123,
    /// Invalid variant.
    InvalidVariant(usize),
    /// Invalid field enum variant.
    InvalidFieldVariant { field: &'static str, value: usize },
    /// Invalid value.
    InvalidValue {
        value: usize,
        min: usize,
        max: usize,
    },
    /// Invalid field value.
    InvalidFieldValue {
        field: &'static str,
        value: usize,
        min: usize,
        max: usize,
    },
    /// Invalid CRC-7.
    InvalidCrc7 { invalid: u8, calculated: u8 },
    /// Invalid length, with expected value.
    InvalidLength { len: usize, expected: usize },
    /// Indicates an unimplemented function or feature.
    Unimplemented,
}

impl Error {
    /// Creates an [InvalidFieldVariant](Self::InvalidFieldVariant) error.
    pub const fn invalid_field_variant(field: &'static str, value: usize) -> Self {
        Self::InvalidFieldVariant { field, value }
    }

    /// Creates an [InvalidFieldValue](Self::InvalidFieldVariant) error.
    pub const fn invalid_field_value(
        field: &'static str,
        value: usize,
        min: usize,
        max: usize,
    ) -> Self {
        Self::InvalidFieldValue {
            field,
            value,
            min,
            max,
        }
    }

    /// Creates an [InvalidCrc7](Self::InvalidCrc7) error.
    pub const fn invalid_crc7(invalid: u8, calculated: u8) -> Self {
        Self::InvalidCrc7 {
            invalid,
            calculated,
        }
    }

    /// Creates an [InvalidLength](Self::InvalidLength) error.
    pub const fn invalid_length(len: usize, expected: usize) -> Self {
        Self::InvalidLength { len, expected }
    }

    /// Creates an unimplemented [Error].
    pub const fn unimplemented() -> Self {
        Self::Unimplemented
    }

    /// Attempts to convert an [`i32`] into an [Error].
    pub const fn from_i32(val: i32) -> Option<Self> {
        match val {
            EINVAL | NEINVAL => Some(Self::Invalid),
            EILSEQ | NEILSEQ => Some(Self::IllegalSequence),
            ETIMEDOUT | NETIMEDOUT => Some(Self::TimedOut),
            ENOMEDIUM | NENOMEDIUM => Some(Self::NoMedium),
            _ => None,
        }
    }
}

impl From<core::array::TryFromSliceError> for Error {
    fn from(err: core::array::TryFromSliceError) -> Self {
        Self::Invalid
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::None => write!(f, "unknown error"),
            Self::Invalid => write!(f, "invalid error"),
            Self::IllegalSequence => write!(f, "illegal command sequence"),
            Self::TimedOut => write!(f, "timed out"),
            Self::NoMedium => write!(f, "no medium"),
            Self::InvalidVariant(err) => write!(f, "invalid enum variant: {err}"),
            Self::InvalidFieldVariant { field, value } => {
                write!(f, "invalid field: {field}, enum variant: {value}")
            }
            Self::InvalidValue { value, min, max } => {
                write!(f, "invalid value: {value}, min: {min}, max: {max}")
            }
            Self::InvalidFieldValue {
                field,
                value,
                min,
                max,
            } => {
                write!(
                    f,
                    "invalid field: {field}, value: {value}, min: {min}, max: {max}"
                )
            }
            Self::InvalidCrc7 {
                invalid,
                calculated,
            } => write!(f, "invalid CRC-7: {invalid}, expected: {calculated}"),
            Self::InvalidLength { len, expected } => {
                write!(f, "invalid structure length: {len}, expected: {expected}")
            }
            Self::Unimplemented => write!(f, "unimplemented"),
        }
    }
}

impl core::error::Error for Error {}