xmloxide 0.4.3

A pure Rust reimplementation of libxml2 — memory-safe XML/HTML parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Abstract syntax tree types for `XPath` 1.0 expressions.
//!
//! This module defines the AST that results from parsing an `XPath` expression
//! string. The AST closely follows the `XPath` 1.0 grammar from
//! <https://www.w3.org/TR/xpath-10/#section-Basics>.
//!
//! The primary type is [`Expr`], which represents any `XPath` expression.
//! Location paths are composed of [`Step`]s, each having an [`Axis`],
//! a [`NodeTest`], and zero or more predicate expressions.

/// An `XPath` 1.0 expression.
///
/// This enum represents the full range of `XPath` 1.0 expressions, including
/// literals, operators, function calls, and location paths.
///
/// See `XPath` 1.0 section 3.
#[derive(Debug, Clone)]
pub enum Expr {
    /// A numeric literal (e.g., `42`, `3.14`).
    ///
    /// See `XPath` 1.0 section 3.5.
    Number(f64),

    /// A string literal (e.g., `"hello"` or `'world'`).
    ///
    /// See `XPath` 1.0 section 3.5.
    String(String),

    /// A variable reference (e.g., `$foo`).
    ///
    /// The string contains the variable name without the leading `$`.
    ///
    /// See `XPath` 1.0 section 3.1.
    Variable(String),

    /// A binary operation (e.g., `a + b`, `x = y`, `p and q`).
    ///
    /// See `XPath` 1.0 sections 3.3, 3.4, 3.5.
    BinaryOp {
        /// The operator.
        op: BinaryOp,
        /// The left-hand operand.
        left: Box<Expr>,
        /// The right-hand operand.
        right: Box<Expr>,
    },

    /// Unary negation (e.g., `-x`).
    ///
    /// See `XPath` 1.0 section 3.5.
    UnaryNeg(Box<Expr>),

    /// A function call (e.g., `contains(name, 'foo')`).
    ///
    /// See `XPath` 1.0 section 3.2.
    FunctionCall {
        /// The function name.
        name: String,
        /// The argument expressions.
        args: Vec<Expr>,
    },

    /// A relative location path (e.g., `child::p/child::a`).
    ///
    /// See `XPath` 1.0 section 2.
    Path {
        /// The steps in the path, evaluated left to right.
        steps: Vec<Step>,
    },

    /// An absolute location path (e.g., `/html/body`).
    ///
    /// An empty `steps` vector represents the bare `/` (root node).
    ///
    /// See `XPath` 1.0 section 2.
    RootPath {
        /// The steps following the initial `/`.
        steps: Vec<Step>,
    },

    /// A filter expression with predicates (e.g., `$nodes[1]`).
    ///
    /// See `XPath` 1.0 section 3.3.
    Filter {
        /// The primary expression being filtered.
        expr: Box<Expr>,
        /// The predicate expressions.
        predicates: Vec<Expr>,
    },

    /// A union of two node-sets (e.g., `a | b`).
    ///
    /// See `XPath` 1.0 section 3.3.
    Union(Box<Expr>, Box<Expr>),
}

/// A binary operator in an `XPath` expression.
///
/// Covers arithmetic, comparison, and logical operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BinaryOp {
    /// Addition (`+`).
    Add,
    /// Subtraction (`-`).
    Sub,
    /// Multiplication (`*`).
    Mul,
    /// Division (`div`).
    Div,
    /// Modulo (`mod`).
    Mod,
    /// Equality (`=`).
    Eq,
    /// Inequality (`!=`).
    Neq,
    /// Less than (`<`).
    Lt,
    /// Less than or equal (`<=`).
    Lte,
    /// Greater than (`>`).
    Gt,
    /// Greater than or equal (`>=`).
    Gte,
    /// Logical and (`and`).
    And,
    /// Logical or (`or`).
    Or,
}

/// A single step in a location path.
///
/// A step consists of an axis, a node test, and zero or more predicates.
/// For example, in `child::p[@class='intro']`, the axis is `Child`,
/// the node test is `Name("p")`, and there is one predicate.
///
/// See `XPath` 1.0 section 2.1.
#[derive(Debug, Clone)]
pub struct Step {
    /// The axis along which to select nodes.
    pub axis: Axis,
    /// The test applied to each candidate node.
    pub node_test: NodeTest,
    /// Predicate expressions that further filter the selected nodes.
    pub predicates: Vec<Expr>,
}

