automapper-validation 0.33.0

AHB condition expression parsing, evaluation, and EDIFACT validation
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
//! The MIG syntax check a CONTRL reports: one message against its MIG alone.
//!
//! No Prüfidentifikator, AHB or condition takes part. A CONTRL reports syntax
//! errors only (CONTRL AHB 1.0 §2.1); a code wrong only in its context is an
//! AHB matter and goes to the APERAK. What is checked:
//!
//! - **UNH and UNT** (UCM): their elements, the segment count (UNT 0074, code
//!   29) and the message reference (UNT 0062 against UNH 0062, code 28). An
//!   error here stops the check of that message: nothing else is reported.
//! - **Segments** (UCS): a segment the MIG marks `M`/`R` missing (13), one the
//!   MIG has no place for (15), more data elements than the MIG defines (16),
//!   a segment or group repeating beyond its `MaxRep` (35, 36).
//! - **Data elements** (UCD): an `M`/`R` element or component empty (13), a
//!   value outside the MIG's code list (12), too many components (16), a bad
//!   decimal notation (19), characters outside the syntax level (21), a
//!   letter in a numeric element (37), no digit before the decimal mark (38),
//!   too long (39), too short (40).
//!
//! The codes are those the CONTRL MIG 2.0b allows in UCM/UCS/UCD 0085. Every
//! finding carries its segment counter from UNH = 1 (UCS 0096); a missing
//! segment or group is reported at its predecessor. Element positions follow
//! S011: the segment tag is 0098 = 1, the first component 0104 = 1.
//!
//! The message is placed with the assembler on the MIG with every segment
//! number kept, i.e. as if a Prüfidentifikator allowed all the MIG allows.

use std::collections::{HashMap, HashSet};

use mig_assembly::assembler::{AssembledGroup, AssembledSegment, Assembler, AssemblerConfig};
use mig_assembly::{pid_filter, structure_check, StructureDiagnosticKind};
use mig_types::schema::common::CodeDefinition;
use mig_types::schema::mig::{MigDataElement, MigSchema, MigSegment, MigSegmentGroup};
use mig_types::segment::OwnedSegment;
use serde::{Deserialize, Serialize};

/// Which CONTRL segment reports a finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SyntaxScope {
    /// UCM: an error in UNH or UNT.
    Message,
    /// UCS: an error of a whole segment (0085 in UCS).
    Segment,
    /// UCD: an error of a data element in a segment.
    Element,
}

/// One syntax error of a message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyntaxFinding {
    /// The CONTRL segment that reports it.
    pub scope: SyntaxScope,
    /// The syntax error code, DE0085 (`"13"`, `"35"`, …).
    pub code: String,
    /// The segment the error is in; for a missing one, the missing segment
    /// (a missing group: its first segment).
    pub segment_tag: String,
    /// The segment counter within the message, UNH = 1 (UCS 0096). A missing
    /// segment or group is reported at its predecessor.
    pub message_segment_number: u32,
    /// The data element's position in the segment, the tag being 1 (0098).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub element_position: Option<u32>,
    /// The component's position in its composite, from 1 (0104).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub component_position: Option<u32>,
    /// What is wrong, for people.
    pub detail: String,
}

/// Values accepted beyond the MIG's own code lists, by MIG segment `Number`,
/// element index and component index (0-based). A MIG's code list can lag
/// its AHBs — a Prüfidentifikator an AHB correction adds, an answer code an
/// AHB takes from a Codeliste the MIG predates — and what an AHB allows is
/// never a syntax error.
pub type AcceptedCodes = HashMap<(String, usize, usize), Accepted>;

/// What an element accepts beyond its MIG code list.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Accepted {
    /// Any value: an AHB leaves the element open.
    pub any: bool,
    /// These codes.
    pub codes: HashSet<String>,
}

impl Accepted {
    fn accepts(&self, value: &str) -> bool {
        self.any || self.codes.contains(value)
    }
}

