einvoice-xml 0.3.0

A streaming (pull) XML 1.0 parser with namespace resolution, zero-copy where it can be, for well-formedness checking and one-pass validation without a tree, in pure Rust
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
//! The comparison with `roxmltree`: the event stream of the reader and a stream
//! derived from `roxmltree`'s tree, as lines of text, or the error of each.

#![allow(dead_code, reason = "each test uses part of this module")]

pub(crate) mod cases;

use std::fmt::Write as _;
use std::path::{Path, PathBuf};

use einvoice_xml::{Error, Event, Reader};

/// What a parser makes of a text: its stream, or its error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Outcome {
    Stream(Vec<String>),
    Error(Failure),
}

/// An error: message, row, column, and whether it is one that only this reader
/// reports (a construct that `roxmltree` accepts against the XML specification).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Failure {
    pub(crate) message: String,
    pub(crate) row: u32,
    pub(crate) col: u32,
    pub(crate) strict: bool,
}

impl std::fmt::Display for Failure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} [{}:{}]", self.message, self.row, self.col)
    }
}

/// Whether an error is one of those that `roxmltree` never reports.
pub(crate) fn is_strict(error: &Error) -> bool {
    matches!(
        error,
        Error::InvalidCharacterReference(_)
            | Error::InvalidPiTarget(_)
            | Error::PiWithoutWhitespace(_)
            | Error::InvalidXmlDeclaration(..)
            | Error::InvalidQName(_)
            | Error::EmptyPrefixedNamespace(..)
            | Error::XmlnsPrefixDeclared(_)
            | Error::DuplicatedNamespaceDeclaration(..)
            | Error::PrefixedXmlnsAttribute(..)
            | Error::DepthLimitReached
    )
}

fn ns(namespace: Option<&str>) -> &str {
    namespace.unwrap_or("")
}

pub(crate) fn element_line(
    namespace: Option<&str>,
    prefix: &str,
    local: &str,
    attributes: &[(String, String, String, String)],
    in_scope: &[(String, String)],
) -> String {
    let mut line = format!("<{{{}}}{local} prefix={prefix:?}", ns(namespace));
    for (namespace, prefix, local, value) in attributes {
        let _ = write!(
            line,
            " @{{{namespace}}}{local} prefix={prefix:?} ={value:?}"
        );
    }
    for (prefix, uri) in in_scope {
        let _ = write!(line, " xmlns[{prefix}]={uri:?}");
    }
    line
}

/// The reader's stream: adjacent text events merged, empty text dropped, the
/// namespaces in scope of each element computed from the declarations.
pub(crate) fn ours(text: &str) -> Outcome {
    ours_with(&mut Reader::new(text))
}

pub(crate) fn ours_with(reader: &mut Reader<'_>) -> Outcome {
    let mut lines = Vec::new();
    let mut pending = String::new();
    let mut scopes: Vec<Vec<(String, String)>> = vec![Vec::new()];
    let flush = |pending: &mut String, lines: &mut Vec<String>| {
        if !pending.is_empty() {
            lines.push(format!("T{pending:?}"));
            pending.clear();
        }
    };
    loop {
        let event = match reader.next_event() {
            Ok(Some(event)) => event,
            Ok(None) => break,
            Err(e) => return Outcome::Error(failure(&e)),
        };
        match event {
            Event::Text(text) => pending.push_str(&text.value),
            Event::Start(start) => {
                flush(&mut pending, &mut lines);
                let mut scope = scopes.last().cloned().unwrap_or_default();
                for (prefix, uri) in start.namespace_declarations() {
                    scope.retain(|(p, _)| p != prefix);
                    scope.push((prefix.to_owned(), uri.to_owned()));
                }
                scope.sort();
                let attributes: Vec<_> = start
                    .attributes()
                    .map(|a| {
                        (
                            ns(a.name.namespace).to_owned(),
                            a.name.prefix.to_owned(),
                            a.name.local.to_owned(),
                            a.value.to_string(),
                        )
                    })
                    .collect();
                let name = start.name();
                lines.push(element_line(
                    name.namespace,
                    name.prefix,
                    name.local,
                    &attributes,
                    &scope,
                ));
                scopes.push(scope);
            }
            Event::End(end) => {
                flush(&mut pending, &mut lines);
                let name = end.name();
                lines.push(format!("</{{{}}}{}>", ns(name.namespace), name.local));
                scopes.pop();
            }
            Event::Comment(comment) => {
                flush(&mut pending, &mut lines);
                lines.push(format!("C{:?}", comment.value));
            }
            Event::ProcessingInstruction(pi) => {
                flush(&mut pending, &mut lines);
                lines.push(format!("P{} {:?}", pi.target, pi.value.as_deref()));
            }
        }
    }
    Outcome::Stream(lines)
}

