Skip to content

Commit e73a878

Browse files
committed
User iterator rather than vector in matcher
1 parent 3599cc1 commit e73a878

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

src/matchers.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
*/
66

77
use serde_json::Value;
8+
use std::iter::once;
89

910
// Matcher maps a node to a list of nodes. If the input node is not matched by the matcher or
1011
// the matcher does not select any subnodes of the input node, then the result is empty.
1112
pub trait Matcher {
12-
fn select<'a>(&self, node: &'a Value) -> Vec<&'a Value>;
13+
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a>;
1314
}
1415

1516
pub struct RootSelector {}
1617

1718
impl Matcher for RootSelector {
18-
fn select<'a>(&self, node: &'a Value) -> Vec<&'a Value> {
19-
let mut results = Vec::new();
20-
results.push(node);
21-
results
19+
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
20+
Box::new(once(node))
2221
}
2322
}

src/path.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@ impl Path for SelectorPath<'_> {
2828
// pass nodes, starting with document alone, through each matcher in turn
2929
Ok((&self.matchers)
3030
.iter()
31-
.fold(doc_node(document), |nodes, matcher| {
31+
.fold(vec![document], |nodes, matcher| {
3232
nodes.iter().flat_map(|node| matcher.select(node)).collect()
3333
}))
3434
}
3535
}
36-
37-
fn doc_node<'a>(document: &'a Value) -> Vec<&'a Value> {
38-
let mut nodes = Vec::new();
39-
nodes.push(document);
40-
nodes
41-
}

0 commit comments

Comments
 (0)