einvoice-xsd 0.2.0

XML Schema validation of UBL 2.1 Invoice and CreditNote documents against the official OASIS schemas, 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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
//! The built-in datatypes of XML Schema 1.0 (Second Edition, Part 2) that the UBL 2.1
//! schemas use, with their ancestors: whitespace processing and lexical spaces.
//!
//! Each check works on the value after whitespace processing (the `whiteSpace` facet
//! of the type: `preserve` for `string`, `replace` for `normalizedString`, `collapse`
//! for every other type here).

use crate::uri;

/// A built-in simple type this crate implements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Builtin {
    /// `xs:anySimpleType`: any string.
    AnySimpleType,
    /// `xs:string`.
    String,
    /// `xs:normalizedString`.
    NormalizedString,
    /// `xs:token`.
    Token,
    /// `xs:language`.
    Language,
    /// `xs:Name`.
    Name,
    /// `xs:NCName`.
    NcName,
    /// `xs:ID`: an `NCName`, unique within the document when it types an attribute.
    Id,
    /// `xs:decimal`, any number of digits.
    Decimal,
    /// `xs:integer`, any number of digits.
    Integer,
    /// `xs:boolean`.
    Boolean,
    /// `xs:date`.
    Date,
    /// `xs:dateTime`.
    DateTime,
    /// `xs:time`.
    Time,
    /// `xs:base64Binary`.
    Base64Binary,
    /// `xs:anyURI`.
    AnyUri,
}

/// The `whiteSpace` facet.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WhiteSpace {
    Preserve,
    Replace,
    Collapse,
}

impl Builtin {
    /// Every implemented built-in, base types before the types derived from them.
    pub(crate) const ALL: [Self; 16] = [
        Self::AnySimpleType,
        Self::String,
        Self::NormalizedString,
        Self::Token,
        Self::Language,
        Self::Name,
        Self::NcName,
        Self::Id,
        Self::Decimal,
        Self::Integer,
        Self::Boolean,
        Self::Date,
        Self::DateTime,
        Self::Time,
        Self::Base64Binary,
        Self::AnyUri,
    ];

