en16931-formats 0.2.0

European e-invoicing formats on top of the EN 16931 semantic model: UBL 2.1 and UN/CEFACT CII in both directions, the XRechnung CIUS, and ZUGFeRD / Factur-X hybrid PDFs. Every business rule is delegated to `en16931`.
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
//! A **document tree that serialises itself into schema order**.
//!
//! Shared by both bindings, because both need exactly the same thing and for
//! exactly the same reason.
//!
//! # Why a tree and not a string
//!
//! UBL and CII content models are XSD `sequence`s: a document carrying the
//! right elements in the wrong order is invalid, and **no Schematron rule says
//! so** — ordering is the schema's job, and this crate ships no schema.
//!
//! A hand-sequenced writer therefore has to get the order right at every call
//! site, in two syntaxes, one of which (UBL) has two document elements that
//! disagree about where `cbc:TaxPointDate` goes. That was tried, and it was
//! wrong. So the writer emits in whatever order reads best and this module
//! sorts by the derived tables ([`crate::ubl::order`], [`crate::cii::order`]).
//! Misordering became unrepresentable rather than tested-for.
//!
//! # It also enforces the prohibitions
//!
//! 1 220 of the 1 339 syntax rules say some element "shall not be used". The
//! serialiser checks each path against the extracted tables, so "the writer
//! cannot emit a forbidden element" is a property of *this* module rather than
//! a habit spread across two hundred call sites.
//!
//! # Nothing is dropped silently
//!
//! Anything the target sequence has no place for is removed **and reported**.
//! UBL's `<CreditNote>` has no `cbc:DueDate`; dropping BT-9 is correct, and
//! dropping it quietly would mean a payment due date vanishing between two
//! systems with nothing in any log.

use core::fmt::Write as _;

/// How a syntax answers the serialiser's two questions.
pub(crate) struct Rules {
    /// The child order for a parent element, by local name.
    pub order: fn(&str) -> Option<&'static [&'static str]>,
    /// The rule forbidding this element path, if any.
    pub forbidden_path: fn(&str) -> Option<&'static str>,
    /// The rule forbidding this attribute anywhere, if any.
    pub forbidden_attribute: fn(&str) -> Option<&'static str>,
}

#[derive(Clone)]
pub(crate) struct Node {
    name: String,
    attrs: Vec<(String, String)>,
    text: Option<String>,
    children: Vec<Node>,
}

/// A document under construction.
pub(crate) struct Xml {
    stack: Vec<Node>,
    rules: &'static Rules,
}

impl Xml {
    pub fn new(root: &str, attrs: Vec<(String, String)>, rules: &'static Rules) -> Self {
        Self {
            stack: vec![Node {
                name: root.to_owned(),
                attrs,
                text: None,
                children: Vec::new(),
            }],
            rules,
        }
    }

    fn push(&mut self, node: Node) {
        self.stack
            .last_mut()
            .expect("the root is never popped")
            .children
            .push(node);
    }

    /// `<name attrs>text</name>`.
    pub fn leaf(&mut self, name: &str, attrs: &[(&str, &str)], text: &str) {
        self.push(Node {
            name: name.to_owned(),
            attrs: attrs
                .iter()
                .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
                .collect(),
            text: Some(text.to_owned()),
            children: Vec::new(),
        });
    }

    /// Run `f` inside `name`, emitting nothing at all if `f` writes nothing.
    ///
    /// Both syntaxes treat an empty aggregate as present-but-blank and several
    /// rules count occurrences, so a wrapper around no children is not merely
    /// untidy — it changes what the document asserts.
    pub fn group(&mut self, name: &str, f: impl FnOnce(&mut Self)) {
        self.stack.push(Node {
            name: name.to_owned(),
            attrs: Vec::new(),
            text: None,
            children: Vec::new(),
        });
        f(self);
        let node = self.stack.pop().expect("group pushed a node");
        if !node.children.is_empty() {
            self.push(node);
        }
    }

