riscv_pac/
result.rs

1use core::fmt;
2
3/// Convenience alias for the [Result](core::result::Result) type for the library.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// Represents error variants for the library.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum Error {
9    /// Attempted out-of-bounds access.
10    IndexOutOfBounds {
11        index: usize,
12        min: usize,
13        max: usize,
14    },
15    /// Invalid field value.
16    InvalidFieldValue {
17        field: &'static str,
18        value: usize,
19        bitmask: usize,
20    },
21    /// Invalid value of a register field that does not match any known variants.
22    InvalidFieldVariant { field: &'static str, value: usize },
23    /// Invalid value.
24    InvalidValue { value: usize, bitmask: usize },
25    /// Invalid value that does not match any known variants.
26    InvalidVariant(usize),
27    /// Unimplemented function or type.
28    Unimplemented,
29}
30
31impl fmt::Display for Error {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::IndexOutOfBounds { index, min, max } => write!(
35                f,
36                "out-of-bounds access, index: {index}, min: {min}, max: {max}"
37            ),
38            Self::InvalidFieldValue {
39                field,
40                value,
41                bitmask,
42            } => write!(
43                f,
44                "invalid {field} field value: {value:#x}, valid bitmask: {bitmask:#x}",
45            ),
46            Self::InvalidFieldVariant { field, value } => {
47                write!(f, "invalid {field} field variant: {value:#x}")
48            }
49            Self::InvalidValue { value, bitmask } => {
50                write!(f, "invalid value: {value:#x}, valid bitmask: {bitmask:#x}",)
51            }
52            Self::InvalidVariant(value) => {
53                write!(f, "invalid variant: {value:#x}")
54            }
55            Self::Unimplemented => write!(f, "unimplemented"),
56        }
57    }
58}