    /// The local name in the XML Schema namespace.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::AnySimpleType => "anySimpleType",
            Self::String => "string",
            Self::NormalizedString => "normalizedString",
            Self::Token => "token",
            Self::Language => "language",
            Self::Name => "Name",
            Self::NcName => "NCName",
            Self::Id => "ID",
            Self::Decimal => "decimal",
            Self::Integer => "integer",
            Self::Boolean => "boolean",
            Self::Date => "date",
            Self::DateTime => "dateTime",
            Self::Time => "time",
            Self::Base64Binary => "base64Binary",
            Self::AnyUri => "anyURI",
        }
    }

    /// The built-in this one is derived from by restriction (`None` for
    /// `anySimpleType`, whose base is `anyType`).
    pub(crate) fn base(self) -> Option<Self> {
        match self {
            Self::AnySimpleType => None,
            Self::NormalizedString => Some(Self::String),
            Self::Token => Some(Self::NormalizedString),
            Self::Language | Self::Name => Some(Self::Token),
            Self::NcName => Some(Self::Name),
            Self::Id => Some(Self::NcName),
            Self::Integer => Some(Self::Decimal),
            Self::String
            | Self::Decimal
            | Self::Boolean
            | Self::Date
            | Self::DateTime
            | Self::Time
            | Self::Base64Binary
            | Self::AnyUri => Some(Self::AnySimpleType),
        }
    }

    pub(crate) fn white_space(self) -> WhiteSpace {
        match self {
            Self::AnySimpleType | Self::String => WhiteSpace::Preserve,
            Self::NormalizedString => WhiteSpace::Replace,
            _ => WhiteSpace::Collapse,
        }
    }

    /// Whether `value`, already whitespace-processed, is in the lexical space.
    pub(crate) fn is_valid(self, value: &str) -> bool {
        match self {
            Self::AnySimpleType | Self::String | Self::NormalizedString | Self::Token => true,
            Self::Language => is_language(value),
            Self::Name => is_name(value),
            Self::NcName | Self::Id => is_ncname(value),
            Self::Decimal => is_decimal(value),
            Self::Integer => is_integer(value),
            Self::Boolean => matches!(value, "true" | "false" | "1" | "0"),
            Self::Date => is_date(value),
            Self::DateTime => is_date_time(value),
            Self::Time => is_time(value),
            Self::Base64Binary => is_base64(value),
            Self::AnyUri => uri::is_any_uri(value),
        }
    }

    /// Whether the length facets (`minLength`, `maxLength`) apply to the type and
    /// this crate implements them: the types derived from `xs:string`, whose length
    /// is counted in characters (Part 2, 4.3.1). They also apply to `xs:anyURI` and
    /// `xs:base64Binary`, which the supported schemas never restrict that way; they
    /// do not apply to the other types here.
    pub(crate) fn has_length(self) -> bool {
        matches!(
            self,
            Self::String
                | Self::NormalizedString
                | Self::Token
                | Self::Language
                | Self::Name
                | Self::NcName
                | Self::Id
        )
    }

    /// The value of a lexical form, already whitespace-processed and valid, as a
    /// key: two valid lexical forms of the type denote the same value (Part 2,
    /// 2.2.1: equality in the value space) exactly when their keys are equal.
    /// `None` for the date and time types, whose values (instants with an optional
    /// timezone) this crate does not compare: a value constraint on them is
    /// refused when the schema is loaded.
    pub(crate) fn value_key(self, value: &str) -> Option<std::borrow::Cow<'_, str>> {
        use std::borrow::Cow;
        match self {
            // The string types and anyURI: the value is the string itself (anyURI
            // values are not URI-normalised: Part 2, 3.2.17 defines no equality
            // beyond identity of the strings).
            Self::AnySimpleType
            | Self::String
            | Self::NormalizedString
            | Self::Token
            | Self::Language
            | Self::Name
            | Self::NcName
            | Self::Id
            | Self::AnyUri => Some(Cow::Borrowed(value)),
            Self::Decimal | Self::Integer => Some(Cow::Owned(canonical_decimal(value))),
            Self::Boolean => Some(Cow::Borrowed(match value {
                "true" | "1" => "true",
                _ => "false",
            })),
            // The lexical space admits one encoding per octet sequence (the bits
            // a padded group leaves unused must be zero), up to the spaces between
            // characters.
            Self::Base64Binary => Some(if value.contains(' ') {
                Cow::Owned(value.replace(' ', ""))
            } else {
                Cow::Borrowed(value)
            }),
            Self::Date | Self::DateTime | Self::Time => None,
        }
    }

    /// Whether [`Self::value_key`] compares the values of the type.
    pub(crate) fn has_value_key(self) -> bool {
        !matches!(self, Self::Date | Self::DateTime | Self::Time)
    }

    /// Whether `raw`, before whitespace processing, is in the lexical space once
    /// processed: `self.is_valid(&normalize(raw, self.white_space()))`, without
    /// building the processed value when the check does not need it (the string
    /// types accept every value; base64 skips whitespace as it goes).
    pub(crate) fn is_valid_raw(self, raw: &str) -> bool {
        match self {
            Self::AnySimpleType | Self::String | Self::NormalizedString | Self::Token => true,
            // Collapsing, then dropping the single spaces left between
            // characters, drops every whitespace character.
            Self::Base64Binary => is_base64_skipping(raw, is_xml_space_byte),
            _ => self.is_valid(&normalize(raw, self.white_space())),
        }
    }
}

