From 77cf5b37875c493a84cd4909d939af8679206556 Mon Sep 17 00:00:00 2001 From: Filip Bachul Date: Fri, 31 Mar 2023 16:24:25 +0200 Subject: [PATCH] handle _geoDistance(x,y,z) filter error --- filter-parser/src/lib.rs | 24 ++++++++++++++++++++++++ filter-parser/src/value.rs | 6 +++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/filter-parser/src/lib.rs b/filter-parser/src/lib.rs index d6fe0e889..6a9e817a6 100644 --- a/filter-parser/src/lib.rs +++ b/filter-parser/src/lib.rs @@ -382,6 +382,20 @@ fn parse_geo_point(input: Span) -> IResult { Err(nom::Err::Failure(Error::new_from_kind(input, ErrorKind::ReservedGeo("_geoPoint")))) } +/// geoPoint = WS* "_geoDistance(float WS* "," WS* float WS* "," WS* float) +fn parse_geo_distance(input: Span) -> IResult { + // 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")))) +} + /// geo = WS* "_geo(float WS* "," WS* float WS* "," WS* float) fn parse_geo(input: Span) -> IResult { // we want to forbid space BEFORE the _geo but not after @@ -645,6 +659,16 @@ pub mod tests { 13:34 position <= _geoPoint(12, 13, 14) "###); + insta::assert_display_snapshot!(p("_geoDistance(12, 13, 14)"), @r###" + `_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. + 1:25 _geoDistance(12, 13, 14) + "###); + + insta::assert_display_snapshot!(p("position <= _geoDistance(12, 13, 14)"), @r###" + `_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. + 13:37 position <= _geoDistance(12, 13, 14) + "###); + insta::assert_display_snapshot!(p("_geo(12, 13, 14)"), @r###" `_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. 1:17 _geo(12, 13, 14) diff --git a/filter-parser/src/value.rs b/filter-parser/src/value.rs index 9d02a7153..fb8f35503 100644 --- a/filter-parser/src/value.rs +++ b/filter-parser/src/value.rs @@ -7,8 +7,8 @@ use nom::{InputIter, InputLength, InputTake, Slice}; use crate::error::{ExpectedValueKind, NomErrorExt}; use crate::{ - parse_geo, parse_geo_bounding_box, parse_geo_point, parse_geo_radius, Error, ErrorKind, - IResult, Span, Token, + parse_geo, parse_geo_bounding_box, parse_geo_distance, parse_geo_point, parse_geo_radius, + Error, ErrorKind, IResult, Span, Token, }; /// This function goes through all characters in the [Span] if it finds any escaped character (`\`). @@ -88,7 +88,7 @@ pub fn parse_value(input: Span) -> IResult { // then, we want to check if the user is misusing a geo expression // This expression can’t finish without error. // We want to return an error in case of failure. - let geo_reserved_parse_functions = [parse_geo_point, parse_geo]; + let geo_reserved_parse_functions = [parse_geo_point, parse_geo_distance, parse_geo]; for parser in geo_reserved_parse_functions { if let Err(err) = parser(input) {