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
//! Content models compiled to Glushkov automata.
//!
//! A particle tree (sequences, choices, element declarations and wildcards, each
//! with `minOccurs`/`maxOccurs`) is expanded into a regular expression over
//! positions (one position per occurrence of a term), from which the Glushkov
//! construction gives the first positions, the follow positions of each position and
//! the final positions. The Unique Particle Attribution constraint of XML Schema
//! (cos-nonambig) is exactly the determinism of that automaton: at load, no two
//! positions reachable from the same state may compete for the same element. A
//! child element therefore matches at most one position, and validation is one
//! table lookup per child.

use crate::model::{ElementDecl, ElementId, Wildcard};

/// The largest finite `minOccurs`/`maxOccurs` accepted; larger finite bounds would
/// multiply positions and are not used by the supported schemas.
pub(crate) const MAX_FINITE_OCCURS: u32 = 64;

#[derive(Debug, Clone)]
pub(crate) enum ParticleTerm {
    Element(ElementId),
    Wildcard(Wildcard),
    Sequence(Vec<Particle>),
    Choice(Vec<Particle>),
}

#[derive(Debug, Clone)]
pub(crate) struct Particle {
    pub(crate) term: ParticleTerm,
    pub(crate) min: u32,
    /// `None` for `unbounded`.
    pub(crate) max: Option<u32>,
}

/// What a position matches.
#[derive(Debug, Clone)]
pub(crate) enum Term {
    Element(ElementId),
    Wildcard(Wildcard),
}

#[derive(Debug, Clone)]
pub(crate) struct Automaton {
    terms: Vec<Term>,
    /// The positions that may come next from each state, sorted, one run per state
    /// (the start state, then the state after each position), in one array:
    /// `successors[offsets[i]..offsets[i + 1]]` for the state of index `i`.
    successors: Vec<u32>,
    offsets: Vec<u32>,
    is_final: Vec<bool>,
    nullable: bool,
}

/// A state: before any child (`None`) or after the child that matched a position.
pub(crate) type State = Option<usize>;

enum Re {
    Position(usize),
    Sequence(Vec<Re>),
    Choice(Vec<Re>),
    Star(Box<Re>),
    Optional(Box<Re>),
}

fn expand(particle: &Particle, terms: &mut Vec<Term>) -> Re {
    let build = |terms: &mut Vec<Term>| -> Re {
        match &particle.term {
            ParticleTerm::Element(id) => {
                terms.push(Term::Element(*id));
                Re::Position(terms.len() - 1)
            }
            ParticleTerm::Wildcard(w) => {
                terms.push(Term::Wildcard(w.clone()));
                Re::Position(terms.len() - 1)
            }
            ParticleTerm::Sequence(items) => {
                Re::Sequence(items.iter().map(|p| expand(p, terms)).collect())
            }
            ParticleTerm::Choice(items) => {
                Re::Choice(items.iter().map(|p| expand(p, terms)).collect())
            }
        }
    };
    let mut parts = Vec::new();
    for _ in 0..particle.min {
        parts.push(build(terms));
    }
    match particle.max {
        None => parts.push(Re::Star(Box::new(build(terms)))),
        Some(max) => {
            // a{min,max} = a^min (a (a (...)?)?)? : nested optionals keep the
            // expansion deterministic when the particle itself is.
            let mut optional: Option<Re> = None;
            for _ in particle.min..max {
                let copy = build(terms);
                optional = Some(Re::Optional(Box::new(match optional.take() {
                    None => copy,
                    Some(inner) => Re::Sequence(vec![copy, inner]),
                })));
            }
            parts.extend(optional);
        }
    }
    Re::Sequence(parts)
}

struct Sets {
    nullable: bool,
    first: Vec<usize>,
    last: Vec<usize>,
}

fn glushkov(re: &Re, follow: &mut [Vec<usize>]) -> Sets {
    match re {
        Re::Position(p) => Sets {
            nullable: false,
            first: vec![*p],
            last: vec![*p],
        },
        Re::Sequence(items) => {
            let mut result = Sets {
                nullable: true,
                first: Vec::new(),
                last: Vec::new(),
            };
            for item in items {
                let sets = glushkov(item, follow);
                for &p in &result.last {
                    follow[p].extend_from_slice(&sets.first);
                }
                if result.nullable {
                    result.first.extend_from_slice(&sets.first);
                }
                if sets.nullable {
                    result.last.extend_from_slice(&sets.last);
                } else {
                    result.last = sets.last;
                }
                result.nullable &= sets.nullable;
            }
            result
        }
        Re::Choice(items) => {
            let mut result = Sets {
                nullable: false,
                first: Vec::new(),
                last: Vec::new(),
            };
            for item in items {
                let sets = glushkov(item, follow);
                result.nullable |= sets.nullable;
                result.first.extend(sets.first);
                result.last.extend(sets.last);
            }
            result
        }
        Re::Star(inner) => {
            let sets = glushkov(inner, follow);
            for &p in &sets.last {
                follow[p].extend_from_slice(&sets.first);
            }
            Sets {
                nullable: true,
                ..sets
            }
        }
        Re::Optional(inner) => Sets {
            nullable: true,
            ..glushkov(inner, follow)
        },
    }
}