/// The local names of the XML Schema 1.0 built-in types that this crate does not
/// implement: naming one in `xsi:type` is refused as unsupported, not reported as
/// an unknown type.
pub(crate) const UNIMPLEMENTED_BUILTINS: [&str; 31] = [
    "anyType",
    "IDREF",
    "IDREFS",
    "ENTITY",
    "ENTITIES",
    "NMTOKEN",
    "NMTOKENS",
    "NOTATION",
    "QName",
    "float",
    "double",
    "duration",
    "gYearMonth",
    "gYear",
    "gMonthDay",
    "gDay",
    "gMonth",
    "hexBinary",
    "nonPositiveInteger",
    "negativeInteger",
    "long",
    "int",
    "short",
    "byte",
    "nonNegativeInteger",
    "unsignedLong",
    "unsignedInt",
    "unsignedShort",
    "unsignedByte",
    "positiveInteger",
    "anyAtomicType",
];

/// XML whitespace: space, tab, line feed, carriage return.
pub(crate) fn is_xml_space(c: char) -> bool {
    matches!(c, ' ' | '\t' | '\n' | '\r')
}

/// [`is_xml_space`] on a byte of UTF-8 text.
fn is_xml_space_byte(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\n' | b'\r')
}

/// Applies a `whiteSpace` facet.
pub(crate) fn normalize(value: &str, white_space: WhiteSpace) -> std::borrow::Cow<'_, str> {
    use std::borrow::Cow;
    match white_space {
        WhiteSpace::Preserve => Cow::Borrowed(value),
        WhiteSpace::Replace => {
            if value.contains(['\t', '\n', '\r']) {
                Cow::Owned(value.replace(['\t', '\n', '\r'], " "))
            } else {
                Cow::Borrowed(value)
            }
        }
        WhiteSpace::Collapse => {
            // Leading and trailing whitespace only: the value is a slice.
            let trimmed = value.trim_matches(is_xml_space);
            if !trimmed.contains(['\t', '\n', '\r']) && !trimmed.contains("  ") {
                Cow::Borrowed(trimmed)
            } else {
                let mut collapsed = String::with_capacity(trimmed.len());
                for word in trimmed.split(is_xml_space).filter(|s| !s.is_empty()) {
                    if !collapsed.is_empty() {
                        collapsed.push(' ');
                    }
                    collapsed.push_str(word);
                }
                Cow::Owned(collapsed)
            }
        }
    }
}

fn is_digits(s: &str) -> bool {
    !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())
}

/// `(\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)`
pub(crate) fn is_decimal(value: &str) -> bool {
    let unsigned = value.strip_prefix(['+', '-']).unwrap_or(value);
    match unsigned.split_once('.') {
        None => is_digits(unsigned),
        Some((int, frac)) => {
            (int.is_empty() || is_digits(int))
                && (frac.is_empty() || is_digits(frac))
                && !(int.is_empty() && frac.is_empty())
        }
    }
}

/// The canonical representation of a valid `xs:decimal` lexical form (Part 2,
/// 3.2.3.2), with no digit limit: no `+`, no leading zeros before the point, no
/// trailing zeros after it, no point for an integral value, and `0` for zero
/// (whatever its sign).
pub(crate) fn canonical_decimal(value: &str) -> String {
    let (negative, unsigned) = match value.strip_prefix('-') {
        Some(rest) => (true, rest),
        None => (false, value.strip_prefix('+').unwrap_or(value)),
    };
    let (int, frac) = unsigned.split_once('.').unwrap_or((unsigned, ""));
    let int = int.trim_start_matches('0');
    let frac = frac.trim_end_matches('0');
    if int.is_empty() && frac.is_empty() {
        return "0".to_owned();
    }
    let mut out = String::with_capacity(int.len() + frac.len() + 3);
    if negative {
        out.push('-');
    }
    out.push_str(if int.is_empty() { "0" } else { int });
    if !frac.is_empty() {
        out.push('.');
        out.push_str(frac);
    }
    out
}

/// `[\-+]?[0-9]+`
pub(crate) fn is_integer(value: &str) -> bool {
    is_digits(value.strip_prefix(['+', '-']).unwrap_or(value))
}

