Skip to content

Commit 3a3bad9

Browse files
committed
address review comments
1 parent 64cd35d commit 3a3bad9

5 files changed

Lines changed: 18 additions & 23 deletions

File tree

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ pub enum PropertyError {
119119
},
120120
}
121121

122-
#[cfg(feature = "write")]
123122
/// An error that can occur when building or modifying a device tree model.
124123
#[derive(Debug, Clone, PartialEq, Eq, Error)]
125124
#[non_exhaustive]
125+
#[cfg(feature = "write")]
126126
pub enum ModelError {
127127
/// The node name is invalid.
128128
#[error("Invalid node name: '{0}'")]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mod memreserve;
8888
#[cfg(feature = "write")]
8989
pub mod model;
9090
pub mod standard;
91-
pub(crate) mod validate;
91+
mod validate;
9292

9393
use core::ffi::CStr;
9494
use core::fmt::{self, Display, Formatter};

src/model/node.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use indexmap::IndexMap;
1313
use twox_hash::xxhash64;
1414

1515
use super::property::DeviceTreeProperty;
16+
use crate::error::ModelError;
1617
use crate::{Node, Property};
1718

1819
/// A mutable, in-memory representation of a device tree node.
@@ -107,8 +108,7 @@ impl DeviceTreeNode {
107108
///
108109
/// # Errors
109110
///
110-
/// Returns a [`ModelError`](crate::error::ModelError) if the node name is
111-
/// invalid.
111+
/// Returns a [`ModelError::InvalidNodeName`] if the node name is invalid.
112112
///
113113
/// # Examples
114114
///
@@ -119,10 +119,10 @@ impl DeviceTreeNode {
119119
/// let node = DeviceTreeNode::new("my-node").unwrap();
120120
/// assert_eq!((&node).name(), "my-node");
121121
/// ```
122-
pub fn new(name: impl Into<String>) -> Result<Self, crate::error::ModelError> {
122+
pub fn new(name: impl Into<String>) -> Result<Self, ModelError> {
123123
let name = name.into();
124124
if !crate::validate::is_valid_node_name(&name) {
125-
return Err(crate::error::ModelError::InvalidNodeName(name));
125+
return Err(ModelError::InvalidNodeName(name));
126126
}
127127
Ok(Self::new_unchecked(name))
128128
}
@@ -140,11 +140,8 @@ impl DeviceTreeNode {
140140
///
141141
/// # Errors
142142
///
143-
/// Returns a [`ModelError`](crate::error::ModelError) if the node name is
144-
/// invalid.
145-
pub fn builder(
146-
name: impl Into<String>,
147-
) -> Result<DeviceTreeNodeBuilder, crate::error::ModelError> {
143+
/// Returns a [`ModelError::InvalidNodeName`] if the node name is invalid.
144+
pub fn builder(name: impl Into<String>) -> Result<DeviceTreeNodeBuilder, ModelError> {
148145
DeviceTreeNodeBuilder::new(name)
149146
}
150147

@@ -325,7 +322,7 @@ pub struct DeviceTreeNodeBuilder {
325322
}
326323

327324
impl DeviceTreeNodeBuilder {
328-
fn new(name: impl Into<String>) -> Result<Self, crate::error::ModelError> {
325+
fn new(name: impl Into<String>) -> Result<Self, ModelError> {
329326
Ok(Self {
330327
node: DeviceTreeNode::new(name)?,
331328
})

src/model/property.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use alloc::vec::Vec;
1111
use core::str;
1212

1313
use crate::Property;
14+
use crate::error::ModelError;
1415

1516
/// A mutable, in-memory representation of a device tree property.
1617
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -34,8 +35,8 @@ impl DeviceTreeProperty {
3435
///
3536
/// # Errors
3637
///
37-
/// Returns a [`ModelError`](crate::error::ModelError) if the property name
38-
/// is invalid.
38+
/// Returns a [`ModelError::InvalidPropertyName`] if the property name is
39+
/// invalid.
3940
///
4041
/// # Examples
4142
///
@@ -47,13 +48,10 @@ impl DeviceTreeProperty {
4748
/// assert_eq!((&prop).name(), "my-prop");
4849
/// assert_eq!((&prop).value(), &[1, 2, 3, 4]);
4950
/// ```
50-
pub fn new(
51-
name: impl Into<String>,
52-
value: impl Into<Vec<u8>>,
53-
) -> Result<Self, crate::error::ModelError> {
51+
pub fn new(name: impl Into<String>, value: impl Into<Vec<u8>>) -> Result<Self, ModelError> {
5452
let name = name.into();
5553
if !crate::validate::is_valid_property_name(&name) {
56-
return Err(crate::error::ModelError::InvalidPropertyName(name));
54+
return Err(ModelError::InvalidPropertyName(name));
5755
}
5856
Ok(Self {
5957
name,

src/validate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
//! Validation functions for Device Tree node and property names.
1010
11-
fn is_valid_node_char(c: char) -> bool {
12-
c.is_ascii_alphanumeric() || c == ',' || c == '.' || c == '_' || c == '+' || c == '-'
11+
const fn is_valid_node_char(c: char) -> bool {
12+
c.is_ascii_alphanumeric() || matches!(c, ',' | '.' | '_' | '+' | '-')
1313
}
1414

15-
fn is_valid_property_char(c: char) -> bool {
16-
is_valid_node_char(c) || c == '?' || c == '#'
15+
const fn is_valid_property_char(c: char) -> bool {
16+
is_valid_node_char(c) || matches!(c, '?' | '#')
1717
}
1818

1919
/// Validates a node name according to the Devicetree Specification.

0 commit comments

Comments
 (0)