pxml 0.2.0

A parallel, StAX-style (pull) XML reader for documents made of many uniform, order-independent records
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
//! Phase B — per-record pull parser over a single record's byte slice.
//!
//! [`RecordReader`] wraps a borrowed `quick_xml::Reader` over the record's
//! `&[u8]`, mapping `quick_xml`'s events to our [`Event`]. Text and attribute
//! entity references are resolved against the shared [`Prelude`] (predefined
//! entities plus the document's internal-subset `<!ENTITY>` definitions), so a
//! record parses correctly even though it is read in isolation.
//!
//! `quick_xml` borrows element names from the event rather than the input, so
//! events are tied to the reader: process (or copy out of) each event before
//! requesting the next.
//!
//! Namespace prefixes are surfaced lexically (as written). The root/prolog
//! [`NamespaceContext`](crate::NamespaceContext) is available on the prelude for
//! callers that need to resolve them.

use std::borrow::Cow;
use std::sync::Arc;

use quick_xml::Reader;
use quick_xml::escape::EscapeError;
use quick_xml::events::{BytesText, Event as QxEvent};

use crate::XmlError;
use crate::event::{Attrs, Event};
use crate::prelude::Prelude;

/// A StAX-style pull cursor over one record's events.
pub struct RecordReader<'doc> {
    reader: Reader<&'doc [u8]>,
    prelude: Arc<Prelude>,
    current: Option<QxEvent<'doc>>,
    /// One-slot lookahead: an event already read from the reader but not yet
    /// surfaced (the structural event that terminated a coalesced text run).
    pending: Option<QxEvent<'doc>>,
    index: usize,
}

impl std::fmt::Debug for RecordReader<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RecordReader")
            .field("index", &self.index)
            .field("position", &self.reader.buffer_position())
            .finish_non_exhaustive()
    }
}

impl<'doc> RecordReader<'doc> {
    /// Build a reader over a single record's slice with shared prolog context.
    /// `index` is the record's position in document order, used to tag errors.
    pub(crate) fn new(bytes: &'doc [u8], prelude: Arc<Prelude>, index: usize) -> Self {
        let mut reader = Reader::from_reader(bytes);
        // Surface `<a/>` as a Start/End pair so consumers see matched tags.
        reader.config_mut().expand_empty_elements = true;
        Self {
            reader,
            prelude,
            current: None,
            pending: None,
            index,
        }
    }

    /// This record's position in document order.
    pub fn index(&self) -> usize {
        self.index
    }

    /// Advance to the next event, or `Ok(None)` at the end of the record.
    /// Comments, PIs, and the XML/DOCTYPE declarations are skipped. Text content
    /// is coalesced: quick-xml surfaces entity references in text as separate
    /// `GeneralRef` events, so a maximal run of `Text`/`GeneralRef` events is
    /// merged back into one resolved [`Event::Text`].
    pub fn next_event(&mut self) -> Result<Option<Event<'_>>, XmlError> {
        let Some(ev) = self.next_surfaced()? else {
            return Ok(None);
        };

        if is_text_run(&ev) {
            // Peek the *immediately* following event (raw, skipping nothing): a
            // comment or PI between two text nodes is a boundary, so the run must
            // not coalesce across it. The terminator is buffered in `pending`;
            // the next call's skip loop drops it if it is ignorable markup.
            let next = self.read_raw()?;
            // Fast path: a lone literal text node decodes straight from the
            // document buffer (zero-copy for UTF-8), no allocation.
            let lone_literal =
                matches!(ev, QxEvent::Text(_)) && !next.as_ref().is_some_and(is_text_run);
            if lone_literal {
                let QxEvent::Text(e) = ev else {
                    unreachable!("checked Text above")
                };
                let text = text_content(e);
                self.pending = next;
                return Ok(Some(Event::Text(text)));
            }
            // Otherwise coalesce the run into one owned, fully-resolved string.
            let mut out = String::new();
            append_run_event(&mut out, &ev, self.prelude.as_ref(), self.index)?;
            let mut cur = next;
            while let Some(ev) = cur {
                if !is_text_run(&ev) {
                    self.pending = Some(ev);
                    break;
                }
                append_run_event(&mut out, &ev, self.prelude.as_ref(), self.index)?;
                cur = self.read_raw()?;
            }
            return Ok(Some(Event::Text(Cow::Owned(out))));
        }

