edifact 0.20.0

EDIFACT (ISO 9735) for Rust: zero-copy parser, envelope-computing writer, typed derive, layered validation, DTM dates and CONTRL
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
#![cfg(feature = "derive")]

//! Code-addressed element access: runtime accessors and the `layout` derive.
//!
//! The property under test throughout is that a *wrong* data element reference
//! fails loudly — as a directory lookup error at runtime, or as a failed `const`
//! assertion at compile time — instead of quietly reading the neighbouring
//! element the way a transposed positional index does.

use edifact::{
    ComponentRef, EdifactDeserialize, EdifactError, EdifactSerialize, ElementRef,
    OwnedComponentRef, OwnedElementRef, OwnedSegmentDef, SegmentDefinition, SegmentLayout, Status,
};

// ── directory tables ──────────────────────────────────────────────────────────
//
// `const` rather than `static`: the derive resolves identifiers during const
// evaluation, and a `const` item cannot read a `static`.

const C082_COMPONENTS: &[ComponentRef] = &[
    ComponentRef::new(1, "3039", Status::Mandatory),
    ComponentRef::new(2, "1131", Status::Conditional),
    ComponentRef::new(3, "3055", Status::Conditional),
];

const NAD_ELEMENTS: &[ElementRef] = &[
    ElementRef::new(1, "3035", Status::Mandatory, 1),
    ElementRef::composite(2, "C082", Status::Conditional, 1, C082_COMPONENTS),
];

pub const NAD: SegmentDefinition = SegmentDefinition::new("NAD", "Name and address", NAD_ELEMENTS);

const C507_COMPONENTS: &[ComponentRef] = &[
    ComponentRef::new(1, "2005", Status::Mandatory),
    ComponentRef::new(2, "2380", Status::Conditional),
    ComponentRef::new(3, "2379", Status::Conditional),
];

const DTM_ELEMENTS: &[ElementRef] = &[ElementRef::composite(
    1,
    "C507",
    Status::Mandatory,
    1,
    C507_COMPONENTS,
)];

pub const DTM: SegmentDefinition = SegmentDefinition::new("DTM", "Date/time/period", DTM_ELEMENTS);

// Resolution runs at compile time, so these are checked by the build itself.
const _: () = assert!(NAD.element_slot("3035") == 0);
const _: () = assert!(NAD.element_slot("3055") == 1);
const _: () = assert!(NAD.component_slot("3055") == 2);
const _: () = assert!(NAD.component_slot("C082") == 0);
const _: () = assert!(NAD.code_positions("9999") == 0);

// ── derive under a layout ─────────────────────────────────────────────────────

#[derive(Debug, PartialEq, EdifactDeserialize, EdifactSerialize)]
#[edifact(segment = "NAD", qualifier = "MS", layout = NAD)]
struct SenderParty {
    #[edifact(element = "3039")]
    party_id: String,
    #[edifact(element = "3055")]
    agency: Option<String>,
}

#[derive(Debug, PartialEq, EdifactDeserialize, EdifactSerialize)]
#[edifact(segment = "DTM", layout = DTM)]
struct DocumentDate {
    #[edifact(element = "2005")]
    qualifier: String,
    #[edifact(element = "2380")]
    value: String,
    #[edifact(element = "2379")]
    format: Option<String>,
}

/// A whole-element identifier and a component identifier, both `required`.
///
/// Exists to pin which "missing required" variant each shape reports.
#[derive(Debug, PartialEq, EdifactDeserialize)]
#[edifact(segment = "NAD", layout = NAD)]
struct RequiredShapes {
    #[edifact(element = "3035", required)]
    qualifier: Option<String>,
    #[edifact(element = "3055", required)]
    agency: Option<String>,
}

fn parse(input: &[u8]) -> Vec<edifact::Segment<'_>> {
    edifact::from_bytes(input)
        .collect::<Result<Vec<_>, _>>()
        .expect("fixture must parse")
}

// ── runtime accessors ─────────────────────────────────────────────────────────