/// `[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*` (XML Schema 1.0 Second Edition).
pub(crate) fn is_language(value: &str) -> bool {
    let mut parts = value.split('-');
    let first_ok = parts
        .next()
        .is_some_and(|p| (1..=8).contains(&p.len()) && p.bytes().all(|b| b.is_ascii_alphabetic()));
    first_ok
        && parts.all(|p| (1..=8).contains(&p.len()) && p.bytes().all(|b| b.is_ascii_alphanumeric()))
}

/// `NameStartChar` of XML 1.0 Fifth Edition.
pub(crate) fn is_name_start_char(c: char) -> bool {
    matches!(c,
        ':' | 'A'..='Z' | '_' | 'a'..='z'
        | '\u{C0}'..='\u{D6}' | '\u{D8}'..='\u{F6}' | '\u{F8}'..='\u{2FF}'
        | '\u{370}'..='\u{37D}' | '\u{37F}'..='\u{1FFF}' | '\u{200C}'..='\u{200D}'
        | '\u{2070}'..='\u{218F}' | '\u{2C00}'..='\u{2FEF}' | '\u{3001}'..='\u{D7FF}'
        | '\u{F900}'..='\u{FDCF}' | '\u{FDF0}'..='\u{FFFD}' | '\u{10000}'..='\u{EFFFF}')
}

/// `NameChar` of XML 1.0 Fifth Edition.
pub(crate) fn is_name_char(c: char) -> bool {
    is_name_start_char(c)
        || matches!(c, '-' | '.' | '0'..='9' | '\u{B7}' | '\u{300}'..='\u{36F}' | '\u{203F}'..='\u{2040}')
}

/// `Name` of XML 1.0 Fifth Edition.
pub(crate) fn is_name(value: &str) -> bool {
    let mut chars = value.chars();
    chars.next().is_some_and(is_name_start_char) && chars.all(is_name_char)
}

/// `NCName` of Namespaces in XML 1.0: a `Name` without colons.
pub(crate) fn is_ncname(value: &str) -> bool {
    !value.contains(':') && is_name(value)
}

/// `QName` of Namespaces in XML 1.0: `(NCName ':')? NCName`.
pub(crate) fn split_qname(value: &str) -> Option<(Option<&str>, &str)> {
    match value.split_once(':') {
        Some((prefix, local)) if is_ncname(prefix) && is_ncname(local) => {
            Some((Some(prefix), local))
        }
        None if is_ncname(value) => Some((None, value)),
        _ => None,
    }
}

/// The base64 alphabet.
fn is_base64_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'+' || b == b'/'
}

/// The lexical space of `base64Binary` (Part 2, 3.2.16, as corrected by the errata of
/// the Second Edition): groups of four characters of the base64 alphabet, the last
/// group optionally padded with one `=` (third character in `AEIMQUYcgkosw048`) or
/// two (second character in `AQgw`), with single spaces allowed between characters.
/// The value is already collapsed, so it has no leading, trailing or double spaces.
pub(crate) fn is_base64(value: &str) -> bool {
    is_base64_skipping(value, |b| b == b' ')
}

/// [`is_base64`] on the characters of `value` that `skip` does not drop, in one
/// pass: the characters left must be a multiple of four, every one of them base64
/// except a final run of at most two `=`, and the padding must fit the last
/// character before it.
fn is_base64_skipping(value: &str, skip: impl Fn(u8) -> bool) -> bool {
    let mut count = 0_usize;
    let mut padding = 0_usize;
    let mut last = None;
    for b in value.bytes().filter(|&b| !skip(b)) {
        count += 1;
        if b == b'=' {
            padding += 1;
        } else if padding > 0 || !is_base64_char(b) {
            // A character after padding, or outside the alphabet.
            return false;
        } else {
            last = Some(b);
        }
    }
    if !count.is_multiple_of(4) || padding > 2 {
        return false;
    }
    match (padding, last) {
        (0, _) => true,
        (1, Some(last)) => b"AEIMQUYcgkosw048".contains(&last),
        (2, Some(last)) => b"AQgw".contains(&last),
        _ => false,
    }
}