/// An `XPath` axis, specifying the direction of node selection.
///
/// `XPath` 1.0 defines 13 axes. See `XPath` 1.0 section 2.2.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Axis {
    /// The `child` axis: direct children.
    Child,
    /// The `descendant` axis: all descendants (children, grandchildren, etc.).
    Descendant,
    /// The `parent` axis: the immediate parent.
    Parent,
    /// The `ancestor` axis: all ancestors up to and including the root.
    Ancestor,
    /// The `following-sibling` axis: siblings that come after this node.
    FollowingSibling,
    /// The `preceding-sibling` axis: siblings that come before this node.
    PrecedingSibling,
    /// The `following` axis: all nodes after this node in document order.
    Following,
    /// The `preceding` axis: all nodes before this node in document order.
    Preceding,
    /// The `attribute` axis: attributes of the context node.
    Attribute,
    /// The `namespace` axis: namespace nodes of the context node.
    Namespace,
    /// The `self` axis: just the context node itself.
    Self_,
    /// The `descendant-or-self` axis: the context node and its descendants.
    DescendantOrSelf,
    /// The `ancestor-or-self` axis: the context node and its ancestors.
    AncestorOrSelf,
}

impl Axis {
    /// Returns the axis name as it appears in `XPath` syntax.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// assert_eq!(Axis::Child.as_str(), "child");
    /// assert_eq!(Axis::DescendantOrSelf.as_str(), "descendant-or-self");
    /// ```
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Child => "child",
            Self::Descendant => "descendant",
            Self::Parent => "parent",
            Self::Ancestor => "ancestor",
            Self::FollowingSibling => "following-sibling",
            Self::PrecedingSibling => "preceding-sibling",
            Self::Following => "following",
            Self::Preceding => "preceding",
            Self::Attribute => "attribute",
            Self::Namespace => "namespace",
            Self::Self_ => "self",
            Self::DescendantOrSelf => "descendant-or-self",
            Self::AncestorOrSelf => "ancestor-or-self",
        }
    }

    /// Parses an axis name string into an `Axis` variant.
    ///
    /// Returns `None` if the string is not a recognized axis name.
    #[must_use]
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "child" => Some(Self::Child),
            "descendant" => Some(Self::Descendant),
            "parent" => Some(Self::Parent),
            "ancestor" => Some(Self::Ancestor),
            "following-sibling" => Some(Self::FollowingSibling),
            "preceding-sibling" => Some(Self::PrecedingSibling),
            "following" => Some(Self::Following),
            "preceding" => Some(Self::Preceding),
            "attribute" => Some(Self::Attribute),
            "namespace" => Some(Self::Namespace),
            "self" => Some(Self::Self_),
            "descendant-or-self" => Some(Self::DescendantOrSelf),
            "ancestor-or-self" => Some(Self::AncestorOrSelf),
            _ => None,
        }
    }
}

impl std::fmt::Display for Axis {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A node test in a location path step.
///
/// Node tests filter candidate nodes by name or kind.
///
/// See `XPath` 1.0 section 2.3.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeTest {
    /// A name test matching a specific element or attribute name.
    ///
    /// The string may be an `NCName` or a `QName` (with prefix).
    Name(String),

    /// The `*` wildcard, matching any name.
    Wildcard,

    /// A prefixed wildcard like `prefix:*`, matching any local name in
    /// the namespace bound to the given prefix.
    PrefixWildcard(String),

    /// The `node()` node type test, matching any node.
    Node,

    /// The `text()` node type test, matching text nodes.
    Text,

    /// The `comment()` node type test, matching comment nodes.
    Comment,

    /// The `processing-instruction()` node type test.
    ///
    /// When the optional string is `Some`, it matches only PIs with that target
    /// name (e.g., `processing-instruction('xml-stylesheet')`).
    ProcessingInstruction(Option<String>),
}

impl std::fmt::Display for NodeTest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Name(name) => write!(f, "{name}"),
            Self::Wildcard => f.write_str("*"),
            Self::PrefixWildcard(prefix) => write!(f, "{prefix}:*"),
            Self::Node => f.write_str("node()"),
            Self::Text => f.write_str("text()"),
            Self::Comment => f.write_str("comment()"),
            Self::ProcessingInstruction(None) => f.write_str("processing-instruction()"),
            Self::ProcessingInstruction(Some(name)) => {
                write!(f, "processing-instruction('{name}')")
            }
        }
    }
}

