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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Push/incremental XML parser.
//!
//! Provides a chunk-oriented parsing interface inspired by libxml2's push parser
//! (`xmlCreatePushParserCtxt` / `xmlParseChunk`). Data can be fed to the parser
//! in arbitrarily sized chunks via [`PushParser::push`], and the final document
//! is obtained by calling [`PushParser::finish`].
//!
//! This is useful for scenarios where XML data arrives incrementally, such as
//! reading from a network socket or streaming from another process.
//!
//! # Design
//!
//! The current implementation buffers all pushed data internally and performs
//! the full parse on [`PushParser::finish`]. This provides correct chunk-boundary
//! handling with minimal complexity. A future optimization may parse eagerly
//! after each [`PushParser::push`] call.
//!
//! # Examples
//!
//! ```
//! use xmloxide::parser::PushParser;
//!
//! let mut parser = PushParser::new();
//! parser.push(b"<root>");
//! parser.push(b"<child>Hello</child>");
//! parser.push(b"</root>");
//!
//! let doc = parser.finish().unwrap();
//! let root = doc.root_element().unwrap();
//! assert_eq!(doc.node_name(root), Some("root"));
//! ```

use crate::encoding::decode_to_utf8;
use crate::error::{ParseError, SourceLocation};
use crate::parser::ParseOptions;
use crate::tree::Document;

/// A push-based (incremental) XML parser.
///
/// Accepts XML data in arbitrarily sized chunks and builds a [`Document`] tree
/// when parsing is finalized. This mirrors libxml2's push parser interface
/// (`xmlCreatePushParserCtxt` / `xmlParseChunk`).
///
/// # Construction
///
/// Use [`PushParser::new`] for default options, or [`PushParser::with_options`]
/// to configure parser behavior.
///
/// # Examples
///
/// Basic usage with multiple chunks:
///
/// ```
/// use xmloxide::parser::PushParser;
///
/// let mut parser = PushParser::new();
/// parser.push(b"<?xml version=\"1.0\"?>");
/// parser.push(b"<root attr=\"value\">");
/// parser.push(b"Hello, world!");
/// parser.push(b"</root>");
///
/// let doc = parser.finish().unwrap();
/// let root = doc.root_element().unwrap();
/// assert_eq!(doc.node_name(root), Some("root"));
/// assert_eq!(doc.text_content(root), "Hello, world!");
/// ```
///
/// With parse options:
///
/// ```
/// use xmloxide::parser::{ParseOptions, PushParser};
///
/// let opts = ParseOptions::default().recover(true);
/// let mut parser = PushParser::with_options(opts);
/// parser.push(b"<root>");
/// parser.push(b"</root>");
///
/// let doc = parser.finish().unwrap();
/// assert!(doc.root_element().is_some());
/// ```
pub struct PushParser {
    /// Accumulated raw bytes from all `push()` calls.
    buffer: Vec<u8>,
    /// Parser options.
    options: ParseOptions,
    /// Whether `finish()` has already been called.
    finished: bool,
}

