Skip to content

Commit

Permalink
fix: Make Node::filtered_parent recursive as it was meant to be (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcampbell committed Jan 6, 2023
1 parent 1b74804 commit d2faef5
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions consumer/src/node.rs
Expand Up @@ -91,8 +91,13 @@ impl<'a> Node<'a> {
}

pub fn filtered_parent(&self, filter: &impl Fn(&Node) -> FilterResult) -> Option<Node<'a>> {
self.parent()
.filter(|parent| filter(parent) == FilterResult::Include)
self.parent().and_then(move |parent| {
if filter(&parent) == FilterResult::Include {
Some(parent)
} else {
parent.filtered_parent(filter)
}
})
}

pub fn parent_and_index(self) -> Option<(Node<'a>, usize)> {
Expand Down Expand Up @@ -785,6 +790,25 @@ mod tests {
.is_none());
}

#[test]
fn filtered_parent() {
let tree = test_tree();
assert_eq!(
ROOT_ID,
tree.read()
.node_by_id(STATIC_TEXT_1_0_ID)
.unwrap()
.filtered_parent(&test_tree_filter)
.unwrap()
.id()
);
assert!(tree
.read()
.root()
.filtered_parent(&test_tree_filter)
.is_none());
}

#[test]
fn deepest_first_filtered_child() {
let tree = test_tree();
Expand Down

0 comments on commit d2faef5

Please sign in to comment.