#[test]
fn value_by_code_reads_elements_and_composite_components() {
    let segments = parse(b"NAD+MS+9900112233445::293'");
    let nad = &segments[0];

    assert_eq!(nad.value_by_code(&NAD, "3035").unwrap(), Some("MS"));
    assert_eq!(
        nad.value_by_code(&NAD, "3039").unwrap(),
        Some("9900112233445")
    );
    assert_eq!(nad.value_by_code(&NAD, "1131").unwrap(), Some(""));
    assert_eq!(nad.value_by_code(&NAD, "3055").unwrap(), Some("293"));
    // The composite addressed as a whole yields its first component.
    assert_eq!(
        nad.value_by_code(&NAD, "C082").unwrap(),
        Some("9900112233445")
    );
}

#[test]
fn unknown_data_element_is_an_error_not_a_wrong_read() {
    let segments = parse(b"NAD+MS+9900112233445::293'");
    let nad = &segments[0];

    // DE 2380 belongs to DTM, not NAD.  Positionally this would have read
    // *something*; by code it cannot.
    let err = nad.value_by_code(&NAD, "2380").unwrap_err();
    assert!(
        matches!(&err, EdifactError::UnknownDataElement { tag, data_element }
            if tag == "NAD" && data_element == "2380"),
        "expected UnknownDataElement, got {err:?}"
    );
    assert_eq!(err.stable_code(), "E033");
}

#[test]
fn applying_a_layout_to_the_wrong_segment_is_rejected() {
    let segments = parse(b"NAD+MS+9900112233445::293'");
    let err = segments[0].value_by_code(&DTM, "2005").unwrap_err();
    assert!(
        matches!(&err, EdifactError::SegmentLayoutMismatch { expected, actual }
            if expected == "DTM" && actual == "NAD"),
        "expected SegmentLayoutMismatch, got {err:?}"
    );
}

#[test]
fn ambiguous_data_element_is_rejected() {
    // A directory that repeats a code cannot be code-addressed for it.
    const REPEATED: &[ElementRef] = &[
        ElementRef::new(1, "1153", Status::Mandatory, 1),
        ElementRef::new(2, "1153", Status::Conditional, 1),
    ];
    const RFF: SegmentDefinition = SegmentDefinition::new("RFF", "Reference", REPEATED);

    let err = RFF.resolve_code("1153").unwrap_err();
    assert!(
        matches!(&err, EdifactError::AmbiguousDataElement { data_element, .. } if data_element == "1153"),
        "expected AmbiguousDataElement, got {err:?}"
    );
    assert_eq!(err.stable_code(), "E034");
}

#[test]
fn span_by_code_points_at_the_addressed_value() {
    let input = b"NAD+MS+9900112233445::293'";
    let segments = parse(input);
    let span = segments[0]
        .span_by_code(&NAD, "3055")
        .unwrap()
        .expect("DE 3055 is present");
    assert_eq!(&input[span.start..span.end], b"293");
}

#[test]
fn owned_and_borrowed_segments_resolve_identically() {
    let input = b"NAD+MS+9900112233445::293'";
    let owned: Vec<edifact::OwnedSegment> = edifact::from_reader(std::io::Cursor::new(input))
        .collect::<Result<Vec<_>, _>>()
        .expect("fixture must parse");

    assert_eq!(owned[0].value_by_code(&NAD, "3055").unwrap(), Some("293"));
    assert_eq!(
        owned[0].value_by_code(&NAD, "3039").unwrap(),
        Some("9900112233445")
    );
    assert_eq!(
        owned[0].span_by_code(&NAD, "3055").unwrap(),
        owned[0].span_by_code(&NAD, "3055").unwrap(),
    );
}