fn dedup(mut v: Vec<usize>) -> Vec<usize> {
    v.sort_unstable();
    v.dedup();
    v
}

impl Automaton {
    /// Compiles a particle. Fails with a description when the content model is not
    /// deterministic (Unique Particle Attribution).
    pub(crate) fn compile(particle: &Particle, elements: &[ElementDecl]) -> Result<Self, String> {
        let mut terms = Vec::new();
        let re = expand(particle, &mut terms);
        let mut follow = vec![Vec::new(); terms.len()];
        let sets = glushkov(&re, &mut follow);
        let mut is_final = vec![false; terms.len()];
        for &p in &sets.last {
            is_final[p] = true;
        }
        let too_large = || format!("content model with too many positions ({})", terms.len());
        let mut successors = Vec::new();
        let mut offsets = Vec::with_capacity(terms.len() + 2);
        offsets.push(0);
        for set in std::iter::once(sets.first).chain(follow) {
            for p in dedup(set) {
                successors.push(u32::try_from(p).map_err(|_| too_large())?);
            }
            offsets.push(u32::try_from(successors.len()).map_err(|_| too_large())?);
        }
        terms.shrink_to_fit();
        successors.shrink_to_fit();
        let automaton = Self {
            terms,
            successors,
            offsets,
            is_final,
            nullable: sets.nullable,
        };
        automaton.check_deterministic(elements)?;
        Ok(automaton)
    }

    /// Whether the content model has no position at all (an empty content type
    /// unless the type is mixed).
    pub(crate) fn is_empty(&self) -> bool {
        self.terms.is_empty() && self.nullable
    }