impl std::fmt::Display for BinaryOp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Add => f.write_str("+"),
            Self::Sub => f.write_str("-"),
            Self::Mul => f.write_str("*"),
            Self::Div => f.write_str("div"),
            Self::Mod => f.write_str("mod"),
            Self::Eq => f.write_str("="),
            Self::Neq => f.write_str("!="),
            Self::Lt => f.write_str("<"),
            Self::Lte => f.write_str("<="),
            Self::Gt => f.write_str(">"),
            Self::Gte => f.write_str(">="),
            Self::And => f.write_str("and"),
            Self::Or => f.write_str("or"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_axis_roundtrip() {
        let axes = [
            Axis::Child,
            Axis::Descendant,
            Axis::Parent,
            Axis::Ancestor,
            Axis::FollowingSibling,
            Axis::PrecedingSibling,
            Axis::Following,
            Axis::Preceding,
            Axis::Attribute,
            Axis::Namespace,
            Axis::Self_,
            Axis::DescendantOrSelf,
            Axis::AncestorOrSelf,
        ];
        for axis in axes {
            let name = axis.as_str();
            let parsed = Axis::parse(name);
            assert_eq!(parsed, Some(axis), "roundtrip failed for {name}");
        }
    }

    #[test]
    fn test_axis_from_str_invalid() {
        assert_eq!(Axis::parse("invalid"), None);
        assert_eq!(Axis::parse(""), None);
        assert_eq!(Axis::parse("children"), None);
    }

    #[test]
    fn test_axis_display() {
        assert_eq!(Axis::Child.to_string(), "child");
        assert_eq!(Axis::DescendantOrSelf.to_string(), "descendant-or-self");
        assert_eq!(Axis::AncestorOrSelf.to_string(), "ancestor-or-self");
        assert_eq!(Axis::FollowingSibling.to_string(), "following-sibling");
    }

    #[test]
    fn test_node_test_display() {
        assert_eq!(NodeTest::Name("foo".to_string()).to_string(), "foo");
        assert_eq!(NodeTest::Wildcard.to_string(), "*");
        assert_eq!(
            NodeTest::PrefixWildcard("svg".to_string()).to_string(),
            "svg:*"
        );
        assert_eq!(NodeTest::Node.to_string(), "node()");
        assert_eq!(NodeTest::Text.to_string(), "text()");
        assert_eq!(NodeTest::Comment.to_string(), "comment()");
        assert_eq!(
            NodeTest::ProcessingInstruction(None).to_string(),
            "processing-instruction()"
        );
        assert_eq!(
            NodeTest::ProcessingInstruction(Some("xml-stylesheet".to_string())).to_string(),
            "processing-instruction('xml-stylesheet')"
        );
    }

    #[test]
    fn test_binary_op_display() {
        assert_eq!(BinaryOp::Add.to_string(), "+");
        assert_eq!(BinaryOp::Sub.to_string(), "-");
        assert_eq!(BinaryOp::Mul.to_string(), "*");
        assert_eq!(BinaryOp::Div.to_string(), "div");
        assert_eq!(BinaryOp::Mod.to_string(), "mod");
        assert_eq!(BinaryOp::Eq.to_string(), "=");
        assert_eq!(BinaryOp::Neq.to_string(), "!=");
        assert_eq!(BinaryOp::Lt.to_string(), "<");
        assert_eq!(BinaryOp::Lte.to_string(), "<=");
        assert_eq!(BinaryOp::Gt.to_string(), ">");
        assert_eq!(BinaryOp::Gte.to_string(), ">=");
        assert_eq!(BinaryOp::And.to_string(), "and");
        assert_eq!(BinaryOp::Or.to_string(), "or");
    }

    #[test]
    fn test_expr_clone() {
        let expr = Expr::BinaryOp {
            op: BinaryOp::Add,
            left: Box::new(Expr::Number(1.0)),
            right: Box::new(Expr::Number(2.0)),
        };
        let cloned = expr.clone();
        match cloned {
            Expr::BinaryOp { op, .. } => assert_eq!(op, BinaryOp::Add),
            _ => panic!("unexpected variant after clone"),
        }
    }

    #[test]
    fn test_step_construction() {
        let step = Step {
            axis: Axis::Child,
            node_test: NodeTest::Name("p".to_string()),
            predicates: vec![Expr::Number(1.0)],
        };
        assert_eq!(step.axis, Axis::Child);
        assert_eq!(step.node_test, NodeTest::Name("p".to_string()));
        assert_eq!(step.predicates.len(), 1);
    }
}