/// The character repertoire of a syntax level (UNB S001 0001).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Charset {
    /// UNOA: upper-case letters, digits, space and a few signs.
    Unoa,
    /// UNOB: printable ASCII.
    Unob,
    /// UNOC: ISO 8859-1, the energy market's level.
    Unoc,
    /// Any other level: every character but control characters.
    Other,
}

impl Charset {
    /// The repertoire of syntax identifier `id` (`"UNOC"`).
    pub fn from_syntax_identifier(id: &str) -> Self {
        match id {
            "UNOA" => Self::Unoa,
            "UNOB" => Self::Unob,
            "UNOC" => Self::Unoc,
            _ => Self::Other,
        }
    }

    fn allows(self, c: char) -> bool {
        match self {
            Self::Unoa => {
                c.is_ascii_uppercase() || c.is_ascii_digit() || " .,-()/='+:?!\"%&*;<>".contains(c)
            }
            Self::Unob => (' '..='~').contains(&c),
            Self::Unoc => (' '..='~').contains(&c) || ('\u{A0}'..='\u{FF}').contains(&c),
            Self::Other => !c.is_control(),
        }
    }
}

/// The interchange's syntax settings a value is judged by.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SyntaxSettings {
    pub charset: Charset,
    /// The decimal mark (UNA, default `.`).
    pub decimal_mark: char,
}

impl Default for SyntaxSettings {
    fn default() -> Self {
        Self {
            charset: Charset::Unoc,
            decimal_mark: '.',
        }
    }
}