    /// Like [`Xml::group`], but emitted even when empty.
    ///
    /// CII's `rsm:ExchangedDocumentContext` and `ram:ApplicableHeaderTradeDelivery`
    /// are **mandatory in the D16B sequence** and may legitimately have no
    /// children — a minimal invoice delivers nothing and says nothing about a
    /// process. Pruning them produces a document that fails schema validation
    /// while looking tidier, so the two cases are distinguished at the call
    /// site rather than guessed at here.
    ///
    /// CII-only: UBL has no aggregate that is both mandatory and legitimately
    /// empty, so gating this keeps the `ubl`-only build free of dead code
    /// rather than merely free of a warning about it.
    #[cfg(feature = "cii")]
    pub fn group_required(&mut self, name: &str, f: impl FnOnce(&mut Self)) {
        self.stack.push(Node {
            name: name.to_owned(),
            attrs: Vec::new(),
            text: None,
            children: Vec::new(),
        });
        f(self);
        let node = self.stack.pop().expect("group pushed a node");
        self.push(node);
    }

    /// Serialise, ordering every level and enforcing the prohibitions.
    ///
    /// Returns the document and everything the syntax could not carry.
    pub fn finish(mut self) -> (String, Vec<String>) {
        let root = self.stack.pop().expect("the root");
        debug_assert!(self.stack.is_empty(), "unbalanced group()");
        let mut out = String::with_capacity(4096);
        out.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        let mut dropped = Vec::new();
        let name = root.name.clone();
        render(&root, &name, 0, &mut out, &mut dropped, self.rules);
        (out, dropped)
    }
}

/// Does a document path fall under `context` with `relative` beneath it?
///
/// A context beginning with a single `/` anchors at the document element; `//`
/// or a bare name matches at any depth. That distinction is load-bearing:
/// `/ubl:Invoice` + `not(cbc:UUID)` forbids a `cbc:UUID` **child of the
/// document element**, and treating it as floating would forbid the element
/// wherever it appears.
///
/// The document element is compared by local name, because a writer using a
/// default namespace emits `Invoice` where the Schematron writes `ubl:Invoice`.
/// Everything below it is compared qualified, as both sides spell it.
pub(crate) fn path_matches(path: &str, context: &str, relative: &str) -> bool {
    let floating = context.starts_with("//") || !context.starts_with('/');
    let ctx = context.trim_start_matches('/');
    if floating {
        let needle = format!("{ctx}/{relative}");
        return path == needle || path.ends_with(&format!("/{needle}"));
    }
    // Anchored: the whole path must be the context element followed by the
    // relative path, and only the first segment is matched loosely.
    let Some((head, rest)) = path.split_once('/') else {
        return false;
    };
    local(head) == local(ctx) && rest == relative
}

/// The local name, as the order tables key them — `cac:Party` → `Party`.
fn local(name: &str) -> &str {
    name.rsplit(':').next().unwrap_or(name)
}

/// Sort `children` into sequence order, reporting any the sequence cannot place.
///
/// Repeated elements keep the order they were written in: `sort_by_key` is
/// stable, so two invoice lines stay in line order.
fn order_children(parent: &str, children: &mut Vec<Node>, dropped: &mut Vec<String>, r: &Rules) {
    let Some(seq) = (r.order)(local(parent)) else {
        return; // no evidence for this parent; leave the writer's order alone
    };
    children.retain(|c| {
        let known = seq.contains(&local(&c.name));
        if !known {
            // The sequences were derived from the authorities' own instances.
            // An element absent from one is an element no published document
            // places there — `cac:ProjectReference` under `<CreditNote>`,
            // because UBL's credit note has no such element. Emitting it anyway
            // produces a document the counterparty's schema rejects, which is
            // worse than dropping it and saying so.
            dropped.push(format!("{}/{}", local(parent), c.name));
        }
        known
    });
    children.sort_by_key(|c| {
        seq.iter()
            .position(|e| *e == local(&c.name))
            .unwrap_or(usize::MAX)
    });
}