fn two_digits(s: &str) -> Option<u32> {
    if s.len() == 2 && s.bytes().all(|b| b.is_ascii_digit()) {
        s.parse().ok()
    } else {
        None
    }
}

/// Whether a year, given by its digits (no sign), is a leap year. Negative years
/// follow the same rule on their absolute value, as libxml2 does: XML Schema 1.0 has
/// no year zero and does not spell out the rule for years before the common era.
fn is_leap_year(digits: &str) -> bool {
    // 10^4 is a multiple of 400: the last four digits decide.
    let tail = &digits[digits.len().saturating_sub(4)..];
    let year: u32 = tail.parse().unwrap_or(1);
    (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}

/// `-?yyyy-mm-dd` with at least four year digits, no leading zero when there are more,
/// year zero excluded, and a day that exists in that month.
fn date_part(s: &str) -> Option<()> {
    let unsigned = s.strip_prefix('-').unwrap_or(s);
    let mut fields = unsigned.rsplitn(3, '-');
    let day = two_digits(fields.next()?)?;
    let month = two_digits(fields.next()?)?;
    let year = fields.next()?;
    if year.len() < 4 || !is_digits(year) || (year.len() > 4 && year.starts_with('0')) {
        return None;
    }
    if year.bytes().all(|b| b == b'0') {
        return None;
    }
    let days = match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 if is_leap_year(year) => 29,
        2 => 28,
        _ => return None,
    };
    (1..=days).contains(&day).then_some(())
}

/// `hh:mm:ss(.s+)?`, hours 00 to 23, or 24:00:00 with a zero fraction.
fn time_part(s: &str) -> Option<()> {
    let (whole, fraction) = match s.split_once('.') {
        Some((whole, fraction)) => (whole, Some(fraction)),
        None => (s, None),
    };
    if let Some(fraction) = fraction
        && !is_digits(fraction)
    {
        return None;
    }
    let mut fields = whole.split(':');
    let hour = two_digits(fields.next()?)?;
    let minute = two_digits(fields.next()?)?;
    let second = two_digits(fields.next()?)?;
    if fields.next().is_some() || minute > 59 || second > 59 {
        return None;
    }
    let zero_fraction = fraction.is_none_or(|f| f.bytes().all(|b| b == b'0'));
    (hour < 24 || (hour == 24 && minute == 0 && second == 0 && zero_fraction)).then_some(())
}

/// Splits an optional timezone (`Z` or `(+|-)hh:mm`, at most 14:00 away from UTC)
/// off the end of a date or time; `None` if a timezone is present but invalid.
fn split_timezone(s: &str) -> Option<&str> {
    if let Some(rest) = s.strip_suffix('Z') {
        return Some(rest);
    }
    let bytes = s.as_bytes();
    if bytes.len() >= 6
        && matches!(bytes[bytes.len() - 6], b'+' | b'-')
        && bytes[bytes.len() - 3] == b':'
    {
        let (rest, zone) = s.split_at(s.len() - 6);
        let hours = two_digits(&zone[1..3])?;
        let minutes = two_digits(&zone[4..6])?;
        // A date ending in `-dd` also matches `-hh:mm` only if it has a colon, which
        // dates do not: the shape check above is unambiguous.
        return (minutes <= 59 && (hours < 14 || (hours == 14 && minutes == 0))).then_some(rest);
    }
    Some(s)
}

pub(crate) fn is_date(value: &str) -> bool {
    split_timezone(value).and_then(date_part).is_some()
}

pub(crate) fn is_time(value: &str) -> bool {
    split_timezone(value).and_then(time_part).is_some()
}

