Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

//! Error types for the `dtoolkit` crate.

use core::fmt::{self, Display, Formatter};

use thiserror::Error;

/// An error that can occur when parsing or accessing a device tree.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum FdtError {
/// There was an error parsing the device tree.
#[error("{0}")]
Parse(#[from] FdtParseError),
/// An error that can occur when accessing a standard node or property.
#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
pub enum StandardError {
/// There was an error when converting the property value.
#[error("error occurred when converting the property value: {0}")]
PropertyConversion(#[from] PropertyError),
/// The `status` property of a node had an invalid value.
#[error("Invalid status value")]
InvalidStatus,
Expand All @@ -30,17 +28,6 @@ pub enum FdtError {
/// The required `/memory` node wasn't found.
#[error("/memory node missing")]
MemoryMissing,
/// The size of a prop-encoded-array property wasn't a multiple of the
/// expected element size.
#[error(
"prop-encoded-array property was {size} bytes, but should have been a multiple of {chunk} cells"
)]
PropEncodedArraySizeMismatch {
/// The size in bytes of the prop-encoded-array property.
size: usize,
/// The number of 4 byte cells expected in each element of the array.
chunk: usize,
},
/// Tried to convert part of a prop-encoded-array property to a type which
/// was too small.
#[error("prop-encoded-array field too big for chosen type ({cells} cells)")]
Expand All @@ -53,6 +40,7 @@ pub enum FdtError {
/// An error that can occur when parsing a device tree.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
#[error("{kind} at offset {offset}")]
pub struct FdtParseError {
offset: usize,
/// The type of the error that has occurred.
Expand All @@ -65,11 +53,6 @@ impl FdtParseError {
}
}

impl Display for FdtParseError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} at offset {}", self.kind, self.offset)
}
}
/// The kind of an error that can occur when parsing a device tree.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
Expand Down Expand Up @@ -103,3 +86,26 @@ pub enum FdtErrorKind {
#[error("Memory reservation block has an entry that is unaligned or has invalid size")]
MemReserveInvalid,
}

/// An error that can occur when parsing a property.
#[derive(Debug, Clone, Copy, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum PropertyError {
/// The property's value has an invalid length for the requested conversion.
#[error("property has an invalid length")]
InvalidLength,
/// The property's value is not a valid string.
#[error("property is not a valid string")]
InvalidString,
/// The size of a prop-encoded-array property wasn't a multiple of the
/// expected element size.
#[error(
"prop-encoded-array property was {size} bytes, but should have been a multiple of {chunk} cells"
)]
PropEncodedArraySizeMismatch {
/// The size in bytes of the prop-encoded-array property.
size: usize,
/// The number of 4 byte cells expected in each element of the array.
chunk: usize,
},
}
Loading
Loading