/// Check one message against `mig`.
///
/// `segments` are the message's segments as the assembler takes them for
/// `mig` (`MessageChunk::segments_for_mig`): UNH…UNT, with the envelope
/// before them where the MIG covers it. Only UNH…UNT is judged.
pub fn check_message_syntax(
    segments: &[OwnedSegment],
    mig: &MigSchema,
    settings: SyntaxSettings,
    accepted: &AcceptedCodes,
) -> Vec<SyntaxFinding> {
    let Some(unh_index) = segments.iter().position(|s| s.id == "UNH") else {
        return Vec::new();
    };
    let unh = &segments[unh_index];
    let unh_number = unh.segment_number;
    let counter = |segment_number: u32| segment_number.saturating_sub(unh_number) + 1;
    let unt_index = segments.iter().rposition(|s| s.id == "UNT");

    // UCM: UNH and UNT. An error here stops the message.
    let mut out = Vec::new();
    for seg in [Some(unh_index), unt_index]
        .into_iter()
        .flatten()
        .map(|i| &segments[i])
    {
        if let Some(slot) = mig.segments.iter().find(|s| s.id == seg.id) {
            check_elements(
                seg,
                slot,
                counter(seg.segment_number),
                settings,
                accepted,
                &mut out,
            );
        }
    }
    match unt_index {
        Some(i) => {
            let unt = &segments[i];
            let count = (i - unh_index + 1).to_string();
            if !unt.get_element(0).is_empty() && unt.get_element(0) != count {
                out.push(finding(
                    SyntaxScope::Message,
                    "29",
                    unt,
                    counter(unt.segment_number),
                    Some((2, None)),
                    format!(
                        "UNT counts {} segments, the message has {count}",
                        unt.get_element(0)
                    ),
                ));
            }
            if !unt.get_element(1).is_empty() && unt.get_element(1) != unh.get_element(0) {
                out.push(finding(
                    SyntaxScope::Message,
                    "28",
                    unt,
                    counter(unt.segment_number),
                    Some((3, None)),
                    format!(
                        "UNT names message '{}', UNH '{}'",
                        unt.get_element(1),
                        unh.get_element(0)
                    ),
                ));
            }
        }
        None => out.push(SyntaxFinding {
            scope: SyntaxScope::Message,
            code: "13".into(),
            segment_tag: "UNT".into(),
            message_segment_number: counter(
                segments.last().map_or(unh_number, |s| s.segment_number),
            ),
            element_position: None,
            component_position: None,
            detail: "UNT is missing".into(),
        }),
    }
    for f in &mut out {
        f.scope = SyntaxScope::Message;
    }
    if !out.is_empty() {
        return out;
    }

    // Place the message on the MIG with every segment number kept. The
    // assembler can merge a group's variants or keep them apart; each loses
    // some messages (merged: the order within a variant, UTILTS SEQ+Z43 DTM
    // RFF; apart: ORDERS' UNS after its LIN groups). A message fits the MIG
    // when some placement fits it, so the one with fewer findings is taken.
    let numbers = all_numbers(mig);
    let unmerged = pid_filter::filter_mig_for_pid_unmerged(mig, &numbers);
    let place = |assembly_mig: &MigSchema| {
        let assembler = Assembler::with_config(
            assembly_mig,
            AssemblerConfig {
                strict_code_matching: true,
                skip_unknown_segments: true,
                ..Default::default()
            },
        );
        let (tree, mut diagnostics) = assembler.assemble_with_diagnostics(segments);
        diagnostics.extend(structure_check::check_structure(&tree, &unmerged));
        (tree, diagnostics)
    };
    let merged = place(&pid_filter::filter_mig_for_pid(mig, &numbers));
    let apart = place(&pid_filter::filter_mig_variant_aware(mig, &numbers));
    let (tree, diagnostics) = if apart.1.len() < merged.1.len() {
        apart
    } else {
        merged
    };

    let first_segments = group_first_segments(mig);
    let within =
        |n: u32| n >= unh_number && unt_index.map_or(true, |i| n <= segments[i].segment_number);
    let mut reported = HashSet::new();
    for diag in diagnostics {
        let n = u32::try_from(diag.position).unwrap_or(0);
        if !within(n) {
            continue;
        }
        let (code, tag) = match diag.kind {
            StructureDiagnosticKind::MissingRequiredSegment => ("13", diag.segment_id.clone()),
            StructureDiagnosticKind::MissingRequiredGroup => (
                "13",
                first_segments
                    .get(diag.segment_id.as_str())
                    .cloned()
                    .unwrap_or(diag.segment_id.clone()),
            ),
            StructureDiagnosticKind::MaxRepetitionsExceeded => ("35", diag.segment_id.clone()),
            StructureDiagnosticKind::GroupMaxRepetitionsExceeded => (
                "36",
                first_segments
                    .get(diag.segment_id.as_str())
                    .cloned()
                    .unwrap_or(diag.segment_id.clone()),
            ),
            _ => ("15", diag.segment_id.clone()),
        };
        // The assembler and the structure check can both see one defect.
        if !reported.insert((code, tag.clone(), n)) {
            continue;
        }
        out.push(SyntaxFinding {
            scope: SyntaxScope::Segment,
            code: code.into(),
            segment_tag: tag,
            message_segment_number: counter(n),
            element_position: None,
            component_position: None,
            detail: diag.message,
        });
    }

    // UCD: every segment between UNH and UNT against its slot, each group
    // repetition judged by its own MIG variant.
    let mut root: Vec<&AssembledSegment> = tree.segments.iter().collect();
    root.extend(tree.inter_group_segments.values().flatten());
    let mut walk = ElementWalk {
        settings,
        accepted,
        within: &within,
        counter: &counter,
        out: &mut out,
    };
    walk.segments(&root, &unmerged.segments);
    walk.groups(&tree.groups, &unmerged.segment_groups);

    // A segment whose only slot it fills with a wrong qualifier is reported
    // once, as that value (UCD 12), not also as out of place (UCS 15).
    let wrong_codes: HashSet<u32> = out
        .iter()
        .filter(|f| f.scope == SyntaxScope::Element && f.code == "12")
        .map(|f| f.message_segment_number)
        .collect();
    out.retain(|f| {
        !(f.scope == SyntaxScope::Segment
            && f.code == "15"
            && wrong_codes.contains(&f.message_segment_number))
    });

    out.sort_by_key(|f| {
        (
            f.message_segment_number,
            f.element_position,
            f.component_position,
        )
    });
    out
}

