diff --git a/src/matchers.rs b/src/matchers.rs index 7b60c90..78f5b08 100644 --- a/src/matchers.rs +++ b/src/matchers.rs @@ -10,13 +10,13 @@ use std::iter; // Matcher maps a node to a list of nodes. If the input node is not matched by the matcher or // the matcher does not select any subnodes of the input node, then the result is empty. pub trait Matcher { - fn select<'a>(&self, node: &'a Value) -> Box + 'a>; + fn select<'a>(&'a self, node: &'a Value) -> Box + 'a>; } pub struct RootSelector {} impl Matcher for RootSelector { - fn select<'a>(&self, node: &'a Value) -> Box + 'a> { + fn select<'a>(&'a self, node: &'a Value) -> Box + 'a> { Box::new(iter::once(node)) } } @@ -42,7 +42,7 @@ pub fn new_child_matcher(name: String) -> Child { } impl Matcher for Child { - fn select<'a>(&self, node: &'a Value) -> Box + 'a> { + fn select<'a>(&'a self, node: &'a Value) -> Box + 'a> { if node.is_object() { let mapping = node.as_object().unwrap(); if mapping.contains_key(&self.name) { @@ -65,15 +65,7 @@ pub fn new_union(elements: Vec>) -> Union { } impl Matcher for Union { - fn select<'a, 'b>(&'a self, node: &'b Value) -> Box + 'b> { - // union of matches of the matchers in the union - let mut u = vec![]; - for m in &self.elements { - let m_selection = m.select(node); - for s in m_selection { - u.push(s); - } - } - Box::new(u.into_iter()) + fn select<'a>(&'a self, node: &'a Value) -> Box + 'a> { + Box::new(self.elements.iter().flat_map(move |it| it.select(node))) } } diff --git a/src/path.rs b/src/path.rs index a2e0a0a..a02dd64 100644 --- a/src/path.rs +++ b/src/path.rs @@ -12,7 +12,7 @@ pub enum FindError { } pub trait Path { - fn find<'a>(&self, document: &'a Value) -> Result, FindError>; + fn find<'a>(&'a self, document: &'a Value) -> Result, FindError>; } struct SelectorPath { @@ -24,7 +24,7 @@ pub fn new<'a>(matchers: Vec>) -> impl Path + 'a { } impl Path for SelectorPath { - fn find<'a>(&self, document: &'a Value) -> Result, FindError> { + fn find<'a>(&'a self, document: &'a Value) -> Result, FindError> { // pass nodes, starting with document alone, through each matcher in turn Ok((&self.matchers) .iter()