1use std::fmt::Write as _;
11use std::sync::OnceLock;
12
13use super::pins::{self, Pins};
14use crate::{Error, Syntax};
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub(crate) enum Algorithm {
19 Sha256,
20}
21
22#[derive(Debug)]
24pub(crate) struct SourceSpec {
25 pub(crate) what: &'static str,
27 pub(crate) url: &'static str,
29 pub(crate) file: &'static str,
32 pub(crate) algorithm: Algorithm,
33 pub(crate) digest: &'static str,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub(crate) enum Kind {
40 FacturX,
41 XRechnung,
42 FrCtc,
43 Peppol,
44}
45
46#[derive(Debug)]
49pub(crate) struct FamilySpec {
50 pub(crate) kind: Kind,
51 pub(crate) name: &'static str,
53 pub(crate) title: &'static str,
54 pub(crate) pins: Pins,
55 pub(crate) release: &'static str,
58 pub(crate) version: &'static str,
60 pub(crate) publisher: &'static str,
62 pub(crate) sources: &'static [SourceSpec],
64}
65
66#[derive(Debug)]
68pub(crate) struct RuleSetSpec {
69 pub(crate) name: &'static str,
71 pub(crate) title: &'static str,
72 pub(crate) family: &'static FamilySpec,
73 pub(crate) dir: &'static str,
76 pub(crate) syntax: Option<Syntax>,
78}
79
80static FACTURX: FamilySpec = FamilySpec {
83 kind: Kind::FacturX,
84 name: "factur-x",
85 title: "Factur-X",
86 pins: pins::FACTURX,
87 release: "factur-x-{facturx}",
88 version: "{facturx}",
89 publisher: "the Mustang validator {mustang} (org.mustangproject:validator, Maven Central)",
90 sources: &[SourceSpec {
91 what: "the Mustang validator {mustang}",
92 url: "https://repo1.maven.org/maven2/org/mustangproject/validator/{mustang}/validator-{mustang}.jar",
93 file: "mustang-validator-{mustang}.jar",
94 algorithm: Algorithm::Sha256,
95 digest: "mustang_jar_sha256",
96 }],
97};
98
99#[derive(Debug)]
101pub(crate) struct FacturXProfile {
102 pub(crate) dir: &'static str,
104 pub(crate) file: &'static str,
107 pub(crate) xslt: &'static str,
109 pub(crate) codedb: &'static str,
111 pub(crate) schema: [&'static str; 4],
114}
115
116pub(crate) const FACTURX_PROFILES: [FacturXProfile; 5] = [
118 FacturXProfile {
119 dir: "minimum",
120 file: "MINIMUM",
121 xslt: "minimum_xslt_sha256",
122 codedb: "minimum_codedb_sha256",
123 schema: [
124 "minimum_xsd_sha256",
125 "minimum_xsd_qdt_sha256",
126 "minimum_xsd_ram_sha256",
127 "minimum_xsd_udt_sha256",
128 ],
129 },
130 FacturXProfile {
131 dir: "basic-wl",
132 file: "BASIC-WL",
133 xslt: "basic_wl_xslt_sha256",
134 codedb: "basic_wl_codedb_sha256",
135 schema: [
136 "basic_wl_xsd_sha256",
137 "basic_wl_xsd_qdt_sha256",
138 "basic_wl_xsd_ram_sha256",
139 "basic_wl_xsd_udt_sha256",
140 ],
141 },
142 FacturXProfile {
143 dir: "basic",
144 file: "BASIC",
145 xslt: "basic_xslt_sha256",
146 codedb: "basic_codedb_sha256",
147 schema: [
148 "basic_xsd_sha256",
149 "basic_xsd_qdt_sha256",
150 "basic_xsd_ram_sha256",
151 "basic_xsd_udt_sha256",
152 ],
153 },
154 FacturXProfile {
155 dir: "en16931",
156 file: "EN16931",
157 xslt: "en16931_xslt_sha256",
158 codedb: "en16931_codedb_sha256",
159 schema: [
160 "en16931_xsd_sha256",
161 "en16931_xsd_qdt_sha256",
162 "en16931_xsd_ram_sha256",
163 "en16931_xsd_udt_sha256",
164 ],
165 },
166 FacturXProfile {
167 dir: "extended",
168 file: "EXTENDED",
169 xslt: "extended_xslt_sha256",
170 codedb: "extended_codedb_sha256",
171 schema: [
172 "extended_xsd_sha256",
173 "extended_xsd_qdt_sha256",
174 "extended_xsd_ram_sha256",
175 "extended_xsd_udt_sha256",
176 ],
177 },
178];
179
180const CII_FAMILY_MODULES: [&str; 3] = [
183 "QualifiedDataType",
184 "ReusableAggregateBusinessInformationEntity",
185 "UnqualifiedDataType",
186];
187
188pub(crate) fn facturx_schema_documents(file: &str) -> [String; 4] {
193 let name = file.replace('-', "");
194 let module =
195 |m: &str| format!("FACTUR-X_{name}_urn_un_unece_uncefact_data_standard_{m}_100.xsd");
196 [
197 format!("FACTUR-X_{name}.xsd"),
198 module(CII_FAMILY_MODULES[0]),
199 module(CII_FAMILY_MODULES[1]),
200 module(CII_FAMILY_MODULES[2]),
201 ]
202}
203
204static XRECHNUNG: FamilySpec = FamilySpec {
207 kind: Kind::XRechnung,
208 name: "xrechnung",
209 title: "XRechnung",
210 pins: pins::XRECHNUNG,
211 release: "xrechnung-{xrechnung}",
212 version: "{xrechnung}",
213 publisher: "the KoSIT validator configuration {configuration} for XRechnung {xrechnung} (projekte.kosit.org)",
214 sources: &[SourceSpec {
215 what: "the XRechnung {xrechnung} validator configuration {configuration}",
216 url: "https://projekte.kosit.org/api/v4/projects/356/packages/maven/de/xeinkauf/xrechnung-{xrechnung}-validator-configuration/{configuration}/xrechnung-{xrechnung}-validator-configuration-{configuration}.zip",
217 file: "xrechnung-{xrechnung}-validator-configuration-{configuration}.zip",
218 algorithm: Algorithm::Sha256,
219 digest: "configuration_sha256",
220 }],
221};
222
223pub(crate) const XRECHNUNG_STYLESHEETS: [(&str, &str, &str, &str); 4] = [
226 (
227 "EN16931-UBL",
228 "resources/ubl/2.1/xsl/EN16931-UBL-validation.xsl",
229 "cen_ubl_sha256",
230 "cen",
231 ),
232 (
233 "XRechnung-UBL",
234 "resources/xrechnung/{xrechnung}/xsl/XRechnung-UBL-validation.xsl",
235 "xrechnung_ubl_sha256",
236 "schematron",
237 ),
238 (
239 "EN16931-CII",
240 "resources/cii/16b/xsl/EN16931-CII-validation.xsl",
241 "cen_cii_sha256",
242 "cen",
243 ),
244 (
245 "XRechnung-CII",
246 "resources/xrechnung/{xrechnung}/xsl/XRechnung-CII-validation.xsl",
247 "xrechnung_cii_sha256",
248 "schematron",
249 ),
250];
251
252const XRECHNUNG_UBL_INVOICE_NAMESPACES: &str = "cbc=urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2 invoice=urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 rep=http://www.xoev.de/de/validator/varl/1";
255const XRECHNUNG_UBL_CREDIT_NOTE_NAMESPACES: &str = "cbc=urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2 creditnote=urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2 rep=http://www.xoev.de/de/validator/varl/1";
257const XRECHNUNG_CII_NAMESPACES: &str = "rsm=urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100 ram=urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 rep=http://www.xoev.de/de/validator/varl/1";
259
260type XRechnungScenario = (
265 &'static str,
266 &'static str,
267 &'static str,
268 &'static str,
269 &'static str,
270 &'static str,
271 &'static str,
272 &'static str,
273);
274
275pub(crate) const XRECHNUNG_SCENARIOS: [XRechnungScenario; 11] = [
279 (
280 "ubl",
281 "EN16931 XRechnung (UBL Invoice)",
282 XRECHNUNG_UBL_INVOICE_NAMESPACES,
283 "exists(/invoice:Invoice/cbc:CustomizationID[ . = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0']) ",
284 "EN16931-UBL XRechnung-UBL",
285 "UBL-CR-646",
286 "BR-CL-23 BR-CL-21",
287 "",
288 ),
289 (
290 "ubl",
291 "EN16931 XRechnung Extension (UBL Invoice)",
292 XRECHNUNG_UBL_INVOICE_NAMESPACES,
293 "exists(/invoice:Invoice/cbc:CustomizationID[ . = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0#conformant#urn:xeinkauf.de:kosit:extension:xrechnung_3.0'])",
294 "EN16931-UBL XRechnung-UBL",
295 "",
296 "BR-CL-23",
297 "BR-CL-21 BR-CL-24 BR-CL-10 BR-CL-11 BR-CL-25 BR-CL-26 BR-CO-16 UBL-CR-470 UBL-CR-646",
298 ),
299 (
300 "ubl",
301 "EN16931 XRechnung CVD (UBL Invoice)",
302 XRECHNUNG_UBL_INVOICE_NAMESPACES,
303 "exists(/invoice:Invoice/cbc:CustomizationID[ . = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0#compliant#urn:xeinkauf.de:kosit:xrechnung:cvd_0.9']) ",
304 "EN16931-UBL XRechnung-UBL",
305 "UBL-CR-646",
306 "BR-CL-23 BR-CL-21",
307 "BR-CL-13",
308 ),
309 (
310 "ubl",
311 "EN16931 XRechnung (UBL CreditNote)",
312 XRECHNUNG_UBL_CREDIT_NOTE_NAMESPACES,
313 "exists(/creditnote:CreditNote[ cbc:CustomizationID/text() = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0'])",
314 "EN16931-UBL XRechnung-UBL",
315 "",
316 "BR-CL-23 BR-CL-21",
317 "",
318 ),
319 (
320 "ubl",
321 "EN16931 XRechnung CVD (UBL CreditNote)",
322 XRECHNUNG_UBL_CREDIT_NOTE_NAMESPACES,
323 "exists(/creditnote:CreditNote[ cbc:CustomizationID/text() = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0#compliant#urn:xeinkauf.de:kosit:xrechnung:cvd_0.9'])",
324 "EN16931-UBL XRechnung-UBL",
325 "UBL-CR-646",
326 "BR-CL-23 BR-CL-21",
327 "BR-CL-13",
328 ),
329 (
330 "cii",
331 "EN16931 XRechnung (CII)",
332 XRECHNUNG_CII_NAMESPACES,
333 "exists(/rsm:CrossIndustryInvoice[rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID/text() = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0'])",
334 "EN16931-CII XRechnung-CII",
335 "CII-SR-452 CII-SR-453 CII-SR-454 CII-SR-465 CII-SR-466",
336 "BR-CL-23 BR-CL-21",
337 "CII-SR-475 CII-SR-476",
338 ),
339 (
340 "cii",
341 "EN16931 XRechnung Extension (CII)",
342 XRECHNUNG_CII_NAMESPACES,
343 "exists(/rsm:CrossIndustryInvoice[rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID/text() = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0#conformant#urn:xeinkauf.de:kosit:extension:xrechnung_3.0'])",
344 "EN16931-CII XRechnung-CII",
345 "CII-SR-452 CII-SR-453 CII-SR-454 CII-SR-465 CII-SR-466",
346 "BR-CL-23",
347 "BR-CL-21 BR-CL-11 BR-CL-10 BR-CL-24 BR-CL-25 BR-CL-26 CII-SR-475 CII-SR-476",
348 ),
349 (
350 "cii",
351 "EN16931 XRechnung CVD (CII)",
352 XRECHNUNG_CII_NAMESPACES,
353 "exists(/rsm:CrossIndustryInvoice[rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID/text() = 'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0#compliant#urn:xeinkauf.de:kosit:xrechnung:cvd_0.9'])",
354 "EN16931-CII XRechnung-CII",
355 "CII-SR-452 CII-SR-453 CII-SR-454 CII-SR-465 CII-SR-466",
356 "BR-CL-23 BR-CL-21",
357 "BR-CL-13 CII-SR-475 CII-SR-476",
358 ),
359 (
360 "ubl",
361 "EN16931 (UBL Invoice)",
362 XRECHNUNG_UBL_INVOICE_NAMESPACES,
363 "exists(/invoice:Invoice[ cbc:CustomizationID/text() = 'urn:cen.eu:en16931:2017' ])",
364 "EN16931-UBL",
365 "",
366 "",
367 "",
368 ),
369 (
370 "ubl",
371 "EN16931 (UBL CreditNote)",
372 XRECHNUNG_UBL_CREDIT_NOTE_NAMESPACES,
373 "exists(/creditnote:CreditNote[cbc:CustomizationID/text() = 'urn:cen.eu:en16931:2017'])",
374 "EN16931-UBL",
375 "",
376 "",
377 "",
378 ),
379 (
380 "cii",
381 "EN16931 (CII)",
382 XRECHNUNG_CII_NAMESPACES,
383 "exists(/rsm:CrossIndustryInvoice[ rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID/text() = 'urn:cen.eu:en16931:2017'])",
384 "EN16931-CII",
385 "",
386 "",
387 "",
388 ),
389];
390
391static FR_CTC: FamilySpec = FamilySpec {
394 kind: Kind::FrCtc,
395 name: "fr-ctc",
396 title: "French e-invoicing rules (FNFE-MPE)",
397 pins: pins::FR_CTC,
398 release: "fr-ctc-{pack}",
399 version: "{pack}",
400 publisher: "the FNFE-MPE package Schematrons FR CTC {pack} (fnfe-mpe.org)",
401 sources: &[SourceSpec {
402 what: "the FNFE-MPE package {pack}",
403 url: "https://fnfe-mpe.org/wp-content/uploads/2026/09/2026_09_04_FNFE_SCHEMATRONS_FR_CTC_V{pack}.zip",
404 file: "2026_09_04_FNFE_SCHEMATRONS_FR_CTC_V{pack}.zip",
405 algorithm: Algorithm::Sha256,
406 digest: "pack_sha256",
407 }],
408};
409
410#[cfg(feature = "fetch")]
412pub(crate) const FR_CTC_PACK_DIR: &str = "2026_09_04_FNFE_SCHEMATRONS_FR_CTC_V{pack}";
413
414pub(crate) const FR_CTC_STYLESHEETS: [(&str, &str, &str, Option<&str>); 14] = [
418 (
419 "EN16931-CII",
420 "CII/EN16931/2xslt/EN16931-CII-validation.xslt",
421 "cii_en16931_sha256",
422 None,
423 ),
424 (
425 "EXTENDED-CTC-FR-CII",
426 "CII/EXTENDED-CTC-FR/2xslt/EXTENDED-CTC-FR-CII.xslt",
427 "cii_extended_ctc_fr_sha256",
428 None,
429 ),
430 (
431 "BR-FR-Flux2-CII",
432 "CII/EN16931/2xslt/BR-FR-Flux2-Schematron-CII.xslt",
433 "cii_br_fr_sha256",
434 None,
435 ),
436 (
437 "BR-FR-Flux2-CII_WARNING",
438 "CII/EN16931/2xslt/BR-FR-Flux2-Schematron-CII_WARNING.xslt",
439 "cii_br_fr_warning_sha256",
440 None,
441 ),
442 (
443 "EN16931-UBL",
444 "UBL/EN16931/2xslt/EN16931-UBL-validation.xslt",
445 "ubl_en16931_sha256",
446 None,
447 ),
448 (
449 "EXTENDED-CTC-FR-UBL",
450 "UBL/EXTENDED-CTC-FR/2xslt/EXTENDED-CTC-FR-UBL.xslt",
451 "ubl_extended_ctc_fr_sha256",
452 None,
453 ),
454 (
455 "BR-FR-Flux2-UBL",
456 "UBL/EN16931/2xslt/BR-FR-Flux2-Schematron-UBL.xslt",
457 "ubl_br_fr_sha256",
458 None,
459 ),
460 (
461 "BR-FR-Flux2-UBL_WARNING",
462 "UBL/EN16931/2xslt/BR-FR-Flux2-Schematron-UBL_WARNING.xslt",
463 "ubl_br_fr_warning_sha256",
464 None,
465 ),
466 (
467 "BR-FR-CDV-CDAR",
468 "CDAR/2xslt/BR-FR-CDV-Schematron-CDAR.xslt",
469 "cdar_sha256",
470 None,
471 ),
472 (
473 "BR-FR-CDV-CDAR_WARNING",
474 "CDAR/2xslt/BR-FR-CDV-Schematron-CDAR_WARNING.xslt",
475 "cdar_warning_sha256",
476 None,
477 ),
478 ("Flux10", "Flux10/Flux10.xslt", "flux10_sha256", None),
479 (
480 "FACTUR-X_BASIC-WL",
481 "Factur-X_1.09.2.fixFR04/BASICWL/2xslt/FACTUR-X_BASIC-WL.xslt",
482 "fx_basic_wl_sha256",
483 Some("fx_basic_wl_codedb_sha256"),
484 ),
485 (
486 "FACTUR-X_EN16931",
487 "Factur-X_1.09.2.fixFR04/EN16931/2xslt/FACTUR-X_EN16931.xslt",
488 "fx_en16931_sha256",
489 Some("fx_en16931_codedb_sha256"),
490 ),
491 (
492 "FACTUR-X_EXTENDED",
493 "Factur-X_1.09.2.fixFR04/EXTENDED/2xslt/FACTUR-X_EXTENDED.xslt",
494 "fx_extended_sha256",
495 Some("fx_extended_codedb_sha256"),
496 ),
497];
498
499pub(crate) const FR_CTC_SCHEMAS: [(&str, [(&str, &str); 4]); 5] = [
503 (
504 "cii-d22b",
505 [
506 (
507 "CII/1xsd-CII_D22B_uncoupled/CrossIndustryInvoice_100pD22B.xsd",
508 "cii_xsd_sha256",
509 ),
510 (
511 "CII/1xsd-CII_D22B_uncoupled/CrossIndustryInvoice_100pD22B_urn_un_unece_uncefact_data_standard_QualifiedDataType_100.xsd",
512 "cii_xsd_qdt_sha256",
513 ),
514 (
515 "CII/1xsd-CII_D22B_uncoupled/CrossIndustryInvoice_100pD22B_urn_un_unece_uncefact_data_standard_ReusableAggregateBusinessInformationEntity_100.xsd",
516 "cii_xsd_ram_sha256",
517 ),
518 (
519 "CII/1xsd-CII_D22B_uncoupled/CrossIndustryInvoice_100pD22B_urn_un_unece_uncefact_data_standard_UnqualifiedDataType_100.xsd",
520 "cii_xsd_udt_sha256",
521 ),
522 ],
523 ),
524 (
525 "cdar-d22b",
526 [
527 (
528 "CDAR/1xsd-CDAR_D22B_uncoupled/CrossDomainAcknowledgementAndResponse_100pD22B.xsd",
529 "cdar_xsd_sha256",
530 ),
531 (
532 "CDAR/1xsd-CDAR_D22B_uncoupled/CrossDomainAcknowledgementAndResponse_100pD22B_urn_un_unece_uncefact_data_standard_QualifiedDataType_100.xsd",
533 "cdar_xsd_qdt_sha256",
534 ),
535 (
536 "CDAR/1xsd-CDAR_D22B_uncoupled/CrossDomainAcknowledgementAndResponse_100pD22B_urn_un_unece_uncefact_data_standard_ReusableAggregateBusinessInformationEntity_100.xsd",
537 "cdar_xsd_ram_sha256",
538 ),
539 (
540 "CDAR/1xsd-CDAR_D22B_uncoupled/CrossDomainAcknowledgementAndResponse_100pD22B_urn_un_unece_uncefact_data_standard_UnqualifiedDataType_100.xsd",
541 "cdar_xsd_udt_sha256",
542 ),
543 ],
544 ),
545 (
546 "factur-x-basic-wl",
547 [
548 (
549 "Factur-X_1.09.2.fixFR04/BASICWL/1xsd/Factur-X_BASICWL.xsd",
550 "fx_basic_wl_xsd_sha256",
551 ),
552 (
553 "Factur-X_1.09.2.fixFR04/BASICWL/1xsd/Factur-X_1.09.2_BASICWL_urn_un_unece_uncefact_data_standard_QualifiedDataType_100.xsd",
554 "fx_basic_wl_xsd_qdt_sha256",
555 ),
556 (
557 "Factur-X_1.09.2.fixFR04/BASICWL/1xsd/Factur-X_1.09.2_BASICWL_urn_un_unece_uncefact_data_standard_ReusableAggregateBusinessInformationEntity_100.xsd",
558 "fx_basic_wl_xsd_ram_sha256",
559 ),
560 (
561 "Factur-X_1.09.2.fixFR04/BASICWL/1xsd/Factur-X_1.09.2_BASICWL_urn_un_unece_uncefact_data_standard_UnqualifiedDataType_100.xsd",
562 "fx_basic_wl_xsd_udt_sha256",
563 ),
564 ],
565 ),
566 (
567 "factur-x-en16931",
568 [
569 (
570 "Factur-X_1.09.2.fixFR04/EN16931/1xsd/Factur-X_EN16931.xsd",
571 "fx_en16931_xsd_sha256",
572 ),
573 (
574 "Factur-X_1.09.2.fixFR04/EN16931/1xsd/Factur-X_1.09.2_EN16931_urn_un_unece_uncefact_data_standard_QualifiedDataType_100.xsd",
575 "fx_en16931_xsd_qdt_sha256",
576 ),
577 (
578 "Factur-X_1.09.2.fixFR04/EN16931/1xsd/Factur-X_1.09.2_EN16931_urn_un_unece_uncefact_data_standard_ReusableAggregateBusinessInformationEntity_100.xsd",
579 "fx_en16931_xsd_ram_sha256",
580 ),
581 (
582 "Factur-X_1.09.2.fixFR04/EN16931/1xsd/Factur-X_1.09.2_EN16931_urn_un_unece_uncefact_data_standard_UnqualifiedDataType_100.xsd",
583 "fx_en16931_xsd_udt_sha256",
584 ),
585 ],
586 ),
587 (
588 "factur-x-extended",
589 [
590 (
591 "Factur-X_1.09.2.fixFR04/EXTENDED/1xsd/Factur-X_EXTENDED.xsd",
592 "fx_extended_xsd_sha256",
593 ),
594 (
595 "Factur-X_1.09.2.fixFR04/EXTENDED/1xsd/Factur-X_1.09.2_EXTENDED_urn_un_unece_uncefact_data_standard_QualifiedDataType_100.xsd",
596 "fx_extended_xsd_qdt_sha256",
597 ),
598 (
599 "Factur-X_1.09.2.fixFR04/EXTENDED/1xsd/Factur-X_1.09.2_EXTENDED_urn_un_unece_uncefact_data_standard_ReusableAggregateBusinessInformationEntity_100.xsd",
600 "fx_extended_xsd_ram_sha256",
601 ),
602 (
603 "Factur-X_1.09.2.fixFR04/EXTENDED/1xsd/Factur-X_1.09.2_EXTENDED_urn_un_unece_uncefact_data_standard_UnqualifiedDataType_100.xsd",
604 "fx_extended_xsd_udt_sha256",
605 ),
606 ],
607 ),
608];
609
610#[derive(Debug, Clone, Copy, PartialEq, Eq)]
613pub(crate) enum FrCtcSchema {
614 None,
615 Ubl21,
616 Package(&'static str),
617}
618
619pub(crate) fn fr_ctc_schema(
621 name: &str,
622) -> Result<&'static [(&'static str, &'static str); 4], String> {
623 FR_CTC_SCHEMAS
624 .iter()
625 .find(|(n, _)| *n == name)
626 .map(|(_, documents)| documents)
627 .ok_or_else(|| format!("no schema {name} of the French package"))
628}
629
630pub(crate) const FR_CTC_BUNDLES: [(&str, FrCtcSchema, &str, &[&str]); 14] = [
635 (
636 "cii-en16931",
637 FrCtcSchema::Package("cii-d22b"),
638 "EN 16931 CII and the French rules",
639 &["EN16931-CII", "BR-FR-Flux2-CII"],
640 ),
641 (
642 "cii-en16931-warning",
643 FrCtcSchema::Package("cii-d22b"),
644 "EN 16931 CII and the French rules (WARNING version)",
645 &["EN16931-CII", "BR-FR-Flux2-CII_WARNING"],
646 ),
647 (
648 "cii-extended-ctc-fr",
649 FrCtcSchema::Package("cii-d22b"),
650 "EXTENDED-CTC-FR CII and the French rules",
651 &["EXTENDED-CTC-FR-CII", "BR-FR-Flux2-CII"],
652 ),
653 (
654 "cii-extended-ctc-fr-warning",
655 FrCtcSchema::Package("cii-d22b"),
656 "EXTENDED-CTC-FR CII and the French rules (WARNING version)",
657 &["EXTENDED-CTC-FR-CII", "BR-FR-Flux2-CII_WARNING"],
658 ),
659 (
660 "ubl-en16931",
661 FrCtcSchema::Ubl21,
662 "EN 16931 UBL and the French rules",
663 &["EN16931-UBL", "BR-FR-Flux2-UBL"],
664 ),
665 (
666 "ubl-en16931-warning",
667 FrCtcSchema::Ubl21,
668 "EN 16931 UBL and the French rules (WARNING version)",
669 &["EN16931-UBL", "BR-FR-Flux2-UBL_WARNING"],
670 ),
671 (
672 "ubl-extended-ctc-fr",
673 FrCtcSchema::Ubl21,
674 "EXTENDED-CTC-FR UBL and the French rules",
675 &["EXTENDED-CTC-FR-UBL", "BR-FR-Flux2-UBL"],
676 ),
677 (
678 "ubl-extended-ctc-fr-warning",
679 FrCtcSchema::Ubl21,
680 "EXTENDED-CTC-FR UBL and the French rules (WARNING version)",
681 &["EXTENDED-CTC-FR-UBL", "BR-FR-Flux2-UBL_WARNING"],
682 ),
683 (
684 "cdar",
685 FrCtcSchema::Package("cdar-d22b"),
686 "Life-cycle statuses (CDAR)",
687 &["BR-FR-CDV-CDAR"],
688 ),
689 (
690 "cdar-warning",
691 FrCtcSchema::Package("cdar-d22b"),
692 "Life-cycle statuses (CDAR, WARNING version)",
693 &["BR-FR-CDV-CDAR_WARNING"],
694 ),
695 (
696 "factur-x-basic-wl",
697 FrCtcSchema::Package("factur-x-basic-wl"),
698 "Factur-X BASIC WL and the French rules",
699 &["FACTUR-X_BASIC-WL", "BR-FR-Flux2-CII"],
700 ),
701 (
702 "factur-x-en16931",
703 FrCtcSchema::Package("factur-x-en16931"),
704 "Factur-X EN16931 and the French rules",
705 &["FACTUR-X_EN16931", "BR-FR-Flux2-CII"],
706 ),
707 (
708 "factur-x-extended",
709 FrCtcSchema::Package("factur-x-extended"),
710 "Factur-X EXTENDED and the French rules",
711 &["FACTUR-X_EXTENDED", "BR-FR-Flux2-CII"],
712 ),
713 (
714 "flux10",
715 FrCtcSchema::None,
716 "E-reporting (Flux 10)",
717 &["Flux10"],
718 ),
719];
720
721pub(crate) fn fr_ctc_factur_x(name: &str) -> bool {
724 name.starts_with("FACTUR-X_")
725}
726
727pub(crate) fn fr_ctc_side_file(path: &str) -> Option<String> {
730 let base = path.rsplit('/').next().unwrap_or(path);
731 if base.starts_with("FACTUR-X_") && path.contains('/') {
732 path.strip_suffix(".xslt")
733 .map(|p| format!("{p}_codedb.xml"))
734 } else {
735 None
736 }
737}
738
739static PEPPOL: FamilySpec = FamilySpec {
742 kind: Kind::Peppol,
743 name: "peppol",
744 title: "Peppol BIS Billing",
745 pins: pins::PEPPOL,
746 release: "peppol-bis-billing-ubl-{tag}",
747 version: "{tag}",
748 publisher: "the Peppol BIS Billing {tag} release (OpenPeppol, github.com), its Schematron compiled as ph-schematron {plugin} compiles it, with the ISO Schematron skeleton of ph-schematron-isosch {plugin} (Maven Central)",
749 sources: &[
750 SourceSpec {
751 what: "the Peppol BIS Billing {tag} release archive",
752 url: "https://github.com/OpenPeppol/peppol-bis-invoice-3/archive/refs/tags/{tag}.tar.gz",
753 file: "peppol-bis-invoice-3-{tag}.tar.gz",
754 algorithm: Algorithm::Sha256,
755 digest: "archive_sha256",
756 },
757 SourceSpec {
758 what: "the ISO Schematron skeleton of ph-schematron-isosch {plugin}",
759 url: "https://repo1.maven.org/maven2/com/helger/schematron/ph-schematron-isosch/{plugin}/ph-schematron-isosch-{plugin}.jar",
760 file: "ph-schematron-isosch-{plugin}.jar",
761 algorithm: Algorithm::Sha256,
762 digest: "isosch_sha256",
763 },
764 ],
765};
766
767#[cfg(feature = "fetch")]
770pub(crate) const PEPPOL_STYLESHEETS: [(&str, &str); 2] = [
771 ("CEN-EN16931-UBL", "cen_xslt_sha256"),
772 ("PEPPOL-EN16931-UBL", "peppol_xslt_sha256"),
773];
774
775const fn rule_set(
778 name: &'static str,
779 title: &'static str,
780 family: &'static FamilySpec,
781 dir: &'static str,
782 syntax: Option<Syntax>,
783) -> RuleSetSpec {
784 RuleSetSpec {
785 name,
786 title,
787 family,
788 dir,
789 syntax,
790 }
791}
792
793const CII: Option<Syntax> = Some(Syntax::Cii);
794const UBL: Option<Syntax> = Some(Syntax::Ubl);
795
796pub(crate) static RULE_SETS: [RuleSetSpec; 22] = [
798 rule_set(
799 "factur-x-minimum",
800 "Factur-X MINIMUM",
801 &FACTURX,
802 "minimum",
803 CII,
804 ),
805 rule_set(
806 "factur-x-basic-wl",
807 "Factur-X BASIC-WL",
808 &FACTURX,
809 "basic-wl",
810 CII,
811 ),
812 rule_set("factur-x-basic", "Factur-X BASIC", &FACTURX, "basic", CII),
813 rule_set(
814 "factur-x-en16931",
815 "Factur-X EN16931",
816 &FACTURX,
817 "en16931",
818 CII,
819 ),
820 rule_set(
821 "factur-x-extended",
822 "Factur-X EXTENDED",
823 &FACTURX,
824 "extended",
825 CII,
826 ),
827 rule_set("xrechnung-ubl", "XRechnung UBL", &XRECHNUNG, "ubl", UBL),
828 rule_set("xrechnung-cii", "XRechnung CII", &XRECHNUNG, "cii", CII),
829 rule_set(
830 "fr-ctc-cii-en16931",
831 FR_CTC_BUNDLES[0].2,
832 &FR_CTC,
833 FR_CTC_BUNDLES[0].0,
834 CII,
835 ),
836 rule_set(
837 "fr-ctc-cii-en16931-warning",
838 FR_CTC_BUNDLES[1].2,
839 &FR_CTC,
840 FR_CTC_BUNDLES[1].0,
841 CII,
842 ),
843 rule_set(
844 "fr-ctc-cii-extended-ctc-fr",
845 FR_CTC_BUNDLES[2].2,
846 &FR_CTC,
847 FR_CTC_BUNDLES[2].0,
848 CII,
849 ),
850 rule_set(
851 "fr-ctc-cii-extended-ctc-fr-warning",
852 FR_CTC_BUNDLES[3].2,
853 &FR_CTC,
854 FR_CTC_BUNDLES[3].0,
855 CII,
856 ),
857 rule_set(
858 "fr-ctc-ubl-en16931",
859 FR_CTC_BUNDLES[4].2,
860 &FR_CTC,
861 FR_CTC_BUNDLES[4].0,
862 UBL,
863 ),
864 rule_set(
865 "fr-ctc-ubl-en16931-warning",
866 FR_CTC_BUNDLES[5].2,
867 &FR_CTC,
868 FR_CTC_BUNDLES[5].0,
869 UBL,
870 ),
871 rule_set(
872 "fr-ctc-ubl-extended-ctc-fr",
873 FR_CTC_BUNDLES[6].2,
874 &FR_CTC,
875 FR_CTC_BUNDLES[6].0,
876 UBL,
877 ),
878 rule_set(
879 "fr-ctc-ubl-extended-ctc-fr-warning",
880 FR_CTC_BUNDLES[7].2,
881 &FR_CTC,
882 FR_CTC_BUNDLES[7].0,
883 UBL,
884 ),
885 rule_set(
886 "fr-ctc-cdar",
887 FR_CTC_BUNDLES[8].2,
888 &FR_CTC,
889 FR_CTC_BUNDLES[8].0,
890 None,
891 ),
892 rule_set(
893 "fr-ctc-cdar-warning",
894 FR_CTC_BUNDLES[9].2,
895 &FR_CTC,
896 FR_CTC_BUNDLES[9].0,
897 None,
898 ),
899 rule_set(
900 "fr-ctc-factur-x-basic-wl",
901 FR_CTC_BUNDLES[10].2,
902 &FR_CTC,
903 FR_CTC_BUNDLES[10].0,
904 CII,
905 ),
906 rule_set(
907 "fr-ctc-factur-x-en16931",
908 FR_CTC_BUNDLES[11].2,
909 &FR_CTC,
910 FR_CTC_BUNDLES[11].0,
911 CII,
912 ),
913 rule_set(
914 "fr-ctc-factur-x-extended",
915 FR_CTC_BUNDLES[12].2,
916 &FR_CTC,
917 FR_CTC_BUNDLES[12].0,
918 CII,
919 ),
920 rule_set(
921 "fr-ctc-flux10",
922 FR_CTC_BUNDLES[13].2,
923 &FR_CTC,
924 FR_CTC_BUNDLES[13].0,
925 None,
926 ),
927 rule_set(
928 "peppol-bis-billing-ubl",
929 "Peppol BIS Billing 3.0 UBL",
930 &PEPPOL,
931 "",
932 UBL,
933 ),
934];
935
936#[cfg(any(test, feature = "fetch"))]
938pub(crate) static FAMILIES: [&FamilySpec; 4] = [&FACTURX, &XRECHNUNG, &FR_CTC, &PEPPOL];
939
940pub(crate) type AliasTargets = &'static [(Option<Syntax>, &'static str)];
942
943pub(crate) const ALIASES: [(&str, AliasTargets); 2] = [
946 ("peppol", &[(None, "peppol-bis-billing-ubl")]),
947 (
948 "xrechnung",
949 &[(UBL, "xrechnung-ubl"), (CII, "xrechnung-cii")],
950 ),
951];
952
953#[derive(Debug, Clone, PartialEq, Eq)]
957pub struct Source {
958 what: String,
959 url: String,
960 file: String,
961 algorithm: Algorithm,
962 digest: String,
963}
964
965impl Source {
966 #[must_use]
968 pub fn what(&self) -> &str {
969 &self.what
970 }
971
972 #[must_use]
974 pub fn url(&self) -> &str {
975 &self.url
976 }
977
978 #[must_use]
982 pub fn file(&self) -> &str {
983 &self.file
984 }
985
986 #[must_use]
988 pub const fn algorithm(&self) -> &'static str {
989 match self.algorithm {
990 Algorithm::Sha256 => "SHA-256",
991 }
992 }
993
994 #[cfg(any(test, feature = "fetch"))]
995 pub(crate) const fn algorithm_kind(&self) -> Algorithm {
996 self.algorithm
997 }
998
999 #[must_use]
1001 pub fn digest(&self) -> &str {
1002 &self.digest
1003 }
1004
1005 #[cfg(all(test, feature = "fetch"))]
1007 pub(crate) fn for_test(url: &str, file: &str, sha256: &str) -> Self {
1008 Self {
1009 what: format!("test file {file}"),
1010 url: url.to_owned(),
1011 file: file.to_owned(),
1012 algorithm: Algorithm::Sha256,
1013 digest: sha256.to_owned(),
1014 }
1015 }
1016}
1017
1018#[derive(Debug)]
1023pub struct FetchedRuleSet {
1024 pub(crate) spec: &'static RuleSetSpec,
1025 version: String,
1026 release: String,
1027 publisher: String,
1028 sources: Vec<Source>,
1029 manifest: String,
1030}
1031
1032impl FetchedRuleSet {
1033 #[must_use]
1036 pub const fn name(&self) -> &'static str {
1037 self.spec.name
1038 }
1039
1040 #[must_use]
1042 pub const fn title(&self) -> &'static str {
1043 self.spec.title
1044 }
1045
1046 #[must_use]
1048 pub fn version(&self) -> &str {
1049 &self.version
1050 }
1051
1052 #[must_use]
1055 pub const fn family(&self) -> &'static str {
1056 self.spec.family.name
1057 }
1058
1059 #[must_use]
1061 pub const fn family_title(&self) -> &'static str {
1062 self.spec.family.title
1063 }
1064
1065 #[must_use]
1067 pub fn publisher(&self) -> &str {
1068 &self.publisher
1069 }
1070
1071 #[must_use]
1073 pub fn sources(&self) -> &[Source] {
1074 &self.sources
1075 }
1076
1077 #[must_use]
1079 pub const fn syntax(&self) -> Option<Syntax> {
1080 self.spec.syntax
1081 }
1082
1083 #[must_use]
1087 pub fn bundle_path(&self) -> String {
1088 if self.spec.dir.is_empty() {
1089 self.release.clone()
1090 } else {
1091 format!("{}/{}", self.release, self.spec.dir)
1092 }
1093 }
1094
1095 #[cfg(any(test, feature = "fetch"))]
1097 pub(crate) fn release(&self) -> &str {
1098 &self.release
1099 }
1100
1101 #[must_use]
1103 pub fn manifest(&self) -> &str {
1104 &self.manifest
1105 }
1106}
1107
1108static CATALOGUE: OnceLock<Result<Vec<FetchedRuleSet>, String>> = OnceLock::new();
1109
1110pub fn fetched_rule_sets() -> Result<&'static [FetchedRuleSet], Error> {
1119 CATALOGUE
1120 .get_or_init(resolve_all)
1121 .as_deref()
1122 .map_err(|e| Error::internal(format!("the rule-set catalogue: {e}")))
1123}
1124
1125pub fn fetched_rule_set(
1132 name: &str,
1133 syntax: Option<Syntax>,
1134) -> Result<Option<&'static FetchedRuleSet>, Error> {
1135 let all = fetched_rule_sets()?;
1136 let name = ALIASES
1137 .iter()
1138 .find(|(alias, _)| *alias == name)
1139 .map_or(name, |(_, targets)| {
1140 targets
1141 .iter()
1142 .find(|(s, _)| s.is_some() && *s == syntax)
1143 .or_else(|| targets.first())
1144 .map_or(name, |(_, target)| *target)
1145 });
1146 Ok(all.iter().find(|r| r.name() == name))
1147}
1148
1149#[must_use]
1153pub fn fetched_rule_set_aliases() -> Vec<&'static str> {
1154 ALIASES.iter().map(|(alias, _)| *alias).collect()
1155}
1156
1157#[cfg(any(test, feature = "fetch"))]
1160pub(crate) fn named(name: &str) -> Result<Vec<&'static FetchedRuleSet>, Error> {
1161 let all = fetched_rule_sets()?;
1162 if let Some((_, targets)) = ALIASES.iter().find(|(alias, _)| *alias == name) {
1163 return Ok(all
1164 .iter()
1165 .filter(|r| targets.iter().any(|(_, t)| *t == r.name()))
1166 .collect());
1167 }
1168 if FAMILIES.iter().any(|f| f.name == name) {
1169 return Ok(all.iter().filter(|r| r.family() == name).collect());
1170 }
1171 Ok(all.iter().filter(|r| r.name() == name).collect())
1172}
1173
1174fn resolve_all() -> Result<Vec<FetchedRuleSet>, String> {
1175 RULE_SETS.iter().map(resolve).collect()
1176}
1177
1178fn resolve(spec: &'static RuleSetSpec) -> Result<FetchedRuleSet, String> {
1179 let family = spec.family;
1180 let pins = family.pins;
1181 let sources = family
1182 .sources
1183 .iter()
1184 .map(|s| {
1185 Ok(Source {
1186 what: pins.expand(s.what)?,
1187 url: pins.expand(s.url)?,
1188 file: pins.expand(s.file)?,
1189 algorithm: s.algorithm,
1190 digest: pins.get(s.digest)?.to_owned(),
1191 })
1192 })
1193 .collect::<Result<Vec<_>, String>>()?;
1194 Ok(FetchedRuleSet {
1195 spec,
1196 version: pins.expand(family.version)?,
1197 release: pins.expand(family.release)?,
1198 publisher: pins.expand(family.publisher)?,
1199 sources,
1200 manifest: manifest(spec)?,
1201 })
1202}
1203
1204fn manifest(spec: &RuleSetSpec) -> Result<String, String> {
1208 match spec.family.kind {
1209 Kind::FacturX => manifest_facturx(spec),
1210 Kind::XRechnung => manifest_xrechnung(spec),
1211 Kind::FrCtc => manifest_fr_ctc(spec),
1212 Kind::Peppol => manifest_peppol(spec),
1213 }
1214}
1215
1216fn manifest_facturx(spec: &RuleSetSpec) -> Result<String, String> {
1218 let p = |name: &str| spec.family.pins.get(name);
1219 let profile = FACTURX_PROFILES
1220 .iter()
1221 .find(|profile| profile.dir == spec.dir)
1222 .ok_or_else(|| format!("{}: no Factur-X profile", spec.name))?;
1223 let (dir, file) = (profile.dir, profile.file);
1224 let (facturx, mustang) = (p("facturx")?, p("mustang")?);
1225 let documents = facturx_schema_documents(file);
1226 let entry = &documents[0];
1227 let mut m = format!(
1228 "# Factur-X {facturx}, profile {file}, from the Mustang validator {mustang}: the
1229# rule-set bundle manifest of einvoice-validator, as scripts/fetch-facturx.sh and
1230# einvoice-validate rules fetch write it (einvoice-validate --bundle DIR). The
1231# document is validated first against the XML schema of the profile,
1232# {entry} and the modules it imports, next to it, as the Mustang
1233# validator validates it: a schema error makes it invalid, and the stylesheet
1234# runs all the same. The stylesheet reads, with document(), the code list
1235# FACTUR-X_{file}_codedb.xml next to it, a resource of the bundle. A failed
1236# assert without a flag is fatal, as the Mustang validator reads the report.
1237format = 2
1238name = \"factur-x-{dir}\"
1239title = \"Factur-X {file}\"
1240version = \"{facturx}\"
1241schema = \"{entry}\"
1242
1243[[stylesheet]]
1244name = \"FACTUR-X_{file}\"
1245path = \"FACTUR-X_{file}.xslt\"
1246sha256 = \"{xslt}\"
1247unflagged-assert = \"fatal\"
1248
1249[[resource]]
1250path = \"FACTUR-X_{file}_codedb.xml\"
1251sha256 = \"{codedb}\"
1252",
1253 xslt = p(profile.xslt)?,
1254 codedb = p(profile.codedb)?,
1255 );
1256 for (document, pin) in documents.iter().zip(profile.schema) {
1257 let _ = write!(
1258 m,
1259 "\n[[schema-document]]\npath = \"{document}\"\nsha256 = \"{}\"\n",
1260 p(pin)?
1261 );
1262 }
1263 Ok(m)
1264}
1265
1266fn manifest_xrechnung(spec: &RuleSetSpec) -> Result<String, String> {
1268 let p = |name: &str| spec.family.pins.get(name);
1269 let upper = spec.dir.to_ascii_uppercase();
1270 let mut m = format!(
1271 "# XRechnung {xrechnung} ({upper}), validator configuration {configuration}: the
1272# rule-set bundle manifest of einvoice-validator, as scripts/fetch-xrechnung.sh
1273# and einvoice-validate rules fetch write it (einvoice-validate --bundle DIR).
1274# As the KoSIT validator runs the configuration: a document must match exactly
1275# one of its scenarios (match), which names the stylesheets that run and gives
1276# rule ids levels of their own (customLevel: error is fatal, information
1277# other); a document that matches none is rejected; a failed assert without a
1278# flag is fatal.
1279",
1280 xrechnung = p("xrechnung")?,
1281 configuration = p("configuration")?,
1282 );
1283 if spec.dir == "cii" {
1284 m.push_str(
1285 "# The scenarios validate the documents against the UN/CEFACT CII D16B XML
1286# schema first (CrossIndustryInvoice_100pD16B.xsd): schema = \"cii-d16b\".
1287",
1288 );
1289 }
1290 let _ = write!(
1291 m,
1292 "format = 2\nname = \"xrechnung-{syntax}\"\ntitle = \"XRechnung {upper}\"\nversion = \"{xrechnung}\"\n",
1293 syntax = spec.dir,
1294 xrechnung = p("xrechnung")?,
1295 );
1296 if spec.dir == "ubl" {
1298 m.push_str("schema = \"ubl-2.1\"\n");
1299 } else {
1300 m.push_str("schema = \"cii-d16b\"\n");
1301 }
1302 for (name, path, sha, version) in XRECHNUNG_STYLESHEETS {
1303 if !name.ends_with(&format!("-{upper}")) {
1304 continue;
1305 }
1306 let file = path.rsplit('/').next().unwrap_or(path);
1307 let _ = write!(
1308 m,
1309 "\n[[stylesheet]]\nname = \"{name}\"\npath = \"{file}\"\nsha256 = \"{}\"\nversion = \"{}\"\nunflagged-assert = \"fatal\"\n",
1310 p(sha)?,
1311 p(version)?
1312 );
1313 }
1314 for (syntax, name, namespaces, matching, stylesheets, fatal, warning, other) in
1315 XRECHNUNG_SCENARIOS
1316 {
1317 if syntax != spec.dir {
1318 continue;
1319 }
1320 let _ = write!(
1321 m,
1322 "\n[[scenario]]\nname = \"{name}\"\nnamespaces = \"{namespaces}\"\nmatch = \"{matching}\"\nstylesheets = \"{stylesheets}\"\n"
1323 );
1324 for (key, ids) in [("fatal", fatal), ("warning", warning), ("other", other)] {
1325 if !ids.is_empty() {
1326 let _ = writeln!(m, "{key} = \"{ids}\"");
1327 }
1328 }
1329 }
1330 Ok(m)
1331}
1332
1333fn manifest_fr_ctc(spec: &RuleSetSpec) -> Result<String, String> {
1335 let p = |name: &str| spec.family.pins.get(name);
1336 let (dir, schema, title, names) = FR_CTC_BUNDLES
1337 .iter()
1338 .find(|(dir, ..)| *dir == spec.dir)
1339 .ok_or_else(|| format!("{}: no bundle of the French rules", spec.name))?;
1340 let pack = p("pack")?;
1341 let mut m = format!(
1342 "# {title}, FNFE-MPE package {pack}: the rule-set bundle
1343# manifest of einvoice-validator, as scripts/fetch-fr-ctc.sh and
1344# einvoice-validate rules fetch write it (einvoice-validate --bundle DIR).
1345"
1346 );
1347 let documents: &[(&str, &str)] = match schema {
1349 FrCtcSchema::Package(name) => fr_ctc_schema(name)?,
1350 FrCtcSchema::None | FrCtcSchema::Ubl21 => &[],
1351 };
1352 if let Some((path, _)) = documents.first() {
1353 let (directory, entry) = path.rsplit_once('/').unwrap_or(("", path));
1354 let _ = write!(
1355 m,
1356 "# The document is validated first against the XML schema the package ships in\n# {directory}/: {entry}\n# and the modules it imports, next to it.\n"
1357 );
1358 }
1359 let stylesheets = names
1360 .iter()
1361 .map(|name| {
1362 FR_CTC_STYLESHEETS
1363 .iter()
1364 .find(|(n, ..)| n == name)
1365 .ok_or_else(|| format!("no stylesheet {name}"))
1366 })
1367 .collect::<Result<Vec<_>, String>>()?;
1368 let mut side = Vec::new();
1370 for (_, path, _, codedb) in &stylesheets {
1371 if let Some(file) = fr_ctc_side_file(path) {
1372 let base = file.rsplit('/').next().unwrap_or(&file).to_owned();
1373 let pin = codedb.ok_or_else(|| format!("{path}: no pin of its code list"))?;
1374 side.push((base, pin));
1375 }
1376 }
1377 for (base, _) in &side {
1378 let stem = base.strip_suffix("_codedb.xml").unwrap_or(base);
1379 let _ = write!(
1380 m,
1381 "# The stylesheet {stem}.xslt reads, with document(), the code list\n# {base} next to it, a resource of the bundle.\n"
1382 );
1383 }
1384 for (name, ..) in &stylesheets {
1385 if fr_ctc_factur_x(name) {
1386 let _ = write!(
1387 m,
1388 "# A failed assert of {name} without a flag is fatal, as the\n# Mustang validator reads the Factur-X reports.\n"
1389 );
1390 }
1391 }
1392 let _ = write!(
1393 m,
1394 "format = 2\nname = \"fr-ctc-{dir}\"\ntitle = \"{title}\"\nversion = \"{pack}\"\n"
1395 );
1396 match schema {
1397 FrCtcSchema::None => {}
1398 FrCtcSchema::Ubl21 => m.push_str("schema = \"ubl-2.1\"\n"),
1399 FrCtcSchema::Package(_) => {
1400 let entry = documents.first().map_or("", |(path, _)| base_name(path));
1401 let _ = writeln!(m, "schema = \"{entry}\"");
1402 }
1403 }
1404 for (name, _, sha, _) in &stylesheets {
1405 let _ = write!(
1406 m,
1407 "\n[[stylesheet]]\nname = \"{name}\"\npath = \"{name}.xslt\"\nsha256 = \"{}\"\n",
1408 p(sha)?
1409 );
1410 if fr_ctc_factur_x(name) {
1411 m.push_str("unflagged-assert = \"fatal\"\n");
1412 }
1413 }
1414 for (base, pin) in &side {
1415 let _ = write!(
1416 m,
1417 "\n[[resource]]\npath = \"{base}\"\nsha256 = \"{}\"\n",
1418 p(pin)?
1419 );
1420 }
1421 for (path, pin) in documents {
1422 let _ = write!(
1423 m,
1424 "\n[[schema-document]]\npath = \"{}\"\nsha256 = \"{}\"\n",
1425 base_name(path),
1426 p(pin)?
1427 );
1428 }
1429 Ok(m)
1430}
1431
1432pub(crate) fn base_name(path: &str) -> &str {
1434 path.rsplit('/').next().unwrap_or(path)
1435}
1436
1437fn manifest_peppol(spec: &RuleSetSpec) -> Result<String, String> {
1439 let p = |name: &str| spec.family.pins.get(name);
1440 Ok(format!(
1441 "# Peppol BIS Billing {tag} (UBL): the rule-set bundle manifest of
1442# einvoice-validator, as scripts/fetch-peppol.sh and einvoice-validate rules
1443# fetch write it (einvoice-validate --bundle DIR).
1444format = 1
1445name = \"peppol-bis-billing-ubl\"
1446title = \"Peppol BIS Billing 3.0 UBL\"
1447version = \"{tag}\"
1448schema = \"ubl-2.1\"
1449
1450[[stylesheet]]
1451name = \"CEN-EN16931-UBL\"
1452path = \"xslt/CEN-EN16931-UBL.xslt\"
1453sha256 = \"{cen_sha}\"
1454version = \"{cen}\"
1455
1456[[stylesheet]]
1457name = \"PEPPOL-EN16931-UBL\"
1458path = \"xslt/PEPPOL-EN16931-UBL.xslt\"
1459sha256 = \"{peppol_sha}\"
1460",
1461 tag = p("tag")?,
1462 cen_sha = p("cen_xslt_sha256")?,
1463 cen = p("cen")?,
1464 peppol_sha = p("peppol_xslt_sha256")?,
1465 ))
1466}
1467
1468#[cfg(test)]
1469mod tests {
1470 use super::*;
1471 use crate::RuleSet;
1472
1473 #[test]
1474 fn every_rule_set_resolves_and_its_manifest_parses() {
1475 let all = fetched_rule_sets().unwrap();
1476 assert_eq!(all.len(), RULE_SETS.len());
1477 for r in all {
1478 let parsed =
1479 RuleSet::parse(r.manifest()).unwrap_or_else(|e| panic!("{}: {e}", r.name()));
1480 assert_eq!(parsed.name(), r.name());
1481 assert_eq!(parsed.title(), r.title());
1482 assert_eq!(parsed.version(), r.version());
1483 assert_ne!(r.sources(), []);
1484 for s in r.sources() {
1485 assert!(s.url().starts_with("https://"), "{}", s.url());
1486 let length = match s.algorithm_kind() {
1487 Algorithm::Sha256 => 64,
1488 };
1489 assert_eq!(s.digest().len(), length, "{}", s.url());
1490 assert!(!s.file().contains('/'), "{}", s.file());
1491 }
1492 let (schema, documents) = reference_schema(r.name());
1495 assert_eq!(parsed.schema(), schema.as_ref(), "{}", r.name());
1496 let paths: Vec<&str> = parsed
1497 .schema_documents()
1498 .iter()
1499 .map(crate::SchemaDocumentEntry::path)
1500 .collect();
1501 assert_eq!(paths, documents, "{}", r.name());
1502 if let Some(crate::SchemaId::Bundle(entry)) = &schema {
1503 assert_eq!(Some(entry), documents.first());
1504 }
1505 }
1506 for (i, r) in all.iter().enumerate() {
1508 assert!(
1509 all[..i].iter().all(|o| o.name() != r.name()),
1510 "{}",
1511 r.name()
1512 );
1513 assert!(ALIASES.iter().all(|(a, _)| *a != r.name()));
1514 assert!(FAMILIES.iter().all(|f| f.name != r.name()));
1515 assert!(crate::BUILTIN_RULE_SETS.iter().all(|b| *b != r.name()));
1516 }
1517 }
1518
1519 fn cii_family(entry: &str, modules: &str) -> Vec<String> {
1524 let mut documents = vec![format!("{entry}.xsd")];
1525 for module in [
1526 "QualifiedDataType",
1527 "ReusableAggregateBusinessInformationEntity",
1528 "UnqualifiedDataType",
1529 ] {
1530 documents.push(format!(
1531 "{modules}_urn_un_unece_uncefact_data_standard_{module}_100.xsd"
1532 ));
1533 }
1534 documents
1535 }
1536
1537 fn reference_schema(name: &str) -> (Option<crate::SchemaId>, Vec<String>) {
1541 let bundle = |documents: Vec<String>| {
1542 (
1543 Some(crate::SchemaId::Bundle(documents[0].clone())),
1544 documents,
1545 )
1546 };
1547 match name {
1548 "factur-x-minimum" => bundle(cii_family("FACTUR-X_MINIMUM", "FACTUR-X_MINIMUM")),
1551 "factur-x-basic-wl" => bundle(cii_family("FACTUR-X_BASICWL", "FACTUR-X_BASICWL")),
1552 "factur-x-basic" => bundle(cii_family("FACTUR-X_BASIC", "FACTUR-X_BASIC")),
1553 "factur-x-en16931" => bundle(cii_family("FACTUR-X_EN16931", "FACTUR-X_EN16931")),
1554 "factur-x-extended" => bundle(cii_family("FACTUR-X_EXTENDED", "FACTUR-X_EXTENDED")),
1555 "xrechnung-ubl"
1559 | "peppol-bis-billing-ubl"
1560 | "fr-ctc-ubl-en16931"
1561 | "fr-ctc-ubl-en16931-warning"
1562 | "fr-ctc-ubl-extended-ctc-fr"
1563 | "fr-ctc-ubl-extended-ctc-fr-warning" => (Some(crate::SchemaId::Ubl21), vec![]),
1564 "xrechnung-cii" => (Some(crate::SchemaId::CiiD16b), vec![]),
1566 "fr-ctc-cii-en16931"
1571 | "fr-ctc-cii-en16931-warning"
1572 | "fr-ctc-cii-extended-ctc-fr"
1573 | "fr-ctc-cii-extended-ctc-fr-warning" => bundle(cii_family(
1574 "CrossIndustryInvoice_100pD22B",
1575 "CrossIndustryInvoice_100pD22B",
1576 )),
1577 "fr-ctc-cdar" | "fr-ctc-cdar-warning" => bundle(cii_family(
1578 "CrossDomainAcknowledgementAndResponse_100pD22B",
1579 "CrossDomainAcknowledgementAndResponse_100pD22B",
1580 )),
1581 "fr-ctc-factur-x-basic-wl" => {
1582 bundle(cii_family("Factur-X_BASICWL", "Factur-X_1.09.2_BASICWL"))
1583 }
1584 "fr-ctc-factur-x-en16931" => {
1585 bundle(cii_family("Factur-X_EN16931", "Factur-X_1.09.2_EN16931"))
1586 }
1587 "fr-ctc-factur-x-extended" => {
1588 bundle(cii_family("Factur-X_EXTENDED", "Factur-X_1.09.2_EXTENDED"))
1589 }
1590 "fr-ctc-flux10" => (None, vec![]),
1592 other => panic!("{other}: no reference schema stated"),
1593 }
1594 }
1595
1596 #[test]
1599 fn severities_of_the_reference_validators() {
1600 use crate::Severity;
1601 for r in fetched_rule_sets().unwrap() {
1602 let parsed = RuleSet::parse(r.manifest()).unwrap();
1603 for s in parsed.stylesheets() {
1604 let fatal = r.family() == "factur-x"
1608 || r.family() == "xrechnung"
1609 || s.name().starts_with("FACTUR-X_");
1610 let expected = if fatal {
1611 Severity::Fatal
1612 } else {
1613 Severity::Other
1614 };
1615 assert_eq!(s.unflagged_assert(), expected, "{} {}", r.name(), s.name());
1616 }
1617 let scenarios: Vec<(&str, Option<&[String]>)> = parsed
1618 .scenarios()
1619 .iter()
1620 .map(|s| (s.name(), s.stylesheets()))
1621 .collect();
1622 match r.name() {
1623 "xrechnung-ubl" | "xrechnung-cii" => {
1624 assert_eq!(
1627 scenarios.len(),
1628 if r.name() == "xrechnung-ubl" { 7 } else { 4 }
1629 );
1630 assert!(parsed.selects_by_scenario());
1631 }
1632 _ => {
1633 assert_eq!(scenarios, [], "{}", r.name());
1634 assert!(!parsed.selects_by_scenario());
1635 }
1636 }
1637 assert_eq!(parsed.unchecked_schema(), None, "{}", r.name());
1639 }
1640 let ubl = RuleSet::parse(
1641 fetched_rule_set("xrechnung-ubl", None)
1642 .unwrap()
1643 .unwrap()
1644 .manifest(),
1645 )
1646 .unwrap();
1647 let standard = &ubl.scenarios()[0];
1648 assert_eq!(standard.name(), "EN16931 XRechnung (UBL Invoice)");
1649 assert_eq!(standard.level("UBL-CR-646"), Some(Severity::Fatal));
1650 assert_eq!(standard.level("BR-CL-23"), Some(Severity::Warning));
1651 assert_eq!(
1652 ubl.scenarios()[1].level("UBL-CR-646"),
1653 Some(Severity::Other)
1654 );
1655 let names = |s: &crate::Scenario| s.stylesheets().map(<[String]>::to_vec);
1656 assert_eq!(
1657 names(standard),
1658 Some(vec!["EN16931-UBL".to_owned(), "XRechnung-UBL".to_owned()])
1659 );
1660 let cen = &ubl.scenarios()[5];
1662 assert_eq!(cen.name(), "EN16931 (UBL Invoice)");
1663 assert_eq!(names(cen), Some(vec!["EN16931-UBL".to_owned()]));
1664 assert_eq!(cen.levels(), []);
1665 }
1666
1667 #[test]
1668 fn aliases_and_families() {
1669 let name = |n: &str, s| fetched_rule_set(n, s).unwrap().map(FetchedRuleSet::name);
1670 assert_eq!(name("peppol", None), Some("peppol-bis-billing-ubl"));
1671 assert_eq!(name("xrechnung", Some(Syntax::Ubl)), Some("xrechnung-ubl"));
1672 assert_eq!(name("xrechnung", Some(Syntax::Cii)), Some("xrechnung-cii"));
1673 assert_eq!(name("xrechnung", None), Some("xrechnung-ubl"));
1674 assert_eq!(name("factur-x-basic", None), Some("factur-x-basic"));
1675 assert_eq!(name("nope", None), None);
1676 assert_eq!(named("factur-x").unwrap().len(), 5);
1677 assert_eq!(named("fr-ctc").unwrap().len(), 14);
1678 assert_eq!(named("xrechnung").unwrap().len(), 2);
1679 assert_eq!(named("peppol").unwrap().len(), 1);
1680 assert_eq!(named("xrechnung-cii").unwrap().len(), 1);
1681 assert!(named("cen-ubl").unwrap().is_empty());
1682 }
1683
1684 #[test]
1685 fn side_files_of_the_french_package() {
1686 assert_eq!(
1687 fr_ctc_side_file("a/b/FACTUR-X_EN16931.xslt").as_deref(),
1688 Some("a/b/FACTUR-X_EN16931_codedb.xml")
1689 );
1690 assert_eq!(
1691 fr_ctc_side_file("CII/EN16931/2xslt/EN16931-CII-validation.xslt"),
1692 None
1693 );
1694 for (name, path, _, codedb) in FR_CTC_STYLESHEETS {
1696 assert_eq!(fr_ctc_side_file(path).is_some(), codedb.is_some(), "{name}");
1697 }
1698 }
1699}