fn finding(
    scope: SyntaxScope,
    code: &str,
    seg: &OwnedSegment,
    message_segment_number: u32,
    at: Option<(u32, Option<u32>)>,
    detail: String,
) -> SyntaxFinding {
    SyntaxFinding {
        scope,
        code: code.into(),
        segment_tag: seg.id.clone(),
        message_segment_number,
        element_position: at.map(|(e, _)| e),
        component_position: at.and_then(|(_, c)| c),
        detail,
    }
}

/// Every segment `Number` of `mig`: the filter then keeps all of it.
fn all_numbers(mig: &MigSchema) -> HashSet<String> {
    fn groups(list: &[MigSegmentGroup], out: &mut HashSet<String>) {
        for g in list {
            out.extend(g.segments.iter().filter_map(|s| s.number.clone()));
            groups(&g.nested_groups, out);
        }
    }
    let mut out: HashSet<String> = mig
        .segments
        .iter()
        .filter_map(|s| s.number.clone())
        .collect();
    groups(&mig.segment_groups, &mut out);
    out
}

/// Group id → the tag of its first segment (a missing group is reported as
/// that segment).
fn group_first_segments(mig: &MigSchema) -> HashMap<&str, String> {
    fn walk<'m>(list: &'m [MigSegmentGroup], out: &mut HashMap<&'m str, String>) {
        for g in list {
            if let Some(first) = g.segments.first() {
                out.entry(g.id.as_str()).or_insert_with(|| first.id.clone());
            }
            walk(&g.nested_groups, out);
        }
    }
    let mut out = HashMap::new();
    walk(&mig.segment_groups, &mut out);
    out
}

/// The element check over the assembled tree, by MIG variant.
struct ElementWalk<'a> {
    settings: SyntaxSettings,
    accepted: &'a AcceptedCodes,
    within: &'a dyn Fn(u32) -> bool,
    counter: &'a dyn Fn(u32) -> u32,
    out: &'a mut Vec<SyntaxFinding>,
}

impl ElementWalk<'_> {
    fn groups(&mut self, present: &[AssembledGroup], variants: &[MigSegmentGroup]) {
        for group in present {
            let same_id: Vec<&MigSegmentGroup> =
                variants.iter().filter(|v| v.id == group.group_id).collect();
            for rep in &group.repetitions {
                // A repetition no variant claims is the structure check's
                // finding (UCS 15); its elements have no slots to be judged by.
                // Of several variants with its entry qualifier, the one it
                // fits best.
                let candidates: Vec<&MigSegmentGroup> = match same_id.as_slice() {
                    [only] => vec![*only],
                    _ => rep.segments.first().map_or(Vec::new(), |entry| {
                        same_id
                            .iter()
                            .copied()
                            .filter(|v| {
                                v.entry_qualifier().is_some_and(|q| {
                                    q.tag == entry.tag && q.matches(&entry.elements)
                                })
                            })
                            .collect()
                    }),
                };
                let mut best: Option<Vec<SyntaxFinding>> = None;
                for variant in candidates {
                    let mut found = Vec::new();
                    let mut walk = ElementWalk {
                        settings: self.settings,
                        accepted: self.accepted,
                        within: self.within,
                        counter: self.counter,
                        out: &mut found,
                    };
                    let own: Vec<&AssembledSegment> = rep.segments.iter().collect();
                    walk.segments(&own, &variant.segments);
                    walk.groups(&rep.child_groups, &variant.nested_groups);
                    if best.as_ref().map_or(true, |b| found.len() < b.len()) {
                        best = Some(found);
                    }
                }
                self.out.extend(best.unwrap_or_default());
            }
        }
    }

    fn segments(&mut self, present: &[&AssembledSegment], slots: &[MigSegment]) {
        for seg in present {
            let Some(n) = seg.segment_number else {
                continue;
            };
            if seg.tag == "UNH" || seg.tag == "UNT" || !(self.within)(n) {
                continue;
            }
            let owned = OwnedSegment {
                id: seg.tag.clone(),
                elements: seg.elements.clone(),
                segment_number: n,
            };
            // Of the slots `seg` may fill, the one it fits best: a group can
            // hold several slots of one tag and qualifier (DTM+Z21 by day and
            // by minute), and only one of them need take its values.
            let mut best: Option<Vec<SyntaxFinding>> = None;
            for slot in slots_for(seg, slots) {
                let mut found = Vec::new();
                check_elements(
                    &owned,
                    slot,
                    (self.counter)(n),
                    self.settings,
                    self.accepted,
                    &mut found,
                );
                if best.as_ref().map_or(true, |b| found.len() < b.len()) {
                    best = Some(found);
                }
            }
            self.out.extend(best.unwrap_or_default());
        }
    }
}

