xmlschema 0.0.6

XML Schema (XSD) validation for Rust, with zero unsafe code
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 xmlschema. All rights reserved.

//! *Particle Valid (Restriction)* — the subsumption relation.
//!
//! A restriction must accept nothing its base would reject. The
//! relation is defined case by case on the pair of terms, and each
//! case is exercised here on the pair that distinguishes it from its
//! neighbour.

use xmlschema::derive::{
    Compositor, Particle, Term, is_valid_restriction, namespace_subset,
};

/// Nothing derives from anything, unless it is the same name.
fn nothing(_: &str, _: &str) -> bool {
    false
}

fn element(name: &str, min: usize, max: Option<usize>) -> Particle {
    Particle {
        min,
        max,
        term: Term::Element {
            name: name.to_owned(),
            type_name: None,
        },
    }
}

fn typed(name: &str, type_name: &str) -> Particle {
    Particle {
        min: 1,
        max: Some(1),
        term: Term::Element {
            name: name.to_owned(),
            type_name: Some(type_name.to_owned()),
        },
    }
}

fn wildcard(ns: &str, min: usize, max: Option<usize>) -> Particle {
    Particle {
        min,
        max,
        term: Term::Wildcard(ns.to_owned()),
    }
}

fn group(
    compositor: Compositor,
    particles: Vec<Particle>,
    min: usize,
    max: Option<usize>,
) -> Particle {
    Particle {
        min,
        max,
        term: Term::Group {
            compositor,
            particles,
        },
    }
}

fn ok(derived: &Particle, base: &Particle) -> bool {
    is_valid_restriction(derived, base, &nothing)
}

#[test]
fn an_occurrence_range_must_lie_inside_the_base() {
    let base = element("a", 1, Some(5));
    assert!(ok(&element("a", 1, Some(5)), &base), "identical");
    assert!(ok(&element("a", 2, Some(4)), &base), "narrower");
    assert!(!ok(&element("a", 0, Some(5)), &base), "a lower minimum");
    assert!(!ok(&element("a", 1, Some(6)), &base), "a higher maximum");
    assert!(!ok(&element("a", 1, None), &base), "unbounded past a bound");

    // An unbounded base contains everything.
    let unbounded = element("a", 0, None);
    assert!(ok(&element("a", 5, Some(9)), &unbounded));
    assert!(ok(&element("a", 0, None), &unbounded));
}

#[test]
fn an_element_must_keep_its_name() {
    assert!(ok(&element("a", 1, Some(1)), &element("a", 1, Some(1))));
    assert!(!ok(&element("b", 1, Some(1)), &element("a", 1, Some(1))));
}

#[test]
fn a_base_with_no_named_type_accepts_any_type() {
    // The base is the ur-type, which everything derives from.
    assert!(ok(&typed("a", "MyType"), &element("a", 1, Some(1))));
    // A different named type cannot be established as derived, and
    // `nothing` says so.
    assert!(!ok(&typed("a", "X"), &typed("a", "Y")));
    // The same type is trivially fine.
    assert!(ok(&typed("a", "X"), &typed("a", "X")));
    // A real derivation relation is consulted when one is given.
    let type_derives = |d: &str, b: &str| d == "X" && b == "Y";
    assert!(is_valid_restriction(
        &typed("a", "X"),
        &typed("a", "Y"),
        &type_derives
    ));
}

#[test]
fn a_wildcard_may_only_narrow_its_namespaces() {
    let base = wildcard("urn:a urn:b", 1, Some(1));
    assert!(ok(&wildcard("urn:a", 1, Some(1)), &base));
    assert!(ok(&wildcard("urn:a urn:b", 1, Some(1)), &base));
    assert!(!ok(&wildcard("urn:c", 1, Some(1)), &base));
    assert!(!ok(&wildcard("##any", 1, Some(1)), &base));
    // Anything fits inside `##any`.
    assert!(ok(
        &wildcard("urn:a", 1, Some(1)),
        &wildcard("##any", 1, Some(1))
    ));

    assert!(namespace_subset("urn:a", "##any"));
    assert!(!namespace_subset("##any", "urn:a"));
    assert!(namespace_subset("urn:a urn:b", "urn:a urn:b urn:c"));
    assert!(!namespace_subset("urn:a urn:z", "urn:a urn:b"));
}