impl PushParser {
    /// Creates a new push parser with default options.
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::PushParser;
    ///
    /// let mut parser = PushParser::new();
    /// parser.push(b"<root/>");
    /// let doc = parser.finish().unwrap();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            buffer: Vec::new(),
            options: ParseOptions::default(),
            finished: false,
        }
    }

    /// Creates a new push parser with the specified options.
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::{ParseOptions, PushParser};
    ///
    /// let parser = PushParser::with_options(
    ///     ParseOptions::default().recover(true).no_blanks(true),
    /// );
    /// ```
    #[must_use]
    pub fn with_options(options: ParseOptions) -> Self {
        Self {
            buffer: Vec::new(),
            options,
            finished: false,
        }
    }

    /// Feeds a chunk of raw XML bytes into the parser.
    ///
    /// Data is accumulated in an internal buffer. The chunk can be any size
    /// and may split tokens, elements, or even multi-byte characters at
    /// arbitrary boundaries.
    ///
    /// # Panics
    ///
    /// Panics if called after [`finish`](PushParser::finish) has been invoked.
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::PushParser;
    ///
    /// let mut parser = PushParser::new();
    /// parser.push(b"<ro");
    /// parser.push(b"ot/>");
    /// let doc = parser.finish().unwrap();
    /// ```
    pub fn push(&mut self, data: &[u8]) {
        assert!(
            !self.finished,
            "push() called after finish() — parser has already been consumed"
        );
        self.buffer.extend_from_slice(data);
    }

    /// Finalizes parsing and returns the constructed [`Document`].
    ///
    /// This consumes the parser. All buffered data is decoded (with automatic
    /// encoding detection) and parsed as a complete XML document.
    ///
    /// # Errors
    ///
    /// Returns [`ParseError`] if the accumulated data is not well-formed XML
    /// (unless recovery mode is enabled via [`ParseOptions::recover`]).
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::PushParser;
    ///
    /// let mut parser = PushParser::new();
    /// parser.push(b"<root><child/></root>");
    /// let doc = parser.finish().unwrap();
    /// ```
    pub fn finish(mut self) -> Result<Document, ParseError> {
        self.finished = true;

        let utf8 = decode_to_utf8(&self.buffer).map_err(|e| ParseError {
            message: e.message,
            location: SourceLocation::default(),
            diagnostics: Vec::new(),
        })?;

        crate::parser::parse_str_with_options(&utf8, &self.options)
    }

    /// Returns the number of bytes currently buffered.
    ///
    /// This is the total number of bytes received via [`push`](PushParser::push)
    /// that have not yet been parsed (parsing occurs on [`finish`](PushParser::finish)).
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::PushParser;
    ///
    /// let mut parser = PushParser::new();
    /// assert_eq!(parser.buffered_bytes(), 0);
    /// parser.push(b"<root/>");
    /// assert_eq!(parser.buffered_bytes(), 7);
    /// ```
    #[must_use]
    pub fn buffered_bytes(&self) -> usize {
        self.buffer.len()
    }

    /// Returns `true` if no data has been pushed yet.
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::PushParser;
    ///
    /// let mut parser = PushParser::new();
    /// assert!(parser.is_empty());
    /// parser.push(b"<root/>");
    /// assert!(!parser.is_empty());
    /// ```
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Resets the parser, discarding all buffered data.
    ///
    /// After calling this method, the parser is in the same state as a
    /// newly created one (with the same options). This allows reusing
    /// the parser for a new document without allocating a new instance.
    ///
    /// # Examples
    ///
    /// ```
    /// use xmloxide::parser::PushParser;
    ///
    /// let mut parser = PushParser::new();
    /// parser.push(b"<root/>");
    /// parser.reset();
    /// assert!(parser.is_empty());
    /// parser.push(b"<other/>");
    /// let doc = parser.finish().unwrap();
    /// ```
    pub fn reset(&mut self) {
        self.buffer.clear();
        self.finished = false;
    }
}

