2021-10-22 01:15:42 +02:00
//! BNF grammar:
//!
//! ```text
2022-06-15 09:14:19 +02:00
//! filter = expression EOF
2021-10-22 01:15:42 +02:00
//! expression = or
2022-06-16 09:12:37 +02:00
//! or = and ("OR" WS+ and)*
//! and = not ("AND" WS+ not)*
//! not = ("NOT" WS+ not) | primary
2022-05-30 13:58:11 +02:00
//! primary = (WS* "(" WS* expression WS* ")" WS*) | geoRadius | in | condition | exists | not_exists | to
//! in = value "IN" WS* "[" value_list "]"
2022-06-16 09:12:37 +02:00
//! condition = value ("=" | "!=" | ">" | ">=" | "<" | "<=") value
2022-06-15 09:14:19 +02:00
//! exists = value "EXISTS"
2022-06-16 09:12:37 +02:00
//! not_exists = value "NOT" WS+ "EXISTS"
//! to = value value "TO" WS+ value
//! value = WS* ( word | singleQuoted | doubleQuoted) WS+
2022-05-30 13:58:11 +02:00
//! value_list = (value ("," value)* ","?)?
2021-10-22 01:15:42 +02:00
//! singleQuoted = "'" .* all but quotes "'"
2021-11-09 01:00:42 +01:00
//! doubleQuoted = "\"" .* all but double quotes "\""
2021-10-22 01:15:42 +02:00
//! word = (alphanumeric | _ | - | .)+
2022-06-16 09:12:37 +02:00
//! geoRadius = "_geoRadius(" WS* float WS* "," WS* float WS* "," float WS* ")"
2023-02-06 16:50:27 +01:00
//! geoBoundingBox = "_geoBoundingBox([" WS * float WS* "," WS* float WS* "], [" WS* float WS* "," WS* float WS* "]")
2021-11-02 17:35:17 +01:00
//! ```
//!
//! Other BNF grammar used to handle some specific errors:
//! ```text
2022-06-14 16:42:09 +02:00
//! geoPoint = WS* "_geoPoint(" (float ",")* ")"
2021-10-22 01:15:42 +02:00
//! ```
2021-11-04 14:22:35 +01:00
//!
//! Specific errors:
//! ================
//! - If a user try to use a geoPoint, as a primary OR as a value we must throw an error.
//! ```text
//! field = _geoPoint(12, 13, 14)
//! field < 12 AND _geoPoint(1, 2)
//! ```
//!
2022-06-15 09:14:19 +02:00
//! - If a user try to use a geoRadius as a value we must throw an error.
2021-11-04 14:22:35 +01:00
//! ```text
//! field = _geoRadius(12, 13, 14)
//! ```
//!
2021-10-22 01:15:42 +02:00
2021-10-22 01:59:38 +02:00
mod condition ;
2021-11-02 17:35:17 +01:00
mod error ;
2021-10-22 01:59:38 +02:00
mod value ;
2021-11-02 17:35:17 +01:00
2022-08-18 11:36:38 +02:00
use std ::fmt ::Debug ;
2021-10-22 01:59:38 +02:00
pub use condition ::{ parse_condition , parse_to , Condition } ;
2023-03-14 18:08:12 +01:00
use condition ::{
2024-07-17 11:13:37 +02:00
parse_contains , parse_exists , parse_is_empty , parse_is_not_empty , parse_is_not_null ,
2024-09-17 16:44:11 +02:00
parse_is_null , parse_not_contains , parse_not_exists , parse_not_starts_with , parse_starts_with ,
2023-03-14 18:08:12 +01:00
} ;
2022-08-17 16:06:29 +02:00
use error ::{ cut_with_err , ExpectedValueKind , NomErrorExt } ;
2021-11-02 17:35:17 +01:00
pub use error ::{ Error , ErrorKind } ;
2021-10-22 01:15:42 +02:00
use nom ::branch ::alt ;
2021-10-22 01:59:38 +02:00
use nom ::bytes ::complete ::tag ;
2022-08-17 16:53:40 +02:00
use nom ::character ::complete ::{ char , multispace0 } ;
2022-05-30 13:58:11 +02:00
use nom ::combinator ::{ cut , eof , map , opt } ;
2021-10-22 01:15:42 +02:00
use nom ::multi ::{ many0 , separated_list1 } ;
use nom ::number ::complete ::recognize_float ;
2021-11-02 17:35:17 +01:00
use nom ::sequence ::{ delimited , preceded , terminated , tuple } ;
2021-11-02 20:27:07 +01:00
use nom ::Finish ;
2021-10-22 01:15:42 +02:00
use nom_locate ::LocatedSpan ;
2021-10-22 01:59:38 +02:00
pub ( crate ) use value ::parse_value ;
2022-08-17 16:53:40 +02:00
use value ::word_exact ;
2021-10-22 01:15:42 +02:00
2021-11-02 17:35:17 +01:00
pub type Span < ' a > = LocatedSpan < & ' a str , & ' a str > ;
2021-10-22 01:15:42 +02:00
2021-11-02 20:27:07 +01:00
type IResult < ' a , Ret > = nom ::IResult < Span < ' a > , Ret , Error < ' a > > ;
2021-10-22 15:09:56 +02:00
2022-08-18 11:27:39 +02:00
const MAX_FILTER_DEPTH : usize = 200 ;
2021-11-06 16:02:27 +01:00
#[ derive(Debug, Clone, Eq) ]
2021-12-20 16:18:15 +01:00
pub struct Token < ' a > {
/// The token in the original input, it should be used when possible.
2023-02-02 15:03:34 +01:00
span : Span < ' a > ,
2021-12-20 16:18:15 +01:00
/// If you need to modify the original input you can use the `value` field
/// to store your modified input.
value : Option < String > ,
}
2021-10-22 01:15:42 +02:00
2021-11-06 16:02:27 +01:00
impl < ' a > PartialEq for Token < ' a > {
fn eq ( & self , other : & Self ) -> bool {
2021-12-20 16:18:15 +01:00
self . span . fragment ( ) = = other . span . fragment ( )
2021-11-06 16:02:27 +01:00
}
}
2021-10-22 01:15:42 +02:00
impl < ' a > Token < ' a > {
2021-12-20 16:18:15 +01:00
pub fn new ( span : Span < ' a > , value : Option < String > ) -> Self {
Self { span , value }
}
2023-02-06 11:36:49 +01:00
/// Returns the string contained in the span of the `Token`.
/// This is only useful in the tests. You should always use
/// the value.
#[ cfg(test) ]
2022-06-09 16:03:49 +02:00
pub fn lexeme ( & self ) -> & str {
& self . span
}
2023-02-06 11:36:49 +01:00
/// Return the string contained in the token.
2021-12-20 16:18:15 +01:00
pub fn value ( & self ) -> & str {
self . value . as_ref ( ) . map_or ( & self . span , | value | value )
2021-10-22 01:15:42 +02:00
}
2021-11-04 17:24:55 +01:00
pub fn as_external_error ( & self , error : impl std ::error ::Error ) -> Error < ' a > {
2021-12-20 16:18:15 +01:00
Error ::new_from_external ( self . span , error )
2021-11-04 17:24:55 +01:00
}
2021-11-05 10:46:54 +01:00
2023-02-02 15:03:34 +01:00
/// Returns a copy of the span this token was created with.
pub fn original_span ( & self ) -> Span < ' a > {
self . span
}
2022-11-08 10:17:16 +01:00
pub fn parse_finite_float ( & self ) -> Result < f64 , Error > {
2022-10-28 15:30:53 +02:00
let value : f64 = self . value ( ) . parse ( ) . map_err ( | e | self . as_external_error ( e ) ) ? ;
2022-11-08 10:17:16 +01:00
if value . is_finite ( ) {
Ok ( value )
} else {
Err ( Error ::new_from_kind ( self . span , ErrorKind ::NonFiniteFloat ) )
}
2021-11-05 10:46:54 +01:00
}
2021-10-22 01:15:42 +02:00
}
impl < ' a > From < Span < ' a > > for Token < ' a > {
fn from ( span : Span < ' a > ) -> Self {
2021-12-20 16:18:15 +01:00
Self { span , value : None }
2021-10-22 01:15:42 +02:00
}
}
2022-12-03 19:13:41 +01:00
/// Allow [Token] to be constructed from &[str]
impl < ' a > From < & ' a str > for Token < ' a > {
fn from ( s : & ' a str ) -> Self {
Token ::from ( Span ::new_extra ( s , s ) )
}
}
2021-10-22 01:15:42 +02:00
#[ derive(Debug, Clone, PartialEq, Eq) ]
2021-10-22 01:59:38 +02:00
pub enum FilterCondition < ' a > {
2022-06-14 15:15:05 +02:00
Not ( Box < Self > ) ,
2021-10-22 01:59:38 +02:00
Condition { fid : Token < ' a > , op : Condition < ' a > } ,
2022-05-30 13:58:11 +02:00
In { fid : Token < ' a > , els : Vec < Token < ' a > > } ,
2022-06-20 18:46:57 +02:00
Or ( Vec < Self > ) ,
And ( Vec < Self > ) ,
2021-10-22 01:59:38 +02:00
GeoLowerThan { point : [ Token < ' a > ; 2 ] , radius : Token < ' a > } ,
2023-03-28 18:26:18 +02:00
GeoBoundingBox { top_right_point : [ Token < ' a > ; 2 ] , bottom_left_point : [ Token < ' a > ; 2 ] } ,
2021-10-22 01:15:42 +02:00
}
2024-07-17 11:13:37 +02:00
pub enum TraversedElement < ' a > {
FilterCondition ( & ' a FilterCondition < ' a > ) ,
Condition ( & ' a Condition < ' a > ) ,
}
2021-10-22 01:59:38 +02:00
impl < ' a > FilterCondition < ' a > {
2024-07-17 11:13:37 +02:00
pub fn use_contains_operator ( & self ) -> Option < & Token > {
match self {
FilterCondition ::Condition { fid : _ , op } = > match op {
Condition ::GreaterThan ( _ )
| Condition ::GreaterThanOrEqual ( _ )
| Condition ::Equal ( _ )
| Condition ::NotEqual ( _ )
| Condition ::Null
| Condition ::Empty
| Condition ::Exists
| Condition ::LowerThan ( _ )
| Condition ::LowerThanOrEqual ( _ )
| Condition ::Between { .. } = > None ,
2024-09-17 16:44:11 +02:00
Condition ::Contains { keyword , word : _ }
| Condition ::StartsWith { keyword , word : _ } = > Some ( keyword ) ,
2024-07-17 11:13:37 +02:00
} ,
FilterCondition ::Not ( this ) = > this . use_contains_operator ( ) ,
FilterCondition ::Or ( seq ) | FilterCondition ::And ( seq ) = > {
seq . iter ( ) . find_map ( | filter | filter . use_contains_operator ( ) )
}
FilterCondition ::GeoLowerThan { .. }
| FilterCondition ::GeoBoundingBox { .. }
| FilterCondition ::In { .. } = > None ,
}
}
2021-12-07 15:16:29 +01:00
/// Returns the first token found at the specified depth, `None` if no token at this depth.
pub fn token_at_depth ( & self , depth : usize ) -> Option < & Token > {
2021-12-06 17:35:20 +01:00
match self {
2021-12-07 15:16:29 +01:00
FilterCondition ::Condition { fid , .. } if depth = = 0 = > Some ( fid ) ,
2022-06-20 18:46:57 +02:00
FilterCondition ::Or ( subfilters ) = > {
2021-12-07 17:20:11 +01:00
let depth = depth . saturating_sub ( 1 ) ;
2022-06-20 18:46:57 +02:00
for f in subfilters . iter ( ) {
if let Some ( t ) = f . token_at_depth ( depth ) {
return Some ( t ) ;
}
}
None
2021-12-07 15:16:29 +01:00
}
2022-06-20 18:46:57 +02:00
FilterCondition ::And ( subfilters ) = > {
2021-12-07 17:20:11 +01:00
let depth = depth . saturating_sub ( 1 ) ;
2022-06-20 18:46:57 +02:00
for f in subfilters . iter ( ) {
if let Some ( t ) = f . token_at_depth ( depth ) {
return Some ( t ) ;
}
}
None
2021-12-07 15:16:29 +01:00
}
FilterCondition ::GeoLowerThan { point : [ point , _ ] , .. } if depth = = 0 = > Some ( point ) ,
_ = > None ,
2021-12-06 17:35:20 +01:00
}
}
2021-12-09 11:13:12 +01:00
pub fn parse ( input : & ' a str ) -> Result < Option < Self > , Error > {
2021-10-22 17:49:08 +02:00
if input . trim ( ) . is_empty ( ) {
2021-12-09 11:13:12 +01:00
return Ok ( None ) ;
2021-10-22 17:49:08 +02:00
}
2021-11-02 17:35:17 +01:00
let span = Span ::new_extra ( input , input ) ;
2021-12-09 11:13:12 +01:00
parse_filter ( span ) . finish ( ) . map ( | ( _rem , output ) | Some ( output ) )
2021-10-22 01:59:38 +02:00
}
2021-10-22 01:15:42 +02:00
}
2021-11-09 16:40:05 +01:00
/// remove OPTIONAL whitespaces before AND after the provided parser.
2022-12-13 15:29:52 +01:00
fn ws < ' a , O > (
inner : impl FnMut ( Span < ' a > ) -> IResult < ' a , O > ,
) -> impl FnMut ( Span < ' a > ) -> IResult < ' a , O > {
2021-10-22 01:59:38 +02:00
delimited ( multispace0 , inner , multispace0 )
}
2021-10-22 01:15:42 +02:00
2022-05-30 13:58:11 +02:00
/// value_list = (value ("," value)* ","?)?
2022-10-14 16:44:10 +02:00
fn parse_value_list ( input : Span ) -> IResult < Vec < Token > > {
2022-05-30 13:58:11 +02:00
let ( input , first_value ) = opt ( parse_value ) ( input ) ? ;
if let Some ( first_value ) = first_value {
let value_list_el_parser = preceded ( ws ( tag ( " , " ) ) , parse_value ) ;
let ( input , mut values ) = many0 ( value_list_el_parser ) ( input ) ? ;
let ( input , _ ) = opt ( ws ( tag ( " , " ) ) ) ( input ) ? ;
values . insert ( 0 , first_value ) ;
Ok ( ( input , values ) )
} else {
Ok ( ( input , vec! [ ] ) )
}
}
2022-08-18 11:36:38 +02:00
/// "IN" WS* "[" value_list "]"
2022-08-18 10:58:04 +02:00
fn parse_in_body ( input : Span ) -> IResult < Vec < Token > > {
2022-08-17 16:53:40 +02:00
let ( input , _ ) = ws ( word_exact ( " IN " ) ) ( input ) ? ;
2022-05-30 13:58:11 +02:00
2022-06-15 10:13:34 +02:00
// everything after `IN` can be a failure
2022-07-18 17:09:52 +02:00
let ( input , _ ) =
cut_with_err ( tag ( " [ " ) , | _ | Error ::new_from_kind ( input , ErrorKind ::InOpeningBracket ) ) (
input ,
) ? ;
2022-06-15 10:13:34 +02:00
let ( input , content ) = cut ( parse_value_list ) ( input ) ? ;
2022-07-18 17:09:52 +02:00
2022-06-15 10:13:34 +02:00
// everything after `IN` can be a failure
2022-08-18 10:58:04 +02:00
let ( input , _ ) = cut_with_err ( ws ( tag ( " ] " ) ) , | _ | {
2022-06-15 10:13:34 +02:00
if eof ::< _ , ( ) > ( input ) . is_ok ( ) {
Error ::new_from_kind ( input , ErrorKind ::InClosingBracket )
} else {
2022-08-17 16:06:29 +02:00
let expected_value_kind = match parse_value ( input ) {
Err ( nom ::Err ::Error ( e ) ) = > match e . kind ( ) {
ErrorKind ::ReservedKeyword ( _ ) = > ExpectedValueKind ::ReservedKeyword ,
_ = > ExpectedValueKind ::Other ,
} ,
_ = > ExpectedValueKind ::Other ,
} ;
Error ::new_from_kind ( input , ErrorKind ::InExpectedValue ( expected_value_kind ) )
2022-06-15 10:13:34 +02:00
}
} ) ( input ) ? ;
2022-05-30 13:58:11 +02:00
2022-08-18 10:58:04 +02:00
Ok ( ( input , content ) )
}
/// in = value "IN" "[" value_list "]"
fn parse_in ( input : Span ) -> IResult < FilterCondition > {
let ( input , value ) = parse_value ( input ) ? ;
let ( input , content ) = parse_in_body ( input ) ? ;
2022-05-30 13:58:11 +02:00
let filter = FilterCondition ::In { fid : value , els : content } ;
Ok ( ( input , filter ) )
}
2022-08-18 10:58:04 +02:00
2022-06-15 09:58:47 +02:00
/// in = value "NOT" WS* "IN" "[" value_list "]"
fn parse_not_in ( input : Span ) -> IResult < FilterCondition > {
let ( input , value ) = parse_value ( input ) ? ;
2022-08-17 16:53:40 +02:00
let ( input , _ ) = word_exact ( " NOT " ) ( input ) ? ;
2022-08-18 10:58:04 +02:00
let ( input , content ) = parse_in_body ( input ) ? ;
2022-06-15 10:13:34 +02:00
2022-06-15 09:58:47 +02:00
let filter = FilterCondition ::Not ( Box ::new ( FilterCondition ::In { fid : value , els : content } ) ) ;
Ok ( ( input , filter ) )
}
2022-05-30 13:58:11 +02:00
/// or = and ("OR" and)
2022-08-18 11:27:39 +02:00
fn parse_or ( input : Span , depth : usize ) -> IResult < FilterCondition > {
if depth > MAX_FILTER_DEPTH {
return Err ( nom ::Err ::Error ( Error ::new_from_kind ( input , ErrorKind ::DepthLimitReached ) ) ) ;
}
let ( input , first_filter ) = parse_and ( input , depth + 1 ) ? ;
2021-11-04 14:22:35 +01:00
// if we found a `OR` then we MUST find something next
2022-08-18 11:27:39 +02:00
let ( input , mut ors ) =
many0 ( preceded ( ws ( word_exact ( " OR " ) ) , cut ( | input | parse_and ( input , depth + 1 ) ) ) ) ( input ) ? ;
2022-06-20 18:46:57 +02:00
let filter = if ors . is_empty ( ) {
first_filter
} else {
ors . insert ( 0 , first_filter ) ;
FilterCondition ::Or ( ors )
} ;
2021-10-22 01:15:42 +02:00
2022-06-20 18:46:57 +02:00
Ok ( ( input , filter ) )
2021-10-22 01:15:42 +02:00
}
2022-06-15 09:14:19 +02:00
/// and = not ("AND" not)*
2022-08-18 11:27:39 +02:00
fn parse_and ( input : Span , depth : usize ) -> IResult < FilterCondition > {
if depth > MAX_FILTER_DEPTH {
return Err ( nom ::Err ::Error ( Error ::new_from_kind ( input , ErrorKind ::DepthLimitReached ) ) ) ;
}
let ( input , first_filter ) = parse_not ( input , depth + 1 ) ? ;
2021-11-04 14:22:35 +01:00
// if we found a `AND` then we MUST find something next
2022-08-18 11:27:39 +02:00
let ( input , mut ands ) =
many0 ( preceded ( ws ( word_exact ( " AND " ) ) , cut ( | input | parse_not ( input , depth + 1 ) ) ) ) ( input ) ? ;
2022-06-20 18:46:57 +02:00
let filter = if ands . is_empty ( ) {
first_filter
} else {
ands . insert ( 0 , first_filter ) ;
FilterCondition ::And ( ands )
} ;
Ok ( ( input , filter ) )
2021-10-22 01:15:42 +02:00
}
2022-06-16 09:12:37 +02:00
/// not = ("NOT" WS+ not) | primary
/// We can have multiple consecutive not, eg: `NOT NOT channel = mv`.
2021-11-09 16:47:54 +01:00
/// If we parse a `NOT` we MUST parse something behind.
2022-08-18 11:27:39 +02:00
fn parse_not ( input : Span , depth : usize ) -> IResult < FilterCondition > {
if depth > MAX_FILTER_DEPTH {
return Err ( nom ::Err ::Error ( Error ::new_from_kind ( input , ErrorKind ::DepthLimitReached ) ) ) ;
}
2022-06-16 09:12:37 +02:00
alt ( (
2022-08-18 11:27:39 +02:00
map (
preceded ( ws ( word_exact ( " NOT " ) ) , cut ( | input | parse_not ( input , depth + 1 ) ) ) ,
| e | match e {
FilterCondition ::Not ( e ) = > * e ,
_ = > FilterCondition ::Not ( Box ::new ( e ) ) ,
} ,
) ,
| input | parse_primary ( input , depth + 1 ) ,
2022-06-16 09:12:37 +02:00
) ) ( input )
2021-10-22 01:15:42 +02:00
}
2022-06-16 06:19:33 +02:00
/// geoRadius = WS* "_geoRadius(float WS* "," WS* float WS* "," WS* float)
2021-11-04 14:22:35 +01:00
/// If we parse `_geoRadius` we MUST parse the rest of the expression.
2021-11-02 20:27:07 +01:00
fn parse_geo_radius ( input : Span ) -> IResult < FilterCondition > {
2022-06-16 09:12:37 +02:00
// we want to allow space BEFORE the _geoRadius but not after
2021-11-04 14:22:35 +01:00
let parsed = preceded (
2022-08-17 16:53:40 +02:00
tuple ( ( multispace0 , word_exact ( " _geoRadius " ) ) ) ,
2021-11-09 16:40:05 +01:00
// if we were able to parse `_geoRadius` and can't parse the rest of the input we return a failure
2021-11-09 01:03:02 +01:00
cut ( delimited ( char ( '(' ) , separated_list1 ( tag ( " , " ) , ws ( recognize_float ) ) , char ( ')' ) ) ) ,
2021-11-04 14:22:35 +01:00
) ( input )
2022-10-28 15:30:53 +02:00
. map_err ( | e | e . map ( | _ | Error ::new_from_kind ( input , ErrorKind ::GeoRadius ) ) ) ;
2021-10-22 01:15:42 +02:00
2021-11-04 14:22:35 +01:00
let ( input , args ) = parsed ? ;
2021-10-22 01:15:42 +02:00
if args . len ( ) ! = 3 {
2022-10-28 15:30:53 +02:00
return Err ( nom ::Err ::Failure ( Error ::new_from_kind ( input , ErrorKind ::GeoRadius ) ) ) ;
2021-10-22 01:15:42 +02:00
}
let res = FilterCondition ::GeoLowerThan {
point : [ args [ 0 ] . into ( ) , args [ 1 ] . into ( ) ] ,
radius : args [ 2 ] . into ( ) ,
} ;
Ok ( ( input , res ) )
}
2023-02-06 16:50:27 +01:00
/// geoBoundingBox = WS* "_geoBoundingBox([float WS* "," WS* float WS* "], [float WS* "," WS* float WS* "]")
2022-10-28 13:42:21 +02:00
/// If we parse `_geoBoundingBox` we MUST parse the rest of the expression.
fn parse_geo_bounding_box ( input : Span ) -> IResult < FilterCondition > {
// we want to allow space BEFORE the _geoBoundingBox but not after
let parsed = preceded (
tuple ( ( multispace0 , word_exact ( " _geoBoundingBox " ) ) ) ,
// if we were able to parse `_geoBoundingBox` and can't parse the rest of the input we return a failure
2022-10-28 15:30:53 +02:00
cut ( delimited (
char ( '(' ) ,
separated_list1 (
tag ( " , " ) ,
2023-02-06 16:50:27 +01:00
ws ( delimited ( char ( '[' ) , separated_list1 ( tag ( " , " ) , ws ( recognize_float ) ) , char ( ']' ) ) ) ,
2022-10-28 15:30:53 +02:00
) ,
char ( ')' ) ,
) ) ,
2022-10-28 13:42:21 +02:00
) ( input )
2022-10-28 15:30:53 +02:00
. map_err ( | e | e . map ( | _ | Error ::new_from_kind ( input , ErrorKind ::GeoBoundingBox ) ) ) ;
2022-10-28 13:42:21 +02:00
let ( input , args ) = parsed ? ;
2022-10-28 15:30:53 +02:00
if args . len ( ) ! = 2 | | args [ 0 ] . len ( ) ! = 2 | | args [ 1 ] . len ( ) ! = 2 {
return Err ( nom ::Err ::Failure ( Error ::new_from_kind ( input , ErrorKind ::GeoBoundingBox ) ) ) ;
2022-10-28 13:42:21 +02:00
}
let res = FilterCondition ::GeoBoundingBox {
2023-03-28 18:26:18 +02:00
top_right_point : [ args [ 0 ] [ 0 ] . into ( ) , args [ 0 ] [ 1 ] . into ( ) ] ,
bottom_left_point : [ args [ 1 ] [ 0 ] . into ( ) , args [ 1 ] [ 1 ] . into ( ) ] ,
2022-10-28 13:42:21 +02:00
} ;
Ok ( ( input , res ) )
}
2022-06-16 06:19:33 +02:00
/// geoPoint = WS* "_geoPoint(float WS* "," WS* float WS* "," WS* float)
2021-11-04 14:22:35 +01:00
fn parse_geo_point ( input : Span ) -> IResult < FilterCondition > {
// we want to forbid space BEFORE the _geoPoint but not after
tuple ( (
multispace0 ,
tag ( " _geoPoint " ) ,
// if we were able to parse `_geoPoint` we are going to return a Failure whatever happens next.
2022-01-10 15:59:04 +01:00
cut ( delimited ( char ( '(' ) , separated_list1 ( tag ( " , " ) , ws ( recognize_float ) ) , char ( ')' ) ) ) ,
2021-11-04 14:22:35 +01:00
) ) ( input )
2021-11-04 16:20:53 +01:00
. map_err ( | e | e . map ( | _ | Error ::new_from_kind ( input , ErrorKind ::ReservedGeo ( " _geoPoint " ) ) ) ) ? ;
2021-11-09 16:40:05 +01:00
// if we succeeded we still return a `Failure` because geoPoints are not allowed
2021-11-04 16:20:53 +01:00
Err ( nom ::Err ::Failure ( Error ::new_from_kind ( input , ErrorKind ::ReservedGeo ( " _geoPoint " ) ) ) )
2021-11-04 14:22:35 +01:00
}
2023-03-31 16:24:25 +02:00
/// geoPoint = WS* "_geoDistance(float WS* "," WS* float WS* "," WS* float)
fn parse_geo_distance ( input : Span ) -> IResult < FilterCondition > {
// we want to forbid space BEFORE the _geoDistance but not after
tuple ( (
multispace0 ,
tag ( " _geoDistance " ) ,
// if we were able to parse `_geoDistance` we are going to return a Failure whatever happens next.
cut ( delimited ( char ( '(' ) , separated_list1 ( tag ( " , " ) , ws ( recognize_float ) ) , char ( ')' ) ) ) ,
) ) ( input )
. map_err ( | e | e . map ( | _ | Error ::new_from_kind ( input , ErrorKind ::ReservedGeo ( " _geoDistance " ) ) ) ) ? ;
// if we succeeded we still return a `Failure` because `geoDistance` filters are not allowed
Err ( nom ::Err ::Failure ( Error ::new_from_kind ( input , ErrorKind ::ReservedGeo ( " _geoDistance " ) ) ) )
}
2023-03-31 16:21:27 +02:00
/// geo = WS* "_geo(float WS* "," WS* float WS* "," WS* float)
fn parse_geo ( input : Span ) -> IResult < FilterCondition > {
// we want to forbid space BEFORE the _geo but not after
tuple ( (
multispace0 ,
word_exact ( " _geo " ) ,
// if we were able to parse `_geo` we are going to return a Failure whatever happens next.
cut ( delimited ( char ( '(' ) , separated_list1 ( tag ( " , " ) , ws ( recognize_float ) ) , char ( ')' ) ) ) ,
) ) ( input )
. map_err ( | e | e . map ( | _ | Error ::new_from_kind ( input , ErrorKind ::ReservedGeo ( " _geo " ) ) ) ) ? ;
// if we succeeded we still return a `Failure` because `_geo` filter is not allowed
Err ( nom ::Err ::Failure ( Error ::new_from_kind ( input , ErrorKind ::ReservedGeo ( " _geo " ) ) ) )
}
2022-08-17 16:06:29 +02:00
fn parse_error_reserved_keyword ( input : Span ) -> IResult < FilterCondition > {
match parse_condition ( input ) {
Ok ( result ) = > Ok ( result ) ,
Err ( nom ::Err ::Error ( inner ) | nom ::Err ::Failure ( inner ) ) = > match inner . kind ( ) {
ErrorKind ::ExpectedValue ( ExpectedValueKind ::ReservedKeyword ) = > {
2022-10-14 16:44:10 +02:00
Err ( nom ::Err ::Failure ( inner ) )
2022-08-17 16:06:29 +02:00
}
2022-10-14 16:44:10 +02:00
_ = > Err ( nom ::Err ::Error ( inner ) ) ,
2022-08-17 16:06:29 +02:00
} ,
2022-10-14 16:44:10 +02:00
Err ( e ) = > Err ( e ) ,
2022-08-17 16:06:29 +02:00
}
}
2022-06-16 09:12:37 +02:00
/// primary = (WS* "(" WS* expression WS* ")" WS*) | geoRadius | condition | exists | not_exists | to
2022-08-18 11:27:39 +02:00
fn parse_primary ( input : Span , depth : usize ) -> IResult < FilterCondition > {
if depth > MAX_FILTER_DEPTH {
return Err ( nom ::Err ::Error ( Error ::new_from_kind ( input , ErrorKind ::DepthLimitReached ) ) ) ;
}
2021-10-22 01:15:42 +02:00
alt ( (
2021-11-04 14:22:35 +01:00
// if we find a first parenthesis, then we must parse an expression and find the closing parenthesis
delimited (
ws ( char ( '(' ) ) ,
2022-08-18 11:27:39 +02:00
cut ( | input | parse_expression ( input , depth + 1 ) ) ,
2021-11-04 14:22:35 +01:00
cut_with_err ( ws ( char ( ')' ) ) , | c | {
2021-11-04 16:20:53 +01:00
Error ::new_from_kind ( input , ErrorKind ::MissingClosingDelimiter ( c . char ( ) ) )
2021-11-04 14:22:35 +01:00
} ) ,
) ,
2021-11-09 01:03:02 +01:00
parse_geo_radius ,
2022-10-28 13:42:21 +02:00
parse_geo_bounding_box ,
2022-05-30 13:58:11 +02:00
parse_in ,
2022-06-15 09:58:47 +02:00
parse_not_in ,
2021-11-09 01:03:02 +01:00
parse_condition ,
2023-03-14 10:31:04 +01:00
parse_is_null ,
parse_is_not_null ,
2023-03-14 18:08:12 +01:00
parse_is_empty ,
parse_is_not_empty ,
2022-06-14 16:42:09 +02:00
parse_exists ,
parse_not_exists ,
2021-11-09 01:03:02 +01:00
parse_to ,
2024-07-17 11:13:37 +02:00
parse_contains ,
parse_not_contains ,
2024-09-17 16:44:11 +02:00
parse_starts_with ,
parse_not_starts_with ,
2021-11-04 14:22:35 +01:00
// the next lines are only for error handling and are written at the end to have the less possible performance impact
2023-03-31 16:21:27 +02:00
parse_geo ,
2023-03-31 23:37:58 +02:00
parse_geo_distance ,
2021-11-09 01:03:02 +01:00
parse_geo_point ,
2022-08-17 16:06:29 +02:00
parse_error_reserved_keyword ,
2021-10-22 01:15:42 +02:00
) ) ( input )
2021-11-04 14:22:35 +01:00
// if the inner parsers did not match enough information to return an accurate error
2021-11-04 16:20:53 +01:00
. map_err ( | e | e . map_err ( | _ | Error ::new_from_kind ( input , ErrorKind ::InvalidPrimary ) ) )
2021-10-22 01:15:42 +02:00
}
/// expression = or
2022-08-18 11:27:39 +02:00
pub fn parse_expression ( input : Span , depth : usize ) -> IResult < FilterCondition > {
parse_or ( input , depth )
2021-10-22 01:15:42 +02:00
}
2022-06-15 09:14:19 +02:00
/// filter = expression EOF
2021-11-02 20:27:07 +01:00
pub fn parse_filter ( input : Span ) -> IResult < FilterCondition > {
2022-08-18 11:27:39 +02:00
terminated ( | input | parse_expression ( input , 0 ) , eof ) ( input )
2021-11-02 17:35:17 +01:00
}
2023-07-25 10:30:50 +02:00
impl < ' a > std ::fmt ::Display for FilterCondition < ' a > {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
match self {
FilterCondition ::Not ( filter ) = > {
write! ( f , " NOT ({filter}) " )
}
FilterCondition ::Condition { fid , op } = > {
write! ( f , " {fid} {op} " )
}
FilterCondition ::In { fid , els } = > {
write! ( f , " {fid} IN[ " ) ? ;
for el in els {
write! ( f , " {el}, " ) ? ;
}
write! ( f , " ] " )
}
FilterCondition ::Or ( els ) = > {
write! ( f , " OR[ " ) ? ;
for el in els {
write! ( f , " {el}, " ) ? ;
}
write! ( f , " ] " )
}
FilterCondition ::And ( els ) = > {
write! ( f , " AND[ " ) ? ;
for el in els {
write! ( f , " {el}, " ) ? ;
}
write! ( f , " ] " )
}
FilterCondition ::GeoLowerThan { point , radius } = > {
write! ( f , " _geoRadius({}, {}, {}) " , point [ 0 ] , point [ 1 ] , radius )
}
FilterCondition ::GeoBoundingBox {
top_right_point : top_left_point ,
bottom_left_point : bottom_right_point ,
} = > {
write! (
f ,
" _geoBoundingBox([{}, {}], [{}, {}]) " ,
top_left_point [ 0 ] ,
top_left_point [ 1 ] ,
bottom_right_point [ 0 ] ,
bottom_right_point [ 1 ]
)
}
}
}
}
impl < ' a > std ::fmt ::Display for Condition < ' a > {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
match self {
Condition ::GreaterThan ( token ) = > write! ( f , " > {token} " ) ,
Condition ::GreaterThanOrEqual ( token ) = > write! ( f , " >= {token} " ) ,
Condition ::Equal ( token ) = > write! ( f , " = {token} " ) ,
Condition ::NotEqual ( token ) = > write! ( f , " != {token} " ) ,
Condition ::Null = > write! ( f , " IS NULL " ) ,
Condition ::Empty = > write! ( f , " IS EMPTY " ) ,
Condition ::Exists = > write! ( f , " EXISTS " ) ,
Condition ::LowerThan ( token ) = > write! ( f , " < {token} " ) ,
Condition ::LowerThanOrEqual ( token ) = > write! ( f , " <= {token} " ) ,
Condition ::Between { from , to } = > write! ( f , " {from} TO {to} " ) ,
2024-07-17 16:54:33 +02:00
Condition ::Contains { word , keyword : _ } = > write! ( f , " CONTAINS {word} " ) ,
2024-09-17 16:44:11 +02:00
Condition ::StartsWith { word , keyword : _ } = > write! ( f , " STARTS WITH {word} " ) ,
2023-07-25 10:30:50 +02:00
}
}
}
impl < ' a > std ::fmt ::Display for Token < ' a > {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
write! ( f , " {{{}}} " , self . value ( ) )
}
}
2021-10-22 01:15:42 +02:00
#[ cfg(test) ]
2021-10-22 01:59:38 +02:00
pub mod tests {
2023-09-06 11:39:06 +02:00
use FilterCondition as Fc ;
2021-10-22 01:15:42 +02:00
use super ::* ;
/// Create a raw [Token]. You must specify the string that appear BEFORE your element followed by your element
2021-10-22 01:59:38 +02:00
pub fn rtok < ' a > ( before : & ' a str , value : & ' a str ) -> Token < ' a > {
2021-10-22 01:15:42 +02:00
// if the string is empty we still need to return 1 for the line number
2023-01-17 18:01:26 +01:00
let lines = before . is_empty ( ) . then_some ( 1 ) . unwrap_or_else ( | | before . lines ( ) . count ( ) ) ;
2021-10-22 01:15:42 +02:00
let offset = before . chars ( ) . count ( ) ;
2021-11-02 17:35:17 +01:00
// the extra field is not checked in the tests so we can set it to nothing
unsafe { Span ::new_from_raw_offset ( offset , lines as u32 , value , " " ) } . into ( )
2021-10-22 01:15:42 +02:00
}
2024-07-17 11:13:37 +02:00
#[ track_caller ]
2023-09-06 11:39:06 +02:00
fn p ( s : & str ) -> impl std ::fmt ::Display + '_ {
Fc ::parse ( s ) . unwrap ( ) . unwrap ( )
}
2021-10-22 01:15:42 +02:00
2023-09-06 11:39:06 +02:00
#[ test ]
fn parse_escaped ( ) {
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r "title = 'foo\\'" ) , @ r # "{title} = {foo\}"# ) ;
insta ::assert_snapshot! ( p ( r "title = 'foo\\\\'" ) , @ r # "{title} = {foo\\}"# ) ;
insta ::assert_snapshot! ( p ( r "title = 'foo\\\\\\'" ) , @ r # "{title} = {foo\\\}"# ) ;
insta ::assert_snapshot! ( p ( r "title = 'foo\\\\\\\\'" ) , @ r # "{title} = {foo\\\\}"# ) ;
2024-04-18 08:12:52 +02:00
// but it also works with other sequences
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "title = 'foo\x20\n\t\"\'"'"# ) , @ " {title} = {foo \n \t \" \' \" } " ) ;
2023-09-06 11:39:06 +02:00
}
2022-08-17 17:25:31 +02:00
2023-09-06 11:39:06 +02:00
#[ test ]
fn parse ( ) {
2022-08-17 17:25:31 +02:00
// Test equal
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = Ponce " ) , @ " {channel} = {Ponce} " ) ;
insta ::assert_snapshot! ( p ( " subscribers = 12 " ) , @ " {subscribers} = {12} " ) ;
insta ::assert_snapshot! ( p ( " channel = 'Mister Mv' " ) , @ " {channel} = {Mister Mv} " ) ;
insta ::assert_snapshot! ( p ( " channel = \" Mister Mv \" " ) , @ " {channel} = {Mister Mv} " ) ;
insta ::assert_snapshot! ( p ( " 'dog race' = Borzoi " ) , @ " {dog race} = {Borzoi} " ) ;
insta ::assert_snapshot! ( p ( " \" dog race \" = Chusky " ) , @ " {dog race} = {Chusky} " ) ;
insta ::assert_snapshot! ( p ( " \" dog race \" = \" Bernese Mountain \" " ) , @ " {dog race} = {Bernese Mountain} " ) ;
insta ::assert_snapshot! ( p ( " 'dog race' = 'Bernese Mountain' " ) , @ " {dog race} = {Bernese Mountain} " ) ;
insta ::assert_snapshot! ( p ( " \" dog race \" = 'Bernese Mountain' " ) , @ " {dog race} = {Bernese Mountain} " ) ;
2022-08-17 17:25:31 +02:00
// Test IN
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN[] " ) , @ " {colour} IN[] " ) ;
insta ::assert_snapshot! ( p ( " colour IN[green] " ) , @ " {colour} IN[{green}, ] " ) ;
insta ::assert_snapshot! ( p ( " colour IN[green,] " ) , @ " {colour} IN[{green}, ] " ) ;
insta ::assert_snapshot! ( p ( " colour NOT IN[green,blue] " ) , @ " NOT ({colour} IN[{green}, {blue}, ]) " ) ;
insta ::assert_snapshot! ( p ( " colour IN [ green , blue , ] " ) , @ " {colour} IN[{green}, {blue}, ] " ) ;
2022-08-17 17:25:31 +02:00
2022-08-18 10:58:04 +02:00
// Test IN + OR/AND/()
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN [green, blue] AND color = green " ) , @ " AND[{colour} IN[{green}, {blue}, ], {color} = {green}, ] " ) ;
insta ::assert_snapshot! ( p ( " NOT (colour IN [green, blue]) AND color = green " ) , @ " AND[NOT ({colour} IN[{green}, {blue}, ]), {color} = {green}, ] " ) ;
insta ::assert_snapshot! ( p ( " x = 1 OR NOT (colour IN [green, blue] OR color = green) " ) , @ " OR[{x} = {1}, NOT (OR[{colour} IN[{green}, {blue}, ], {color} = {green}, ]), ] " ) ;
2022-08-18 10:58:04 +02:00
// Test whitespace start/end
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour = green " ) , @ " {colour} = {green} " ) ;
insta ::assert_snapshot! ( p ( " (colour = green OR colour = red) " ) , @ " OR[{colour} = {green}, {colour} = {red}, ] " ) ;
insta ::assert_snapshot! ( p ( " colour IN [green, blue] AND color = green " ) , @ " AND[{colour} IN[{green}, {blue}, ], {color} = {green}, ] " ) ;
insta ::assert_snapshot! ( p ( " colour NOT IN [green, blue] " ) , @ " NOT ({colour} IN[{green}, {blue}, ]) " ) ;
insta ::assert_snapshot! ( p ( " colour IN [green, blue] " ) , @ " {colour} IN[{green}, {blue}, ] " ) ;
2022-08-18 10:58:04 +02:00
2022-08-17 17:25:31 +02:00
// Test conditions
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel != ponce " ) , @ " {channel} != {ponce} " ) ;
insta ::assert_snapshot! ( p ( " NOT channel = ponce " ) , @ " NOT ({channel} = {ponce}) " ) ;
insta ::assert_snapshot! ( p ( " subscribers < 1000 " ) , @ " {subscribers} < {1000} " ) ;
insta ::assert_snapshot! ( p ( " subscribers > 1000 " ) , @ " {subscribers} > {1000} " ) ;
insta ::assert_snapshot! ( p ( " subscribers <= 1000 " ) , @ " {subscribers} <= {1000} " ) ;
insta ::assert_snapshot! ( p ( " subscribers >= 1000 " ) , @ " {subscribers} >= {1000} " ) ;
insta ::assert_snapshot! ( p ( " subscribers <= 1000 " ) , @ " {subscribers} <= {1000} " ) ;
insta ::assert_snapshot! ( p ( " subscribers 100 TO 1000 " ) , @ " {subscribers} {100} TO {1000} " ) ;
2022-08-17 17:25:31 +02:00
2023-03-08 16:57:42 +01:00
// Test NOT
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " NOT subscribers < 1000 " ) , @ " NOT ({subscribers} < {1000}) " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers 100 TO 1000 " ) , @ " NOT ({subscribers} {100} TO {1000}) " ) ;
2023-03-08 16:57:42 +01:00
// Test NULL + NOT NULL
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " subscribers IS NULL " ) , @ " {subscribers} IS NULL " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers IS NULL " ) , @ " NOT ({subscribers} IS NULL) " ) ;
insta ::assert_snapshot! ( p ( " subscribers IS NOT NULL " ) , @ " NOT ({subscribers} IS NULL) " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers IS NOT NULL " ) , @ " {subscribers} IS NULL " ) ;
insta ::assert_snapshot! ( p ( " subscribers IS NOT NULL " ) , @ " NOT ({subscribers} IS NULL) " ) ;
2023-03-08 16:57:42 +01:00
2023-03-14 18:08:12 +01:00
// Test EMPTY + NOT EMPTY
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " subscribers IS EMPTY " ) , @ " {subscribers} IS EMPTY " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers IS EMPTY " ) , @ " NOT ({subscribers} IS EMPTY) " ) ;
insta ::assert_snapshot! ( p ( " subscribers IS NOT EMPTY " ) , @ " NOT ({subscribers} IS EMPTY) " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers IS NOT EMPTY " ) , @ " {subscribers} IS EMPTY " ) ;
insta ::assert_snapshot! ( p ( " subscribers IS NOT EMPTY " ) , @ " NOT ({subscribers} IS EMPTY) " ) ;
2023-03-14 18:08:12 +01:00
2023-03-08 16:57:42 +01:00
// Test EXISTS + NOT EXITS
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " subscribers EXISTS " ) , @ " {subscribers} EXISTS " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers EXISTS " ) , @ " NOT ({subscribers} EXISTS) " ) ;
insta ::assert_snapshot! ( p ( " subscribers NOT EXISTS " ) , @ " NOT ({subscribers} EXISTS) " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers NOT EXISTS " ) , @ " {subscribers} EXISTS " ) ;
insta ::assert_snapshot! ( p ( " subscribers NOT EXISTS " ) , @ " NOT ({subscribers} EXISTS) " ) ;
2022-08-17 17:25:31 +02:00
2024-07-17 11:13:37 +02:00
// Test CONTAINS + NOT CONTAINS
insta ::assert_snapshot! ( p ( " subscribers CONTAINS 'hello' " ) , @ " {subscribers} CONTAINS {hello} " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers CONTAINS 'hello' " ) , @ " NOT ({subscribers} CONTAINS {hello}) " ) ;
insta ::assert_snapshot! ( p ( " subscribers NOT CONTAINS hello " ) , @ " NOT ({subscribers} CONTAINS {hello}) " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers NOT CONTAINS 'hello' " ) , @ " {subscribers} CONTAINS {hello} " ) ;
insta ::assert_snapshot! ( p ( " subscribers NOT CONTAINS 'hello' " ) , @ " NOT ({subscribers} CONTAINS {hello}) " ) ;
2024-09-17 16:44:11 +02:00
// Test STARTS WITH + NOT STARTS WITH
insta ::assert_snapshot! ( p ( " subscribers STARTS WITH 'hel' " ) , @ " {subscribers} STARTS WITH {hel} " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers STARTS WITH 'hel' " ) , @ " NOT ({subscribers} STARTS WITH {hel}) " ) ;
insta ::assert_snapshot! ( p ( " subscribers NOT STARTS WITH hel " ) , @ " NOT ({subscribers} STARTS WITH {hel}) " ) ;
insta ::assert_snapshot! ( p ( " NOT subscribers NOT STARTS WITH 'hel' " ) , @ " {subscribers} STARTS WITH {hel} " ) ;
insta ::assert_snapshot! ( p ( " subscribers NOT STARTS WITH 'hel' " ) , @ " NOT ({subscribers} STARTS WITH {hel}) " ) ;
2022-08-18 10:58:24 +02:00
// Test nested NOT
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " NOT NOT NOT NOT x = 5 " ) , @ " {x} = {5} " ) ;
insta ::assert_snapshot! ( p ( " NOT NOT (NOT NOT x = 5) " ) , @ " {x} = {5} " ) ;
2022-08-18 10:58:24 +02:00
2022-08-17 17:25:31 +02:00
// Test geo radius
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoRadius(12, 13, 14) " ) , @ " _geoRadius({12}, {13}, {14}) " ) ;
insta ::assert_snapshot! ( p ( " NOT _geoRadius(12, 13, 14) " ) , @ " NOT (_geoRadius({12}, {13}, {14})) " ) ;
insta ::assert_snapshot! ( p ( " _geoRadius(12,13,14) " ) , @ " _geoRadius({12}, {13}, {14}) " ) ;
2022-08-17 17:25:31 +02:00
2022-10-28 18:11:11 +02:00
// Test geo bounding box
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoBoundingBox([12, 13], [14, 15]) " ) , @ " _geoBoundingBox([{12}, {13}], [{14}, {15}]) " ) ;
insta ::assert_snapshot! ( p ( " NOT _geoBoundingBox([12, 13], [14, 15]) " ) , @ " NOT (_geoBoundingBox([{12}, {13}], [{14}, {15}])) " ) ;
insta ::assert_snapshot! ( p ( " _geoBoundingBox([12,13],[14,15]) " ) , @ " _geoBoundingBox([{12}, {13}], [{14}, {15}]) " ) ;
2022-10-28 18:11:11 +02:00
2022-08-17 17:25:31 +02:00
// Test OR + AND
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = ponce AND 'dog race' != 'bernese mountain' " ) , @ " AND[{channel} = {ponce}, {dog race} != {bernese mountain}, ] " ) ;
insta ::assert_snapshot! ( p ( " channel = ponce OR 'dog race' != 'bernese mountain' " ) , @ " OR[{channel} = {ponce}, {dog race} != {bernese mountain}, ] " ) ;
insta ::assert_snapshot! ( p ( " channel = ponce AND 'dog race' != 'bernese mountain' OR subscribers > 1000 " ) , @ " OR[AND[{channel} = {ponce}, {dog race} != {bernese mountain}, ], {subscribers} > {1000}, ] " ) ;
insta ::assert_snapshot! (
2022-08-18 13:03:55 +02:00
p ( " channel = ponce AND 'dog race' != 'bernese mountain' OR subscribers > 1000 OR colour = red OR colour = blue AND size = 7 " ) ,
2022-08-17 17:25:31 +02:00
@ " OR[AND[{channel} = {ponce}, {dog race} != {bernese mountain}, ], {subscribers} > {1000}, {colour} = {red}, AND[{colour} = {blue}, {size} = {7}, ], ] "
) ;
2022-08-18 11:27:39 +02:00
// Test parentheses
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = ponce AND ( 'dog race' != 'bernese mountain' OR subscribers > 1000 ) " ) , @ " AND[{channel} = {ponce}, OR[{dog race} != {bernese mountain}, {subscribers} > {1000}, ], ] " ) ;
insta ::assert_snapshot! ( p ( " (channel = ponce AND 'dog race' != 'bernese mountain' OR subscribers > 1000) AND _geoRadius(12, 13, 14) " ) , @ " AND[OR[AND[{channel} = {ponce}, {dog race} != {bernese mountain}, ], {subscribers} > {1000}, ], _geoRadius({12}, {13}, {14}), ] " ) ;
2022-08-18 11:27:39 +02:00
// Test recursion
// This is the most that is allowed
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! (
2022-08-18 13:03:55 +02:00
p ( " (((((((((((((((((((((((((((((((((((((((((((((((((x = 1))))))))))))))))))))))))))))))))))))))))))))))))) " ) ,
2022-08-18 11:27:39 +02:00
@ " {x} = {1} "
) ;
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! (
2022-08-18 13:03:55 +02:00
p ( " NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT x = 1 " ) ,
2022-08-18 11:27:39 +02:00
@ " NOT ({x} = {1}) "
) ;
2022-08-18 11:55:01 +02:00
// Confusing keywords
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "NOT "OR" EXISTS AND "EXISTS" NOT EXISTS"# ) , @ " AND[NOT ({OR} EXISTS), NOT ({EXISTS} EXISTS), ] " ) ;
2021-10-22 01:15:42 +02:00
}
2021-11-02 17:35:17 +01:00
#[ test ]
fn error ( ) {
use FilterCondition as Fc ;
2022-10-27 16:58:13 +02:00
fn p ( s : & str ) -> impl std ::fmt ::Display + '_ {
2022-08-18 13:03:55 +02:00
Fc ::parse ( s ) . unwrap_err ( ) . to_string ( )
2021-11-02 17:35:17 +01:00
}
2022-08-17 17:25:31 +02:00
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = Ponce = 12 " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Found unexpected characters at the end of the filter : ` = 12 ` . You probably forgot an ` OR ` or an ` AND ` rule .
17 :21 channel = Ponce = 12
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Was expecting a value but instead got nothing .
2022-10-28 13:42:21 +02:00
14 :14 channel =
2022-08-17 17:25:31 +02:00
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = 🐻 " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Was expecting a value but instead got ` 🐻 ` .
11 :12 channel = 🐻
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = 🐻 AND followers < 100 " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Was expecting a value but instead got ` 🐻 ` .
11 :12 channel = 🐻 AND followers < 100
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " 'OR' " ) , @ r ###"
2024-07-17 11:13:37 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` \ ' OR \ ' ` .
2022-08-17 17:25:31 +02:00
1 :5 ' OR '
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " OR " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Was expecting a value but instead got ` OR ` , which is a reserved keyword . To use ` OR ` as a field name or a value , surround it by quotes .
1 :3 OR
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel Ponce " ) , @ r ###"
2024-07-17 11:13:37 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` channel Ponce ` .
2022-08-17 17:25:31 +02:00
1 :14 channel Ponce
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = Ponce OR " ) , @ r ###"
2024-07-17 11:13:37 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` _geoRadius ` , or ` _geoBoundingBox ` but instead got nothing .
2022-08-17 17:25:31 +02:00
19 :19 channel = Ponce OR
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoRadius " ) , @ r ###"
2022-08-17 17:25:31 +02:00
The ` _geoRadius ` filter expects three arguments : ` _geoRadius ( latitude , longitude , radius ) ` .
1 :11 _geoRadius
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoRadius = 12 " ) , @ r ###"
2022-08-17 17:25:31 +02:00
The ` _geoRadius ` filter expects three arguments : ` _geoRadius ( latitude , longitude , radius ) ` .
1 :16 _geoRadius = 12
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoBoundingBox " ) , @ r ###"
2023-02-06 16:50:27 +01:00
The ` _geoBoundingBox ` filter expects two pairs of arguments : ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` .
2022-10-28 18:11:11 +02:00
1 :16 _geoBoundingBox
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoBoundingBox = 12 " ) , @ r ###"
2023-02-06 16:50:27 +01:00
The ` _geoBoundingBox ` filter expects two pairs of arguments : ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` .
2022-10-28 18:11:11 +02:00
1 :21 _geoBoundingBox = 12
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoBoundingBox(1.0, 1.0) " ) , @ r ###"
2023-02-06 16:50:27 +01:00
The ` _geoBoundingBox ` filter expects two pairs of arguments : ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` .
2022-10-28 18:11:11 +02:00
1 :26 _geoBoundingBox ( 1.0 , 1.0 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoPoint(12, 13, 14) " ) , @ r ###"
2023-03-31 22:10:37 +02:00
` _geoPoint ` is a reserved keyword and thus can ' t be used as a filter expression . Use the ` _geoRadius ( latitude , longitude , distance ) ` or ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` built - in rules to filter on ` _geo ` coordinates .
2022-08-17 17:25:31 +02:00
1 :22 _geoPoint ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " position <= _geoPoint(12, 13, 14) " ) , @ r ###"
2023-03-31 22:10:37 +02:00
` _geoPoint ` is a reserved keyword and thus can ' t be used as a filter expression . Use the ` _geoRadius ( latitude , longitude , distance ) ` or ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` built - in rules to filter on ` _geo ` coordinates .
2022-08-17 17:25:31 +02:00
13 :34 position < = _geoPoint ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geoDistance(12, 13, 14) " ) , @ r ###"
2023-03-31 22:10:37 +02:00
` _geoDistance ` is a reserved keyword and thus can ' t be used as a filter expression . Use the ` _geoRadius ( latitude , longitude , distance ) ` or ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` built - in rules to filter on ` _geo ` coordinates .
2023-03-31 16:24:25 +02:00
1 :25 _geoDistance ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " position <= _geoDistance(12, 13, 14) " ) , @ r ###"
2023-03-31 22:10:37 +02:00
` _geoDistance ` is a reserved keyword and thus can ' t be used as a filter expression . Use the ` _geoRadius ( latitude , longitude , distance ) ` or ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` built - in rules to filter on ` _geo ` coordinates .
2023-03-31 16:24:25 +02:00
13 :37 position < = _geoDistance ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " _geo(12, 13, 14) " ) , @ r ###"
2023-03-31 22:10:37 +02:00
` _geo ` is a reserved keyword and thus can ' t be used as a filter expression . Use the ` _geoRadius ( latitude , longitude , distance ) ` or ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` built - in rules to filter on ` _geo ` coordinates .
2023-03-31 16:21:27 +02:00
1 :17 _geo ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " position <= _geo(12, 13, 14) " ) , @ r ###"
2023-03-31 22:10:37 +02:00
` _geo ` is a reserved keyword and thus can ' t be used as a filter expression . Use the ` _geoRadius ( latitude , longitude , distance ) ` or ` _geoBoundingBox ( [ latitude , longitude ] , [ latitude , longitude ] ) ` built - in rules to filter on ` _geo ` coordinates .
2023-03-31 16:21:27 +02:00
13 :29 position < = _geo ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " position <= _geoRadius(12, 13, 14) " ) , @ r ###"
2022-08-17 17:25:31 +02:00
The ` _geoRadius ` filter is an operation and can ' t be used as a value .
13 :35 position < = _geoRadius ( 12 , 13 , 14 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = 'ponce " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expression ` \ ' ponce ` is missing the following closing delimiter : ` ' ` .
11 :17 channel = ' ponce
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = \" ponce " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expression ` \ " ponce` is missing the following closing delimiter: ` " ` .
11 :17 channel = " ponce
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = mv OR (followers >= 1000 " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expression ` ( followers > = 1000 ` is missing the following closing delimiter : ` ) ` .
17 :35 channel = mv OR ( followers > = 1000
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = mv OR followers >= 1000) " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Found unexpected characters at the end of the filter : ` ) ` . You probably forgot an ` OR ` or an ` AND ` rule .
34 :35 channel = mv OR followers > = 1000 )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour NOT EXIST " ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` colour NOT EXIST ` .
2022-08-17 17:25:31 +02:00
1 :17 colour NOT EXIST
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " subscribers 100 TO1000 " ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` subscribers 100 TO1000 ` .
2022-08-17 17:25:31 +02:00
1 :23 subscribers 100 TO1000
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " channel = ponce ORdog != 'bernese mountain' " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Found unexpected characters at the end of the filter : ` ORdog ! = \ ' bernese mountain \ ' ` . You probably forgot an ` OR ` or an ` AND ` rule .
17 :44 channel = ponce ORdog ! = ' bernese mountain '
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN blue, green] " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expected ` [ ` after ` IN ` keyword .
11 :23 colour IN blue , green ]
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN [blue, green, 'blue' > 2] " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expected only comma - separated field names inside ` IN [ .. ] ` but instead found ` > 2 ] ` .
32 :36 colour IN [ blue , green , ' blue ' > 2 ]
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN [blue, green, AND] " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expected only comma - separated field names inside ` IN [ .. ] ` but instead found ` AND ] ` .
25 :29 colour IN [ blue , green , AND ]
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN [blue, green " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expected matching ` ] ` after the list of field names given to ` IN [ `
23 :23 colour IN [ blue , green
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " colour IN ['blue, green " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Expression ` \ ' blue , green ` is missing the following closing delimiter : ` ' ` .
12 :24 colour IN [ ' blue , green
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " x = EXISTS " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Was expecting a value but instead got ` EXISTS ` , which is a reserved keyword . To use ` EXISTS ` as a field name or a value , surround it by quotes .
5 :11 x = EXISTS
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " AND = 8 " ) , @ r ###"
2022-08-17 17:25:31 +02:00
Was expecting a value but instead got ` AND ` , which is a reserved keyword . To use ` AND ` as a field name or a value , surround it by quotes .
1 :4 AND = 8
" ###);
2022-08-18 11:27:39 +02:00
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( " ((((((((((((((((((((((((((((((((((((((((((((((((((x = 1)))))))))))))))))))))))))))))))))))))))))))))))))) " ) , @ r ###"
2022-08-18 11:27:39 +02:00
The filter exceeded the maximum depth limit . Try rewriting the filter so that it contains fewer nested conditions .
51 :106 ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( x = 1 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! (
2022-08-18 13:03:55 +02:00
p ( " NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT x = 1 " ) ,
2022-08-18 11:27:39 +02:00
@ r ###"
The filter exceeded the maximum depth limit . Try rewriting the filter so that it contains fewer nested conditions .
797 :802 NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT NOT x = 1
" ###
) ;
2022-08-18 11:55:01 +02:00
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "NOT OR EXISTS AND EXISTS NOT EXISTS"# ) , @ r ###"
2022-08-18 11:55:01 +02:00
Was expecting a value but instead got ` OR ` , which is a reserved keyword . To use ` OR ` as a field name or a value , surround it by quotes .
5 :7 NOT OR EXISTS AND EXISTS NOT EXISTS
" ###);
2023-03-16 11:09:20 +01:00
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value NULL"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value NULL ` .
2023-03-16 11:09:20 +01:00
1 :11 value NULL
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value NOT NULL"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value NOT NULL ` .
2023-03-16 11:09:20 +01:00
1 :15 value NOT NULL
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value EMPTY"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value EMPTY ` .
2023-03-16 11:09:20 +01:00
1 :12 value EMPTY
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value NOT EMPTY"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value NOT EMPTY ` .
2023-03-16 11:09:20 +01:00
1 :16 value NOT EMPTY
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value IS"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value IS ` .
2023-03-16 11:09:20 +01:00
1 :9 value IS
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value IS NOT"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value IS NOT ` .
2023-03-16 11:09:20 +01:00
1 :13 value IS NOT
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value IS EXISTS"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value IS EXISTS ` .
2023-03-16 11:09:20 +01:00
1 :16 value IS EXISTS
" ###);
2024-07-08 18:38:05 +02:00
insta ::assert_snapshot! ( p ( r # "value IS NOT EXISTS"# ) , @ r ###"
2024-09-17 16:44:11 +02:00
Was expecting an operation ` = ` , ` ! = ` , ` > = ` , ` > ` , ` < = ` , ` < ` , ` IN ` , ` NOT IN ` , ` TO ` , ` EXISTS ` , ` NOT EXISTS ` , ` IS NULL ` , ` IS NOT NULL ` , ` IS EMPTY ` , ` IS NOT EMPTY ` , ` CONTAINS ` , ` NOT CONTAINS ` , ` STARTS WITH ` , ` NOT STARTS WITH ` , ` _geoRadius ` , or ` _geoBoundingBox ` at ` value IS NOT EXISTS ` .
2023-03-16 11:09:20 +01:00
1 :20 value IS NOT EXISTS
" ###);
2021-11-02 17:35:17 +01:00
}
2021-12-06 17:35:20 +01:00
#[ test ]
fn depth ( ) {
2021-12-09 11:13:12 +01:00
let filter = FilterCondition ::parse ( " account_ids=1 OR account_ids=2 OR account_ids=3 OR account_ids=4 OR account_ids=5 OR account_ids=6 " ) . unwrap ( ) . unwrap ( ) ;
2022-06-20 18:46:57 +02:00
assert! ( filter . token_at_depth ( 1 ) . is_some ( ) ) ;
assert! ( filter . token_at_depth ( 2 ) . is_none ( ) ) ;
let filter = FilterCondition ::parse ( " (account_ids=1 OR (account_ids=2 AND account_ids=3) OR (account_ids=4 AND account_ids=5) OR account_ids=6) " ) . unwrap ( ) . unwrap ( ) ;
assert! ( filter . token_at_depth ( 2 ) . is_some ( ) ) ;
assert! ( filter . token_at_depth ( 3 ) . is_none ( ) ) ;
let filter = FilterCondition ::parse ( " account_ids=1 OR account_ids=2 AND account_ids=3 OR account_ids=4 AND account_ids=5 OR account_ids=6 " ) . unwrap ( ) . unwrap ( ) ;
assert! ( filter . token_at_depth ( 2 ) . is_some ( ) ) ;
assert! ( filter . token_at_depth ( 3 ) . is_none ( ) ) ;
2021-12-06 17:35:20 +01:00
}
2022-12-03 19:13:41 +01:00
#[ test ]
fn token_from_str ( ) {
let s = " test string that should not be parsed " ;
let token : Token = s . into ( ) ;
assert_eq! ( token . value ( ) , s ) ;
}
2021-10-22 01:15:42 +02:00
}