From 27a6a26b4be13936f52172d31a9da4407dbc24d8 Mon Sep 17 00:00:00 2001 From: Tamo Date: Fri, 5 Nov 2021 10:46:54 +0100 Subject: [PATCH] makes the parse function part of the filter_parser --- filter_parser/src/lib.rs | 9 +++++++ milli/src/search/facet/filter_condition.rs | 29 ++++++---------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/filter_parser/src/lib.rs b/filter_parser/src/lib.rs index 31aa973ab..d09744196 100644 --- a/filter_parser/src/lib.rs +++ b/filter_parser/src/lib.rs @@ -40,6 +40,7 @@ mod error; mod value; use std::fmt::Debug; +use std::str::FromStr; pub use condition::{parse_condition, parse_to, Condition}; use error::{cut_with_err, ExtendNomError}; @@ -73,6 +74,14 @@ impl<'a> Token<'a> { pub fn as_external_error(&self, error: impl std::error::Error) -> Error<'a> { Error::new_from_external(self.position, error) } + + pub fn parse(&self) -> Result + where + T: FromStr, + T::Err: std::error::Error, + { + self.inner.parse().map_err(|e| self.as_external_error(e)) + } } impl<'a> From> for Token<'a> { diff --git a/milli/src/search/facet/filter_condition.rs b/milli/src/search/facet/filter_condition.rs index 83873285f..164e9aed5 100644 --- a/milli/src/search/facet/filter_condition.rs +++ b/milli/src/search/facet/filter_condition.rs @@ -1,6 +1,5 @@ use std::fmt::{Debug, Display}; use std::ops::Bound::{self, Excluded, Included}; -use std::str::FromStr; use either::Either; pub use filter_parser::{Condition, Error as FPError, FilterCondition, Span, Token}; @@ -57,20 +56,6 @@ impl<'a> From> for Error { } } -fn parse(tok: &Token) -> Result -where - T: FromStr, - T::Err: std::error::Error, -{ - match tok.inner.parse::() { - Ok(t) => Ok(t), - Err(e) => { - Err(UserError::InvalidFilter(FPError::new_from_external(tok.position, e).to_string()) - .into()) - } - } -} - impl<'a> From> for FilterCondition<'a> { fn from(f: Filter<'a>) -> Self { f.condition @@ -254,11 +239,11 @@ impl<'a> Filter<'a> { // field id and the level. let (left, right) = match operator { - Condition::GreaterThan(val) => (Excluded(parse(val)?), Included(f64::MAX)), - Condition::GreaterThanOrEqual(val) => (Included(parse(val)?), Included(f64::MAX)), - Condition::LowerThan(val) => (Included(f64::MIN), Excluded(parse(val)?)), - Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(parse(val)?)), - Condition::Between { from, to } => (Included(parse(from)?), Included(parse(to)?)), + Condition::GreaterThan(val) => (Excluded(val.parse()?), Included(f64::MAX)), + Condition::GreaterThanOrEqual(val) => (Included(val.parse()?), Included(f64::MAX)), + Condition::LowerThan(val) => (Included(f64::MIN), Excluded(val.parse()?)), + Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(val.parse()?)), + Condition::Between { from, to } => (Included(from.parse()?), Included(to.parse()?)), Condition::Equal(val) => { let (_original_value, string_docids) = strings_db .get(rtxn, &(field_id, &val.inner.to_lowercase()))? @@ -373,7 +358,7 @@ impl<'a> Filter<'a> { FilterCondition::GeoLowerThan { point, radius } => { let filterable_fields = index.fields_ids_map(rtxn)?; if filterable_fields.id("_geo").is_some() { - let base_point: [f64; 2] = [parse(&point[0])?, parse(&point[1])?]; + let base_point: [f64; 2] = [point[0].parse()?, point[1].parse()?]; if !(-90.0..=90.0).contains(&base_point[0]) { return Err( point[0].as_external_error(FilterError::BadGeoLat(base_point[0])) @@ -384,7 +369,7 @@ impl<'a> Filter<'a> { point[1].as_external_error(FilterError::BadGeoLng(base_point[1])) )?; } - let radius = parse(&radius)?; + let radius = radius.parse()?; let rtree = match index.geo_rtree(rtxn)? { Some(rtree) => rtree, None => return Ok(RoaringBitmap::new()),