pub(crate) fn is_date_time(value: &str) -> bool {
    split_timezone(value)
        .and_then(|rest| rest.split_once('T'))
        .and_then(|(date, time)| date_part(date).and(time_part(time)))
        .is_some()
}

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

    #[test]
    fn decimals() {
        for ok in [
            "0",
            "-1",
            "+1.5",
            ".5",
            "5.",
            "-.5",
            "00.",
            "0001.000",
            "123456789012345678901234567890",
        ] {
            assert!(is_decimal(ok), "{ok}");
        }
        for bad in [
            "", ".", "-", "+", "+.", "1.2.3", "1e3", "1,5", "--1", " 1", "NaN", "INF", "0x10", "1",
        ] {
            assert!(!is_decimal(bad), "{bad}");
        }
    }

    #[test]
    fn canonical_decimals() {
        for (value, canonical) in [
            ("0", "0"),
            ("-0", "0"),
            ("+0.000", "0"),
            (".0", "0"),
            ("0.", "0"),
            ("1.50", "1.5"),
            ("+01.500", "1.5"),
            ("-001", "-1"),
            ("-.5", "-0.5"),
            ("5.", "5"),
            ("100", "100"),
            ("10.010", "10.01"),
            (
                "000123456789012345678901234567890.000",
                "123456789012345678901234567890",
            ),
        ] {
            assert_eq!(canonical_decimal(value), canonical, "{value}");
        }
    }

    #[test]
    fn value_keys() {
        let key = |builtin: Builtin, value: &str| {
            builtin.value_key(value).map(std::borrow::Cow::into_owned)
        };
        assert_eq!(key(Builtin::Boolean, "1"), key(Builtin::Boolean, "true"));
        assert_eq!(key(Builtin::Boolean, "0"), key(Builtin::Boolean, "false"));
        assert_ne!(key(Builtin::Boolean, "0"), key(Builtin::Boolean, "true"));
        assert_eq!(key(Builtin::Integer, "+002"), key(Builtin::Integer, "2"));
        assert_eq!(key(Builtin::Decimal, "1.5"), key(Builtin::Decimal, "1.50"));
        assert_eq!(
            key(Builtin::Base64Binary, "Q U J D"),
            key(Builtin::Base64Binary, "QUJD")
        );
        // Strings are compared as they are: whitespace processing comes first.
        assert_ne!(key(Builtin::String, " a"), key(Builtin::String, "a"));
        assert_ne!(key(Builtin::Language, "EN"), key(Builtin::Language, "en"));
        assert_ne!(key(Builtin::AnyUri, "a%20b"), key(Builtin::AnyUri, "a b"));
        for builtin in [Builtin::Date, Builtin::DateTime, Builtin::Time] {
            assert!(!builtin.has_value_key());
            assert_eq!(builtin.value_key("2024-01-01"), None);
        }
        for builtin in Builtin::ALL {
            assert_eq!(
                builtin.has_value_key(),
                builtin.value_key("").is_some(),
                "{builtin:?}"
            );
        }
    }

    #[test]
    fn length_facets_apply_to_the_string_types() {
        let with_length: Vec<Builtin> = Builtin::ALL
            .into_iter()
            .filter(|b| b.has_length())
            .collect();
        assert_eq!(
            with_length,
            [
                Builtin::String,
                Builtin::NormalizedString,
                Builtin::Token,
                Builtin::Language,
                Builtin::Name,
                Builtin::NcName,
                Builtin::Id
            ]
        );
    }

    #[test]
    fn integers() {
        for ok in ["0", "-0", "+42", "1234567890123456789012345"] {
            assert!(is_integer(ok), "{ok}");
        }
        for bad in ["", "+", "1.0", "1 2", "a"] {
            assert!(!is_integer(bad), "{bad}");
        }
    }

    #[test]
    fn dates() {
        for ok in [
            "2024-02-29",
            "2000-02-29",
            "12024-01-01",
            "-0001-01-01",
            "-0004-02-29",
            "2024-03-01Z",
            "2024-03-01+14:00",
            "2024-03-01-14:00",
            "2024-03-01+13:59",
        ] {
            assert!(is_date(ok), "{ok}");
        }
        for bad in [
            "2024-02-30",
            "2023-02-29",
            "1900-02-29",
            "-0001-02-29",
            "2024-13-01",
            "2024-00-10",
            "2024-03-00",
            "24-03-01",
            "2024-3-1",
            "0000-01-01",
            "02024-01-01",
            "+2024-03-01",
            "2024-03-01+14:01",
            "2024-03-01+15:00",
            "2024-03-01+01:60",
            "2024-03-01+0100",
            "2024-03-01z",
            "2024-03-01T00:00:00",
            "",
            "2024-03-01 Z",
        ] {
            assert!(!is_date(bad), "{bad}");
        }
    }

    #[test]
    fn times() {
        for ok in [
            "10:15:30",
            "24:00:00",
            "24:00:00.000",
            "24:00:00+01:00",
            "10:15:30.123Z",
            "23:59:59.99999999999999999999",
        ] {
            assert!(is_time(ok), "{ok}");
        }
        for bad in [
            "24:00:01",
            "24:01:00",
            "25:00:00",
            "23:60:00",
            "23:59:60",
            "10:15",
            "10:15:30.",
            "1:15:30",
            "10:15:30+24:00",
            "10:15:30,5",
            "24:00:00.1",
        ] {
            assert!(!is_time(bad), "{bad}");
        }
    }

    #[test]
    fn date_times() {
        for ok in [
            "2024-03-01T10:15:30",
            "2024-12-31T24:00:00Z",
            "2024-03-01T10:15:30.250+01:00",
        ] {
            assert!(is_date_time(ok), "{ok}");
        }
        for bad in [
            "2024-03-01 10:15:30",
            "2024-03-01t10:15:30",
            "2024-03-01",
            "2023-02-29T10:00:00",
            "2024-03-01T10:15Z",
            "2024-03-01T10:15:30Zx",
        ] {
            assert!(!is_date_time(bad), "{bad}");
        }
    }

    #[test]
    fn base64() {
        for ok in [
            "",
            "QQ==",
            "QUI=",
            "QUJD",
            "Q U J D Q Q = =",
            "QUJD QUJD QUI=",
        ] {
            assert!(is_base64(ok), "{ok}");
        }
        for bad in [
            "abc",
            "QR==",
            "QUJ=",
            "Q===",
            "====",
            "QQ=",
            "QQ==QUJD",
            "QU!JD",
            "-_-_",
            "QQ== QQ==",
        ] {
            assert!(!is_base64(bad), "{bad}");
        }
    }

    #[test]
    fn languages() {
        for ok in ["en", "en-GB", "x-a1-B2", "englishx-language"] {
            assert!(is_language(ok), "{ok}");
        }
        for bad in [
            "",
            "en_GB",
            "123",
            "e1",
            "en-123456789",
            "englishxx",
            "en-",
            "en--GB",
            "-en",
        ] {
            assert!(!is_language(bad), "{bad}");
        }
    }

    #[test]
    fn names() {
        assert!(is_ncname("signature-1"));
        assert!(is_ncname("sïgnature"));
        assert!(!is_ncname("1signature"));
        assert!(!is_ncname("a:b"));
        assert!(!is_ncname(""));
        assert_eq!(split_qname("a:b"), Some((Some("a"), "b")));
        assert_eq!(split_qname("b"), Some((None, "b")));
        assert_eq!(split_qname("a:b:c"), None);
        assert_eq!(split_qname("1"), None);
    }

    #[test]
    fn white_space() {
        assert_eq!(normalize(" a \t\n b ", WhiteSpace::Collapse), "a b");
        assert_eq!(normalize("a\tb\n", WhiteSpace::Replace), "a b ");
        assert_eq!(normalize(" a ", WhiteSpace::Preserve), " a ");
        // Only leading and trailing whitespace: a slice of the value, not a copy.
        assert!(matches!(
            normalize("\n  12.50\t", WhiteSpace::Collapse),
            std::borrow::Cow::Borrowed("12.50")
        ));
    }

    /// The collapsed value as XML Schema Part 2 defines it: runs of whitespace
    /// become one space, then leading and trailing spaces go.
    fn reference_collapse(value: &str) -> String {
        value
            .split(is_xml_space)
            .filter(|s| !s.is_empty())
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// `base64Binary` checked on a copy of the value without its spaces.
    fn reference_base64(value: &str) -> bool {
        let bytes: Vec<u8> = value.bytes().filter(|&b| b != b' ').collect();
        if !bytes.len().is_multiple_of(4) {
            return false;
        }
        let padding = bytes.iter().rev().take_while(|&&b| b == b'=').count();
        let data = &bytes[..bytes.len() - padding];
        if padding > 2 || !data.iter().all(|&b| is_base64_char(b)) {
            return false;
        }
        match (padding, data.last()) {
            (0, _) => true,
            (1, Some(last)) => b"AEIMQUYcgkosw048".contains(last),
            (2, Some(last)) => b"AQgw".contains(last),
            _ => false,
        }
    }

    proptest::proptest! {
        #![proptest_config(proptest::prelude::ProptestConfig::with_cases(4000))]

        #[test]
        fn collapse_matches_the_definition(value in "[ \t\n\ra-c.]{0,12}") {
            let collapsed = normalize(&value, WhiteSpace::Collapse);
            proptest::prop_assert_eq!(collapsed.as_ref(), reference_collapse(&value));
        }

        #[test]
        fn base64_in_one_pass(value in "[A-Da-dQgwU0-3+/= !\t\n\r-]{0,16}") {
            proptest::prop_assert_eq!(is_base64(&value), reference_base64(&value));
        }

        #[test]
        fn decimal_keys_are_numeric_equality(
            a in "[+-]?[0-9]{0,6}(\\.[0-9]{0,6})?",
            b in "[+-]?[0-9]{0,6}(\\.[0-9]{0,6})?",
        ) {
            proptest::prop_assume!(is_decimal(&a) && is_decimal(&b));
            // Reference: the value scaled by 10^6, exact in an i64.
            let scaled = |value: &str| -> i64 {
                let negative = value.starts_with('-');
                let unsigned = value.trim_start_matches(['+', '-']);
                let (int, frac) = unsigned.split_once('.').unwrap_or((unsigned, ""));
                let int: i64 = if int.is_empty() { 0 } else { int.parse().unwrap() };
                let frac: i64 = format!("{frac:0<6}").parse().unwrap();
                let magnitude = int * 1_000_000 + frac;
                if negative { -magnitude } else { magnitude }
            };
            proptest::prop_assert_eq!(
                canonical_decimal(&a) == canonical_decimal(&b),
                scaled(&a) == scaled(&b),
                "{} {}", a, b
            );
            // The canonical form is itself a valid decimal with the same value.
            proptest::prop_assert!(is_decimal(&canonical_decimal(&a)));
            proptest::prop_assert_eq!(scaled(&canonical_decimal(&a)), scaled(&a));
        }

        #[test]
        fn decimal_keys_ignore_zeros_and_plus(
            negative: bool,
            int in "[0-9]{0,4}",
            frac in "[0-9]{0,4}",
            lead in 0_usize..3,
            trail in 0_usize..3,
            plus: bool,
        ) {
            proptest::prop_assume!(!int.is_empty() || !frac.is_empty());
            let sign = if negative { "-" } else { "" };
            let a = format!("{sign}{int}.{frac}");
            let other_sign = if negative { "-" } else if plus { "+" } else { "" };
            let b = format!("{other_sign}{}{int}.{frac}{}", "0".repeat(lead), "0".repeat(trail));
            proptest::prop_assert_eq!(canonical_decimal(&a), canonical_decimal(&b));
        }

        #[test]
        fn raw_values_as_processed_values(
            value in "[ \t\n\r0-9a-zA-Z+/=:.T-]{0,16}",
            index in 0..Builtin::ALL.len(),
        ) {
            let builtin = Builtin::ALL[index];
            let processed = normalize(&value, builtin.white_space());
            proptest::prop_assert_eq!(builtin.is_valid_raw(&value), builtin.is_valid(&processed));
        }
    }
}