#[test]
fn a_wildcard_cannot_restrict_an_element() {
    // It admits more, which is the opposite of restricting.
    assert!(!ok(
        &wildcard("##any", 1, Some(1)),
        &element("a", 1, Some(1))
    ));
}

/// A group of one, occurring once, *is* the particle it contains.
#[test]
fn a_pointless_particle_is_eliminated() {
    let wrapped = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&wrapped, &element("a", 1, Some(1))));
    assert!(ok(&element("a", 1, Some(1)), &wrapped));

    // Nested wrappers collapse all the way down.
    let deep = group(Compositor::Choice, vec![wrapped.clone()], 1, Some(1));
    assert!(ok(&deep, &element("a", 1, Some(1))));

    // A group that occurs more than once is not pointless.
    let repeated = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1))],
        1,
        Some(3),
    );
    assert!(!ok(&repeated, &element("a", 1, Some(1))));
}

/// A group restricting a wildcard is decided on its *total* range,
/// not on each particle's.
#[test]
fn a_group_restricting_a_wildcard_counts_its_whole_content() {
    let base = wildcard("##any", 3, Some(3));
    let three = group(
        Compositor::All,
        vec![
            element("e1", 1, Some(1)),
            element("e2", 1, Some(1)),
            element("e3", 1, Some(1)),
        ],
        1,
        Some(1),
    );
    assert!(ok(&three, &base), "three elements match three wildcards");

    // Two do not reach the base's minimum of three.
    let two = group(
        Compositor::All,
        vec![element("e1", 1, Some(1)), element("e2", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&two, &base));

    // Namespace still applies to each particle.
    let narrow = wildcard("urn:a", 1, Some(3));
    let any_group = group(
        Compositor::Sequence,
        vec![wildcard("urn:b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&any_group, &narrow));
}

#[test]
fn a_sequence_restricting_a_sequence_keeps_its_order() {
    let base = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    let same = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&same, &base));

    let swapped = group(
        Compositor::Sequence,
        vec![element("b", 1, Some(1)), element("a", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&swapped, &base), "a sequence's order is required");
}

/// Anything the restriction drops must have been skippable.
#[test]
fn a_dropped_particle_must_have_been_optional() {
    let optional_b = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 0, Some(1))],
        1,
        Some(1),
    );
    let just_a = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&just_a, &optional_b), "b was optional");

    let required_b = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&just_a, &required_b), "b was required");
}