/// The slots of `slots` that `seg` may fill: those of its tag whose
/// qualifier it carries, else those of its tag without a qualifier. None
/// when every slot of its tag names qualifiers and `seg` carries none of
/// them: the structure check reports that segment (UCS 15) — unless there is
/// only one slot, whose qualifier is then judged as a value (UCD 12).
fn slots_for<'m>(seg: &AssembledSegment, slots: &'m [MigSegment]) -> Vec<&'m MigSegment> {
    let tagged: Vec<&MigSegment> = slots.iter().filter(|s| s.id == seg.tag).collect();
    let carries = |slot: &MigSegment| {
        qualifier(slot).map(|(e, c, codes)| {
            seg.elements
                .get(e)
                .and_then(|el| el.get(c))
                .is_some_and(|v| codes.contains(v))
        })
    };
    let qualified: Vec<&MigSegment> = tagged
        .iter()
        .copied()
        .filter(|slot| carries(slot) == Some(true))
        .collect();
    if !qualified.is_empty() {
        return qualified;
    }
    let open: Vec<&MigSegment> = tagged
        .iter()
        .copied()
        .filter(|slot| carries(slot).is_none())
        .collect();
    if !open.is_empty() {
        return open;
    }
    match tagged.as_slice() {
        [only] => vec![*only],
        _ => Vec::new(),
    }
}

/// A MIG example code (`XYZ`) stands for any value.
fn is_placeholder(code: &CodeDefinition) -> bool {
    code.name == "Beispielcode" || code.value == "XYZ"
}

/// The real codes of an element; empty when it takes any value.
fn codes(d: &MigDataElement) -> Vec<&str> {
    if d.codes.iter().any(is_placeholder) {
        return Vec::new();
    }
    d.codes
        .iter()
        .map(|c| c.value.as_str())
        .filter(|v| !v.is_empty())
        .collect()
}

/// A slot's qualifier: its first coded element, as (element, component, codes).
fn qualifier(slot: &MigSegment) -> Option<(usize, usize, Vec<String>)> {
    let simple = slot.data_elements.iter().map(|d| (d.position, 0, d));
    let composite = slot.composites.iter().flat_map(|c| {
        c.data_elements
            .iter()
            .map(move |d| (c.position, d.position, d))
    });
    simple
        .chain(composite)
        .filter(|(_, _, d)| !codes(d).is_empty())
        .min_by_key(|(e, c, _)| (*e, *c))
        .map(|(e, c, d)| (e, c, codes(d).into_iter().map(String::from).collect()))
}

fn required(spec: Option<&str>, std: Option<&str>) -> bool {
    matches!(spec.or(std).map(str::trim), Some("M" | "R"))
}