    fn check_deterministic(&self, elements: &[ElementDecl]) -> Result<(), String> {
        let compete = |a: &Term, b: &Term| match (a, b) {
            (Term::Element(x), Term::Element(y)) => elements[*x].name == elements[*y].name,
            (Term::Element(e), Term::Wildcard(w)) | (Term::Wildcard(w), Term::Element(e)) => {
                w.allows(elements[*e].name.namespace.as_deref())
            }
            // The supported namespace constraints (##any, ##other) always share
            // some namespace.
            (Term::Wildcard(_), Term::Wildcard(_)) => true,
        };
        for state in 0..self.offsets.len() - 1 {
            let set = self.state_successors(state);
            for (i, &a) in set.iter().enumerate() {
                for &b in &set[i + 1..] {
                    let (a, b) = (a as usize, b as usize);
                    if compete(&self.terms[a], &self.terms[b]) {
                        return Err(format!(
                            "content model is not deterministic: {} and {} compete",
                            self.describe(a, elements),
                            self.describe(b, elements)
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    /// The successors of the state of index `state` (0 for the start state, `p + 1`
    /// after position `p`).
    fn state_successors(&self, state: usize) -> &[u32] {
        let start = self.offsets[state] as usize;
        let end = self.offsets[state + 1] as usize;
        &self.successors[start..end]
    }

    fn describe(&self, position: usize, elements: &[ElementDecl]) -> String {
        match &self.terms[position] {
            Term::Element(e) => elements[*e].name.to_string(),
            Term::Wildcard(_) => "a wildcard".to_owned(),
        }
    }

    fn successors(&self, state: State) -> &[u32] {
        self.state_successors(state.map_or(0, |p| p + 1))
    }

    /// The position a child element matches from `state`, with its term.
    pub(crate) fn step(
        &self,
        state: State,
        namespace: Option<&str>,
        local: &str,
        elements: &[ElementDecl],
    ) -> Option<(usize, &Term)> {
        self.successors(state).iter().find_map(|&p| {
            let p = p as usize;
            let term = &self.terms[p];
            let matches = match term {
                Term::Element(e) => elements[*e].name.matches(namespace, local),
                Term::Wildcard(w) => w.allows(namespace),
            };
            matches.then_some((p, term))
        })
    }

    pub(crate) fn accepts(&self, state: State) -> bool {
        match state {
            None => self.nullable,
            Some(p) => self.is_final[p],
        }
    }

    /// What may come next from `state`, for error messages.
    pub(crate) fn expected(&self, state: State, elements: &[ElementDecl]) -> Vec<String> {
        let mut names: Vec<String> = self
            .successors(state)
            .iter()
            .map(|&p| match &self.terms[p as usize] {
                Term::Element(e) => elements[*e].name.to_string(),
                Term::Wildcard(w) => match &w.namespaces {
                    crate::model::NamespaceConstraint::Any => "##any".to_owned(),
                    crate::model::NamespaceConstraint::Other(Some(ns)) => {
                        format!("##other{{{ns}}}")
                    }
                    crate::model::NamespaceConstraint::Other(None) => "##other".to_owned(),
                },
            })
            .collect();
        names.dedup();
        names
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{NamespaceConstraint, ProcessContents, QName};

    fn decls(names: &[&str]) -> Vec<ElementDecl> {
        names
            .iter()
            .map(|n| ElementDecl {
                name: QName::new(Some("urn:t"), n),
                type_id: 0,
            })
            .collect()
    }

    fn element(id: ElementId, min: u32, max: Option<u32>) -> Particle {
        Particle {
            term: ParticleTerm::Element(id),
            min,
            max,
        }
    }

    fn run(automaton: &Automaton, elements: &[ElementDecl], children: &[&str]) -> bool {
        let mut state = None;
        for child in children {
            match automaton.step(state, Some("urn:t"), child, elements) {
                Some((p, _)) => state = Some(p),
                None => return false,
            }
        }
        automaton.accepts(state)
    }

    #[test]
    fn sequence_with_occurrences() {
        let elements = decls(&["a", "b", "c"]);
        let model = Particle {
            term: ParticleTerm::Sequence(vec![
                element(0, 0, Some(1)),
                element(1, 1, None),
                element(2, 2, Some(3)),
            ]),
            min: 1,
            max: Some(1),
        };
        let automaton = Automaton::compile(&model, &elements).unwrap();
        assert!(run(&automaton, &elements, &["a", "b", "c", "c"]));
        assert!(run(&automaton, &elements, &["b", "b", "c", "c", "c"]));
        assert!(!run(&automaton, &elements, &["b", "c"]));
        assert!(!run(&automaton, &elements, &["b", "c", "c", "c", "c"]));
        assert!(!run(&automaton, &elements, &["a", "a", "b", "c", "c"]));
        assert!(!run(&automaton, &elements, &["c", "c"]));
    }

    #[test]
    fn repeated_choice_of_sequences() {
        let elements = decls(&["a", "b", "c"]);
        let model = Particle {
            term: ParticleTerm::Choice(vec![
                Particle {
                    term: ParticleTerm::Sequence(vec![
                        element(0, 1, Some(1)),
                        element(1, 0, Some(1)),
                    ]),
                    min: 1,
                    max: Some(1),
                },
                element(2, 1, Some(1)),
            ]),
            min: 0,
            max: None,
        };
        let automaton = Automaton::compile(&model, &elements).unwrap();
        assert!(run(&automaton, &elements, &[]));
        assert!(run(&automaton, &elements, &["a", "c", "a", "b", "c"]));
        assert!(!run(&automaton, &elements, &["b"]));
    }

    /// Every end index reachable by matching `particle` from `start` in `word`
    /// (element ids as letters): a direct backtracking reading of the particle,
    /// independent of the Glushkov construction.
    fn ends(
        particle: &Particle,
        word: &[ElementId],
        start: usize,
    ) -> std::collections::BTreeSet<usize> {
        use std::collections::BTreeSet;
        let once = |from: usize| -> BTreeSet<usize> {
            match &particle.term {
                ParticleTerm::Element(id) => word
                    .get(from)
                    .filter(|&&c| c == *id)
                    .map(|_| from + 1)
                    .into_iter()
                    .collect(),
                ParticleTerm::Wildcard(_) => word.get(from).map(|_| from + 1).into_iter().collect(),
                ParticleTerm::Sequence(items) => {
                    items
                        .iter()
                        .fold(BTreeSet::from([from]), |positions, item| {
                            positions
                                .iter()
                                .flat_map(|&p| ends(item, word, p))
                                .collect()
                        })
                }
                ParticleTerm::Choice(items) => items
                    .iter()
                    .flat_map(|item| ends(item, word, from))
                    .collect(),
            }
        };
        let mut result = BTreeSet::new();
        let mut current = BTreeSet::from([start]);
        let limit = particle
            .max
            .unwrap_or(u32::try_from(word.len()).unwrap() + particle.min + 1);
        for count in 0..=limit {
            if count >= particle.min {
                result.extend(current.iter().copied());
            }
            if count == limit {
                break;
            }
            current = current.iter().flat_map(|&p| once(p)).collect();
        }
        result
    }

    fn particle_strategy() -> impl proptest::strategy::Strategy<Value = Particle> {
        use proptest::prelude::*;
        let occurs = prop_oneof![
            Just((1_u32, Some(1_u32))),
            Just((0, Some(1))),
            Just((0, None)),
            Just((1, None)),
            Just((2, Some(3))),
            Just((0, Some(2)))
        ];
        let leaf = (0_usize..3, occurs.clone()).prop_map(|(id, (min, max))| Particle {
            term: ParticleTerm::Element(id),
            min,
            max,
        });
        leaf.prop_recursive(3, 12, 3, move |inner| {
            (
                prop::collection::vec(inner, 0..4),
                any::<bool>(),
                occurs.clone(),
            )
                .prop_map(|(items, sequence, (min, max))| Particle {
                    term: if sequence {
                        ParticleTerm::Sequence(items)
                    } else {
                        ParticleTerm::Choice(items)
                    },
                    min,
                    max,
                })
        })
    }

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

        #[test]
        fn automaton_agrees_with_backtracking(particle in particle_strategy(), word in proptest::collection::vec(0_usize..3, 0..7)) {
            let elements = decls(&["a", "b", "c"]);
            if let Ok(automaton) = Automaton::compile(&particle, &elements) {
                let names = ["a", "b", "c"];
                let letters: Vec<&str> = word.iter().map(|&i| names[i]).collect();
                let expected = ends(&particle, &word, 0).contains(&word.len());
                proptest::prop_assert_eq!(run(&automaton, &elements, &letters), expected);
            }
        }
    }

    #[test]
    fn random_models_are_mostly_usable() {
        use proptest::strategy::{Strategy, ValueTree};
        use proptest::test_runner::TestRunner;
        let elements = decls(&["a", "b", "c"]);
        let mut runner = TestRunner::deterministic();
        let (mut compiled, mut accepted, mut rejected) = (0, 0, 0);
        for _ in 0..2000 {
            let particle = particle_strategy().new_tree(&mut runner).unwrap().current();
            let Ok(automaton) = Automaton::compile(&particle, &elements) else {
                continue;
            };
            compiled += 1;
            for word in [&["a"][..], &["a", "b"], &["b", "c", "a"], &[]] {
                if run(&automaton, &elements, word) {
                    accepted += 1;
                } else {
                    rejected += 1;
                }
            }
        }
        assert!(
            compiled > 500,
            "{compiled} deterministic models out of 2000"
        );
        assert!(
            accepted > 200 && rejected > 200,
            "{accepted} accepted, {rejected} rejected"
        );
    }

    #[test]
    fn non_deterministic_models_are_refused() {
        let elements = decls(&["a", "b"]);
        // (a?, a)
        let model = Particle {
            term: ParticleTerm::Sequence(vec![element(0, 0, Some(1)), element(0, 1, Some(1))]),
            min: 1,
            max: Some(1),
        };
        assert!(Automaton::compile(&model, &elements).is_err());
        // (##any | a)
        let model = Particle {
            term: ParticleTerm::Choice(vec![
                Particle {
                    term: ParticleTerm::Wildcard(Wildcard {
                        namespaces: NamespaceConstraint::Any,
                        process: ProcessContents::Lax,
                    }),
                    min: 1,
                    max: Some(1),
                },
                element(0, 1, Some(1)),
            ]),
            min: 1,
            max: Some(1),
        };
        assert!(Automaton::compile(&model, &elements).is_err());
        // (##other{urn:t} | a) is deterministic: a is in urn:t.
        let model = Particle {
            term: ParticleTerm::Choice(vec![
                Particle {
                    term: ParticleTerm::Wildcard(Wildcard {
                        namespaces: NamespaceConstraint::Other(Some("urn:t".to_owned())),
                        process: ProcessContents::Lax,
                    }),
                    min: 1,
                    max: Some(1),
                },
                element(0, 1, Some(1)),
            ]),
            min: 1,
            max: Some(1),
        };
        assert!(Automaton::compile(&model, &elements).is_ok());
    }
}