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
use alloc::string::String;
use core::fmt;

/// Convenience alias for [`Result`](core::result::Result) type for the library.
pub type Result<T> = core::result::Result<T, Error>;

/// Represents error types for the library.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
    Io(String),
    DeviceTree(String),
    Svd(String),
    InvalidBitRange,
}

impl From<fdt::FdtError> for Error {
    fn from(err: fdt::FdtError) -> Self {
        Self::DeviceTree(format!("{err:?}"))
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Self::Io(format!("{err}"))
    }
}

#[cfg(feature = "std")]
impl From<svd::SvdError> for Error {
    fn from(err: svd::SvdError) -> Self {
        Self::Svd(format!("{err}"))
    }
}

#[cfg(feature = "std")]
impl From<svd_encoder::EncodeError> for Error {
    fn from(err: svd_encoder::EncodeError) -> Error {
        Self::Svd(format!("{err}"))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(err) => write!(f, "I/O error: {err}"),
            Self::DeviceTree(err) => write!(f, "DeviceTree error: {err}"),
            Self::Svd(err) => write!(f, "SVD error: {err}"),
            Self::InvalidBitRange => write!(f, "invalid bit range"),
        }
    }
}