        self.current = Some(ev);
        let event = map_event(
            self.current.as_ref().expect("event stored above"),
            self.prelude.as_ref(),
            self.index,
        )?;
        Ok(Some(event))
    }

    /// Read the next *surfaced* event, draining the lookahead buffer first and
    /// skipping comments, PIs, and the XML/DOCTYPE declarations. Used only to
    /// start an event; the text-run lookahead uses [`read_raw`](Self::read_raw)
    /// so skipped markup still bounds a text node. `Ok(None)` at end of input.
    fn next_surfaced(&mut self) -> Result<Option<QxEvent<'doc>>, XmlError> {
        loop {
            match self.read_raw()? {
                None => return Ok(None),
                Some(
                    QxEvent::Comment(_) | QxEvent::PI(_) | QxEvent::Decl(_) | QxEvent::DocType(_),
                ) => continue,
                Some(keep) => return Ok(Some(keep)),
            }
        }
    }

    /// Read one raw event — from the one-slot lookahead buffer if present,
    /// otherwise straight from the reader, skipping nothing. `Ok(None)` at Eof.
    fn read_raw(&mut self) -> Result<Option<QxEvent<'doc>>, XmlError> {
        if let Some(ev) = self.pending.take() {
            return Ok(Some(ev));
        }
        match self
            .reader
            .read_event()
            .map_err(|e| record_error(self.index, e))?
        {
            QxEvent::Eof => Ok(None),
            ev => Ok(Some(ev)),
        }
    }
}

/// Map a structural `quick_xml` event to a [`crate::Event`], resolving attribute
/// entities against `prelude`. Shared by [`RecordReader`] and the crate's
/// sequential reader. `index` tags any decode error.
///
/// Only `Start`/`Empty`/`End`/`CData` are valid here. Text content — `Text` and
/// the `GeneralRef` entity references now interleaved with it by quick-xml — is
/// coalesced by the callers' cursors, and comments/PIs/declarations are skipped,
/// before this is reached.
pub(crate) fn map_event<'e>(
    ev: &'e QxEvent<'e>,
    prelude: &'e Prelude,
    index: usize,
) -> Result<Event<'e>, XmlError> {
    Ok(match ev {
        QxEvent::Start(e) | QxEvent::Empty(e) => Event::Start {
            name: e.name(),
            attrs: Attrs::new(e.attributes_raw().as_bytes(), prelude, index),
        },
        QxEvent::End(e) => Event::End { name: e.name() },
        QxEvent::CData(e) => {
            let bytes: &[u8] = e.as_bytes();
            Event::Cdata(bytes)
        }
        _ => unreachable!("non-surfaced event passed to map_event"),
    })
}

/// True for events that make up a text run — literal text or an entity
/// reference. quick-xml 0.40 surfaces `&…;` references in text content as
/// standalone [`GeneralRef`](QxEvent::GeneralRef) events, so a run of these must
/// be coalesced back into a single [`Event::Text`].
pub(crate) fn is_text_run(ev: &QxEvent) -> bool {
    matches!(ev, QxEvent::Text(_) | QxEvent::GeneralRef(_))
}

