Simplify FilterCondition code, made possible by the new NOT operator

This commit is contained in:
Loïc Lecrenier 2022-06-14 15:28:34 +02:00
parent 44744d9e67
commit cc7415bb31
3 changed files with 16 additions and 52 deletions

View file

@ -21,30 +21,12 @@ pub enum Condition<'a> {
Equal(Token<'a>),
NotEqual(Token<'a>),
Exists,
NotExists,
LowerThan(Token<'a>),
LowerThanOrEqual(Token<'a>),
Between { from: Token<'a>, to: Token<'a> },
}
impl<'a> Condition<'a> {
/// This method can return two operations in case it must express
/// an OR operation for the between case (i.e. `TO`).
pub fn negate(self) -> (Self, Option<Self>) {
match self {
GreaterThan(n) => (LowerThanOrEqual(n), None),
GreaterThanOrEqual(n) => (LowerThan(n), None),
Equal(s) => (NotEqual(s), None),
NotEqual(s) => (Equal(s), None),
Exists => (NotExists, None),
NotExists => (Exists, None),
LowerThan(n) => (GreaterThanOrEqual(n), None),
LowerThanOrEqual(n) => (GreaterThan(n), None),
Between { from, to } => (LowerThan(from), Some(GreaterThan(to))),
}
}
}
/// condition = value ("=" | "!=" | ">" | ">=" | "<" | "<=") value
/// condition = value ("==" | ">" ...) value
pub fn parse_condition(input: Span) -> IResult<FilterCondition> {
let operator = alt((tag("<="), tag(">="), tag("!="), tag("<"), tag(">"), tag("=")));
let (input, (fid, op, value)) = tuple((parse_value, operator, cut(parse_value)))(input)?;
@ -73,7 +55,10 @@ pub fn parse_not_exists(input: Span) -> IResult<FilterCondition> {
let (input, key) = parse_value(input)?;
let (input, _) = tuple((tag("NOT"), multispace1, tag("EXISTS")))(input)?;
Ok((input, FilterCondition::Condition { fid: key.into(), op: NotExists }))
Ok((
input,
FilterCondition::Not(Box::new(FilterCondition::Condition { fid: key.into(), op: Exists })),
))
}
/// to = value value "TO" WS+ value

View file

@ -117,7 +117,6 @@ pub enum FilterCondition<'a> {
Or(Vec<Self>),
And(Vec<Self>),
GeoLowerThan { point: [Token<'a>; 2], radius: Token<'a> },
GeoGreaterThan { point: [Token<'a>; 2], radius: Token<'a> },
}
impl<'a> FilterCondition<'a> {
@ -144,7 +143,6 @@ impl<'a> FilterCondition<'a> {
None
}
FilterCondition::GeoLowerThan { point: [point, _], .. } if depth == 0 => Some(point),
FilterCondition::GeoGreaterThan { point: [point, _], .. } if depth == 0 => Some(point),
_ => None,
}
}
@ -443,17 +441,17 @@ pub mod tests {
),
(
"NOT subscribers EXISTS",
Fc::Condition {
Fc::Not(Box::new(Fc::Condition {
fid: rtok("NOT ", "subscribers"),
op: Condition::NotExists,
},
op: Condition::Exists,
})),
),
(
"subscribers NOT EXISTS",
Fc::Condition {
Fc::Not(Box::new(Fc::Condition {
fid: rtok("", "subscribers"),
op: Condition::NotExists,
},
op: Condition::Exists,
})),
),
(
"NOT subscribers NOT EXISTS",
@ -464,10 +462,10 @@ pub mod tests {
),
(
"subscribers NOT EXISTS",
Fc::Condition {
Fc::Not(Box::new(Fc::Condition {
fid: rtok("", "subscribers"),
op: Condition::NotExists,
},
op: Condition::Exists,
})),
),
(
"subscribers 100 TO 1000",
@ -503,10 +501,10 @@ pub mod tests {
),
(
"NOT _geoRadius(12, 13, 14)",
Fc::GeoGreaterThan {
Fc::Not(Box::new(Fc::GeoLowerThan {
point: [rtok("NOT _geoRadius(", "12"), rtok("NOT _geoRadius(12, ", "13")],
radius: rtok("NOT _geoRadius(12, 13, ", "14"),
},
})),
),
// test simple `or` and `and`
(