2021-10-22 01:59:38 +02:00
|
|
|
use nom::branch::alt;
|
2021-11-04 14:22:35 +01:00
|
|
|
use nom::bytes::complete::{take_till, take_while, take_while1};
|
|
|
|
use nom::character::complete::{char, multispace0};
|
|
|
|
use nom::combinator::cut;
|
|
|
|
use nom::sequence::{delimited, terminated};
|
2021-10-22 01:59:38 +02:00
|
|
|
|
2021-11-04 14:22:35 +01:00
|
|
|
use crate::error::ExtendNomError;
|
|
|
|
use crate::{parse_geo_point, parse_geo_radius, Error, ErrorKind, IResult, Span, Token};
|
2021-10-22 01:59:38 +02:00
|
|
|
|
|
|
|
/// value = WS* ~ ( word | singleQuoted | doubleQuoted) ~ WS*
|
2021-11-02 20:27:07 +01:00
|
|
|
pub fn parse_value(input: Span) -> IResult<Token> {
|
2021-11-04 14:22:35 +01:00
|
|
|
// before anything we want to check if the user is misusing a geo expression
|
|
|
|
let err = parse_geo_point(input).unwrap_err();
|
|
|
|
if err.is_failure() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
match parse_geo_radius(input) {
|
|
|
|
Ok(_) => return Err(nom::Err::Failure(Error::kind(input, ErrorKind::MisusedGeo))),
|
|
|
|
// if we encountered a failure it means the user badly wrote a _geoRadius filter.
|
|
|
|
// But instead of showing him how to fix his syntax we are going to tell him he should not use this filter as a value.
|
|
|
|
Err(e) if e.is_failure() => {
|
|
|
|
return Err(nom::Err::Failure(Error::kind(input, ErrorKind::MisusedGeo)))
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:59:38 +02:00
|
|
|
// singleQuoted = "'" .* all but quotes "'"
|
2021-11-02 20:27:07 +01:00
|
|
|
let simple_quoted = |input| take_till(|c: char| c == '\'')(input);
|
2021-10-22 01:59:38 +02:00
|
|
|
// doubleQuoted = "\"" (word | spaces)* "\""
|
2021-11-02 20:27:07 +01:00
|
|
|
let double_quoted = |input| take_till(|c: char| c == '"')(input);
|
2021-10-22 01:59:38 +02:00
|
|
|
// word = (alphanumeric | _ | - | .)+
|
|
|
|
let word = |input| take_while1(is_key_component)(input);
|
|
|
|
|
2021-11-04 14:22:35 +01:00
|
|
|
// we want to remove the space before entering the alt because if we don't,
|
|
|
|
// when we create the errors from the output of the alt we have spaces everywhere
|
|
|
|
let (input, _) = take_while(char::is_whitespace)(input)?;
|
|
|
|
|
|
|
|
terminated(
|
|
|
|
alt((
|
|
|
|
delimited(char('\''), simple_quoted, cut(char('\''))),
|
|
|
|
delimited(char('"'), double_quoted, cut(char('"'))),
|
|
|
|
word,
|
|
|
|
)),
|
|
|
|
multispace0,
|
|
|
|
)(input)
|
2021-10-22 01:59:38 +02:00
|
|
|
.map(|(s, t)| (s, t.into()))
|
2021-11-04 14:22:35 +01:00
|
|
|
// if we found nothing in the alt it means the user did not input any value
|
|
|
|
.map_err(|e| e.map_err(|_| Error::kind(input, ErrorKind::ExpectedValue)))
|
|
|
|
// if we found encountered a failure it means the user really tried to input a value, but had an unmatched quote
|
|
|
|
.map_err(|e| e.map_fail(|c| Error::kind(input, ErrorKind::MissingClosingDelimiter(c.char()))))
|
2021-10-22 01:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_key_component(c: char) -> bool {
|
|
|
|
c.is_alphanumeric() || ['_', '-', '.'].contains(&c)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::tests::rtok;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn name() {
|
|
|
|
let test_case = [
|
|
|
|
("channel", rtok("", "channel")),
|
|
|
|
(".private", rtok("", ".private")),
|
|
|
|
("I-love-kebab", rtok("", "I-love-kebab")),
|
2021-11-04 14:22:35 +01:00
|
|
|
("but_snakes_is_also_good", rtok("", "but_snakes_is_also_good")),
|
2021-10-22 01:59:38 +02:00
|
|
|
("parens(", rtok("", "parens")),
|
|
|
|
("parens)", rtok("", "parens")),
|
|
|
|
("not!", rtok("", "not")),
|
|
|
|
(" channel", rtok(" ", "channel")),
|
|
|
|
("channel ", rtok("", "channel")),
|
2021-11-04 14:22:35 +01:00
|
|
|
(" channel ", rtok(" ", "channel")),
|
2021-10-22 01:59:38 +02:00
|
|
|
("'channel'", rtok("'", "channel")),
|
|
|
|
("\"channel\"", rtok("\"", "channel")),
|
|
|
|
("'cha)nnel'", rtok("'", "cha)nnel")),
|
|
|
|
("'cha\"nnel'", rtok("'", "cha\"nnel")),
|
|
|
|
("\"cha'nnel\"", rtok("\"", "cha'nnel")),
|
|
|
|
("\" some spaces \"", rtok("\"", " some spaces ")),
|
|
|
|
("\"cha'nnel\"", rtok("'", "cha'nnel")),
|
|
|
|
("\"cha'nnel\"", rtok("'", "cha'nnel")),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in test_case {
|
2021-11-02 17:35:17 +01:00
|
|
|
let input = Span::new_extra(input, input);
|
2021-11-02 20:27:07 +01:00
|
|
|
let result = parse_value(input);
|
2021-10-22 01:59:38 +02:00
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_ok(),
|
|
|
|
"Filter `{:?}` was supposed to be parsed but failed with the following error: `{}`",
|
|
|
|
expected,
|
|
|
|
result.unwrap_err()
|
|
|
|
);
|
|
|
|
let value = result.unwrap().1;
|
|
|
|
assert_eq!(value, expected, "Filter `{}` failed.", input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|