convert the errors

This commit is contained in:
Tamo 2021-10-22 16:38:35 +02:00
parent d6ba84ea99
commit efb2f8b325
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
4 changed files with 26 additions and 33 deletions

View File

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
nom = "7.0.0" nom = "7.0.0"
nom_locate = "4.0.0" nom_locate = "4.0.0"
nom-greedyerror = "0.4.0"

View File

@ -24,17 +24,19 @@ use nom::branch::alt;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::character::complete::{char, multispace0}; use nom::character::complete::{char, multispace0};
use nom::combinator::map; use nom::combinator::map;
use nom::error::{ContextError, Error, VerboseError}; use nom::error::{ContextError, Error, ErrorKind, VerboseError};
use nom::multi::{many0, separated_list1}; use nom::multi::{many0, separated_list1};
use nom::number::complete::recognize_float; use nom::number::complete::recognize_float;
use nom::sequence::{delimited, preceded, tuple}; use nom::sequence::{delimited, preceded, tuple};
use nom::{Finish, IResult}; use nom::{Finish, IResult};
use nom_greedyerror::GreedyError;
use nom_locate::LocatedSpan; use nom_locate::LocatedSpan;
pub(crate) use value::parse_value; pub(crate) use value::parse_value;
pub type Span<'a> = LocatedSpan<&'a str>; pub type Span<'a> = LocatedSpan<&'a str>;
pub trait FilterParserError<'a>: nom::error::ParseError<Span<'a>> + ContextError<Span<'a>> {} pub trait FilterParserError<'a>: nom::error::ParseError<Span<'a>> + ContextError<Span<'a>> {}
impl<'a> FilterParserError<'a> for GreedyError<Span<'a>, ErrorKind> {}
impl<'a> FilterParserError<'a> for VerboseError<Span<'a>> {} impl<'a> FilterParserError<'a> for VerboseError<Span<'a>> {}
impl<'a> FilterParserError<'a> for Error<Span<'a>> {} impl<'a> FilterParserError<'a> for Error<Span<'a>> {}

View File

@ -41,6 +41,7 @@ uuid = { version = "0.8.2", features = ["v4"] }
# facet filter parser # facet filter parser
filter_parser = { path = "../filter_parser" } filter_parser = { path = "../filter_parser" }
nom = "7.0.0" nom = "7.0.0"
nom-greedyerror = "0.4.0"
# documents words self-join # documents words self-join
itertools = "0.10.0" itertools = "0.10.0"

View File

@ -2,10 +2,11 @@ use std::fmt::Debug;
use std::ops::Bound::{self, Excluded, Included}; use std::ops::Bound::{self, Excluded, Included};
use either::Either; use either::Either;
use filter_parser::{Condition, FilterCondition, Span, Token}; use filter_parser::{Condition, FilterCondition, FilterParserError, Span, Token};
use heed::types::DecodeIgnore; use heed::types::DecodeIgnore;
use log::debug; use log::debug;
use nom::error::{convert_error, VerboseError}; use nom::error::{ErrorKind, VerboseError};
use nom_greedyerror::{convert_error, GreedyError};
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
use super::FacetNumberRange; use super::FacetNumberRange;
@ -20,12 +21,14 @@ pub struct Filter<'a> {
condition: FilterCondition<'a>, condition: FilterCondition<'a>,
} }
impl<'a> From<VerboseError<Span<'a>>> for Error {
fn from(nom_error: VerboseError<Span<'a>>) -> Self {
UserError::InvalidFilter { input: nom_error.to_string() }.into()
}
}
impl<'a> Filter<'a> { impl<'a> Filter<'a> {
pub fn from_array<I, J>( pub fn from_array<I, J>(array: I) -> Result<Option<Self>>
rtxn: &heed::RoTxn,
index: &Index,
array: I,
) -> Result<Option<FilterCondition<'a>>>
where where
I: IntoIterator<Item = Either<J, &'a str>>, I: IntoIterator<Item = Either<J, &'a str>>,
J: IntoIterator<Item = &'a str>, J: IntoIterator<Item = &'a str>,
@ -37,8 +40,7 @@ impl<'a> Filter<'a> {
Either::Left(array) => { Either::Left(array) => {
let mut ors = None; let mut ors = None;
for rule in array { for rule in array {
let condition = let condition = Self::from_str(rule.as_ref())?.condition;
FilterCondition::parse::<VerboseError<Span>>(rule.as_ref()).unwrap();
ors = match ors.take() { ors = match ors.take() {
Some(ors) => { Some(ors) => {
Some(FilterCondition::Or(Box::new(ors), Box::new(condition))) Some(FilterCondition::Or(Box::new(ors), Box::new(condition)))
@ -57,8 +59,7 @@ impl<'a> Filter<'a> {
} }
} }
Either::Right(rule) => { Either::Right(rule) => {
let condition = let condition = Self::from_str(rule.as_ref())?.condition;
FilterCondition::parse::<VerboseError<Span>>(rule.as_ref()).unwrap();
ands = match ands.take() { ands = match ands.take() {
Some(ands) => { Some(ands) => {
Some(FilterCondition::And(Box::new(ands), Box::new(condition))) Some(FilterCondition::And(Box::new(ands), Box::new(condition)))
@ -69,29 +70,16 @@ impl<'a> Filter<'a> {
} }
} }
Ok(ands) Ok(ands.map(|ands| Self { condition: ands }))
} }
pub fn from_str(rtxn: &heed::RoTxn, index: &Index, expression: &'a str) -> Result<Self> { pub fn from_str(expression: &'a str) -> Result<Self> {
let fields_ids_map = index.fields_ids_map(rtxn)?; let condition = match FilterCondition::parse::<GreedyError<Span, ErrorKind>>(expression) {
let filterable_fields = index.filterable_fields(rtxn)?;
// TODO TAMO
let condition = FilterCondition::parse::<VerboseError<Span>>(expression).ok().unwrap();
/*
let condition = match FilterCondition::parse::<VerboseError<Span>>(expression) {
Ok(fc) => Ok(fc), Ok(fc) => Ok(fc),
Err(e) => { Err(e) => Err(Error::UserError(UserError::InvalidFilter {
let ve = match e { input: convert_error(Span::new(expression), e).to_string(),
nom::Err::Error(x) => x, })),
nom::Err::Failure(x) => x, }?;
_ => unreachable!(),
};
Err(Error::UserError(UserError::InvalidFilter {
input: convert_error(Span::new(expression), ve).to_string(),
}))
}
};
*/
Ok(Self { condition }) Ok(Self { condition })
} }
} }
@ -345,7 +333,8 @@ impl<'a> Filter<'a> {
let rhs = Self::evaluate(&(rhs.as_ref().clone()).into(), rtxn, index)?; let rhs = Self::evaluate(&(rhs.as_ref().clone()).into(), rtxn, index)?;
Ok(lhs & rhs) Ok(lhs & rhs)
} }
Empty => Ok(RoaringBitmap::new()), FilterCondition::Empty => Ok(RoaringBitmap::new()),
_ => panic!("do the geosearch"),
} }
} }
} }