/// Borrow a literal `Text` event's content. quick-xml decodes to UTF-8 as it
/// reads, so the event already carries `str`; because entity references are
/// separate `GeneralRef` events, a `Text` event also carries no `&…;` to expand.
/// Borrowing is therefore enough, and stays zero-copy.
pub(crate) fn text_content(e: BytesText<'_>) -> Cow<'_, str> {
    e.into_inner()
}

/// Append one text-run event's resolved content to `out`: literal text is
/// decoded; a `GeneralRef` is resolved as a character reference, or else a named
/// entity from `prelude` (an unknown name is rejected, never silently dropped).
pub(crate) fn append_run_event(
    out: &mut String,
    ev: &QxEvent,
    prelude: &Prelude,
    index: usize,
) -> Result<(), XmlError> {
    match ev {
        QxEvent::Text(e) => out.push_str(e),
        QxEvent::GeneralRef(e) => {
            if let Some(ch) = e
                .resolve_char_ref()
                .map_err(|err| record_error(index, err))?
            {
                out.push(ch);
            } else {
                let name: &str = e;
                match prelude.resolve_entity(name) {
                    Some(value) => out.push_str(value),
                    None => {
                        return Err(record_error(
                            index,
                            quick_xml::Error::from(EscapeError::UnrecognizedEntity(
                                0..name.len(),
                                name.to_owned(),
                            )),
                        ));
                    }
                }
            }
        }
        _ => unreachable!("append_run_event called with a non-text-run event"),
    }
    Ok(())
}

fn record_error(
    index: usize,
    source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> XmlError {
    XmlError::RecordError {
        index,
        source: source.into(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::Event;
    use crate::prelude::{Encoding, NamespaceContext};
    use proptest::prelude::*;
    use std::collections::HashMap;

    fn prelude(entities: &[(&str, &str)]) -> Arc<Prelude> {
        let mut map = HashMap::new();
        for (k, v) in entities {
            map.insert((*k).into(), (*v).into());
        }
        Arc::new(Prelude {
            encoding: Encoding::Utf8,
            root_name: "root".into(),
            namespaces: NamespaceContext::new(),
            entities: map,
        })
    }

    /// Render a record's events as compact tags for easy assertions.
    fn events(bytes: &[u8], prelude: Arc<Prelude>) -> Vec<String> {
        let mut r = RecordReader::new(bytes, prelude, 0);
        let mut out = Vec::new();
        while let Some(ev) = r.next_event().expect("event ok") {
            out.push(match ev {
                Event::Start { name, .. } => {
                    format!("S:{}", name.as_ref())
                }
                Event::End { name } => {
                    format!("E:{}", name.as_ref())
                }
                Event::Text(t) => format!("T:{t}"),
                Event::Cdata(c) => format!("C:{}", std::str::from_utf8(c).unwrap()),
            });
        }
        out
    }

    #[test]
    fn nested_elements_and_text() {
        assert_eq!(
            events(b"<trade><id>7</id></trade>", prelude(&[])),
            ["S:trade", "S:id", "T:7", "E:id", "E:trade"]
        );
    }

    #[test]
    fn self_closing_expands_to_start_end() {
        assert_eq!(events(b"<trade/>", prelude(&[])), ["S:trade", "E:trade"]);
    }

    #[test]
    fn predefined_entities_in_text() {
        assert_eq!(
            events(b"<t>a &lt; b &amp; c</t>", prelude(&[])),
            ["S:t", "T:a < b & c", "E:t"]
        );
    }

    #[test]
    fn prelude_entity_in_text() {
        assert_eq!(
            events(b"<t>&foo;</t>", prelude(&[("foo", "BAR")])),
            ["S:t", "T:BAR", "E:t"]
        );
    }

    #[test]
    fn char_references_in_text() {
        // Decimal and hex character references resolve and coalesce with the
        // surrounding literal text into one event.
        assert_eq!(
            events(b"<t>&#65;&#x42;C</t>", prelude(&[])),
            ["S:t", "T:ABC", "E:t"]
        );
    }

    #[test]
    fn adjacent_entities_coalesce() {
        assert_eq!(
            events(b"<t>&lt;&gt;</t>", prelude(&[])),
            ["S:t", "T:<>", "E:t"]
        );
    }

    #[test]
    fn entities_at_text_boundaries() {
        assert_eq!(
            events(b"<t>&amp;mid&amp;</t>", prelude(&[])),
            ["S:t", "T:&mid&", "E:t"]
        );
    }

    #[test]
    fn mixed_char_and_named_refs() {
        assert_eq!(
            events(b"<t>x&#38;&foo;y</t>", prelude(&[("foo", "BAR")])),
            ["S:t", "T:x&BARy", "E:t"]
        );
    }

    #[test]
    fn empty_entity_expansion_yields_empty_text() {
        assert_eq!(
            events(b"<t>&empty;</t>", prelude(&[("empty", "")])),
            ["S:t", "T:", "E:t"]
        );
    }

    #[test]
    fn cdata_breaks_a_text_run() {
        // A text run never spans a CDATA section: the entity-bearing text before
        // it and the literal text after it remain distinct events.
        assert_eq!(
            events(b"<t>a&amp;<![CDATA[b]]>c</t>", prelude(&[])),
            ["S:t", "T:a&", "C:b", "T:c", "E:t"]
        );
    }

    #[test]
    fn comment_breaks_a_text_run() {
        // A comment between text nodes is a boundary: the run must not coalesce
        // across it, even though the comment itself is skipped.
        assert_eq!(
            events(b"<t>a<!--c-->&amp;</t>", prelude(&[])),
            ["S:t", "T:a", "T:&", "E:t"]
        );
    }

    #[test]
    fn pi_breaks_a_text_run() {
        assert_eq!(
            events(b"<t>&amp;<?pi?>b</t>", prelude(&[])),
            ["S:t", "T:&", "T:b", "E:t"]
        );
    }

    #[test]
    fn entity_run_terminated_by_element() {
        assert_eq!(
            events(b"<t>&amp;<b/>c</t>", prelude(&[])),
            ["S:t", "T:&", "S:b", "E:b", "T:c", "E:t"]
        );
    }

    #[test]
    fn unknown_entity_in_text_is_record_error() {
        let mut r = RecordReader::new(b"<t>&nope;</t>", prelude(&[]), 3);
        assert!(r.next_event().is_ok(), "start reads fine");
        assert!(matches!(
            r.next_event(),
            Err(XmlError::RecordError { index: 3, .. })
        ));
    }

    #[test]
    fn cdata_is_raw() {
        assert_eq!(
            events(b"<t><![CDATA[<not>&x;]]></t>", prelude(&[])),
            ["S:t", "C:<not>&x;", "E:t"]
        );
    }

    #[test]
    fn comments_and_pis_are_skipped() {
        assert_eq!(
            events(b"<t><!-- c --><?pi?>hi</t>", prelude(&[])),
            ["S:t", "T:hi", "E:t"]
        );
    }

    #[test]
    fn attributes_decode_values() {
        let mut r = RecordReader::new(
            br#"<a k1="v1" k2="a&amp;b&e;"/>"#,
            prelude(&[("e", "X")]),
            0,
        );
        let ev = r.next_event().unwrap().unwrap();
        let Event::Start { name, attrs } = ev else {
            panic!("expected start");
        };
        assert_eq!(name.as_ref(), "a");
        let got: Vec<(String, String)> = attrs
            .iter()
            .map(|a| {
                let a = a.unwrap();
                (
                    String::from_utf8(a.key.to_vec()).unwrap(),
                    a.value.into_owned(),
                )
            })
            .collect();
        assert_eq!(
            got,
            vec![
                ("k1".to_string(), "v1".to_string()),
                ("k2".to_string(), "a&bX".to_string()),
            ]
        );
    }

    #[test]
    fn lexical_qnames_keep_prefixes() {
        assert_eq!(
            events(b"<p:trade><p:id>1</p:id></p:trade>", prelude(&[])),
            ["S:p:trade", "S:p:id", "T:1", "E:p:id", "E:p:trade"]
        );
    }

    #[test]
    fn mismatched_end_tag_is_record_error() {
        let mut r = RecordReader::new(b"<a></b>", prelude(&[]), 7);
        assert!(r.next_event().is_ok(), "start reads fine");
        assert!(matches!(
            r.next_event(),
            Err(XmlError::RecordError { index: 7, .. })
        ));
    }

    /// One element-content segment paired with the text it should decode to: a
    /// literal run, a predefined or custom entity, or a character reference.
    fn arb_text_seg() -> impl Strategy<Value = (String, String)> {
        prop_oneof![
            "[a-zA-Z0-9 .,_:-]{1,8}".prop_map(|s| (s.clone(), s)),
            Just(("&lt;".to_string(), "<".to_string())),
            Just(("&gt;".to_string(), ">".to_string())),
            Just(("&amp;".to_string(), "&".to_string())),
            Just(("&apos;".to_string(), "'".to_string())),
            Just(("&quot;".to_string(), "\"".to_string())),
            Just(("&foo;".to_string(), "BAR".to_string())),
            (0x20u32..0x7f).prop_map(|c| {
                let ch = char::from_u32(c).unwrap();
                (format!("&#{c};"), ch.to_string())
            }),
            (0x20u32..0x7f).prop_map(|c| {
                let ch = char::from_u32(c).unwrap();
                (format!("&#x{c:x};"), ch.to_string())
            }),
        ]
    }

    proptest! {
        /// Any interleaving of literal text, entity references, and character
        /// references in element content coalesces back into exactly one
        /// resolved `Text` event whose value is the concatenation of the
        /// segments' expansions.
        #[test]
        fn coalesced_text_roundtrips(segs in prop::collection::vec(arb_text_seg(), 1..12)) {
            let mut raw = String::from("<t>");
            let mut expected = String::new();
            for (src, decoded) in &segs {
                raw.push_str(src);
                expected.push_str(decoded);
            }
            raw.push_str("</t>");

            let got = events(raw.as_bytes(), prelude(&[("foo", "BAR")]));
            prop_assert_eq!(got, vec!["S:t".to_string(), format!("T:{expected}"), "E:t".to_string()]);
        }
    }
}