xrust/parser/xpath/
compare.rs

1//! Functions that produce comparisons.
2
3use crate::item::Node;
4use crate::parser::combinators::map::map;
5use crate::parser::combinators::opt::opt;
6use crate::parser::combinators::pair::pair;
7use crate::parser::combinators::tag::anytag;
8use crate::parser::combinators::tuple::tuple3;
9use crate::parser::combinators::whitespace::xpwhitespace;
10use crate::parser::xpath::strings::stringconcat_expr;
11use crate::parser::{ParseError, ParseInput};
12use crate::transform::Transform;
13use crate::value::Operator;
14
15// ComparisonExpr ::= StringConcatExpr ( (ValueComp | GeneralComp | NodeComp) StringConcatExpr)?
16pub(crate) fn comparison_expr<'a, N: Node + 'a>()
17-> Box<dyn Fn(ParseInput<N>) -> Result<(ParseInput<N>, Transform<N>), ParseError> + 'a> {
18    Box::new(map(
19        pair(
20            stringconcat_expr::<N>(),
21            opt(pair(
22                tuple3(
23                    xpwhitespace(),
24                    anytag(vec![
25                        "=", "!=", "<", "<=", "<<", ">", ">=", ">>", "eq", "ne", "lt", "le", "gt",
26                        "ge", "is",
27                    ]),
28                    xpwhitespace(),
29                ),
30                stringconcat_expr::<N>(),
31            )),
32        ),
33        |(v, o)| match o {
34            None => v,
35            Some(((_, b, _), t)) => {
36                match b.as_str() {
37                    "=" | "!=" | "<" | "<=" | ">" | ">=" => {
38                        Transform::GeneralComparison(Operator::from(b), Box::new(v), Box::new(t))
39                    }
40                    "eq" | "ne" | "lt" | "le" | "gt" | "ge" | "is" | "<<" | ">>" => {
41                        Transform::ValueComparison(Operator::from(b), Box::new(v), Box::new(t))
42                    }
43                    _ => Transform::Empty, // error
44                }
45            }
46        },
47    ))
48}