Skip to content

Commit

Permalink
Update syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
nwagner84 committed Jan 19, 2024
1 parent 2c488c1 commit 763f3f2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 60 deletions.
16 changes: 9 additions & 7 deletions crates/pica-matcher/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fmt::{self, Display};

use winnow::ascii::{multispace0, multispace1};
use winnow::combinator::{alt, delimited, fold_repeat, preceded};
use winnow::combinator::{
alt, delimited, fold_repeat, preceded, terminated,
};
use winnow::error::{ContextError, ParserError};
use winnow::stream::{AsChar, Stream, StreamIsPartial};
use winnow::token::{tag, take_till};
Expand Down Expand Up @@ -132,18 +134,18 @@ pub(crate) fn parse_relational_op_usize(

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Quantifier {
Forall,
All,
#[default]
Exists,
Any,
}

#[inline]
pub(crate) fn parse_quantifier(i: &mut &[u8]) -> PResult<Quantifier> {
alt((
"∀".value(Quantifier::Forall),
"ForAll".value(Quantifier::Forall),
"".value(Quantifier::Exists),
"Exists".value(Quantifier::Exists),
terminated("ALL".value(Quantifier::All), multispace1),
terminated("ANY".value(Quantifier::Any), multispace1),
"".value(Quantifier::All),
"".value(Quantifier::Any),
))
.parse_next(i)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/pica-matcher/src/field_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ impl SubfieldsMatcher {
};

match self.quantifier {
Quantifier::Forall => fields.all(check_fn),
Quantifier::Exists => fields.any(check_fn),
Quantifier::All => fields.all(check_fn),
Quantifier::Any => fields.any(check_fn),
}
}
}
Expand Down
68 changes: 21 additions & 47 deletions crates/pica-matcher/src/subfield_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ impl RelationMatcher {
};

match self.quantifier {
Quantifier::Forall => subfields.all(check),
Quantifier::Exists => subfields.any(check),
Quantifier::All => subfields.all(check),
Quantifier::Any => subfields.any(check),
}
}

Expand Down Expand Up @@ -413,21 +413,13 @@ impl RegexMatcher {
/// let options = Default::default();
///
/// let subfield = SubfieldRef::new('0', "Oa");
/// let matcher = RegexMatcher::new(
/// vec!['0'],
/// "^Oa",
/// Quantifier::Exists,
/// false,
/// );
/// let matcher =
/// RegexMatcher::new(vec!['0'], "^Oa", Quantifier::Any, false);
/// assert!(matcher.is_match(&subfield, &options));
///
/// let subfield = SubfieldRef::new('0', "Ob");
/// let matcher = RegexMatcher::new(
/// vec!['0'],
/// "^Oa",
/// Quantifier::Exists,
/// true,
/// );
/// let matcher =
/// RegexMatcher::new(vec!['0'], "^Oa", Quantifier::Any, true);
/// assert!(matcher.is_match(&subfield, &options));
///
/// Ok(())
Expand Down Expand Up @@ -483,8 +475,8 @@ impl RegexMatcher {
};

match self.quantifier {
Quantifier::Forall => subfields.all(check_fn),
Quantifier::Exists => subfields.any(check_fn),
Quantifier::All => subfields.all(check_fn),
Quantifier::Any => subfields.any(check_fn),
}
}
}
Expand Down Expand Up @@ -553,7 +545,7 @@ impl InMatcher {
/// let matcher = InMatcher::new(
/// vec!['0'],
/// vec!["abc", "def"],
/// Quantifier::Exists,
/// Quantifier::Any,
/// false,
/// );
/// let options = Default::default();
Expand All @@ -564,7 +556,7 @@ impl InMatcher {
/// let matcher = InMatcher::new(
/// vec!['0'],
/// vec!["abc", "def"],
/// Quantifier::Exists,
/// Quantifier::Any,
/// true,
/// );
/// let options = Default::default();
Expand Down Expand Up @@ -632,8 +624,8 @@ impl InMatcher {
};

match self.quantifier {
Quantifier::Forall => subfields.all(check_fn),
Quantifier::Exists => subfields.any(check_fn),
Quantifier::All => subfields.all(check_fn),
Quantifier::Any => subfields.any(check_fn),
}
}
}
Expand Down Expand Up @@ -1206,50 +1198,32 @@ mod tests {
};
}

parse_success!(b"0 == 'abc'", Exists, vec!['0'], Eq, b"abc");
parse_success!(b"0 != 'abc'", Exists, vec!['0'], Ne, b"abc");
parse_success!(b"0 == 'abc'", Any, vec!['0'], Eq, b"abc");
parse_success!(b"0 != 'abc'", Any, vec!['0'], Ne, b"abc");
parse_success!(
b"0 =^ 'abc'",
Exists,
Any,
vec!['0'],
StartsWith,
b"abc"
);
parse_success!(
b"0 !^ 'abc'",
Exists,
Any,
vec!['0'],
StartsNotWith,
b"abc"
);
parse_success!(
b"0 =$ 'abc'",
Exists,
vec!['0'],
EndsWith,
b"abc"
);
parse_success!(b"0 =$ 'abc'", Any, vec!['0'], EndsWith, b"abc");
parse_success!(
b"0 !$ 'abc'",
Exists,
Any,
vec!['0'],
EndsNotWith,
b"abc"
);
parse_success!(
b"0 =* 'abc'",
Exists,
vec!['0'],
Similar,
b"abc"
);
parse_success!(
b"0 =? 'abc'",
Exists,
vec!['0'],
Contains,
b"abc"
);
parse_success!(b"0 =* 'abc'", Any, vec!['0'], Similar, b"abc");
parse_success!(b"0 =? 'abc'", Any, vec!['0'], Contains, b"abc");

assert!(parse_relation_matcher.parse(b"0 >= 'abc'").is_err());
assert!(parse_relation_matcher.parse(b"0 > 'abc'").is_err());
Expand All @@ -1266,7 +1240,7 @@ mod tests {
assert_eq!(
parse_regex_matcher.parse($input).unwrap(),
RegexMatcher {
quantifier: Quantifier::Exists,
quantifier: Quantifier::Any,
codes: $codes,
invert: $invert,
re: $re.to_string()
Expand Down
8 changes: 4 additions & 4 deletions crates/pica-matcher/tests/subfield_matcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn regex_matcher_new() {
let _ = RegexMatcher::new(
vec!['0'],
"^T[gpsu][1z]$",
Quantifier::Forall,
Quantifier::All,
false,
);
}
Expand All @@ -363,7 +363,7 @@ fn regex_matcher_new_panic1() {
RegexMatcher::new(
vec!['0'],
"^T[[gpsu][1z]$",
Quantifier::Exists,
Quantifier::Any,
false,
);
}
Expand Down Expand Up @@ -435,7 +435,7 @@ fn in_matcher_new() {
assert!(InMatcher::new(
vec!['0'],
vec!["abc", "def"],
Quantifier::Forall,
Quantifier::All,
false
)
.is_match(&subfield!('0', "abc"), &MatcherOptions::default()));
Expand All @@ -447,7 +447,7 @@ fn in_matcher_new_panic() {
let _ = InMatcher::new(
vec!['!'],
vec!["abc", "def"],
Quantifier::Exists,
Quantifier::Any,
false,
);
}
Expand Down

0 comments on commit 763f3f2

Please sign in to comment.