pub(crate) fn failure(e: &Error) -> Failure {
    Failure {
        message: e.to_string(),
        row: e.pos().row,
        col: e.pos().col,
        strict: is_strict(e),
    }
}

/// `\r\n` and lone `\r` as `\n`.
pub(crate) fn line_ends(s: &str) -> String {
    s.replace("\r\n", "\n").replace('\r', "\n")
}

/// Whether the text starts (after a byte order mark) with `<?xml` and a whitespace
/// other than a space: an XML declaration for the reader, a processing instruction
/// for `roxmltree`.
pub(crate) fn declaration_without_space(text: &str) -> bool {
    let text = text.strip_prefix('\u{FEFF}').unwrap_or(text);
    text.strip_prefix("<?xml")
        .is_some_and(|rest| rest.starts_with(['\t', '\n', '\r']))
}

pub(crate) fn roxmltree_options<'a>() -> roxmltree::ParsingOptions<'a> {
    roxmltree::ParsingOptions {
        allow_dtd: false,
        nodes_limit: u32::MAX,
        ..roxmltree::ParsingOptions::default()
    }
}

/// `roxmltree`'s stream, with the differences that the crate documents taken out:
/// comments and processing instructions get their line ends normalised, an empty
/// namespace name is no namespace, and the processing instruction that `<?xml` and a
/// tab or line end starts is dropped.
pub(crate) fn theirs(text: &str) -> Outcome {
    theirs_with(text, roxmltree_options())
}

pub(crate) fn theirs_with<'a>(text: &'a str, options: roxmltree::ParsingOptions<'a>) -> Outcome {
    let doc = match roxmltree::Document::parse_with_options(text, options) {
        Ok(doc) => doc,
        Err(e) => {
            return Outcome::Error(Failure {
                message: e.to_string(),
                row: e.pos().row,
                col: e.pos().col,
                strict: false,
            });
        }
    };
    let input = doc.input_text();
    let mut lines = Vec::new();
    let mut skip_declaration = declaration_without_space(text);
    let close = |node: roxmltree::Node<'_, '_>, lines: &mut Vec<String>| {
        lines.push(format!(
            "</{{{}}}{}>",
            ns(node.tag_name().namespace().filter(|n| !n.is_empty())),
            node.tag_name().name()
        ));
    };
    // The elements open, innermost last: closed when the walk in document order
    // reaches a node outside them.
    let mut open: Vec<roxmltree::Node<'_, '_>> = Vec::new();
    for node in doc.root().descendants().skip(1) {
        while let Some(&top) = open.last() {
            if node.parent() == Some(top) {
                break;
            }
            open.pop();
            close(top, &mut lines);
        }
        match node.node_type() {
            roxmltree::NodeType::Root => {}
            roxmltree::NodeType::Element => {
                let prefix = qname_prefix(&input[node.range().start + 1..]);
                let attributes: Vec<_> = node
                    .attributes()
                    .map(|a| {
                        (
                            ns(a.namespace().filter(|n| !n.is_empty())).to_owned(),
                            qname_prefix(&input[a.range_qname()]).to_owned(),
                            a.name().to_owned(),
                            a.value().to_owned(),
                        )
                    })
                    .collect();
                let mut scope: Vec<(String, String)> = node
                    .namespaces()
                    .map(|n| (n.name().unwrap_or("").to_owned(), n.uri().to_owned()))
                    .collect();
                scope.sort();
                lines.push(element_line(
                    node.tag_name().namespace().filter(|n| !n.is_empty()),
                    prefix,
                    node.tag_name().name(),
                    &attributes,
                    &scope,
                ));
                open.push(node);
            }
            roxmltree::NodeType::Text => {
                let value = node.text().unwrap_or_default();
                if !value.is_empty() {
                    lines.push(format!("T{value:?}"));
                }
            }
            roxmltree::NodeType::Comment => {
                lines.push(format!("C{:?}", line_ends(node.text().unwrap_or_default())));
            }
            roxmltree::NodeType::PI => {
                let Some(pi) = node.pi() else { continue };
                if skip_declaration && pi.target == "xml" {
                    skip_declaration = false;
                    continue;
                }
                lines.push(format!("P{} {:?}", pi.target, pi.value.map(line_ends)));
            }
        }
    }
    while let Some(top) = open.pop() {
        close(top, &mut lines);
    }
    Outcome::Stream(lines)
}

