Skip to content

Commit

Permalink
Implement resolving paths in DevTrees
Browse files Browse the repository at this point in the history
  • Loading branch information
Kritzefitz committed Oct 23, 2022
1 parent ac6eb7c commit 1c6b7be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/base/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,23 @@ impl<'a, 'dt: 'a> DevTreeNode<'a, 'dt> {
pub fn next_sibling(&self) -> Result<Option<DevTreeNode<'a, 'dt>>> {
self.sibling_nodes().next()
}

/// Returns the [`DevTreeNode`] at the given path below this node.
pub fn node_at_path<'s, I>(&self, path: I) -> Result<Option<DevTreeNode<'a, 'dt>>>
where
I: Iterator<Item = &'s str>,
{
let mut current = self.clone();
for component in path {
if let Some(found) = current
.child_nodes()
.find(|node| Ok(node.name()? == component))?
{
current = found;
} else {
return Ok(None);
}
}
Ok(Some(current))
}
}
12 changes: 12 additions & 0 deletions src/base/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,16 @@ impl<'dt> DevTree<'dt> {
pub fn root(&self) -> Result<Option<DevTreeNode<'_, 'dt>>> {
self.nodes().next()
}

/// Returns the [`DevTreeNode`] at the given path
pub fn node_at_path<'s, I>(&self, path: I) -> Result<Option<DevTreeNode<'_, 'dt>>>
where
I: Iterator<Item = &'s str>,
{
if let Some(root) = self.root()? {
root.node_at_path(path)
} else {
Ok(None)
}
}
}

0 comments on commit 1c6b7be

Please sign in to comment.