#[test]
fn runtime_loaded_definitions_resolve_the_same_codes() {
    // Directories loaded from JSON at startup get the same addressing as
    // compile-time tables.
    let def = OwnedSegmentDef::new_unchecked(
        "NAD".to_owned(),
        "Name and address".to_owned(),
        vec![
            OwnedElementRef::new_unchecked(1, "3035".to_owned(), Status::Mandatory, 1),
            OwnedElementRef::new_unchecked(2, "C082".to_owned(), Status::Conditional, 1)
                .with_components(vec![
                    OwnedComponentRef::new_unchecked(1, "3039".to_owned(), Status::Mandatory),
                    OwnedComponentRef::new_unchecked(2, "1131".to_owned(), Status::Conditional),
                    OwnedComponentRef::new_unchecked(3, "3055".to_owned(), Status::Conditional),
                ]),
        ],
    );

    let segments = parse(b"NAD+MS+9900112233445::293'");
    assert_eq!(
        segments[0].value_by_code(&def, "3055").unwrap(),
        Some("293")
    );
    assert_eq!(
        def.resolve_code("3039").unwrap(),
        NAD.resolve_code("3039").unwrap()
    );
    assert!(segments[0].value_by_code(&def, "2380").is_err());
}

// ── derive round-trip ─────────────────────────────────────────────────────────

#[test]
fn code_addressed_derive_deserializes_from_the_right_slots() {
    let segments = parse(b"NAD+MS+9900112233445::293'");
    let party = SenderParty::edifact_deserialize(&segments).expect("deserialize");
    assert_eq!(party.party_id, "9900112233445");
    assert_eq!(party.agency.as_deref(), Some("293"));
}

#[test]
fn code_addressed_derive_round_trips() {
    let segments = parse(b"NAD+MS+9900112233445::293'DTM+137:20260101:102'");

    let party = SenderParty::edifact_deserialize(&segments).expect("deserialize NAD");
    assert_eq!(
        edifact::to_edifact_string(&party).expect("serialize NAD"),
        "NAD+MS+9900112233445::293'"
    );

    let date = DocumentDate::edifact_deserialize(&segments).expect("deserialize DTM");
    assert_eq!(date.qualifier, "137");
    assert_eq!(date.value, "20260101");
    assert_eq!(date.format.as_deref(), Some("102"));
    assert_eq!(
        edifact::to_edifact_string(&date).expect("serialize DTM"),
        "DTM+137:20260101:102'"
    );
}

#[test]
fn code_addressed_derive_matches_the_owned_path() {
    let input = b"NAD+MS+9900112233445::293'";
    let owned: Vec<edifact::OwnedSegment> = edifact::from_reader(std::io::Cursor::new(input))
        .collect::<Result<Vec<_>, _>>()
        .expect("parse");
    let borrowed = parse(input);

    assert_eq!(
        SenderParty::edifact_deserialize(&owned).expect("owned"),
        SenderParty::edifact_deserialize(&borrowed).expect("borrowed"),
    );
}

#[test]
fn required_reports_the_variant_matching_what_the_field_addresses() {
    // A whole-element identifier must report E008, a component identifier E021.
    // Both resolve to component index 0 for the first component, so a naive
    // "has a component index" test picks the wrong code — and downstream
    // routing keyed on `error_code` then takes the wrong branch.
    let missing_qualifier = parse(b"NAD++9900112233445::293'");
    let err = RequiredShapes::edifact_deserialize(&missing_qualifier).unwrap_err();
    assert!(
        matches!(&err, EdifactError::MissingRequiredElement { tag, element_index }
            if tag == "NAD" && *element_index == 0),
        "DE 3035 names a whole element, expected MissingRequiredElement, got {err:?}"
    );
    assert_eq!(err.stable_code(), "E008");

    let missing_agency = parse(b"NAD+MS+9900112233445'");
    let err = RequiredShapes::edifact_deserialize(&missing_agency).unwrap_err();
    assert!(
        matches!(&err, EdifactError::MissingRequiredComponent { tag, element_index, component_index }
            if tag == "NAD" && *element_index == 1 && *component_index == 2),
        "DE 3055 names a component, expected MissingRequiredComponent, got {err:?}"
    );
    assert_eq!(err.stable_code(), "E021");
}