impl Default for PushParser {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for PushParser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PushParser")
            .field("buffered_bytes", &self.buffer.len())
            .field("options", &self.options)
            .field("finished", &self.finished)
            .finish()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_push_parser_single_chunk() {
        let mut parser = PushParser::new();
        parser.push(b"<root/>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
    }

    #[test]
    fn test_push_parser_multiple_chunks() {
        let mut parser = PushParser::new();
        parser.push(b"<root>");
        parser.push(b"<child>text</child>");
        parser.push(b"</root>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));

        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_name(child), Some("child"));
        assert_eq!(doc.text_content(child), "text");
    }

    #[test]
    fn test_push_parser_split_token() {
        // Split an element tag across chunk boundaries.
        let mut parser = PushParser::new();
        parser.push(b"<ro");
        parser.push(b"ot att");
        parser.push(b"r=\"val");
        parser.push(b"ue\"/>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
        assert_eq!(doc.attribute(root, "attr"), Some("value"));
    }

    #[test]
    fn test_push_parser_byte_at_a_time() {
        let xml = b"<root><child/></root>";
        let mut parser = PushParser::new();
        for &byte in xml {
            parser.push(&[byte]);
        }
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_name(child), Some("child"));
    }

    #[test]
    fn test_push_parser_xml_declaration_split() {
        let mut parser = PushParser::new();
        parser.push(b"<?xml ver");
        parser.push(b"sion=\"1.0\" encoding=\"UTF-8\"?>");
        parser.push(b"<root/>");
        let doc = parser.finish().unwrap();
        assert_eq!(doc.version.as_deref(), Some("1.0"));
        assert_eq!(doc.encoding.as_deref(), Some("UTF-8"));
    }

    #[test]
    fn test_push_parser_empty_input() {
        let parser = PushParser::new();
        let result = parser.finish();
        // XML 1.0 §2.1 requires a root element
        assert!(result.is_err());
    }

    #[test]
    fn test_push_parser_with_options_recover() {
        let opts = ParseOptions::default().recover(true);
        let mut parser = PushParser::with_options(opts);
        // Mismatched tags — should succeed in recovery mode.
        parser.push(b"<a></b>");
        let result = parser.finish();
        assert!(result.is_ok());
    }

    #[test]
    fn test_push_parser_with_options_no_blanks() {
        let opts = ParseOptions::default().no_blanks(true);
        let mut parser = PushParser::with_options(opts);
        parser.push(b"<root>  <child/>  </root>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        // The blank text nodes ("  ") should have been stripped.
        let children: Vec<_> = doc.children(root).collect();
        assert_eq!(children.len(), 1);
        assert_eq!(doc.node_name(children[0]), Some("child"));
    }

    #[test]
    fn test_push_parser_error_malformed() {
        let mut parser = PushParser::new();
        parser.push(b"<a></b>");
        let result = parser.finish();
        assert!(result.is_err());
    }

    #[test]
    fn test_push_parser_buffered_bytes() {
        let mut parser = PushParser::new();
        assert_eq!(parser.buffered_bytes(), 0);
        parser.push(b"<root>");
        assert_eq!(parser.buffered_bytes(), 6);
        parser.push(b"</root>");
        assert_eq!(parser.buffered_bytes(), 13);
    }

    #[test]
    fn test_push_parser_is_empty() {
        let mut parser = PushParser::new();
        assert!(parser.is_empty());
        parser.push(b"<root/>");
        assert!(!parser.is_empty());
    }

    #[test]
    fn test_push_parser_reset() {
        let mut parser = PushParser::new();
        parser.push(b"<invalid");
        parser.reset();
        assert!(parser.is_empty());
        assert_eq!(parser.buffered_bytes(), 0);
        parser.push(b"<root/>");
        let doc = parser.finish().unwrap();
        assert!(doc.root_element().is_some());
    }

    #[test]
    fn test_push_parser_default_trait() {
        let parser = PushParser::default();
        assert!(parser.is_empty());
    }

    #[test]
    fn test_push_parser_debug_trait() {
        let mut parser = PushParser::new();
        parser.push(b"<root/>");
        let debug_str = format!("{parser:?}");
        assert!(debug_str.contains("PushParser"));
        assert!(debug_str.contains("buffered_bytes: 7"));
    }

    #[test]
    fn test_push_parser_utf8_bom() {
        let mut parser = PushParser::new();
        // Push UTF-8 BOM followed by XML.
        parser.push(b"\xEF\xBB\xBF");
        parser.push(b"<root/>");
        let doc = parser.finish().unwrap();
        assert!(doc.root_element().is_some());
    }

    #[test]
    fn test_push_parser_comment_split() {
        let mut parser = PushParser::new();
        parser.push(b"<root><!-");
        parser.push(b"- comment -");
        parser.push(b"-></root>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_text(child), Some(" comment "));
    }

    #[test]
    fn test_push_parser_cdata_split() {
        let mut parser = PushParser::new();
        parser.push(b"<root><![CDA");
        parser.push(b"TA[some data]]");
        parser.push(b"></root>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_text(child), Some("some data"));
    }

    #[test]
    fn test_push_parser_entity_references() {
        let mut parser = PushParser::new();
        parser.push(b"<root>&am");
        parser.push(b"p; &lt; &gt;</root>");
        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        assert_eq!(doc.text_content(root), "& < >");
    }

    #[test]
    fn test_push_parser_roundtrip() {
        let input = b"<root><child attr=\"val\">text</child></root>";
        let mut parser = PushParser::new();
        parser.push(&input[..10]);
        parser.push(&input[10..25]);
        parser.push(&input[25..]);
        let doc = parser.finish().unwrap();
        let output = crate::serial::serialize(&doc);
        let expected = format!(
            "<?xml version=\"1.0\"?>\n{}\n",
            std::str::from_utf8(input).unwrap()
        );
        assert_eq!(output, expected);
    }

    #[test]
    #[should_panic(expected = "push() called after finish()")]
    fn test_push_parser_push_after_finish_panics() {
        let mut parser = PushParser::new();
        parser.push(b"<root/>");
        // Simulate calling push after finish by using an unsafe trick.
        // Actually we need to keep a reference — but finish() consumes self.
        // The assertion in push() uses self.finished, so we test indirectly.
        //
        // We cannot directly test this because `finish()` takes self by value.
        // Instead, test the assert fires when finished is set.
        parser.finished = true;
        parser.push(b"more data");
    }

    #[test]
    fn test_push_parser_large_document() {
        let mut parser = PushParser::new();
        parser.push(b"<root>");
        for i in 0..100 {
            let chunk = format!("<item id=\"{i}\">value {i}</item>");
            parser.push(chunk.as_bytes());
        }
        parser.push(b"</root>");

        let doc = parser.finish().unwrap();
        let root = doc.root_element().unwrap();
        let children: Vec<_> = doc.children(root).collect();
        assert_eq!(children.len(), 100);
    }
}