sdmmc_core/
result.rs

1/// Convenience alias for the MMC [`Result`](core::result::Result) type.
2pub type Result<T> = core::result::Result<T, Error>;
3
4pub const EINVAL: i32 = 22;
5pub const NEINVAL: i32 = -EINVAL;
6pub const EILSEQ: i32 = 84;
7pub const NEILSEQ: i32 = -EILSEQ;
8pub const ETIMEDOUT: i32 = 110;
9pub const NETIMEDOUT: i32 = -ETIMEDOUT;
10pub const ENOMEDIUM: i32 = 123;
11pub const NENOMEDIUM: i32 = -ENOMEDIUM;
12
13/// Represents MMC error types.
14#[repr(i32)]
15#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
16pub enum Error {
17    #[default]
18    None = 0,
19    /// Invalid argument.
20    Invalid = 22,
21    /// Illegal byte sequence.
22    IllegalSequence = 84,
23    /// Connection timed out.
24    TimedOut = 110,
25    /// No medium found.
26    NoMedium = 123,
27    /// Invalid variant.
28    InvalidVariant(usize),
29    /// Invalid field enum variant.
30    InvalidFieldVariant { field: &'static str, value: usize },
31    /// Invalid value.
32    InvalidValue {
33        value: usize,
34        min: usize,
35        max: usize,
36    },
37    /// Invalid field value.
38    InvalidFieldValue {
39        field: &'static str,
40        value: usize,
41        min: usize,
42        max: usize,
43    },
44    /// Invalid CRC-7.
45    InvalidCrc7 { invalid: u8, calculated: u8 },
46    /// Invalid length, with expected value.
47    InvalidLength { len: usize, expected: usize },
48    /// Indicates an unimplemented function or feature.
49    Unimplemented,
50}
51
52impl Error {
53    /// Creates an [InvalidFieldVariant](Self::InvalidFieldVariant) error.
54    pub const fn invalid_field_variant(field: &'static str, value: usize) -> Self {
55        Self::InvalidFieldVariant { field, value }
56    }
57
58    /// Creates an [InvalidFieldValue](Self::InvalidFieldVariant) error.
59    pub const fn invalid_field_value(
60        field: &'static str,
61        value: usize,
62        min: usize,
63        max: usize,
64    ) -> Self {
65        Self::InvalidFieldValue {
66            field,
67            value,
68            min,
69            max,
70        }
71    }
72
73    /// Creates an [InvalidCrc7](Self::InvalidCrc7) error.
74    pub const fn invalid_crc7(invalid: u8, calculated: u8) -> Self {
75        Self::InvalidCrc7 {
76            invalid,
77            calculated,
78        }
79    }
80
81    /// Creates an [InvalidLength](Self::InvalidLength) error.
82    pub const fn invalid_length(len: usize, expected: usize) -> Self {
83        Self::InvalidLength { len, expected }
84    }
85
86    /// Creates an unimplemented [Error].
87    pub const fn unimplemented() -> Self {
88        Self::Unimplemented
89    }
90
91    /// Attempts to convert an [`i32`] into an [Error].
92    pub const fn from_i32(val: i32) -> Option<Self> {
93        match val {
94            EINVAL | NEINVAL => Some(Self::Invalid),
95            EILSEQ | NEILSEQ => Some(Self::IllegalSequence),
96            ETIMEDOUT | NETIMEDOUT => Some(Self::TimedOut),
97            ENOMEDIUM | NENOMEDIUM => Some(Self::NoMedium),
98            _ => None,
99        }
100    }
101}
102
103impl From<core::array::TryFromSliceError> for Error {
104    fn from(err: core::array::TryFromSliceError) -> Self {
105        Self::Invalid
106    }
107}
108
109impl core::fmt::Display for Error {
110    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
111        match self {
112            Self::None => write!(f, "unknown error"),
113            Self::Invalid => write!(f, "invalid error"),
114            Self::IllegalSequence => write!(f, "illegal command sequence"),
115            Self::TimedOut => write!(f, "timed out"),
116            Self::NoMedium => write!(f, "no medium"),
117            Self::InvalidVariant(err) => write!(f, "invalid enum variant: {err}"),
118            Self::InvalidFieldVariant { field, value } => {
119                write!(f, "invalid field: {field}, enum variant: {value}")
120            }
121            Self::InvalidValue { value, min, max } => {
122                write!(f, "invalid value: {value}, min: {min}, max: {max}")
123            }
124            Self::InvalidFieldValue {
125                field,
126                value,
127                min,
128                max,
129            } => {
130                write!(
131                    f,
132                    "invalid field: {field}, value: {value}, min: {min}, max: {max}"
133                )
134            }
135            Self::InvalidCrc7 {
136                invalid,
137                calculated,
138            } => write!(f, "invalid CRC-7: {invalid}, expected: {calculated}"),
139            Self::InvalidLength { len, expected } => {
140                write!(f, "invalid structure length: {len}, expected: {expected}")
141            }
142            Self::Unimplemented => write!(f, "unimplemented"),
143        }
144    }
145}
146
147impl core::error::Error for Error {}