#[test]
fn required_variant_agrees_across_borrowed_and_owned_paths() {
    let input = b"NAD++9900112233445::293'";
    let owned: Vec<edifact::OwnedSegment> = edifact::from_reader(std::io::Cursor::new(input))
        .collect::<Result<Vec<_>, _>>()
        .expect("parse");
    let borrowed = parse(input);

    let from_owned = RequiredShapes::edifact_deserialize(&owned).unwrap_err();
    let from_borrowed = RequiredShapes::edifact_deserialize(&borrowed).unwrap_err();
    assert_eq!(from_owned.stable_code(), from_borrowed.stable_code());
    assert_eq!(from_owned, from_borrowed);
}

#[test]
fn declared_components_cap_a_composite_arity() {
    use edifact::{DirectoryValidator, ValidationReport, Validator};

    // C082 declares three components; a fourth is a structural error the
    // directory can now catch on its own.
    const DEFS: &[SegmentDefinition] = &[NAD];
    let validator = DirectoryValidator::from_definitions(DEFS).enforce_known_tags(false);

    let too_many = parse(b"NAD+MS+9900112233445::293:EXTRA'");
    let mut report = ValidationReport::default();
    validator.validate_batch(
        &too_many,
        &mut report,
        &edifact::ValidationRuleContext::empty(),
    );
    assert!(
        report
            .errors()
            .iter()
            .any(|i| i.error_code() == Some("E013")),
        "expected InvalidComponentCount, got {}",
        report.render_deterministic()
    );

    // Fewer components than declared is normal: conditional components may be
    // omitted, and trailing empties are stripped per ISO 9735-1 §8.7.2.
    let fewer = parse(b"NAD+MS+9900112233445'");
    let mut report = ValidationReport::default();
    validator.validate_batch(
        &fewer,
        &mut report,
        &edifact::ValidationRuleContext::empty(),
    );
    assert!(
        !report.has_errors(),
        "omitting conditional components must stay valid, got {}",
        report.render_deterministic()
    );
}

#[test]
fn absent_trailing_component_is_truncated() {
    // ISO 9735-1 §8.7.2: a trailing empty component and its separator are omitted.
    let party = SenderParty {
        party_id: "9900112233445".to_owned(),
        agency: None,
    };
    assert_eq!(
        edifact::to_edifact_string(&party).expect("serialize"),
        "NAD+MS+9900112233445'"
    );
}

// ── composites that repeat a data element by design ───────────────────────────

/// `C080 PARTY NAME` carries `3036` five times, then `3045`.
///
/// Declared as five separate `ComponentRef::new` entries, `code_positions` counts
/// five positions and `3036` resolves as ambiguous — so a *faithful* declaration
/// made the component unaddressable, and the only way to use named access was to
/// declare the composite incompletely.
const C080_FAITHFUL: &[ComponentRef] = &[
    ComponentRef::repeated(1, "3036", Status::Mandatory, 5),
    ComponentRef::new(6, "3045", Status::Conditional),
];
const NAD_C080_ELEMENTS: &[ElementRef] = &[
    ElementRef::new(1, "3035", Status::Mandatory, 1),
    ElementRef::composite(2, "C080", Status::Conditional, 1, C080_FAITHFUL),
];
const NAD_C080: SegmentDefinition =
    SegmentDefinition::new("NAD", "Name and address", NAD_C080_ELEMENTS);

#[test]
fn a_by_design_repeat_stays_addressable_and_points_at_the_first_occurrence() {
    assert_eq!(
        NAD_C080.code_positions("3036"),
        1,
        "one entry, one position"
    );

    let path = NAD_C080.resolve_code("3036").expect("addressable");
    assert_eq!((path.element, path.component), (1, Some(0)));

    let segments: Vec<_> = edifact::from_bytes(b"NAD+BY+ACME:GMBH:::+X'")
        .collect::<Result<Vec<_>, _>>()
        .expect("parse");
    assert_eq!(
        segments[0].value_by_code(&NAD_C080, "3036").unwrap(),
        Some("ACME"),
    );
    // The later components keep the positions the repeat pushed them to.
    assert_eq!(NAD_C080.resolve_code("3045").unwrap().component, Some(5));
}

