svd_generator/
error.rs

1use alloc::string::String;
2use core::fmt;
3
4/// Convenience alias for [`Result`](core::result::Result) type for the library.
5pub type Result<T> = core::result::Result<T, Error>;
6
7/// Represents error types for the library.
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub enum Error {
10    Io(String),
11    DeviceTree(String),
12    Svd(String),
13    InvalidBitRange,
14    InvalidWriteConstraint((u64, u64)),
15}
16
17impl From<fdt::Error> for Error {
18    fn from(err: fdt::Error) -> Self {
19        Self::DeviceTree(format!("{err:?}"))
20    }
21}
22
23#[cfg(feature = "std")]
24impl From<std::io::Error> for Error {
25    fn from(err: std::io::Error) -> Self {
26        Self::Io(format!("{err}"))
27    }
28}
29
30#[cfg(feature = "std")]
31impl From<svd::SvdError> for Error {
32    fn from(err: svd::SvdError) -> Self {
33        Self::Svd(format!("{err}"))
34    }
35}
36
37#[cfg(feature = "std")]
38impl From<svd_encoder::EncodeError> for Error {
39    fn from(err: svd_encoder::EncodeError) -> Error {
40        Self::Svd(format!("{err}"))
41    }
42}
43
44impl fmt::Display for Error {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::Io(err) => write!(f, "I/O error: {err}"),
48            Self::DeviceTree(err) => write!(f, "DeviceTree error: {err}"),
49            Self::Svd(err) => write!(f, "SVD error: {err}"),
50            Self::InvalidBitRange => write!(f, "invalid bit range"),
51            Self::InvalidWriteConstraint((min, max)) => write!(
52                f,
53                "write-constraint reversed range, min: {min} > max: {max}"
54            ),
55        }
56    }
57}