oxml 0.0.11

A pure Rust XML toolkit with zero unsafe code: parsing, an ergonomic tree, and XPath
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 oxml. All rights reserved.

//! One construct per call, through the tree parser's own scanner.
//!
//! The dispatch mirrors `parse_children` and `parse_document` arm for
//! arm, so the two agree on what a document contains. What differs is
//! what is kept: the tree parser keeps an arena, this keeps the names
//! of the open elements and the state a later construct may need --
//! the namespaces in scope, the entities the DOCTYPE declared, the
//! expansion budget.

use alloc::borrow::{Cow, ToOwned};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;

use super::event::BorrowedEvent;
use crate::error::{Error, ErrorKind, Result};
use crate::parser::{Parser, Run, Version};
use crate::tree::{Chars, ExpandedName};
use crate::{Document, Limits};

/// Parser state that outlives a single event.
#[derive(Debug)]
pub(super) struct Carried {
    /// Boxed, so lending it to a parser for each event moves a
    /// pointer rather than the whole arena header; `None` only while
    /// it is lent. Moving it by value, twice per event, cost the
    /// reader about a fifth of its throughput.
    document: Option<Box<Document>>,
    namespaces: crate::parser::Namespaces,
    names: alloc::collections::BTreeMap<String, Vec<u32>>,
    version: Version,
    pub(super) limits: Limits,
    depth: usize,
    /// Declarations from the internal subset.
    ///
    /// Carried because the `DOCTYPE` is consumed by the first event
    /// and its entities are referenced by later ones.
    dtd: Option<crate::dtd::Dtd>,
    /// Whether the document declared `standalone="yes"`.
    standalone: bool,
    /// What is left of the document's entity-expansion budget.
    ///
    /// Per document, not per event. Rebuilding it each scan handed a
    /// bomb split across fifty text nodes fifty full budgets.
    entity_budget: usize,
    /// Validated entity replacement texts, keyed by entity name and
    /// the namespace bindings active when referenced.
    entity_cache: alloc::collections::BTreeSet<(String, u64)>,
}

impl Carried {
    pub(super) fn new(limits: Limits) -> Self {
        Self {
            document: Some(Box::new(Document::placeholder())),
            namespaces: crate::parser::Namespaces::default(),
            names: alloc::collections::BTreeMap::new(),
            version: Version::V10,
            limits,
            depth: 0,
            dtd: None,
            standalone: false,
            entity_budget: limits.max_entity_expansion,
            entity_cache: alloc::collections::BTreeSet::new(),
        }
    }

    /// What the XML declaration settled.
    pub(super) fn declare(&mut self, version: Version, standalone: bool) {
        self.version = version;
        self.standalone = standalone;
    }

    /// A parser over `text` holding this state, to be given back by
    /// [`Self::reclaim`].
    fn lend<'a>(
        &mut self,
        text: &'a str,
        pos: usize,
        external: &'a dyn crate::external::ExternalSource,
    ) -> Parser<'a> {
        Parser {
            input: text,
            bytes: text.as_bytes(),
            pos,
            doc: self
                .document
                .take()
                .unwrap_or_else(|| Box::new(Document::placeholder())),
            name_index: core::mem::take(&mut self.names),
            external,
            ns: core::mem::take(&mut self.namespaces),
            depth: self.depth,
            limits: self.limits,
            dtd: self.dtd.take(),
            version: self.version,
            entity_budget: self.entity_budget,
            entity_depth: 0,
            standalone: self.standalone,
            entity_cache: core::mem::take(&mut self.entity_cache),
        }
    }

    /// Take the state back from a parser [`Self::lend`] made.
    fn reclaim(&mut self, parser: Parser<'_>) {
        self.depth = parser.depth;
        self.document = Some(parser.doc);
        self.names = parser.name_index;
        self.namespaces = parser.ns;
        self.dtd = parser.dtd;
        self.entity_budget = parser.entity_budget;
        self.entity_cache = parser.entity_cache;
    }
}

