WIP: remove '_nom' suffix/redundant error enum/...

This commit is contained in:
刘瀚骋 2021-10-13 11:06:15 +08:00
parent 2c65781d91
commit 5de5dd80a3
4 changed files with 11 additions and 55 deletions

View File

@ -59,9 +59,7 @@ pub enum UserError {
InvalidDocumentId { document_id: Value },
InvalidFacetsDistribution { invalid_facets_name: HashSet<String> },
InvalidGeoField { document_id: Value, object: Value },
InvalidFilterAttributeNom,
InvalidFilterValue,
InvalidFilterNom { input: String },
InvalidFilter { input: String },
InvalidSortName { name: String },
InvalidSortableAttribute { field: String, valid_fields: HashSet<String> },
SortRankingRuleMissing,
@ -209,10 +207,7 @@ impl StdError for InternalError {}
impl fmt::Display for UserError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
//TODO
Self::InvalidFilterAttributeNom => write!(f, "parser error "),
Self::InvalidFilterValue => write!(f, "parser error "),
Self::InvalidFilterNom { input } => write!(f, "parser error {}", input),
Self::InvalidFilter { input } => write!(f, "parser error {}", input),
Self::AttributeLimitReached => f.write_str("maximum number of attributes reached"),
Self::CriterionError(error) => write!(f, "{}", error),
Self::DocumentLimitReached => f.write_str("maximum number of documents reached"),

View File

@ -90,7 +90,7 @@ impl FilterCondition {
nom::Err::Failure(x) => x,
_ => unreachable!(),
};
Err(Error::UserError(UserError::InvalidFilterNom {
Err(Error::UserError(UserError::InvalidFilter {
input: convert_error(expression, ve).to_string(),
}))
}

View File

@ -56,13 +56,12 @@ pub struct ParseContext<'a> {
}
impl<'a> ParseContext<'a> {
fn parse_or_nom<E>(&'a self, input: &'a str) -> IResult<&'a str, FilterCondition, E>
fn parse_or<E>(&'a self, input: &'a str) -> IResult<&'a str, FilterCondition, E>
where
E: ParseError<&'a str> + ContextError<&'a str> + Debug,
{
let (input, lhs) = self.parse_and_nom(input)?;
let (input, ors) =
many0(preceded(self.ws(tag("OR")), |c| Self::parse_or_nom(self, c)))(input)?;
let (input, lhs) = self.parse_and(input)?;
let (input, ors) = many0(preceded(self.ws(tag("OR")), |c| Self::parse_or(self, c)))(input)?;
let expr = ors
.into_iter()
@ -70,25 +69,20 @@ impl<'a> ParseContext<'a> {
Ok((input, expr))
}
fn parse_and_nom<E>(&'a self, input: &'a str) -> IResult<&'a str, FilterCondition, E>
fn parse_and<E>(&'a self, input: &'a str) -> IResult<&'a str, FilterCondition, E>
where
E: ParseError<&'a str> + ContextError<&'a str> + Debug,
{
let (input, lhs) = self.parse_not_nom(input)?;
// let (input, lhs) = alt((
// delimited(self.ws(char('(')), |c| Self::parse_not_nom(self, c), self.ws(char(')'))),
// |c| self.parse_not_nom(c),
// ))(input)?;
let (input, lhs) = self.parse_not(input)?;
let (input, ors) =
many0(preceded(self.ws(tag("AND")), |c| Self::parse_and_nom(self, c)))(input)?;
many0(preceded(self.ws(tag("AND")), |c| Self::parse_and(self, c)))(input)?;
let expr = ors
.into_iter()
.fold(lhs, |acc, branch| FilterCondition::And(Box::new(acc), Box::new(branch)));
Ok((input, expr))
}
fn parse_not_nom<E>(&'a self, input: &'a str) -> IResult<&'a str, FilterCondition, E>
fn parse_not<E>(&'a self, input: &'a str) -> IResult<&'a str, FilterCondition, E>
where
E: ParseError<&'a str> + ContextError<&'a str> + Debug,
{
@ -307,7 +301,7 @@ impl<'a> ParseContext<'a> {
where
E: ParseError<&'a str> + ContextError<&'a str> + Debug,
{
let a = self.parse_or_nom(input);
let a = self.parse_or(input);
a
}
}

View File

@ -1,33 +0,0 @@
key = _{reserved | quoted | word }
value = _{quoted | word }
quoted = _{ (PUSH("'") | PUSH("\"")) ~ string ~ POP }
string = {char*}
word = ${(LETTER | NUMBER | "_" | "-" | ".")+}
char = _{ !(PEEK | "\\") ~ ANY
| "\\" ~ (PEEK | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
| "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})}
reserved = { "_geoDistance" | ("_geoPoint" ~ parameters) | "_geo" }
// we deliberately choose to allow empty parameters to generate more specific error message later
parameters = {("(" ~ (value ~ ",")* ~ value? ~ ")") | ""}
condition = _{between | eq | greater | less | geq | leq | neq}
between = {key ~ value ~ "TO" ~ value}
geq = {key ~ ">=" ~ value}
leq = {key ~ "<=" ~ value}
neq = {key ~ "!=" ~ value}
eq = {key ~ "=" ~ value}
greater = {key ~ ">" ~ value}
less = {key ~ "<" ~ value}
geo_radius = {"_geoRadius" ~ parameters }
prgm = {SOI ~ expr ~ EOI}
expr = _{ ( term ~ (operation ~ term)* ) }
term = { ("(" ~ expr ~ ")") | condition | not | geo_radius }
operation = _{ and | or }
and = {"AND"}
or = {"OR"}
not = {"NOT" ~ term}
WHITESPACE = _{ " " }