1use context::EvaluationCtx;
6use eval::Evaluatable;
7pub(crate) use eval_value::{NodesetHelpers, Value};
8use parser::OwnedParserError;
9pub(crate) use parser::{Expr, parse as parse_impl};
10
11use super::dom::node::Node;
12
13mod context;
14#[allow(dead_code)]
15mod eval;
16mod eval_function;
17#[allow(dead_code)]
18mod eval_value;
19#[allow(dead_code)]
20mod parser;
21
22#[derive(Debug, PartialEq)]
24pub(crate) enum Error {
25 Parsing { source: OwnedParserError },
27 Evaluating { source: eval::Error },
29}
30
31impl std::fmt::Display for Error {
32 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
33 match self {
34 Error::Parsing { source } => write!(f, "Unable to parse XPath: {}", source),
35 Error::Evaluating { source } => write!(f, "Unable to evaluate XPath: {}", source),
36 }
37 }
38}
39
40impl std::error::Error for Error {
41 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42 match self {
43 Error::Parsing { source } => Some(source),
44 Error::Evaluating { source } => Some(source),
45 }
46 }
47}
48
49pub(crate) fn parse(xpath: &str) -> Result<Expr, Error> {
51 match parse_impl(xpath) {
52 Ok(expr) => {
53 debug!("Parsed XPath: {:?}", expr);
54 Ok(expr)
55 },
56 Err(e) => {
57 debug!("Unable to parse XPath: {}", e);
58 Err(Error::Parsing { source: e })
59 },
60 }
61}
62
63pub(crate) fn evaluate_parsed_xpath(expr: &Expr, context_node: &Node) -> Result<Value, Error> {
65 let context = EvaluationCtx::new(context_node);
66 match expr.evaluate(&context) {
67 Ok(v) => {
68 debug!("Evaluated XPath: {:?}", v);
69 Ok(v)
70 },
71 Err(e) => {
72 debug!("Unable to evaluate XPath: {}", e);
73 Err(Error::Evaluating { source: e })
74 },
75 }
76}