/// Where the scan has reached in the document.
#[derive(Debug)]
pub(super) struct Cursor {
    /// Scanner position, carried between events.
    pub(super) pos: usize,
    /// Names of the elements currently open, innermost last.
    pub(super) open: Vec<(String, ExpandedName)>,
    /// Whether the XML declaration has been consumed.
    started: bool,
    /// Whether the one permitted root element has been seen.
    seen_root: bool,
    /// Still inside the run of `DOCTYPE`s the tree parser's
    /// `skip_prolog` consumes without objection: every one that
    /// directly follows the declaration, separated by whitespace
    /// alone. Only a `DOCTYPE` met after a comment or processing
    /// instruction is checked against an earlier one, so the reader
    /// does the same, or it would refuse a document `parse` accepts.
    prolog_doctypes: bool,
    /// What the tree parser's arena would hold by now.
    nodes: NodeTally,
}

impl Cursor {
    pub(super) const fn new() -> Self {
        Self {
            pos: 0,
            open: Vec::new(),
            started: false,
            seen_root: false,
            prolog_doctypes: true,
            nodes: NodeTally::new(),
        }
    }
}

/// A count of the nodes the tree parser would have created, for
/// [`Limits::max_nodes`].
///
/// The reader builds no arena, so the bound the tree parser applies
/// to its arena has to be applied to a count of what the arena would
/// hold: the document node, every element with its attribute and
/// namespace nodes, every text run, comment and processing
/// instruction. Counted the same way and checked at the same point,
/// so the two entry points refuse the same document at the same
/// offset -- which is what the agreement tests hold them to.
#[derive(Debug)]
struct NodeTally {
    /// Nodes so far, the document node included.
    count: usize,
}

impl NodeTally {
    const fn new() -> Self {
        Self { count: 1 }
    }

    /// Reserve the same complete insertion the tree parser reserves.
    fn add(&mut self, nodes: usize, limits: Limits, at: usize) -> Result<()> {
        limits.check_nodes(self.count, nodes, at)?;
        self.count += nodes;
        Ok(())
    }
}

/// The end event a self-closing tag owes, held until the next pull.
#[derive(Debug)]
pub(super) struct Pending {
    name: ExpandedName,
    prefix: Option<String>,
}

impl Pending {
    pub(super) fn into_event<'a>(self) -> BorrowedEvent<'a> {
        BorrowedEvent::EndElement {
            name: self.name,
            prefix: self.prefix.map(Cow::Owned),
        }
    }
}

/// What one turn of the scanner produced.
pub(super) enum Scanned<'a> {
    Event(BorrowedEvent<'a>),
    /// A self-closing tag, which is two events.
    SelfClosed(BorrowedEvent<'a>, Pending),
    Eof,
}

/// Run `f` over a parser on `text` at the cursor, moving the carried
/// state into the parser for the duration and back out again.
///
/// The alternative is a self-referential struct: the parser borrows
/// `text`, which the caller owns.
fn with_parser<'a, T>(
    text: &'a str,
    carried: &mut Carried,
    cursor: &mut Cursor,
    external: &'a dyn crate::external::ExternalSource,
    f: impl FnOnce(&mut Parser<'a>, &mut Cursor) -> Result<T>,
) -> Result<T> {
    let mut parser = carried.lend(text, cursor.pos, external);
    let result = f(&mut parser, cursor);
    cursor.pos = parser.pos;
    carried.reclaim(parser);
    result
}

/// Scan the construct at the cursor: an event, or the end.
pub(super) fn scan<'a>(
    text: &'a str,
    carried: &mut Carried,
    cursor: &mut Cursor,
    external: &'a dyn crate::external::ExternalSource,
) -> Result<Scanned<'a>> {
    with_parser(text, carried, cursor, external, one_event)
}