fn render(
    node: &Node,
    path: &str,
    depth: usize,
    out: &mut String,
    dropped: &mut Vec<String>,
    r: &Rules,
) {
    for _ in 0..depth {
        out.push_str("  ");
    }
    let _ = write!(out, "<{}", node.name);
    for (k, v) in &node.attrs {
        if let Some(rule) = (r.forbidden_attribute)(k) {
            dropped.push(format!("{path}/@{k} ({rule})"));
            continue;
        }
        let _ = write!(out, " {k}=\"");
        escape(v, out);
        out.push('"');
    }
    if node.children.is_empty() {
        // A required-but-empty aggregate serialises as `<x/>` rather than
        // `<x></x>`: both are the same infoset, and the short form is what
        // every producer in this field emits.
        if node.text.is_none() {
            let _ = writeln!(out, "/>");
            return;
        }
        out.push('>');
        escape(node.text.as_deref().unwrap_or_default(), out);
        let _ = writeln!(out, "</{}>", node.name);
        return;
    }
    let _ = writeln!(out, ">");
    let mut kids = node.children.clone();
    order_children(&node.name, &mut kids, dropped, r);
    for c in &kids {
        let child_path = if path.is_empty() {
            c.name.clone()
        } else {
            format!("{path}/{}", c.name)
        };
        // Enforcing here rather than trusting every call site is what makes
        // "the writer cannot emit a forbidden element" a property instead of a
        // habit — `cbc:CompanyLegalForm` is BT-33, the *seller's*, and
        // `UBL-CR-244` forbids it on the customer, which a hand-written writer
        // got wrong.
        if let Some(rule) = (r.forbidden_path)(&child_path) {
            dropped.push(format!("{child_path} ({rule})"));
            continue;
        }
        render(c, &child_path, depth + 1, out, dropped, r);
    }
    for _ in 0..depth {
        out.push_str("  ");
    }
    let _ = writeln!(out, "</{}>", node.name);
}

/// Escape the five metacharacters, and drop control characters XML 1.0 cannot
/// represent at all.
pub(crate) fn escape(s: &str, out: &mut String) {
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&apos;"),
            c if (c as u32) < 0x20 && !matches!(c, '\t' | '\n' | '\r') => {}
            c => out.push(c),
        }
    }
}

/// Base64, RFC 4648 §4, no line breaks.
///
/// Fifteen lines rather than a dependency, and both syntaxes need it: BT-125
/// carries an attachment's bytes inline.
pub(crate) fn base64(bytes: &[u8]) -> String {
    const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for c in bytes.chunks(3) {
        let b = [c[0], *c.get(1).unwrap_or(&0), *c.get(2).unwrap_or(&0)];
        let n = u32::from(b[0]) << 16 | u32::from(b[1]) << 8 | u32::from(b[2]);
        out.push(A[(n >> 18 & 63) as usize] as char);
        out.push(A[(n >> 12 & 63) as usize] as char);
        out.push(if c.len() > 1 {
            A[(n >> 6 & 63) as usize] as char
        } else {
            '='
        });
        out.push(if c.len() > 2 {
            A[(n & 63) as usize] as char
        } else {
            '='
        });
    }
    out
}