#[test]
fn the_repeat_count_is_recorded_rather_than_lost() {
    let c080 = NAD_C080.elements[1];
    assert_eq!(c080.components()[0].repeat_count(), 5);
    assert_eq!(c080.components()[1].repeat_count(), 1);
}

#[test]
fn a_repeated_composite_is_not_capped_at_its_entry_count() {
    // Six values in the composite: the arity check must count slots (6), not
    // declared entries (2), or a conformant C080 is rejected.
    let segments: Vec<_> = edifact::from_bytes(b"NAD+BY+A:B:C:D:E:F'")
        .collect::<Result<Vec<_>, _>>()
        .expect("parse");

    let validator =
        edifact::DirectoryValidator::from_definitions(std::slice::from_ref(&NAD_C080_STATIC));
    let report = edifact::ValidationContext::builder()
        .with_validator(edifact::ValidationLayer::Structure, validator)
        .build()
        .validate(&segments);

    assert!(
        !report
            .errors()
            .iter()
            .any(|i| i.error_code() == Some("E013")),
        "six components must fit a composite declaring 5+1 slots: {:#?}",
        report.errors(),
    );
}

static NAD_C080_STATIC: SegmentDefinition = NAD_C080;

// ── layout auditing ───────────────────────────────────────────────────────────

mod layout_audit {
    use edifact::{
        ComponentRef, ElementRef, LayoutFinding, SegmentDefinition, SegmentLayout, Status,
        from_bytes,
    };

    /// A deliberately short `C507`: the directory declares 2005/2380/2379.
    static SHORT_C507: &[ComponentRef] = &[
        ComponentRef::new(1, "2005", Status::Mandatory),
        ComponentRef::new(2, "2380", Status::Conditional),
    ];
    static SHORT_DTM_ELEMENTS: &[ElementRef] = &[ElementRef::composite(
        1,
        "C507",
        Status::Mandatory,
        1,
        SHORT_C507,
    )];
    static SHORT_DTM: SegmentDefinition =
        SegmentDefinition::new("DTM", "Date/time/period", SHORT_DTM_ELEMENTS);

    /// The same segment declared correctly.
    static FULL_C507: &[ComponentRef] = &[
        ComponentRef::new(1, "2005", Status::Mandatory),
        ComponentRef::new(2, "2380", Status::Conditional),
        ComponentRef::new(3, "2379", Status::Conditional),
    ];
    static FULL_DTM_ELEMENTS: &[ElementRef] = &[ElementRef::composite(
        1,
        "C507",
        Status::Mandatory,
        1,
        FULL_C507,
    )];
    static FULL_DTM: SegmentDefinition =
        SegmentDefinition::new("DTM", "Date/time/period", FULL_DTM_ELEMENTS);

