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