/// Consume a construct at the cursor that yields no event -- the XML
/// declaration, or a `DOCTYPE` outside the root -- and say whether
/// there was one.
///
/// Kept apart from [`scan`] because what follows such a construct may
/// not have arrived yet. The caller checks that before scanning on;
/// a loop inside the scanner took an empty buffer for the end of the
/// document.
pub(super) fn skip_eventless<'a>(
    text: &'a str,
    carried: &mut Carried,
    cursor: &mut Cursor,
    external: &'a dyn crate::external::ExternalSource,
) -> Result<bool> {
    // Inside the root element nothing is eventless, and this is asked
    // before every event. Answering before a parser is lent saves a
    // lend and reclaim per event, which cost the reader about half its
    // throughput. The same answer `eventless_step` gives.
    if cursor.started && !cursor.open.is_empty() {
        return Ok(false);
    }
    with_parser(text, carried, cursor, external, eventless_step)
}

fn eventless_step(
    parser: &mut Parser<'_>,
    cursor: &mut Cursor,
) -> Result<bool> {
    if !cursor.started {
        parser.limits.check_nodes(0, 1, 0)?;
        declaration(parser)?;
        cursor.started = true;
        return Ok(true);
    }
    if !cursor.open.is_empty() {
        return Ok(false);
    }
    parser.skip_whitespace();
    if parser.starts_with("<!DOCTYPE") {
        doctype(parser, cursor)?;
        return Ok(true);
    }
    if parser.pos < parser.bytes.len() {
        // Anything else outside the root ends the run of prolog
        // `DOCTYPE`s that `skip_prolog` would have consumed.
        cursor.prolog_doctypes = false;
    }
    Ok(false)
}

/// Read whichever construct comes next.
fn one_event<'a>(
    parser: &mut Parser<'a>,
    cursor: &mut Cursor,
) -> Result<Scanned<'a>> {
    // Outside the root element the grammar is not element content
    // but `Misc*`: comments, processing instructions and whitespace,
    // around exactly one element.
    if cursor.open.is_empty() {
        return outside_root(parser, cursor);
    }
    if parser.pos >= parser.bytes.len() {
        return Err(Error::new(ErrorKind::UnexpectedEof, parser.pos));
    }
    if !parser.peek_is(b'<') || parser.starts_with("<![CDATA[") {
        let text = char_data(parser)?;
        tally_text(&text, &mut cursor.nodes, parser)?;
        return Ok(Scanned::Event(text));
    }
    if parser.starts_with("</") {
        return end_tag(parser, &mut cursor.open);
    }
    if let Some(event) = misc(parser, &mut cursor.nodes)? {
        return Ok(Scanned::Event(event));
    }
    start_tag(parser, cursor)
}

fn tally_text(
    event: &BorrowedEvent<'_>,
    nodes: &mut NodeTally,
    parser: &Parser<'_>,
) -> Result<()> {
    if matches!(event, BorrowedEvent::Text(t) if !t.is_empty()) {
        nodes.add(1, parser.limits, parser.pos)?;
    }
    Ok(())
}

/// Consume the XML declaration, and nothing after it.
///
/// `skip_prolog` is the one way to the declaration checks, and it
/// goes on to consume any `DOCTYPE` that follows -- which may not have
/// wholly arrived. It is given the declaration alone to work on, so it
/// cannot run on past it.
///
/// The end is measured from the cursor, not from offset 0: the reader
/// may already have stepped over whitespace while waiting for the
/// first construct to arrive, and a cut behind the cursor leaves the
/// parser positioned past the end of its input.
fn declaration(parser: &mut Parser<'_>) -> Result<()> {
    let (input, bytes) = (parser.input, parser.bytes);
    let end = parser.pos + declaration_end(&input[parser.pos..]);
    parser.input = &input[..end];
    parser.bytes = &bytes[..end];
    let outcome = parser.skip_prolog();
    parser.input = input;
    parser.bytes = bytes;
    outcome
}

