Skip to content

Commit cc2e597

Browse files
committed
Use thiserror for FdtParseError and FdtErrorKind.
1 parent 126b517 commit cc2e597

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/error.rs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::fmt::{self, Display, Formatter};
1313
use thiserror::Error;
1414

1515
/// An error that can occur when parsing or accessing a device tree.
16-
#[derive(Clone, Debug, Eq, PartialEq, Error)]
16+
#[derive(Clone, Debug, Eq, Error, PartialEq)]
1717
pub enum FdtError {
1818
/// There was an error parsing the device tree.
1919
#[error("{0}")]
@@ -24,7 +24,7 @@ pub enum FdtError {
2424
}
2525

2626
/// An error that can occur when parsing a device tree.
27-
#[derive(Clone, Debug, Eq, PartialEq)]
27+
#[derive(Clone, Debug, Eq, Error, PartialEq)]
2828
#[non_exhaustive]
2929
pub struct FdtParseError {
3030
offset: usize,
@@ -38,61 +38,41 @@ impl FdtParseError {
3838
}
3939
}
4040

41+
impl Display for FdtParseError {
42+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
43+
write!(f, "{} at offset {}", self.kind, self.offset)
44+
}
45+
}
4146
/// The kind of an error that can occur when parsing a device tree.
42-
#[derive(Clone, Debug, Eq, PartialEq)]
47+
#[derive(Clone, Debug, Eq, Error, PartialEq)]
4348
#[non_exhaustive]
4449
pub enum FdtErrorKind {
4550
/// The magic number of the device tree is invalid.
51+
#[error("Invalid FDT magic number")]
4652
InvalidMagic,
4753
/// The Device Tree version is not supported by this library.
54+
#[error("FDT version {0} is not supported")]
4855
UnsupportedVersion(u32),
4956
/// The length of the device tree is invalid.
57+
#[error("Invalid FDT length")]
5058
InvalidLength,
5159
/// The header failed validation.
60+
#[error("FDT header has failed validation: {0}")]
5261
InvalidHeader(&'static str),
5362
/// An invalid token was encountered.
63+
#[error("Bad FDT token: {0:#x}")]
5464
BadToken(u32),
5565
/// A read from data at invalid offset was attempted.
66+
#[error("Invalid offset in FDT")]
5667
InvalidOffset,
5768
/// An invalid string was encountered.
69+
#[error("Invalid string in FDT")]
5870
InvalidString,
5971
/// Memory reservation block has not been terminated with a null entry.
72+
#[error("Memory reservation block was not terminated with a null entry")]
6073
MemReserveNotTerminated,
6174
/// Memory reservation block has an entry that is unaligned or has invalid
6275
/// size.
76+
#[error("Memory reservation block has an entry that is unaligned or has invalid size")]
6377
MemReserveInvalid,
6478
}
65-
66-
impl Display for FdtParseError {
67-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
68-
write!(f, "{} at offset {}", self.kind, self.offset)
69-
}
70-
}
71-
72-
impl Display for FdtErrorKind {
73-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
74-
match self {
75-
FdtErrorKind::InvalidMagic => write!(f, "invalid FDT magic number"),
76-
FdtErrorKind::UnsupportedVersion(version) => {
77-
write!(f, "the FDT version {version} is not supported")
78-
}
79-
FdtErrorKind::InvalidLength => write!(f, "invalid FDT length"),
80-
FdtErrorKind::InvalidHeader(msg) => {
81-
write!(f, "FDT header has failed validation: {msg}")
82-
}
83-
FdtErrorKind::BadToken(token) => write!(f, "bad FDT token: 0x{token:x}"),
84-
FdtErrorKind::InvalidOffset => write!(f, "invalid offset in FDT"),
85-
FdtErrorKind::InvalidString => write!(f, "invalid string in FDT"),
86-
FdtErrorKind::MemReserveNotTerminated => write!(
87-
f,
88-
"memory reservation block not terminated with a null entry"
89-
),
90-
FdtErrorKind::MemReserveInvalid => write!(
91-
f,
92-
"memory reservation block has an entry that is unaligned or has invalid size"
93-
),
94-
}
95-
}
96-
}
97-
98-
impl core::error::Error for FdtParseError {}

0 commit comments

Comments
 (0)