/// Decode base64, RFC 4648 §4, ignoring whitespace.
///
/// Handing the *encoded* text back as an attachment's content is a bug that
/// survives every schema check and every rule: the document is valid, the
/// attachment is present, and the bytes are wrong. Only a round-trip finds it.
pub(crate) fn decode_base64(s: &str) -> Vec<u8> {
    let mut out = Vec::with_capacity(s.len() / 4 * 3);
    let mut acc: u32 = 0;
    let mut bits = 0u32;
    for c in s.bytes() {
        let v = match c {
            b'A'..=b'Z' => c - b'A',
            b'a'..=b'z' => c - b'a' + 26,
            b'0'..=b'9' => c - b'0' + 52,
            b'+' => 62,
            b'/' => 63,
            b'=' => break,
            _ => continue, // whitespace and line breaks are legal in XML text
        };
        acc = acc << 6 | u32::from(v);
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            // Truncation is the decode: six-bit groups are reassembled into
            // bytes, and the high bits shifted past are the previous byte's.
            #[allow(clippy::cast_possible_truncation)]
            out.push((acc >> bits) as u8);
        }
    }
    out
}

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

    static NO_RULES: Rules = Rules {
        order: |_| None,
        forbidden_path: |_| None,
        forbidden_attribute: |_| None,
    };

    #[test]
    fn base64_matches_rfc_4648_vectors() {
        for (plain, encoded) in [
            (&b""[..], ""),
            (b"f", "Zg=="),
            (b"fo", "Zm8="),
            (b"foo", "Zm9v"),
            (b"foob", "Zm9vYg=="),
            (b"fooba", "Zm9vYmE="),
            (b"foobar", "Zm9vYmFy"),
        ] {
            assert_eq!(base64(plain), encoded);
            assert_eq!(decode_base64(encoded), plain, "round trip {encoded}");
        }
    }

    #[test]
    fn decoding_ignores_the_whitespace_xml_permits() {
        assert_eq!(decode_base64("Zm9v\n  YmFy"), b"foobar");
    }

    #[test]
    fn text_is_escaped() {
        let mut s = String::new();
        escape("x<y & \"z\" 'q'\u{7}", &mut s);
        assert_eq!(s, "x&lt;y &amp; &quot;z&quot; &apos;q&apos;");
    }

    #[test]
    fn empty_groups_vanish() {
        let mut x = Xml::new("Root", vec![], &NO_RULES);
        x.group("a:Empty", |_| {});
        let (xml, dropped) = x.finish();
        assert!(!xml.contains("Empty"), "{xml}");
        assert!(dropped.is_empty());
    }

    #[cfg(feature = "cii")]
    #[test]
    fn a_required_group_survives_being_empty() {
        let mut x = Xml::new("Root", vec![], &NO_RULES);
        x.group_required("a:Mandatory", |_| {});
        x.group("a:Optional", |_| {});
        let (xml, dropped) = x.finish();
        assert!(xml.contains("<a:Mandatory/>"), "{xml}");
        assert!(!xml.contains("Optional"), "{xml}");
        assert!(dropped.is_empty());
    }

    #[test]
    fn an_anchored_context_matches_only_at_the_root() {
        assert!(path_matches("Invoice/cbc:UUID", "/ubl:Invoice", "cbc:UUID"));
        // A default-namespace writer emits `Invoice`; the Schematron writes
        // `ubl:Invoice`. Only the document element is matched loosely.
        assert!(path_matches(
            "ubl:Invoice/cbc:UUID",
            "/ubl:Invoice",
            "cbc:UUID"
        ));
        assert!(!path_matches(
            "Invoice/cac:Party/cbc:UUID",
            "/ubl:Invoice",
            "cbc:UUID"
        ));
        assert!(!path_matches(
            "CreditNote/cbc:UUID",
            "/ubl:Invoice",
            "cbc:UUID"
        ));
    }

    #[test]
    fn a_floating_context_matches_at_any_depth() {
        assert!(path_matches(
            "Invoice/cac:Party/cbc:X",
            "//cac:Party",
            "cbc:X"
        ));
        assert!(path_matches("cac:Party/cbc:X", "//cac:Party", "cbc:X"));
        assert!(path_matches("A/B/cac:Party/cbc:X", "cac:Party", "cbc:X"));
        // Element boundaries are respected — no partial-name match.
        assert!(!path_matches(
            "Invoice/cac:MyParty/cbc:X",
            "//cac:Party",
            "cbc:X"
        ));
    }

    /// With no order table for a parent, the writer's own order is preserved
    /// rather than mangled — the fallback must be inert, not lossy.
    #[test]
    fn an_unknown_parent_keeps_the_writers_order() {
        let mut x = Xml::new("Root", vec![], &NO_RULES);
        x.leaf("a:Second", &[], "2");
        x.leaf("a:First", &[], "1");
        let (xml, dropped) = x.finish();
        assert!(xml.find("Second").unwrap() < xml.find("First").unwrap());
        assert!(dropped.is_empty());
    }
}