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
//! **European e-invoicing formats**, on top of the EN 16931 semantic model.
//!
//! ```text
//! ┌──────────────┐ ┌──────────────────────┐
//! │ billing │ │ inbound documents │
//! │ calculations │ │ UBL / CII / EDIFACT │
//! └──────┬───────┘ └──────────┬───────────┘
//! │ adapter (optional feature) │
//! └──────────────┬───────────────────────────┘
//! ▼
//! ┌─────────────────────┐
//! │ en16931 │ semantic model, 316 rules,
//! │ proof of validity │ no XML, no PDF, no I/O
//! └──────────┬──────────┘
//! ▼
//! ┌─────────────────────┐
//! │ en16931-formats │ ← you are here
//! │ UBL · CII · PDF │
//! └─────────────────────┘
//! ```
//!
//! [`en16931`] decides whether an invoice is **correct**. This crate decides
//! what it looks like **on the wire**, and re-implements not one of the 316
//! rules.
//!
//! # Why one crate, not one per format
//!
//! XRechnung is carried in UBL *and* CII; every ZUGFeRD payload is CII. A crate
//! per format would need the CII binding twice, and two bindings drift. Cargo
//! features already express which syntax a consumer wants, so a crate boundary
//! here would be solving with a package what `--no-default-features` solves for
//! free.
//!
//! What *is* a separate crate is [`en16931`], and that boundary is
//! load-bearing: this crate depends on it, so rustc forbids the reverse. "The
//! semantic rules do not depend on a syntax" is enforced rather than asked for,
//! and `en16931`'s dependency graph stays at ten crates and builds for
//! `wasm32`.
//!
//! # Features, and what each one costs
//!
//! | Feature | Default | Graph | What |
//! |---|---|---|---|
//! | [`ubl`] | ✅ | 13 crates | UBL 2.1, both directions |
//! | [`cii`] | — | 13 crates | UN/CEFACT CII D16B, both directions |
//! | [`zugferd`] | — | **57 crates** | ZUGFeRD / Factur-X hybrid PDFs |
//! | `render` | — | + a typesetting engine | Corporate design — **not yet implemented** |
//!
//! `zugferd` is off by default and that matters: `lopdf` brings AES, ChaCha20,
//! SHA-2, `getrandom` and `libc`, and the result does not build for
//! `wasm32-unknown-unknown`. Nobody reading a UBL invoice should pay for that.
//!
//! # The 91 % that costs a writer nothing
//!
//! CEN's artefacts carry **1 339** syntax rules, and **1 220 of them (91 %)**
//! say some element "shall not be used" — they fence off the parts of UBL 2.1
//! and CII D16B that EN 16931 does not use. That inverts the usual expectation:
//!
//! * A **writer** driven from the semantic model cannot violate them. It has no
//! way to express `cbc:UUID`, because the model has no term for it. They are
//! *unreachable*, not cheaply satisfied — the same shape as `InvoiceAmount`
//! making `BR-DEC-*` unrepresentable. So the writer answers to roughly **119**
//! real rules.
//! * A **reader** must cope with all 1 339, because the document came from
//! somewhere else.
//!
//! Unreachability is a claim, so the serialiser enforces it against the
//! prohibitions extracted from CEN's own Schematron, and `tests/subset.rs`
//! asserts the writer never needs that safety net. See [`ubl::prohibitions`].
//!
//! # Quick start
//!
//! ```
//! # #[cfg(feature = "ubl")] {
//! use en16931::Invoice;
//!
//! let xml = en16931_formats::ubl::to_string(&Invoice::default());
//! let read = en16931_formats::ubl::from_str(&xml).expect("readable");
//! assert!(read.unmapped.is_empty(), "nothing was silently dropped");
//! # }
//! ```
//!
//! # Attribution
//!
//! The bindings are derived from CEN's EUPL-1.2 validation artefacts and the
//! element order from the authorities' published instances. The notice is a
//! licence condition, not decoration, and it has a test.
pub use ;
/// A document was **not** written, because it did not pass the profile it was
/// asked to be written for.
///
/// Returned by [`ubl::to_string_for`] / [`ubl::write_for`] and their CII twins.
///
/// # Why an error type rather than the report itself
///
/// [`en16931::ValidationReport`] is a *product* — you store it, diff it, show
/// it to an operator — and it deliberately does not implement
/// [`std::error::Error`]. `report.is_valid()` being false is an ordinary
/// outcome of validating, not a failure of validation.
///
/// It *is* an error when the thing you asked for was a shippable document. This
/// type is that framing, so `?` works and the message says what happened rather
/// than spilling forty findings into a log line. The report is right there in
/// [`report`](Self::report) when you want it.
///
/// [`ubl::to_string_for`]: crate::ubl::to_string_for
/// [`ubl::write_for`]: crate::ubl::write_for
/// Validate against `profile`, stamping BT-24 from it, or say why not.
///
/// Shared by both syntaxes: the check is about the *model*, so doing it twice
/// would be two places for it to differ.
///
/// The stamp happens **before** validation, not after. `BR-01` requires BT-24
/// and XRechnung's `BR-DE-21` constrains its value, so a document validated
/// carrying the caller's BT-24 and shipped carrying the profile's would have
/// been checked as something other than what it claims to be.
/// The CEN attribution notice, as [`en16931`] carries it.
///
/// Re-exported rather than restated, so the two cannot drift.
pub const ATTRIBUTION: &str = ATTRIBUTION;
/// Which syntax a document is written in.
/// Guess the syntax from the document element, without parsing.
///
/// Returns `None` rather than guessing when the root is neither — "not an
/// e-invoice at all" and "an e-invoice in the other syntax" need different
/// messages, and a caller that cannot tell them apart writes a bad one.