/// Check `seg`'s data elements against `slot`.
fn check_elements(
    seg: &OwnedSegment,
    slot: &MigSegment,
    counter: u32,
    settings: SyntaxSettings,
    accepted: &AcceptedCodes,
    out: &mut Vec<SyntaxFinding>,
) {
    let number = slot.number.as_deref().unwrap_or("");
    let also = |e: usize, c: usize| accepted.get(&(number.to_string(), e, c));
    let defined = slot
        .data_elements
        .iter()
        .map(|d| d.position)
        .chain(slot.composites.iter().map(|c| c.position))
        .max()
        .map_or(0, |p| p + 1);
    let last_filled = seg
        .elements
        .iter()
        .rposition(|e| e.iter().any(|c| !c.is_empty()))
        .map_or(0, |p| p + 1);
    if last_filled > defined {
        out.push(finding(
            SyntaxScope::Segment,
            "16",
            seg,
            counter,
            None,
            format!(
                "{} has {last_filled} data elements, the MIG defines {defined}",
                seg.id
            ),
        ));
    }

    for d in &slot.data_elements {
        let element = seg.elements.get(d.position);
        let at = d.position as u32 + 2;
        let value = element.and_then(|e| e.first()).map_or("", String::as_str);
        if element.is_some_and(|e| e.iter().skip(1).any(|c| !c.is_empty())) {
            out.push(finding(
                SyntaxScope::Element,
                "16",
                seg,
                counter,
                Some((at, None)),
                format!(
                    "{} {} is a simple data element but has components",
                    seg.id, d.id
                ),
            ));
        }
        check_value(
            seg,
            d,
            value,
            counter,
            (at, None),
            settings,
            also(d.position, 0),
            out,
        );
    }

    for c in &slot.composites {
        let at = c.position as u32 + 2;
        let components: &[String] = seg.elements.get(c.position).map_or(&[], Vec::as_slice);
        if components.iter().all(String::is_empty) {
            if required(c.status_spec.as_deref(), c.status_std.as_deref()) {
                out.push(finding(
                    SyntaxScope::Element,
                    "13",
                    seg,
                    counter,
                    Some((at, None)),
                    format!("{} {} ({}) is missing", seg.id, c.id, c.name),
                ));
            }
            continue;
        }
        let defined = c
            .data_elements
            .iter()
            .map(|d| d.position)
            .max()
            .map_or(0, |p| p + 1);
        let last_filled = components
            .iter()
            .rposition(|v| !v.is_empty())
            .map_or(0, |p| p + 1);
        if last_filled > defined {
            out.push(finding(
                SyntaxScope::Element,
                "16",
                seg,
                counter,
                Some((at, None)),
                format!(
                    "{} {} has {last_filled} components, the MIG defines {defined}",
                    seg.id, c.id
                ),
            ));
        }
        for d in &c.data_elements {
            let value = components.get(d.position).map_or("", String::as_str);
            check_value(
                seg,
                d,
                value,
                counter,
                (at, Some(d.position as u32 + 1)),
                settings,
                also(c.position, d.position),
                out,
            );
        }
    }
}

/// Check one simple or component data element's value.
fn check_value(
    seg: &OwnedSegment,
    d: &MigDataElement,
    value: &str,
    counter: u32,
    at: (u32, Option<u32>),
    settings: SyntaxSettings,
    also: Option<&Accepted>,
    out: &mut Vec<SyntaxFinding>,
) {
    let mut push = |code: &str, detail: String| {
        out.push(finding(
            SyntaxScope::Element,
            code,
            seg,
            counter,
            Some(at),
            detail,
        ));
    };
    let name = format!("{} {} ({})", seg.id, d.id, d.name);
    if value.is_empty() {
        if required(d.status_spec.as_deref(), d.status_std.as_deref()) {
            push("13", format!("{name} is missing"));
        }
        return;
    }
    if let Some(bad) = value.chars().find(|&c| !settings.charset.allows(c)) {
        push(
            "21",
            format!("{name} holds '{bad}', which its syntax level does not allow"),
        );
        return;
    }
    if let Some(format) = d.format_spec.as_deref().or(d.format_std.as_deref()) {
        if let Some((code, detail)) = check_format(value, format, settings.decimal_mark) {
            push(code, format!("{name} '{value}': {detail}"));
            return;
        }
    }
    let allowed = codes(d);
    if !allowed.is_empty() && !allowed.contains(&value) && !also.is_some_and(|a| a.accepts(value)) {
        push(
            "12",
            format!(
                "{name} '{value}' is none of the MIG's codes {}",
                allowed.join(", ")
            ),
        );
    }
}

