Skip to content

Commit 2ccdfec

Browse files
qwandorm4tx
authored andcommitted
Wrap Option in Result rather than vice-versa for find_node.
This makes more sense logically, and makes error handling easier.
1 parent d3d8b18 commit 2ccdfec

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/fdt/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ impl<'a> Fdt<'a> {
390390
/// first. [`DeviceTree`](crate::model::DeviceTree) stores the nodes in a
391391
/// hash map for constant-time lookup.
392392
///
393+
/// # Errors
394+
///
395+
/// Returns an [`FdtErrorKind::InvalidLength`] if the FDT structure is
396+
/// truncated or, an [`FdtErrorKind::BadToken`] if an unexpected token is
397+
/// encountered while searching, or an [`FdtErrorKind::InvalidString`] if a
398+
/// node has an invalid name.
399+
///
393400
/// # Examples
394401
///
395402
/// ```
@@ -409,17 +416,13 @@ impl<'a> Fdt<'a> {
409416
/// let node = fdt.find_node("/child2@42").unwrap().unwrap();
410417
/// assert_eq!(node.name().unwrap(), "child2@42");
411418
/// ```
412-
#[must_use]
413-
pub fn find_node(&self, path: &str) -> Option<Result<FdtNode<'_>, FdtError>> {
419+
pub fn find_node(&self, path: &str) -> Result<Option<FdtNode<'_>>, FdtError> {
414420
if !path.starts_with('/') {
415-
return None;
421+
return Ok(None);
416422
}
417-
let mut current_node = match self.root() {
418-
Ok(node) => node,
419-
Err(e) => return Some(Err(e)),
420-
};
423+
let mut current_node = self.root()?;
421424
if path == "/" {
422-
return Some(Ok(current_node));
425+
return Ok(Some(current_node));
423426
}
424427
for component in path.split('/').filter(|s| !s.is_empty()) {
425428
let include_address = component.contains('@');
@@ -433,11 +436,11 @@ impl<'a> Fdt<'a> {
433436
})
434437
}) {
435438
Some(Ok(node)) => current_node = node,
436-
Some(Err(e)) => return Some(Err(e)),
437-
None => return None,
439+
Some(Err(e)) => return Err(e),
440+
None => return Ok(None),
438441
}
439442
}
440-
Some(Ok(current_node))
443+
Ok(Some(current_node))
441444
}
442445

443446
pub(crate) fn read_token(&self, offset: usize) -> Result<FdtToken, FdtError> {

tests/fdt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ fn find_node_by_path() {
141141
let d = fdt.find_node("/d").unwrap().unwrap();
142142
assert_eq!(d.name().unwrap(), "d");
143143

144-
assert!(fdt.find_node("/a/c").is_none());
145-
assert!(fdt.find_node("/x").is_none());
146-
assert!(fdt.find_node("").is_none());
144+
assert!(fdt.find_node("/a/c").unwrap().is_none());
145+
assert!(fdt.find_node("/x").unwrap().is_none());
146+
assert!(fdt.find_node("").unwrap().is_none());
147147
}
148148

149149
#[macro_export]

0 commit comments

Comments
 (0)