sdmmc_core/
result.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/// 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 },
}

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 }
    }

    /// 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 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}")
            }
        }
    }
}

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