2021-10-22 07:59:38 +08:00
|
|
|
//! BNF grammar:
|
|
|
|
//!
|
|
|
|
//! ```text
|
|
|
|
//! condition = value ("==" | ">" ...) value
|
|
|
|
//! to = value value TO value
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2021-11-03 00:35:17 +08:00
|
|
|
use nom::combinator::cut;
|
2021-10-22 07:59:38 +08:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
use Condition::*;
|
|
|
|
|
2021-11-03 03:27:07 +08:00
|
|
|
use crate::{parse_value, FilterCondition, IResult, Span, Token};
|
2021-10-22 07:59:38 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Condition<'a> {
|
|
|
|
GreaterThan(Token<'a>),
|
|
|
|
GreaterThanOrEqual(Token<'a>),
|
|
|
|
Equal(Token<'a>),
|
|
|
|
NotEqual(Token<'a>),
|
|
|
|
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),
|
|
|
|
LowerThan(n) => (GreaterThanOrEqual(n), None),
|
|
|
|
LowerThanOrEqual(n) => (GreaterThan(n), None),
|
|
|
|
Between { from, to } => (LowerThan(from), Some(GreaterThan(to))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// condition = value ("==" | ">" ...) value
|
2021-11-03 03:27:07 +08:00
|
|
|
pub fn parse_condition(input: Span) -> IResult<FilterCondition> {
|
2021-10-22 07:59:38 +08:00
|
|
|
let operator = alt((tag("<="), tag(">="), tag("!="), tag("<"), tag(">"), tag("=")));
|
2021-11-09 07:45:46 +08:00
|
|
|
let (input, (fid, op, value)) = tuple((parse_value, operator, cut(parse_value)))(input)?;
|
2021-10-22 07:59:38 +08:00
|
|
|
|
2021-11-09 07:49:13 +08:00
|
|
|
let condition = match *op.fragment() {
|
|
|
|
"<=" => FilterCondition::Condition { fid, op: LowerThanOrEqual(value) },
|
|
|
|
">=" => FilterCondition::Condition { fid, op: GreaterThanOrEqual(value) },
|
2021-11-10 00:05:36 +08:00
|
|
|
"!=" => FilterCondition::Condition { fid, op: NotEqual(value) },
|
|
|
|
"<" => FilterCondition::Condition { fid, op: LowerThan(value) },
|
|
|
|
">" => FilterCondition::Condition { fid, op: GreaterThan(value) },
|
|
|
|
"=" => FilterCondition::Condition { fid, op: Equal(value) },
|
2021-10-22 07:59:38 +08:00
|
|
|
_ => unreachable!(),
|
2021-11-09 07:49:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((input, condition))
|
2021-10-22 07:59:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// to = value value TO value
|
2021-11-03 03:27:07 +08:00
|
|
|
pub fn parse_to(input: Span) -> IResult<FilterCondition> {
|
2021-10-22 07:59:38 +08:00
|
|
|
let (input, (key, from, _, to)) =
|
2021-11-09 07:45:46 +08:00
|
|
|
tuple((parse_value, parse_value, tag("TO"), cut(parse_value)))(input)?;
|
2021-10-22 07:59:38 +08:00
|
|
|
|
|
|
|
Ok((
|
|
|
|
input,
|
|
|
|
FilterCondition::Condition {
|
|
|
|
fid: key.into(),
|
|
|
|
op: Between { from: from.into(), to: to.into() },
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|