Skip to content

Commit 59a4384

Browse files
committed
Use iterators more idiomatically
1 parent bb21517 commit 59a4384

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/parser.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,17 @@ pub fn parse(selector: &str) -> Result<Box<dyn Path>, String> {
3939

4040
impl Path for SelectorPath<'_> {
4141
fn find<'a>(&self, document: &'a Value) -> Result<Vec<&'a Value>, FindError> {
42-
let mut nodes = Vec::new();
43-
nodes.push(document);
44-
45-
// pass nodes through each matcher in turn
46-
for m in &self.matchers {
47-
let mut selected = Vec::new();
48-
for n in &nodes {
49-
for r in m.select(n) {
50-
selected.push(r);
51-
}
52-
}
53-
nodes = selected;
54-
}
55-
56-
Ok(nodes)
42+
// pass nodes, starting with document alone, through each matcher in turn
43+
Ok((&self.matchers)
44+
.iter()
45+
.fold(doc_node(document), |nodes, matcher| {
46+
nodes.iter().flat_map(|node| matcher.select(node)).collect()
47+
}))
5748
}
5849
}
50+
51+
fn doc_node<'a>(document: &'a Value) -> Vec<&'a Value> {
52+
let mut nodes = Vec::new();
53+
nodes.push(document);
54+
nodes
55+
}

0 commit comments

Comments
 (0)