Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Not for GraphNameMatcher and TermMatcher #174

Merged
merged 4 commits into from
Jul 30, 2024
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
19 changes: 18 additions & 1 deletion api/src/term/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ mod _datatype_matcher;
mod _graph_name_matcher;
mod _language_tag_matcher;
mod _matcher_ref;
mod _not;
mod _term_matcher_gn;
mod _trait;

pub use _any::*;
pub use _any::Any;
pub use _datatype_matcher::*;
pub use _graph_name_matcher::*;
pub use _language_tag_matcher::*;
pub use _matcher_ref::*;
pub use _not::Not;
pub use _term_matcher_gn::*;
pub use _trait::*;

Expand Down Expand Up @@ -66,10 +68,12 @@ mod test {
is_graph_name_matcher([Some(T1), Some(T2), None]);
is_graph_name_matcher(&[Some(T1), Some(T2), None][..]);
is_graph_name_matcher(|t: Option<SimpleTerm>| t.is_some());
is_graph_name_matcher(Not(|t: Option<SimpleTerm>| t.is_some()));
is_graph_name_matcher([T1, T2].gn());
is_graph_name_matcher(Some(TermKind::Iri));
is_graph_name_matcher(Some(([T1], [T2], [T3])));
is_graph_name_matcher([Some(T1)].matcher_ref());
is_graph_name_matcher(Not([Some(T1)].matcher_ref()));
}

#[test]
Expand Down Expand Up @@ -177,6 +181,12 @@ mod test {
assert!(TermMatcher::constant(&Any).is_none());
}

#[test]
fn not() {
assert!(Not(TermKind::BlankNode).matches(&T1));
assert!(Not([T1, T2]).matches(&T3));
}

#[test]
fn datatype_matcher() {
let m1 = Any * xsd::string; // testing Mul<NsTerm>
Expand Down Expand Up @@ -332,6 +342,13 @@ mod test {
assert!(GraphNameMatcher::constant(&Any).is_none());
}

#[test]
fn graph_name_not() {
assert!(GraphNameMatcher::matches(&Not([DEFAULT]), Some(&T1)));
assert!(!GraphNameMatcher::matches(&Not([T1, T2].gn()), Some(&T2)));
assert!(GraphNameMatcher::matches(&Not([T1, T2].gn()), Some(&T3)));
}

#[test]
fn graph_name_term_matcher_gn() {
let a1 = [T1].gn();
Expand Down
8 changes: 8 additions & 0 deletions api/src/term/matcher/_graph_name_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,11 @@ impl GraphNameMatcher for Any {
true
}
}

impl<M: GraphNameMatcher> GraphNameMatcher for Not<M> {
type Term = SimpleTerm<'static>; // not actually used

fn matches<T2: Term + ?Sized>(&self, graph_name: GraphName<&T2>) -> bool {
!self.0.matches(graph_name)
}
}
12 changes: 12 additions & 0 deletions api/src/term/matcher/_not.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::*;

/// Matches on the inverse of the inner [`Term`] or [`GraphName`]
pub struct Not<M>(pub M);

impl<M: TermMatcher> TermMatcher for Not<M> {
type Term = SimpleTerm<'static>; // not actually used

fn matches<T2: Term + ?Sized>(&self, term: &T2) -> bool {
!self.0.matches(term)
}
}