xee_xpath_ast/
context.rs

1use xee_name::VariableNames;
2
3use crate::ast;
4use crate::{Namespaces, ParserError};
5
6#[derive(Debug, Default)]
7pub struct XPathParserContext {
8    pub namespaces: Namespaces,
9    pub variable_names: VariableNames,
10}
11
12impl XPathParserContext {
13    /// Construct a new XPath parser context.
14    ///
15    /// This consists of information about namespaces and variable names
16    /// available.
17    pub fn new(namespaces: Namespaces, variable_names: VariableNames) -> Self {
18        Self {
19            namespaces,
20            variable_names,
21        }
22    }
23
24    /// Given an XPath string, parse into an XPath AST
25    ///
26    /// This uses the namespaces and variable names with which
27    /// this static context has been initialized.
28    pub fn parse_xpath(&self, s: &str) -> Result<ast::XPath, ParserError> {
29        ast::XPath::parse(s, &self.namespaces, &self.variable_names)
30    }
31
32    /// Given an XSLT pattern, parse into an AST
33    pub fn parse_pattern(&self, s: &str) -> Result<crate::Pattern<ast::ExprS>, ParserError> {
34        crate::Pattern::parse(s, &self.namespaces, &self.variable_names)
35    }
36
37    /// Parse an XPath string as it would appear in an XSLT value template.
38    /// This means it should have a closing `}` following the xpath expression.
39    pub fn parse_value_template_xpath(&self, s: &str) -> Result<ast::XPath, ParserError> {
40        ast::XPath::parse_value_template(s, &self.namespaces, &self.variable_names)
41    }
42}