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
25 changes: 14 additions & 11 deletions src/fdt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ impl<'a> Fdt<'a> {
/// first. [`DeviceTree`](crate::model::DeviceTree) stores the nodes in a
/// hash map for constant-time lookup.
///
/// # Errors
///
/// Returns an [`FdtErrorKind::InvalidLength`] if the FDT structure is
/// truncated or, an [`FdtErrorKind::BadToken`] if an unexpected token is
/// encountered while searching, or an [`FdtErrorKind::InvalidString`] if a
/// node has an invalid name.
///
/// # Examples
///
/// ```
Expand All @@ -409,17 +416,13 @@ impl<'a> Fdt<'a> {
/// let node = fdt.find_node("/child2@42").unwrap().unwrap();
/// assert_eq!(node.name().unwrap(), "child2@42");
/// ```
#[must_use]
pub fn find_node(&self, path: &str) -> Option<Result<FdtNode<'_>, FdtError>> {
pub fn find_node(&self, path: &str) -> Result<Option<FdtNode<'_>>, FdtError> {
if !path.starts_with('/') {
return None;
return Ok(None);
}
let mut current_node = match self.root() {
Ok(node) => node,
Err(e) => return Some(Err(e)),
};
let mut current_node = self.root()?;
if path == "/" {
return Some(Ok(current_node));
return Ok(Some(current_node));
}
for component in path.split('/').filter(|s| !s.is_empty()) {
let include_address = component.contains('@');
Expand All @@ -433,11 +436,11 @@ impl<'a> Fdt<'a> {
})
}) {
Some(Ok(node)) => current_node = node,
Some(Err(e)) => return Some(Err(e)),
None => return None,
Some(Err(e)) => return Err(e),
None => return Ok(None),
}
}
Some(Ok(current_node))
Ok(Some(current_node))
}

pub(crate) fn read_token(&self, offset: usize) -> Result<FdtToken, FdtError> {
Expand Down
6 changes: 3 additions & 3 deletions tests/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ fn find_node_by_path() {
let d = fdt.find_node("/d").unwrap().unwrap();
assert_eq!(d.name().unwrap(), "d");

assert!(fdt.find_node("/a/c").is_none());
assert!(fdt.find_node("/x").is_none());
assert!(fdt.find_node("").is_none());
assert!(fdt.find_node("/a/c").unwrap().is_none());
assert!(fdt.find_node("/x").unwrap().is_none());
assert!(fdt.find_node("").unwrap().is_none());
}

#[macro_export]
Expand Down