/// `xs:all` imposes no order, so a restriction of it need not keep one.
#[test]
fn restricting_an_all_group_ignores_order() {
    let base = group(
        Compositor::All,
        vec![element("e1", 1, Some(1)), element("e2", 1, Some(1))],
        1,
        Some(1),
    );
    let reversed = group(
        Compositor::Sequence,
        vec![element("e2", 1, Some(1)), element("e1", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&reversed, &base), "order is not the base's requirement");

    // But every required particle must still be there.
    let partial = group(
        Compositor::Sequence,
        vec![element("e2", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&partial, &base), "e1 was required");
}

#[test]
fn a_choice_may_drop_branches_but_not_add_them() {
    let base = group(
        Compositor::Choice,
        vec![
            element("a", 1, Some(1)),
            element("b", 1, Some(1)),
            element("c", 1, Some(1)),
        ],
        1,
        Some(1),
    );
    let fewer = group(
        Compositor::Choice,
        vec![element("a", 1, Some(1)), element("c", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&fewer, &base), "dropping a branch narrows the choice");

    let extra = group(
        Compositor::Choice,
        vec![element("a", 1, Some(1)), element("z", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&extra, &base), "z was never on offer");
}

/// A choice cannot restrict a sequence: it drops the requirement that
/// every particle appear.
#[test]
fn a_choice_cannot_restrict_a_sequence() {
    let base = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    let choice = group(
        Compositor::Choice,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&choice, &base));
}

#[test]
fn a_sequence_restricting_a_choice_maps_each_particle_to_a_branch() {
    let base = group(
        Compositor::Choice,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    let both = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&both, &base), "each particle is one of the branches");

    let stranger = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("z", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&stranger, &base));
}

#[test]
fn emptiability_follows_the_compositor() {
    // A sequence is emptiable when every particle is.
    let seq = group(
        Compositor::Sequence,
        vec![element("a", 0, Some(1)), element("b", 0, Some(1))],
        1,
        Some(1),
    );
    assert!(seq.emptiable());

    let seq_required = group(
        Compositor::Sequence,
        vec![element("a", 0, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!seq_required.emptiable());

    // A choice is emptiable when any branch is.
    let choice = group(
        Compositor::Choice,
        vec![element("a", 0, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(choice.emptiable());

    // And anything with a zero minimum is emptiable outright.
    assert!(element("a", 0, Some(1)).emptiable());
    assert!(!element("a", 1, Some(1)).emptiable());
}

#[test]
fn the_effective_total_range_multiplies_through() {
    // Two elements in a sequence occurring twice: four in total.
    let seq = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        2,
        Some(2),
    );
    assert_eq!(seq.effective_total_range(), (4, Some(4)));

    // A choice takes the least and greatest any branch offers.
    let choice = group(
        Compositor::Choice,
        vec![element("a", 1, Some(1)), element("b", 2, Some(3))],
        1,
        Some(1),
    );
    assert_eq!(choice.effective_total_range(), (1, Some(3)));

    // Unbounded anywhere makes the total unbounded.
    let open = group(
        Compositor::Sequence,
        vec![element("a", 1, None)],
        1,
        Some(1),
    );
    assert_eq!(open.effective_total_range(), (1, None));
}

/// An element restricting a choice takes any branch; restricting a
/// sequence requires the rest to be skippable.
#[test]
fn an_element_restricting_a_group_is_decided_by_the_compositor() {
    let choice = group(
        Compositor::Choice,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(
        ok(&element("a", 1, Some(1)), &choice),
        "one branch is enough"
    );
    assert!(!ok(&element("z", 1, Some(1)), &choice), "z is no branch");

    // A sequence whose other particles are optional.
    let optional = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 0, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&element("a", 1, Some(1)), &optional));

    // And one whose others are not.
    let required = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&element("a", 1, Some(1)), &required));
}

/// A group of one, occurring once, restricting an element unwraps to
/// that element; anything richer has no rule.
#[test]
fn a_wrapped_element_restricts_the_element_it_wraps() {
    let base = element("a", 1, Some(1));
    let wrapped = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&wrapped, &base));

    // Two particles is not a wrapper.
    let two = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("b", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&two, &base));
}

/// An unordered mapping uses each base particle at most once, and
/// leaves nothing required behind.
#[test]
fn an_unordered_mapping_consumes_each_particle_once() {
    let base = group(
        Compositor::All,
        vec![
            element("a", 1, Some(1)),
            element("b", 1, Some(1)),
            element("c", 0, Some(1)),
        ],
        1,
        Some(1),
    );
    // Both required particles present, in any order; `c` was optional.
    let reordered = group(
        Compositor::Sequence,
        vec![element("b", 1, Some(1)), element("a", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(ok(&reordered, &base));

    // `a` twice cannot map onto one `a`.
    let doubled = group(
        Compositor::Sequence,
        vec![element("a", 1, Some(1)), element("a", 1, Some(1))],
        1,
        Some(1),
    );
    assert!(!ok(&doubled, &base), "each base particle is used once");
}

/// A wildcard restricting a wildcard of a different namespace form.
#[test]
fn namespace_subsetting_covers_the_special_forms() {
    // `##other` against a list is left undecided, which means accepted.
    assert!(namespace_subset("##other", "urn:a"));
    assert!(namespace_subset("urn:a", "##other"));
    // Identical constraints are trivially a subset.
    assert!(namespace_subset("##other", "##other"));
    assert!(namespace_subset("##any", "##any"));
}