/// Where the XML declaration ends, by the rule `skip_prolog` uses to
/// recognise one: `<?xml` not followed by a name character. Without a
/// `?>` it runs to the end, where `skip_prolog` reports it.
fn declaration_end(input: &str) -> usize {
    let lead =
        input.len() - input.trim_start_matches([' ', '\t', '\r', '\n']).len();
    let Some(rest) = input[lead..].strip_prefix("<?xml") else {
        return 0;
    };
    if rest
        .bytes()
        .next()
        .is_some_and(|b| crate::parser::is_name_char(char::from(b)))
    {
        return 0;
    }
    rest.find("?>")
        .map_or(input.len(), |i| lead + "<?xml".len() + i + "?>".len())
}

/// Reads one event from outside the root element: the
/// `prolog`/`Misc*` grammar rather than element content. Any
/// `DOCTYPE` here has already been consumed by [`eventless_step`].
fn outside_root<'a>(
    parser: &mut Parser<'a>,
    cursor: &mut Cursor,
) -> Result<Scanned<'a>> {
    parser.skip_whitespace();
    if parser.pos >= parser.bytes.len() {
        return at_end(cursor.seen_root, parser.pos);
    }
    if !parser.peek_is(b'<') {
        // Non-whitespace character data outside the root element
        // is never well-formed.
        return Err(Error::new(ErrorKind::TrailingContent, parser.pos));
    }
    if let Some(event) = misc(parser, &mut cursor.nodes)? {
        return Ok(Scanned::Event(event));
    }
    if parser.starts_with("</") {
        let name = parser.peek_end_tag_name();
        return Err(Error::new(ErrorKind::UnexpectedEndTag(name), parser.pos));
    }
    if cursor.seen_root {
        return Err(Error::new(ErrorKind::TrailingContent, parser.pos));
    }
    cursor.seen_root = true;
    start_tag(parser, cursor)
}

fn at_end(seen_root: bool, pos: usize) -> Result<Scanned<'static>> {
    if seen_root {
        Ok(Scanned::Eof)
    } else {
        // A document with no element in it is not one.
        Err(Error::new(ErrorKind::NoRootElement, pos))
    }
}

/// A `DOCTYPE` met outside the root element.
///
/// Legal once and only before the root -- except that the tree parser
/// consumes every one directly after the declaration (see
/// [`Cursor::prolog_doctypes`]), and agreement with it is the
/// contract.
fn doctype(parser: &mut Parser<'_>, cursor: &Cursor) -> Result<()> {
    if !cursor.prolog_doctypes && (cursor.seen_root || parser.dtd.is_some()) {
        return Err(Error::new(ErrorKind::TrailingContent, parser.pos));
    }
    parser.skip_doctype()
}

/// A comment or processing instruction, which may appear anywhere,
/// or `None` if what comes next is neither.
fn misc<'a>(
    parser: &mut Parser<'a>,
    nodes: &mut NodeTally,
) -> Result<Option<BorrowedEvent<'a>>> {
    if parser.starts_with("<!--") || parser.starts_with("<?") {
        nodes.add(1, parser.limits, parser.pos)?;
    }
    if parser.starts_with("<!--") {
        let c = parser.parse_comment()?;
        return Ok(Some(BorrowedEvent::Comment(chars_to_cow(parser, c))));
    }
    if parser.starts_with("<?") {
        let (target, data) = parser.parse_pi()?;
        return Ok(Some(BorrowedEvent::ProcessingInstruction {
            target: chars_to_cow(parser, target),
            data: chars_to_cow(parser, data),
        }));
    }
    Ok(None)
}

