Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Simplify lifetime declaration for Matcher #7

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Iterator<Item = &'a Value> + 'a>;
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a>;
}

pub struct RootSelector {}

impl Matcher for RootSelector {
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
Box::new(iter::once(node))
}
}
Expand All @@ -42,7 +42,7 @@ pub fn new_child_matcher(name: String) -> Child {
}

impl Matcher for Child {
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
fn select<'a>(&'a self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
if node.is_object() {
let mapping = node.as_object().unwrap();
if mapping.contains_key(&self.name) {
Expand All @@ -65,15 +65,7 @@ pub fn new_union(elements: Vec<Box<dyn Matcher>>) -> Union {
}

impl Matcher for Union {
fn select<'a, 'b>(&'a self, node: &'b Value) -> Box<dyn Iterator<Item = &'b Value> + '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<dyn Iterator<Item = &'a Value> + 'a> {
Box::new(self.elements.iter().flat_map(move |it| it.select(node)))
}
}
4 changes: 2 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum FindError {
}

pub trait Path {
fn find<'a>(&self, document: &'a Value) -> Result<Vec<&'a Value>, FindError>;
fn find<'a>(&'a self, document: &'a Value) -> Result<Vec<&'a Value>, FindError>;
}

struct SelectorPath {
Expand All @@ -24,7 +24,7 @@ pub fn new<'a>(matchers: Vec<Box<dyn matchers::Matcher>>) -> impl Path + 'a {
}

impl Path for SelectorPath {
fn find<'a>(&self, document: &'a Value) -> Result<Vec<&'a Value>, FindError> {
fn find<'a>(&'a self, document: &'a Value) -> Result<Vec<&'a Value>, FindError> {
// pass nodes, starting with document alone, through each matcher in turn
Ok((&self.matchers)
.iter()
Expand Down