    fn corpus(input: &[u8]) -> Vec<edifact::Segment<'_>> {
        from_bytes(input)
            .collect::<Result<Vec<_>, _>>()
            .expect("parse")
    }

    #[test]
    fn a_layout_the_wire_disproves_is_a_contradiction() {
        // The format qualifier `102` has nowhere to go in the short layout.
        let segments = corpus(b"DTM+137:20260101:102'");
        let audit = SHORT_DTM.audit(&segments);

        assert_eq!(audit.segments_examined(), 1);
        assert!(audit.has_contradictions(), "{audit}");
        assert!(audit.findings().iter().any(|f| matches!(
            f,
            LayoutFinding::UndeclaredComponent {
                element: 0,
                component: 2,
                ..
            }
        )));
    }

    #[test]
    fn a_correct_layout_against_a_full_corpus_is_silent() {
        let segments = corpus(b"DTM+137:20260101:102'");
        let audit = FULL_DTM.audit(&segments);

        assert!(!audit.has_contradictions(), "{audit}");
        assert_eq!(audit.unconfirmed().count(), 0);
    }

    #[test]
    fn a_slot_the_corpus_never_reaches_is_reported_as_unconfirmed_not_wrong() {
        // This is the case that stalls a migration: the layout may be right or
        // wrong and the fixtures cannot say.  Silence here would be a lie.
        let segments = corpus(b"DTM+137:20260101'");
        let audit = FULL_DTM.audit(&segments);

        assert!(!audit.has_contradictions(), "{audit}");
        let unconfirmed: Vec<&str> = audit
            .unconfirmed()
            .map(|slot| slot.data_element.as_str())
            .collect();
        assert_eq!(unconfirmed, ["2379"]);
    }

    #[test]
    fn a_mandatory_slot_that_is_empty_everywhere_is_a_contradiction() {
        // `DTM+:20260101:102` leaves the mandatory qualifier empty.
        let segments = corpus(b"DTM+:20260101:102'");
        let audit = FULL_DTM.audit(&segments);

        assert!(audit.has_contradictions(), "{audit}");
        assert!(audit.findings().iter().any(|f| matches!(
            f,
            LayoutFinding::MandatoryNeverPopulated { slot } if slot.data_element == "2005"
        )));
    }

    #[test]
    fn an_empty_corpus_proves_nothing_and_says_so() {
        // Every slot comes back unconfirmed, and the count makes the reason
        // obvious rather than leaving a green test to imply approval.
        let segments = corpus(b"BGM+220'");
        let audit = FULL_DTM.audit(&segments);

        assert_eq!(audit.segments_examined(), 0);
        assert_eq!(audit.unconfirmed().count(), 2); // 2380 and 2379
        // The mandatory 2005 is a contradiction only in the sense that nothing
        // populated it; with no DTM at all that is what "unexamined" looks like.
        assert!(audit.has_contradictions());
    }

    #[test]
    fn repeated_occurrences_all_count_as_evidence() {
        // A component populated only in the second occurrence is still observed.
        let segments = corpus(b"UNA:+.?*'DTM+137:20260101*137:20260102:102'");
        let audit = FULL_DTM.audit(&segments);

        assert!(!audit.has_contradictions(), "{audit}");
        assert_eq!(audit.unconfirmed().count(), 0);
    }

    #[test]
    fn a_mandatory_component_of_an_absent_conditional_composite_is_not_a_violation() {
        // ISO 9735-1 §8.6 makes a mandatory component required "if the composite
        // data element is present" — not unconditionally.  Treating it as
        // unconditional condemns every optional composite in a definition, which
        // is most of them: `UNB` S005 comp 1 (0022) is mandatory inside a
        // conditional composite, so a conformant UNB without a password would
        // have been reported as violating its own layout.
        let corpus = corpus(b"UNB+UNOC:3+S+R+260101:0900+IC1'");
        let audit = edifact::service::UNB.audit(&corpus);

        assert_eq!(audit.segments_examined(), 1);
        assert!(!audit.has_contradictions(), "{audit}");
        // It is still surfaced — as a slot the corpus cannot speak to.
        assert!(audit.unconfirmed().any(|slot| slot.data_element == "0022"));
    }

    #[test]
    fn a_mandatory_component_of_a_present_composite_is_still_required() {
        // The other half of §8.6: once the composite is present, its mandatory
        // components are.  S005 comp 1 is populated here, comp 2 is optional.
        let corpus = corpus(b"UNB+UNOC:3+S+R+260101:0900+IC1+:AA'");
        let audit = edifact::service::UNB.audit(&corpus);

        assert!(audit.has_contradictions(), "{audit}");
        assert!(audit.findings().iter().any(|f| matches!(
            f,
            LayoutFinding::MandatoryNeverPopulated { slot } if slot.data_element == "0022"
        )));
    }

    #[test]
    fn a_directory_wide_audit_covers_every_tag_the_corpus_contains() {
        use edifact::{audit_directory, service};

        let corpus =
            corpus(b"UNB+UNOC:3+S+R+260101:0900+IC1'UNH+M1+ORDERS:D:96A:UN'UNT+2+M1'UNZ+1+IC1'");
        let audits = audit_directory(service::lookup, &corpus);

        // One per distinct tag, in the order they first appear.
        assert_eq!(
            audits
                .iter()
                .map(edifact::LayoutAudit::tag)
                .collect::<Vec<_>>(),
            ["UNB", "UNH", "UNT", "UNZ"],
        );
        assert!(audits.iter().all(|a| !a.has_contradictions()));
        // A tag the corpus does not contain is not audited at all, rather than
        // returning nothing but `NeverObserved` noise.
        assert!(!audits.iter().any(|a| a.tag() == "UNG"));
    }

    /// Audit **every shipped service definition** against a corpus that fills it.
    ///
    /// The directory-wide audit above runs over four nearly-empty envelope
    /// segments, where almost every position is `NeverObserved` — and
    /// `has_contradictions` deliberately ignores those, so it would pass against
    /// a layout with a transposed index. These tables are the ones users trust
    /// most (`value_by_code(&service::UNB, "0020")`) and the ones `Contrl`
    /// writes through, and a wrong index in either reads or writes a different,
    /// still-plausible value with no error anywhere.
    ///
    /// Filling every declared position turns the audit from "nothing disproved
    /// it" into "the wire confirmed it", and asserting against `service::ALL`
    /// means a definition cannot be added without a fixture that exercises it.
    #[test]
    fn every_shipped_service_layout_is_confirmed_by_a_corpus_that_fills_it() {
        use edifact::service;

        // Every service segment the module ships, each populated to the full
        // width its definition declares.  Not a valid interchange, and not
        // meant to be: the audit reads segments, not envelopes.
        let input: &[u8] = b"\
UNB+UNOA:4:SCD:XML:01+SENDER:14:REV:SUB+RECIPIENT:14:FWD:SUB+20260101:0900+IC4711+PW:AA+APPREF+A+1+COMMS+1'\
UNG+ORDERS+APPSND:14+APPRCV:14+20260101:0900+GRP1+UN+D:96A:EAN008+APPPW'\
UNH+MSG1+ORDERS:D:96A:UN:EAN008:CLD1:SUB+CAR+1:C+SUBSET:1:0:UN+GUIDE:1:0:UN+SCEN:1:0:UN'\
UNS+D'\
UGH+SG12'\
UGT+SG12'\
UNT+4+MSG1'\
UNE+1+GRP1'\
UNO+PKG1+AAB:REF1+OBJ:ATTRID:ATTR:UN+1024:3:1:C'\
UNP+1024+PKG1'\
UCI+IC4711+SENDER:14:REV:SUB+RECIPIENT:14:FWD:SUB+4+13+UNB+3:2:1+SEC1+2'\
UCF+GRP1+APPSND:14+APPRCV:14+4+13+UNG+3:2:1+SEC1+2'\
UCM+MSG1+ORDERS:D:96A:UN:EAN008:CLD1:SUB+4+13+UNH+3:2:1+PKG1+AAB:REF1+SEC1+2'\
UCS+7+13'\
UCD+13+3:2:1'\
UNZ+1+IC4711'";
        let segments = corpus(input);

        for definition in service::ALL {
            let audit = definition.audit(&segments);
            assert!(
                audit.segments_examined() > 0,
                "{} is shipped but the fixture never exercises it — add it rather \
                 than letting the audit skip it silently",
                definition.tag,
            );
            assert!(
                !audit.has_contradictions(),
                "{} disagrees with the wire:\n{audit}",
                definition.tag,
            );
            let unconfirmed: Vec<String> = audit
                .unconfirmed()
                .map(|slot| slot.data_element.clone())
                .collect();
            assert!(
                unconfirmed.is_empty(),
                "{} has positions the corpus never reached — either the fixture \
                 does not fill them or the layout declares something that is not \
                 there: {unconfirmed:?}",
                definition.tag,
            );
        }
    }

    #[test]
    fn one_finding_per_position_however_large_the_corpus() {
        // 360 fixtures must not produce 360 copies of the same finding.
        let mut input = Vec::new();
        for _ in 0..360 {
            input.extend_from_slice(b"DTM+137:20260101:102'");
        }
        let segments = corpus(&input);
        let audit = SHORT_DTM.audit(&segments);

        assert_eq!(audit.segments_examined(), 360);
        assert_eq!(audit.contradictions().count(), 1, "{audit}");
    }
}