/// Consumes one character-data run into a single `Text` event.
///
/// Text, `CDATA` sections and references in any order are all
/// character data, so they belong to one run and end at the first
/// construct that is not. The tree parser accumulates exactly this
/// run into one text node.
fn char_data<'a>(parser: &mut Parser<'a>) -> Result<BorrowedEvent<'a>> {
    let mut run = Run::default();
    loop {
        if parser.starts_with("<![CDATA[") {
            parser.parse_cdata(&mut run)?;
        } else if parser.pos < parser.bytes.len() && !parser.peek_is(b'<') {
            parser.parse_text_run(&mut run)?;
        } else {
            break;
        }
    }
    // The same limit the tree parser applies when it flushes a run
    // into a text node.
    if parser.limits.max_text_length.is_some_and(|m| run.len() > m) {
        return Err(Error::new(ErrorKind::TextTooLong, parser.pos));
    }
    Ok(BorrowedEvent::Text(run_to_cow(parser.input, &run)))
}

/// An end tag, which must close the innermost open element.
fn end_tag<'a>(
    parser: &mut Parser<'a>,
    open: &mut Vec<(String, ExpandedName)>,
) -> Result<Scanned<'a>> {
    let start = parser.pos;
    parser.pos += 2;
    let close = parser.parse_name()?;
    parser.skip_whitespace();
    if !parser.peek_is(b'>') {
        return Err(Error::new(ErrorKind::Unterminated("end tag"), start));
    }
    parser.pos += 1;
    // `open` is non-empty: an end tag at top level was reported by
    // `outside_root` before reaching here.
    let (expected, name) = open.pop().expect("inside an element");
    if close != expected {
        return Err(Error::new(
            ErrorKind::MismatchedEndTag {
                expected,
                found: close.to_owned(),
            },
            start,
        ));
    }
    parser.ns.pop_scope();
    parser.depth -= 1;
    Ok(Scanned::Event(BorrowedEvent::EndElement {
        name,
        prefix: prefix_from(expected),
    }))
}

/// The prefix of a qualified name the stack owned, reusing its
/// allocation rather than copying the part before the colon.
fn prefix_from(mut qname: String) -> Option<Cow<'static, str>> {
    let colon = qname.find(':')?;
    qname.truncate(colon);
    Some(Cow::Owned(qname))
}

fn prefix_of(qname: &str) -> Option<&str> {
    qname.split_once(':').map(|(prefix, _)| prefix)
}

/// A start tag, and the end event a self-closing one implies.
fn start_tag<'a>(
    parser: &mut Parser<'a>,
    cursor: &mut Cursor,
) -> Result<Scanned<'a>> {
    // Before scanning, not after. `parse_element` checks depth on
    // entry, so for a malformed tag at the limit the tree parser
    // reports the depth and the reader must too.
    if parser.depth >= parser.limits.max_depth {
        return Err(Error::new(ErrorKind::DepthLimitExceeded, parser.pos));
    }
    let tag = parser.scan_start_tag()?;
    let at = tag.tag_start;
    let attributes = attributes(parser, tag.raw_attrs, tag.qname, at)?;
    // The tree parser's arena holds the element, a node per attribute,
    // a node per declaration and, on the root element, one for the
    // `xml` prefix every document has.
    let is_root = parser.depth == 0;
    cursor.nodes.add(
        1 + attributes.len() + tag.declared.len() + usize::from(is_root),
        parser.limits,
        parser.pos,
    )?;
    let name = resolve(parser, tag.qname, true, at)?;
    let start = BorrowedEvent::StartElement {
        name: name.clone(),
        prefix: prefix_of(tag.qname).map(Cow::Borrowed),
        attributes,
        namespaces: namespaces(tag.declared),
    };
    if tag.self_closing {
        parser.ns.pop_scope();
        let pending = Pending {
            name,
            prefix: prefix_of(tag.qname).map(ToOwned::to_owned),
        };
        return Ok(Scanned::SelfClosed(start, pending));
    }
    parser.depth += 1;
    cursor.open.push((tag.qname.to_owned(), name));
    Ok(Scanned::Event(start))
}

