Skip to content

Commit 8898527

Browse files
committed
refactor: validate Fdt on construction
This makes the APIs more ergonomic, as most of the methods that used to return `Result<T, E>`, return `T` now instead, making it much easier to work with, especially in case of iterators. This obviously causes the constructor to run in linear time, which shouldn't be a huge problem considering that any sensible use of the parser will require linear-time traversal time anyway. In case this would be a problem, however, a separate constructor that skips the validation (and causes the traverse methods to panic instead) is added. This also allows us to unify some error handling between in-memory and internal representation APIs, bringing us closer to unifying these two APIs under a common trait, and supporting the "standard nodes & properties" for the internal representation variant.
1 parent 1a6accb commit 8898527

File tree

17 files changed

+740
-772
lines changed

17 files changed

+740
-772
lines changed

src/error.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88

99
//! Error types for the `dtoolkit` crate.
1010
11-
use core::fmt::{self, Display, Formatter};
12-
1311
use thiserror::Error;
1412

15-
/// An error that can occur when parsing or accessing a device tree.
16-
#[derive(Clone, Debug, Eq, Error, PartialEq)]
17-
pub enum FdtError {
18-
/// There was an error parsing the device tree.
19-
#[error("{0}")]
20-
Parse(#[from] FdtParseError),
13+
/// An error that can occur when accessing a standard node or property.
14+
#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
15+
pub enum StandardError {
16+
/// There was an error when converting the property value.
17+
#[error("error occurred when converting the property value: {0}")]
18+
PropertyConversion(#[from] PropertyError),
2119
/// The `status` property of a node had an invalid value.
2220
#[error("Invalid status value")]
2321
InvalidStatus,
@@ -53,6 +51,7 @@ pub enum FdtError {
5351
/// An error that can occur when parsing a device tree.
5452
#[derive(Clone, Debug, Eq, Error, PartialEq)]
5553
#[non_exhaustive]
54+
#[error("{kind} at offset {offset}")]
5655
pub struct FdtParseError {
5756
offset: usize,
5857
/// The type of the error that has occurred.
@@ -65,11 +64,6 @@ impl FdtParseError {
6564
}
6665
}
6766

68-
impl Display for FdtParseError {
69-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
70-
write!(f, "{} at offset {}", self.kind, self.offset)
71-
}
72-
}
7367
/// The kind of an error that can occur when parsing a device tree.
7468
#[derive(Clone, Debug, Eq, Error, PartialEq)]
7569
#[non_exhaustive]
@@ -103,3 +97,15 @@ pub enum FdtErrorKind {
10397
#[error("Memory reservation block has an entry that is unaligned or has invalid size")]
10498
MemReserveInvalid,
10599
}
100+
101+
/// An error that can occur when parsing a property.
102+
#[derive(Debug, Clone, Copy, Error, PartialEq, Eq)]
103+
#[non_exhaustive]
104+
pub enum PropertyError {
105+
/// The property's value has an invalid length for the requested conversion.
106+
#[error("property has an invalid length")]
107+
InvalidLength,
108+
/// The property's value is not a valid string.
109+
#[error("property is not a valid string")]
110+
InvalidString,
111+
}

0 commit comments

Comments
 (0)