makes the parse function part of the filter_parser

This commit is contained in:
Tamo 2021-11-05 10:46:54 +01:00
parent 76d961cc77
commit 27a6a26b4b
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
2 changed files with 16 additions and 22 deletions

View File

@ -40,6 +40,7 @@ mod error;
mod value; mod value;
use std::fmt::Debug; use std::fmt::Debug;
use std::str::FromStr;
pub use condition::{parse_condition, parse_to, Condition}; pub use condition::{parse_condition, parse_to, Condition};
use error::{cut_with_err, ExtendNomError}; 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> { pub fn as_external_error(&self, error: impl std::error::Error) -> Error<'a> {
Error::new_from_external(self.position, error) Error::new_from_external(self.position, error)
} }
pub fn parse<T>(&self) -> Result<T, Error>
where
T: FromStr,
T::Err: std::error::Error,
{
self.inner.parse().map_err(|e| self.as_external_error(e))
}
} }
impl<'a> From<Span<'a>> for Token<'a> { impl<'a> From<Span<'a>> for Token<'a> {

View File

@ -1,6 +1,5 @@
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::ops::Bound::{self, Excluded, Included}; use std::ops::Bound::{self, Excluded, Included};
use std::str::FromStr;
use either::Either; use either::Either;
pub use filter_parser::{Condition, Error as FPError, FilterCondition, Span, Token}; pub use filter_parser::{Condition, Error as FPError, FilterCondition, Span, Token};
@ -57,20 +56,6 @@ impl<'a> From<FPError<'a>> for Error {
} }
} }
fn parse<T>(tok: &Token) -> Result<T>
where
T: FromStr,
T::Err: std::error::Error,
{
match tok.inner.parse::<T>() {
Ok(t) => Ok(t),
Err(e) => {
Err(UserError::InvalidFilter(FPError::new_from_external(tok.position, e).to_string())
.into())
}
}
}
impl<'a> From<Filter<'a>> for FilterCondition<'a> { impl<'a> From<Filter<'a>> for FilterCondition<'a> {
fn from(f: Filter<'a>) -> Self { fn from(f: Filter<'a>) -> Self {
f.condition f.condition
@ -254,11 +239,11 @@ impl<'a> Filter<'a> {
// field id and the level. // field id and the level.
let (left, right) = match operator { let (left, right) = match operator {
Condition::GreaterThan(val) => (Excluded(parse(val)?), Included(f64::MAX)), Condition::GreaterThan(val) => (Excluded(val.parse()?), Included(f64::MAX)),
Condition::GreaterThanOrEqual(val) => (Included(parse(val)?), Included(f64::MAX)), Condition::GreaterThanOrEqual(val) => (Included(val.parse()?), Included(f64::MAX)),
Condition::LowerThan(val) => (Included(f64::MIN), Excluded(parse(val)?)), Condition::LowerThan(val) => (Included(f64::MIN), Excluded(val.parse()?)),
Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(parse(val)?)), Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(val.parse()?)),
Condition::Between { from, to } => (Included(parse(from)?), Included(parse(to)?)), Condition::Between { from, to } => (Included(from.parse()?), Included(to.parse()?)),
Condition::Equal(val) => { Condition::Equal(val) => {
let (_original_value, string_docids) = strings_db let (_original_value, string_docids) = strings_db
.get(rtxn, &(field_id, &val.inner.to_lowercase()))? .get(rtxn, &(field_id, &val.inner.to_lowercase()))?
@ -373,7 +358,7 @@ impl<'a> Filter<'a> {
FilterCondition::GeoLowerThan { point, radius } => { FilterCondition::GeoLowerThan { point, radius } => {
let filterable_fields = index.fields_ids_map(rtxn)?; let filterable_fields = index.fields_ids_map(rtxn)?;
if filterable_fields.id("_geo").is_some() { 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]) { if !(-90.0..=90.0).contains(&base_point[0]) {
return Err( return Err(
point[0].as_external_error(FilterError::BadGeoLat(base_point[0])) 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])) 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)? { let rtree = match index.geo_rtree(rtxn)? {
Some(rtree) => rtree, Some(rtree) => rtree,
None => return Ok(RoaringBitmap::new()), None => return Ok(RoaringBitmap::new()),