/// A format `an..35`, `an3`, `n..15`, `n5`, `a3`: its kind, its length, and
/// whether the length is a maximum.
fn parse_format(format: &str) -> Option<(&str, usize, bool)> {
    let format = format.trim();
    let kind_end = format.find(|c: char| !c.is_ascii_alphabetic())?;
    let (kind, rest) = format.split_at(kind_end);
    let (upto, digits) = match rest.strip_prefix("..") {
        Some(d) => (true, d),
        None => (false, rest),
    };
    Some((kind, digits.parse().ok()?, upto))
}

/// The UCD code and reason when `value` breaks `format`.
fn check_format(value: &str, format: &str, decimal_mark: char) -> Option<(&'static str, String)> {
    let (kind, length, upto) = parse_format(format)?;
    let counted = match kind {
        "n" => {
            let unsigned = value.strip_prefix('-').unwrap_or(value);
            if let Some(c) = unsigned
                .chars()
                .find(|c| !c.is_ascii_digit() && *c != decimal_mark)
            {
                return Some(match c {
                    '.' | ',' => (
                        "19",
                        format!("'{c}' is not the decimal mark '{decimal_mark}'"),
                    ),
                    _ => ("37", format!("'{c}' in a numeric data element ({format})")),
                });
            }
            let marks = unsigned.matches(decimal_mark).count();
            if marks > 1 || unsigned.ends_with(decimal_mark) {
                return Some(("19", format!("not a decimal number ({format})")));
            }
            if unsigned.starts_with(decimal_mark) {
                return Some(("38", "no digit before the decimal mark".to_string()));
            }
            unsigned.chars().filter(char::is_ascii_digit).count()
        }
        "a" => {
            if let Some(c) = value.chars().find(char::is_ascii_digit) {
                return Some((
                    "37",
                    format!("'{c}' in an alphabetic data element ({format})"),
                ));
            }
            value.chars().count()
        }
        _ => value.chars().count(),
    };
    if counted > length {
        return Some((
            "39",
            format!("{counted} characters, {format} allows {length}"),
        ));
    }
    if !upto && counted < length {
        return Some((
            "40",
            format!("{counted} characters, {format} requires {length}"),
        ));
    }
    None
}

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

    #[test]
    fn formats() {
        assert_eq!(check_format("12345", "n5", '.'), None);
        assert_eq!(check_format("1234", "n5", '.').map(|f| f.0), Some("40"));
        assert_eq!(check_format("123456", "n..5", '.').map(|f| f.0), Some("39"));
        assert_eq!(check_format("-12.5", "n..3", '.'), None);
        assert_eq!(check_format(".5", "n..3", '.').map(|f| f.0), Some("38"));
        assert_eq!(check_format("5.", "n..3", '.').map(|f| f.0), Some("19"));
        assert_eq!(check_format("1,5", "n..3", '.').map(|f| f.0), Some("19"));
        assert_eq!(check_format("1A", "n..3", '.').map(|f| f.0), Some("37"));
        assert_eq!(check_format("Z1", "a..3", '.').map(|f| f.0), Some("37"));
        assert_eq!(check_format("ÄBC", "an3", '.'), None);
        assert_eq!(check_format("AB", "an3", '.').map(|f| f.0), Some("40"));
    }

    #[test]
    fn charsets() {
        assert!(Charset::Unoc.allows('ä'));
        assert!(!Charset::Unoc.allows('€'));
        assert!(!Charset::Unoc.allows('\n'));
        assert!(!Charset::Unoa.allows('a'));
        assert!(Charset::Unob.allows('a'));
    }
}