2021-10-22 01:59:38 +02:00
|
|
|
//! BNF grammar:
|
|
|
|
//!
|
|
|
|
//! ```text
|
|
|
|
//! condition = value ("==" | ">" ...) value
|
|
|
|
//! to = value value TO value
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2022-06-15 09:14:19 +02:00
|
|
|
use nom::character::complete::multispace1;
|
2021-11-02 17:35:17 +01:00
|
|
|
use nom::combinator::cut;
|
2022-05-25 11:55:16 +02:00
|
|
|
use nom::sequence::{terminated, tuple};
|
2021-10-22 01:59:38 +02:00
|
|
|
use Condition::*;
|
|
|
|
|
2022-06-15 13:31:51 +02:00
|
|
|
use crate::{parse_value, FilterCondition, IResult, Span, Token};
|
2021-10-22 01:59:38 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Condition<'a> {
|
|
|
|
GreaterThan(Token<'a>),
|
|
|
|
GreaterThanOrEqual(Token<'a>),
|
|
|
|
Equal(Token<'a>),
|
|
|
|
NotEqual(Token<'a>),
|
2022-06-14 16:42:09 +02:00
|
|
|
Exists,
|
|
|
|
NotExists,
|
2021-10-22 01:59:38 +02:00
|
|
|
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),
|
2022-06-14 16:42:09 +02:00
|
|
|
Exists => (NotExists, None),
|
|
|
|
NotExists => (Exists, None),
|
2021-10-22 01:59:38 +02:00
|
|
|
LowerThan(n) => (GreaterThanOrEqual(n), None),
|
|
|
|
LowerThanOrEqual(n) => (GreaterThan(n), None),
|
|
|
|
Between { from, to } => (LowerThan(from), Some(GreaterThan(to))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 09:12:37 +02:00
|
|
|
/// condition = value ("=" | "!=" | ">" | ">=" | "<" | "<=") value
|
2021-11-02 20:27:07 +01:00
|
|
|
pub fn parse_condition(input: Span) -> IResult<FilterCondition> {
|
2021-10-22 01:59:38 +02:00
|
|
|
let operator = alt((tag("<="), tag(">="), tag("!="), tag("<"), tag(">"), tag("=")));
|
2021-11-09 00:45:46 +01:00
|
|
|
let (input, (fid, op, value)) = tuple((parse_value, operator, cut(parse_value)))(input)?;
|
2021-10-22 01:59:38 +02:00
|
|
|
|
2021-11-09 00:49:13 +01:00
|
|
|
let condition = match *op.fragment() {
|
|
|
|
"<=" => FilterCondition::Condition { fid, op: LowerThanOrEqual(value) },
|
|
|
|
">=" => FilterCondition::Condition { fid, op: GreaterThanOrEqual(value) },
|
2021-11-09 17:05:36 +01: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 01:59:38 +02:00
|
|
|
_ => unreachable!(),
|
2021-11-09 00:49:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((input, condition))
|
2021-10-22 01:59:38 +02:00
|
|
|
}
|
|
|
|
|
2022-06-15 09:14:19 +02:00
|
|
|
/// exist = value "EXISTS"
|
2022-06-14 16:42:09 +02:00
|
|
|
pub fn parse_exists(input: Span) -> IResult<FilterCondition> {
|
|
|
|
let (input, key) = terminated(parse_value, tag("EXISTS"))(input)?;
|
2022-05-25 11:55:16 +02:00
|
|
|
|
2022-06-14 16:42:09 +02:00
|
|
|
Ok((input, FilterCondition::Condition { fid: key.into(), op: Exists }))
|
|
|
|
}
|
2022-06-16 09:12:37 +02:00
|
|
|
/// exist = value "NOT" WS+ "EXISTS"
|
2022-06-14 16:42:09 +02:00
|
|
|
pub fn parse_not_exists(input: Span) -> IResult<FilterCondition> {
|
2022-06-15 09:14:19 +02:00
|
|
|
let (input, key) = parse_value(input)?;
|
2022-06-14 16:42:09 +02:00
|
|
|
|
2022-06-15 09:14:19 +02:00
|
|
|
let (input, _) = tuple((tag("NOT"), multispace1, tag("EXISTS")))(input)?;
|
2022-06-14 16:42:09 +02:00
|
|
|
Ok((input, FilterCondition::Condition { fid: key.into(), op: NotExists }))
|
2022-05-25 11:55:16 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:12:37 +02:00
|
|
|
/// to = value value "TO" WS+ value
|
2021-11-02 20:27:07 +01:00
|
|
|
pub fn parse_to(input: Span) -> IResult<FilterCondition> {
|
2022-06-16 09:12:37 +02:00
|
|
|
let (input, (key, from, _, _, to)) =
|
|
|
|
tuple((parse_value, parse_value, tag("TO"), multispace1, cut(parse_value)))(input)?;
|
2021-10-22 01:59:38 +02:00
|
|
|
|
2022-01-10 15:59:04 +01:00
|
|
|
Ok((input, FilterCondition::Condition { fid: key, op: Between { from, to } }))
|
2021-10-22 01:59:38 +02:00
|
|
|
}
|