/// Namespace declarations as the event reports them: `None` for the
/// default namespace, which the scanner reports as an empty prefix.
fn namespaces(
    declared: Vec<(String, String)>,
) -> Vec<(Option<String>, String)> {
    declared
        .into_iter()
        .map(|(prefix, uri)| ((!prefix.is_empty()).then_some(prefix), uri))
        .collect()
}

/// Resolve and intern a name, as the tree parser does.
fn resolve(
    parser: &mut Parser<'_>,
    qname: &str,
    is_element: bool,
    at: usize,
) -> Result<ExpandedName> {
    let id = parser.intern_qname(qname, is_element, at)?;
    Ok(parser
        .doc
        .name(id)
        .cloned()
        .unwrap_or_else(|| ExpandedName::local(qname)))
}

/// Resolve a start tag's attributes, dropping namespace declarations
/// and refusing duplicates.
///
/// Compared by expanded name, as the tree parser does: Namespaces in
/// XML forbids two attributes with one expanded name however they are
/// spelled.
fn attributes<'a>(
    parser: &mut Parser<'a>,
    raw_attrs: Vec<(&'a str, Run)>,
    element: &str,
    at: usize,
) -> Result<Vec<(ExpandedName, Cow<'a, str>)>> {
    let mut attributes = Vec::with_capacity(raw_attrs.len());
    for (raw, value) in raw_attrs {
        if raw == "xmlns" || raw.starts_with("xmlns:") {
            continue;
        }
        let name = resolve(parser, raw, false, at)?;
        if attributes
            .iter()
            .any(|(seen, _): &(ExpandedName, _)| *seen == name)
        {
            return Err(Error::new(
                ErrorKind::DuplicateAttribute(raw.to_owned()),
                at,
            ));
        }
        // XML 1.0 section 3.3.3: a value whose declared type is not
        // `CDATA` gets a second pass, as the tree parser gives it.
        let tokenized = parser
            .dtd
            .as_ref()
            .is_some_and(|d| d.is_tokenized_attribute(element, raw));
        attributes
            .push((name, attribute_value(parser.input, &value, tokenized)));
    }
    Ok(attributes)
}

/// An attribute's value, borrowed when the input holds it verbatim.
fn attribute_value<'a>(
    input: &'a str,
    value: &Run,
    tokenized: bool,
) -> Cow<'a, str> {
    let text = value.as_str(input);
    if tokenized {
        let collapsed = collapse(text);
        if collapsed != text {
            return Cow::Owned(collapsed);
        }
        // Collapsing changed nothing, so the slice still says it.
    }
    match value.as_span() {
        Some((start, len)) => Cow::Borrowed(&input[start..start + len]),
        None => Cow::Owned(text.to_owned()),
    }
}

/// The further normalisation of a non-`CDATA` attribute value:
/// leading and trailing spaces discarded, runs collapsed to one. The
/// same rewriting as the tree parser's private `collapse`.
fn collapse(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for part in text.split(' ').filter(|p| !p.is_empty()) {
        if !out.is_empty() {
            out.push(' ');
        }
        out.push_str(part);
    }
    out
}

fn run_to_cow<'a>(input: &'a str, run: &Run) -> Cow<'a, str> {
    match run.as_span() {
        Some((start, len)) => Cow::Borrowed(&input[start..start + len]),
        None => Cow::Owned(run.as_str(input).to_owned()),
    }
}

/// Convert a parser character span/expansion into a `Cow<'a, str>`.
fn chars_to_cow<'a>(parser: &Parser<'a>, c: Chars) -> Cow<'a, str> {
    match c {
        Chars::Span(s, l) => {
            let start = s as usize;
            let end = (s + l) as usize;
            Cow::Borrowed(parser.input.get(start..end).unwrap_or_default())
        }
        Chars::Expanded(_) => Cow::Owned(parser.owned(c).to_owned()),
    }
}