/// The prefix of the qualified name at the start of `s`, `""` for none.
fn qname_prefix(s: &str) -> &str {
    let end = s
        .find(|c: char| c.is_ascii_whitespace() || c == '/' || c == '>' || c == '=')
        .unwrap_or(s.len());
    s[..end].split_once(':').map_or("", |(prefix, _)| prefix)
}

/// The first line where two streams differ, for a message.
pub(crate) fn first_difference(a: &[String], b: &[String]) -> String {
    let i = a.iter().zip(b).take_while(|(x, y)| x == y).count();
    format!(
        "line {i}: ours {:?}, roxmltree {:?}",
        a.get(i).map(|s| truncate(s)),
        b.get(i).map(|s| truncate(s))
    )
}

fn truncate(s: &str) -> String {
    if s.chars().count() > 300 {
        format!("{}...", s.chars().take(300).collect::<String>())
    } else {
        s.to_owned()
    }
}

/// The exact comparison: the reader and `roxmltree` must give the same stream, or
/// the same error.
pub(crate) fn exact(text: &str) -> Result<(), String> {
    match (ours(text), theirs(text)) {
        (Outcome::Stream(a), Outcome::Stream(b)) if a == b => Ok(()),
        (Outcome::Stream(a), Outcome::Stream(b)) => Err(first_difference(&a, &b)),
        (Outcome::Error(a), Outcome::Error(b))
            if a.message == b.message && (a.row, a.col) == (b.row, b.col) =>
        {
            Ok(())
        }
        (a, b) => Err(format!("ours {}, roxmltree {}", describe(&a), describe(&b))),
    }
}

pub(crate) fn describe(outcome: &Outcome) -> String {
    match outcome {
        Outcome::Stream(lines) => format!("accepts ({} lines)", lines.len()),
        Outcome::Error(failure) => format!("refuses: {failure}"),
    }
}

/// The comparison on any text, which may contain the constructs where the reader
/// differs from `roxmltree` by design:
///
/// * whatever `roxmltree` refuses, the reader refuses;
/// * when both refuse, the same error, unless the reader's is one that `roxmltree`
///   never reports (raised for a construct it accepts) or the text starts with an XML
///   declaration that `roxmltree` reads as a processing instruction;
/// * when `roxmltree` accepts and the reader refuses, the reader's error is one that
///   `roxmltree` never reports (or the same declaration case);
/// * when both accept, the same stream, except text where a carriage return is
///   written next to a reference.
pub(crate) fn invariants(text: &str) -> Result<(), String> {
    invariants_of(text, &ours(text), &theirs(text))
}

/// [`invariants`] on outcomes already computed.
pub(crate) fn invariants_of(text: &str, ours: &Outcome, theirs: &Outcome) -> Result<(), String> {
    let declaration = declaration_without_space(text);
    match (ours, theirs) {
        (Outcome::Stream(_), Outcome::Error(b)) => {
            Err(format!("roxmltree refuses ({b}), the reader accepts"))
        }
        (Outcome::Error(a), Outcome::Error(b)) => {
            if a.strict
                || declaration
                || (a.message == b.message && (a.row, a.col) == (b.row, b.col))
            {
                Ok(())
            } else {
                Err(format!("ours {a}, roxmltree {b}"))
            }
        }
        (Outcome::Error(a), Outcome::Stream(_)) => {
            if a.strict || declaration {
                Ok(())
            } else {
                Err(format!("roxmltree accepts, ours {a}"))
            }
        }
        (Outcome::Stream(a), Outcome::Stream(b)) => {
            if a == b {
                return Ok(());
            }
            let carriage_return_by_reference = text.contains("\r&") || text.contains(";\r");
            if a.len() == b.len()
                && carriage_return_by_reference
                && a.iter()
                    .zip(b)
                    .all(|(x, y)| x == y || (x.starts_with('T') && y.starts_with('T')))
            {
                return Ok(());
            }
            Err(first_difference(a, b))
        }
    }
}

/// The workspace root.
pub(crate) fn root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
}

/// Every XML file under `dir` (by extension), sorted.
pub(crate) fn xml_files(dir: &Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    let mut stack = vec![dir.to_path_buf()];
    while let Some(dir) = stack.pop() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                stack.push(path);
            } else if path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
                ["xml", "xsl", "xslt", "xsd", "sch", "svrl"]
                    .contains(&e.to_ascii_lowercase().as_str())
            }) {
                files.push(path);
            }
        }
    }
    files.sort();
    files
}