pub mod context;
pub mod error;
pub mod evaluator;
pub mod lexer;
pub mod parser;
pub mod types;
pub mod axes;
pub mod functions;
pub mod operators;
pub use context::{
XmlContext, XmlSafeContext, create_context, create_safe_context, find_nodes_by_xpath,
find_readonly_nodes_by_xpath, find_readonly_nodes_in_elements,
find_safe_readonly_nodes_by_xpath,
};
pub use evaluator::{
XPathEvaluator, XPathResult, collect_text_value, collect_text_values, evaluate,
};
pub use operators::ArithmeticOp;
pub use parser::{Axis, ComparisonOp, Expr, NodeTest, PathExpr, Predicate, Step, parse_xpath};
pub use types::{EvaluationContext, XPathValue};
use crate::error::Result;
#[derive(Debug, Clone)]
pub enum XPathSource {
String(String),
Ast(Expr),
}
impl XPathSource {
pub fn parse(&self) -> Result<Expr> {
match self {
XPathSource::String(s) => parse_xpath(s),
XPathSource::Ast(expr) => Ok(expr.clone()),
}
}
pub fn as_string(&self) -> Option<&str> {
match self {
XPathSource::String(s) => Some(s),
XPathSource::Ast(_) => None,
}
}
}
impl From<&str> for XPathSource {
fn from(s: &str) -> Self {
XPathSource::String(s.to_string())
}
}
impl From<String> for XPathSource {
fn from(s: String) -> Self {
XPathSource::String(s)
}
}
impl From<Expr> for XPathSource {
fn from(expr: Expr) -> Self {
XPathSource::Ast(expr)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_xpath_source_from_str() {
let source: XPathSource = "//test".into();
assert!(matches!(source, XPathSource::String(_)));
assert_eq!(source.as_string(), Some("//test"));
}
#[test]
fn test_xpath_source_from_string() {
let source: XPathSource = String::from("//test").into();
assert!(matches!(source, XPathSource::String(_)));
assert_eq!(source.as_string(), Some("//test"));
}
#[test]
fn test_xpath_source_from_expr() {
let expr = parse_xpath("//test").unwrap();
let source: XPathSource = expr.clone().into();
assert!(matches!(source, XPathSource::Ast(_)));
assert_eq!(source.as_string(), None);
}
#[test]
fn test_xpath_source_parse_string() {
let source = XPathSource::String("//test".to_string());
let result = source.parse();
assert!(result.is_ok());
}
#[test]
fn test_xpath_source_parse_ast() {
let expr = parse_xpath("//test").unwrap();
let source = XPathSource::Ast(expr.clone());
let result = source.parse().unwrap();
assert!(matches!(result, Expr::Path(_)));
}
#[test]
fn test_xpath_source_parse_invalid_string() {
let source = XPathSource::String("[invalid xpath".to_string());
let result = source.parse();
assert!(result.is_err());
}
}