edifact-rs 0.9.1

Zero-copy EDIFACT parser, writer, serde traits, and extensible validation support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//! Profile rule packs: `ProfileRule`, `ProfileRulePack`, and supporting types.

use super::ValidationRuleContext;
use super::Validator;
use crate::group::SegmentGroupIndexed;
use crate::{EdifactError, Segment, ValidationIssue, ValidationReport, ValidationSeverity};
use std::sync::Arc;

/// A profile rule that can be added to a [`ProfileRulePack`].
///
/// Implement this trait to create reusable, composable profile rules for
/// EDIFACT message validation.  Rules receive a [`ValidationRuleContext`] that
/// provides optional typed metadata injected at validation call time via
/// [`super::context::ValidationContext::validate_lenient_with`].
///
/// # Multiple issues per invocation
///
/// [`evaluate`](ProfileRule::evaluate) appends issues into a caller-supplied
/// `Vec` rather than returning a single `Option`.  This lets one rule iterate
/// every matching segment and report *all* violations — not just the first.
///
/// # `bail_on_first_error` interaction
///
/// When [`ProfileRulePack::bail_on_first_error`] is set, the pack stops calling
/// further rules as soon as this method pushes at least one error-severity issue.
/// Issues already pushed remain in the report; subsequent rules in the same pack
/// are skipped.
pub trait ProfileRule: Send + Sync {
    /// Evaluate the rule against the given segments.
    ///
    /// Push any violations into `issues`.  Push nothing if the segments pass.
    fn evaluate(
        &self,
        segments: &[Segment<'_>],
        context: &ValidationRuleContext<'_>,
        issues: &mut Vec<ValidationIssue>,
    );
}

/// Wraps a context-aware closure as a [`ProfileRule`].
struct ClosureProfileRule<F>(F);

impl<F> ProfileRule for ClosureProfileRule<F>
where
    F: for<'a> Fn(&[Segment<'a>], &ValidationRuleContext<'_>, &mut Vec<ValidationIssue>)
        + Send
        + Sync,
{
    fn evaluate(
        &self,
        segments: &[Segment<'_>],
        context: &ValidationRuleContext<'_>,
        issues: &mut Vec<ValidationIssue>,
    ) {
        (self.0)(segments, context, issues);
    }
}

/// Wraps a context-free closure as a [`ProfileRule`] (ignores the context parameter).
struct StatelessClosureProfileRule<F>(F);

impl<F> ProfileRule for StatelessClosureProfileRule<F>
where
    F: for<'a> Fn(&[Segment<'a>], &mut Vec<ValidationIssue>) + Send + Sync,
{
    fn evaluate(
        &self,
        segments: &[Segment<'_>],
        _context: &ValidationRuleContext<'_>,
        issues: &mut Vec<ValidationIssue>,
    ) {
        (self.0)(segments, issues);
    }
}

/// A rule entry inside a [`ProfileRulePack`], optionally carrying a stable identifier.
///
/// The `id` is used by [`ProfileRulePack::merge_with_override`] to de-duplicate rules:
/// when two packs contain a rule with the same id, the rule from the *other* (override)
/// pack replaces the one in `self`.
pub(super) struct NamedRule {
    /// Stable identifier for this rule, e.g. `"AHB-11001-BGM-M"`.
    ///
    /// `None` for anonymous rules that can never be overridden by id.
    pub(super) id: Option<Arc<str>>,
    pub(super) rule: Arc<dyn ProfileRule + Send + Sync>,
}

impl Clone for NamedRule {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            rule: Arc::clone(&self.rule),
        }
    }
}

/// A group-scoped rule entry inside a [`ProfileRulePack`].
///
/// Group rules are evaluated by [`ProfileRulePack`] during a segment-group tree
/// traversal (see [`ValidationContext::validate_lenient_grouped`]).  Each rule
/// receives the current [`SegmentGroupIndexed`] node, the full message segment
/// slice, and the validation context.
///
/// The `group_scope` field restricts evaluation to groups whose `definition` field
/// matches: `Some("SG5")` fires only inside `SG5` groups; `None` fires for every
/// group in the traversal.
pub(super) struct NamedGroupRule {
    /// Stable identifier, used for override deduplication.
    pub(super) id: Option<Arc<str>>,
    /// If `Some(name)`, this rule fires only when `group.definition == name`.
    pub(super) group_scope: Option<&'static str>,
    /// The rule closure.
    pub(super) rule: Arc<
        dyn Fn(
                &SegmentGroupIndexed,
                &[Segment<'_>],
                &ValidationRuleContext<'_>,
                &mut Vec<ValidationIssue>,
            ) + Send
            + Sync,
    >,
}

impl Clone for NamedGroupRule {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            group_scope: self.group_scope,
            rule: Arc::clone(&self.rule),
        }
    }
}

/// A profile/MIG rule pack that can be plugged into `ValidationContext`.
pub struct ProfileRulePack {
    name: String,
    /// Set of EDIFACT message types this pack is scoped to (e.g. `"ORDERS"`, `"INVOIC"`).
    message_types: std::collections::BTreeSet<String>,
    /// Association-assigned code (DE 0057) this pack is bound to, e.g. `"5.5.3a"`.
    release: Option<String>,
    pub(super) rules: Vec<NamedRule>,
    pub(super) group_rules: Vec<NamedGroupRule>,
    pub(super) bail_on_first_error: bool,
}

impl ProfileRulePack {
    /// Create an empty rule pack.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            message_types: std::collections::BTreeSet::new(),
            release: None,
            rules: Vec::new(),
            group_rules: Vec::new(),
            bail_on_first_error: false,
        }
    }

    /// Return the pack name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return the message types this pack is scoped to.
    pub fn message_types(&self) -> impl Iterator<Item = &str> {
        self.message_types.iter().map(|s| s.as_str())
    }

    /// Return the number of rules in this pack.
    pub fn rule_count(&self) -> usize {
        self.rules.len()
    }

    /// Return the number of named rules (those with a stable identifier).
    pub fn named_rule_count(&self) -> usize {
        self.rules.iter().filter(|r| r.id.is_some()).count()
    }

    /// Return the number of anonymous rules (those without a stable identifier).
    pub fn anonymous_rule_count(&self) -> usize {
        self.rules.iter().filter(|r| r.id.is_none()).count()
    }

    /// Iterate over the stable identifiers of all **named** rules in this pack.
    pub fn rule_ids(&self) -> impl Iterator<Item = &str> {
        self.rules.iter().filter_map(|r| r.id.as_deref())
    }

    /// Return the association-assigned release code this pack is bound to, if any.
    pub fn release(&self) -> Option<&str> {
        self.release.as_deref()
    }

    /// Restrict this pack to one or more EDIFACT message types from the `UNH` segment.
    pub fn for_message_type(mut self, message_type: impl Into<String>) -> Self {
        self.message_types.insert(message_type.into());
        self
    }

    /// Bind this pack to a specific association-assigned code (DE 0057).
    pub fn for_release(mut self, release: impl Into<String>) -> Self {
        self.release = Some(release.into());
        self
    }

    /// Stop evaluating rules in this pack after the first `Error`- or `Critical`-severity
    /// finding.
    pub fn bail_on_first_error(mut self, bail: bool) -> Self {
        self.bail_on_first_error = bail;
        self
    }

    /// Add a context-aware rule closure.
    pub fn with_rule_fn<F>(mut self, rule: F) -> Self
    where
        F: for<'a> Fn(&[Segment<'a>], &ValidationRuleContext<'_>, &mut Vec<ValidationIssue>)
            + Send
            + Sync
            + 'static,
    {
        self.rules.push(NamedRule {
            id: None,
            rule: Arc::new(ClosureProfileRule(rule)),
        });
        self
    }

    /// Add a context-aware rule closure with a stable identifier.
    pub fn with_named_rule_fn<F>(mut self, id: impl Into<Arc<str>>, rule: F) -> Self
    where
        F: for<'a> Fn(&[Segment<'a>], &ValidationRuleContext<'_>, &mut Vec<ValidationIssue>)
            + Send
            + Sync
            + 'static,
    {
        self.rules.push(NamedRule {
            id: Some(id.into()),
            rule: Arc::new(ClosureProfileRule(rule)),
        });
        self
    }

    /// Add a context-free rule closure.
    pub fn with_stateless_rule_fn<F>(mut self, rule: F) -> Self
    where
        F: for<'a> Fn(&[Segment<'a>], &mut Vec<ValidationIssue>) + Send + Sync + 'static,
    {
        self.rules.push(NamedRule {
            id: None,
            rule: Arc::new(StatelessClosureProfileRule(rule)),
        });
        self
    }

    /// Add a context-free rule closure with a stable identifier.
    pub fn with_named_stateless_rule_fn<F>(mut self, id: impl Into<Arc<str>>, rule: F) -> Self
    where
        F: for<'a> Fn(&[Segment<'a>], &mut Vec<ValidationIssue>) + Send + Sync + 'static,
    {
        self.rules.push(NamedRule {
            id: Some(id.into()),
            rule: Arc::new(StatelessClosureProfileRule(rule)),
        });
        self
    }

    /// Add a rule that asserts segment `tag` is present at least once.
    ///
    /// Emits an `Error`-severity issue when no segment with `tag` is found.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let pack = ProfileRulePack::new("MY-AHB")
    ///     .require_segment("BGM", "MY-BGM-M")
    ///     .require_segment("DTM", "MY-DTM-M");
    /// ```
    pub fn require_segment(self, tag: &'static str, rule_id: impl Into<Arc<str>>) -> Self {
        let id: Arc<str> = rule_id.into();
        self.with_named_stateless_rule_fn(id.clone(), move |segments, issues| {
            if !segments.iter().any(|s| s.tag == tag) {
                issues.push(
                    ValidationIssue::new(
                        ValidationSeverity::Error,
                        format!("mandatory segment {tag} is missing"),
                    )
                    .with_segment(tag)
                    .with_rule_id(id.as_ref()),
                );
            }
        })
    }

    /// Add a rule that asserts segment `tag` does **not** appear.
    ///
    /// Emits an `Error`-severity issue for each occurrence found.
    pub fn forbid_segment(self, tag: &'static str, rule_id: impl Into<Arc<str>>) -> Self {
        let id: Arc<str> = rule_id.into();
        self.with_named_stateless_rule_fn(id.clone(), move |segments, issues| {
            for (occ, _s) in segments.iter().filter(|s| s.tag == tag).enumerate() {
                issues.push(
                    ValidationIssue::new(
                        ValidationSeverity::Error,
                        format!("segment {tag} must not appear"),
                    )
                    .with_segment(tag)
                    .with_segment_occurrence(u16::try_from(occ).unwrap_or(u16::MAX))
                    .with_rule_id(id.as_ref()),
                );
            }
        })
    }

    /// Add a rule that asserts data element `de_qualifier` at `(element, component)` equals
    /// `qualifier` for every occurrence of `tag`.
    pub fn require_qualifier(
        self,
        tag: &'static str,
        element: u8,
        component: u8,
        qualifier: &'static str,
        rule_id: impl Into<Arc<str>>,
    ) -> Self {
        let id: Arc<str> = rule_id.into();
        self.with_named_stateless_rule_fn(id.clone(), move |segments, issues| {
            for (occ, s) in segments.iter().filter(|s| s.tag == tag).enumerate() {
                let actual = s
                    .get_element(element as usize)
                    .and_then(|e| e.get_component(component as usize));
                if actual != Some(qualifier) {
                    issues.push(
                        ValidationIssue::new(
                            ValidationSeverity::Error,
                            format!(
                                "segment {tag} element {element} component {component} must be \
                                 {qualifier:?} but found {:?}",
                                actual.unwrap_or("<absent>")
                            ),
                        )
                        .with_segment(tag)
                        .with_element_index(element)
                        .with_component_index(component)
                        .with_segment_occurrence(u16::try_from(occ).unwrap_or(u16::MAX))
                        .with_rule_id(id.as_ref()),
                    );
                }
            }
        })
    }

    // ── Group-scoped rule builders ──────────────────────────────────────────

    /// Add a group-aware rule closure that fires for **every** group node in the
    /// DFS traversal of the segment-group tree.
    ///
    /// The closure receives:
    /// - `group: &SegmentGroupIndexed` — the current tree node (with `definition`,
    ///   `total_span`, `children`).
    /// - `group_segments: &[Segment<'_>]` — all segments in this group's subtree
    ///   (`all_segments[group.total_span.clone()]`).
    /// - `context: &ValidationRuleContext<'_>` — per-call metadata and message info.
    /// - `issues: &mut Vec<ValidationIssue>` — push violations here.
    ///
    /// # Group-name scoping
    ///
    /// Use [`with_scoped_group_rule_fn`](Self::with_scoped_group_rule_fn) when you
    /// only want the rule to fire for a specific group definition (e.g. `"SG5"`).
    pub fn with_group_rule_fn<F>(mut self, rule: F) -> Self
    where
        F: Fn(
                &SegmentGroupIndexed,
                &[Segment<'_>],
                &ValidationRuleContext<'_>,
                &mut Vec<ValidationIssue>,
            ) + Send
            + Sync
            + 'static,
    {
        self.group_rules.push(NamedGroupRule {
            id: None,
            group_scope: None,
            rule: Arc::new(rule),
        });
        self
    }

    /// Add a **named** group-aware rule closure that fires for every group node.
    pub fn with_named_group_rule_fn<F>(mut self, id: impl Into<Arc<str>>, rule: F) -> Self
    where
        F: Fn(
                &SegmentGroupIndexed,
                &[Segment<'_>],
                &ValidationRuleContext<'_>,
                &mut Vec<ValidationIssue>,
            ) + Send
            + Sync
            + 'static,
    {
        self.group_rules.push(NamedGroupRule {
            id: Some(id.into()),
            group_scope: None,
            rule: Arc::new(rule),
        });
        self
    }

    /// Add a named group-aware rule closure scoped to a specific group definition.
    ///
    /// The closure is called only when the DFS traversal enters a group whose
    /// [`SegmentGroupIndexed::definition`] equals `group_scope` (e.g. `"SG5"`).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let pack = ProfileRulePack::new("AHB-MSCONS")
    ///     .with_scoped_group_rule_fn("SG5", "SG5-CAV-M", |_group, segs, _ctx, issues| {
    ///         if !segs.iter().any(|s| s.tag == "CAV") {
    ///             issues.push(
    ///                 ValidationIssue::new(ValidationSeverity::Error, "CAV missing in SG5")
    ///                     .with_segment("CAV")
    ///                     .with_rule_id("SG5-CAV-M"),
    ///             );
    ///         }
    ///     });
    /// ```
    pub fn with_scoped_group_rule_fn<F>(
        mut self,
        group_scope: &'static str,
        id: impl Into<Arc<str>>,
        rule: F,
    ) -> Self
    where
        F: Fn(
                &SegmentGroupIndexed,
                &[Segment<'_>],
                &ValidationRuleContext<'_>,
                &mut Vec<ValidationIssue>,
            ) + Send
            + Sync
            + 'static,
    {
        self.group_rules.push(NamedGroupRule {
            id: Some(id.into()),
            group_scope: Some(group_scope),
            rule: Arc::new(rule),
        });
        self
    }

    /// Assert segment `tag` is present in every occurrence of group `group_scope`.
    ///
    /// For example, `require_segment_in_group("SG5", "LOC", "SG5-LOC-M")` fires
    /// once per `SG5` instance that contains no `LOC` segment.
    ///
    /// Issues are automatically annotated with the group name.
    pub fn require_segment_in_group(
        self,
        group_scope: &'static str,
        tag: &'static str,
        rule_id: impl Into<Arc<str>>,
    ) -> Self {
        let id: Arc<str> = rule_id.into();
        self.with_scoped_group_rule_fn(
            group_scope,
            id.clone(),
            move |_group, segs, _ctx, issues| {
                if !segs.iter().any(|s| s.tag == tag) {
                    issues.push(
                        ValidationIssue::new(
                            ValidationSeverity::Error,
                            format!("mandatory segment {tag} is missing from group {group_scope}"),
                        )
                        .with_segment(tag)
                        .with_rule_id(id.as_ref()),
                    );
                }
            },
        )
    }

    /// Assert segment `tag` does **not** appear in any occurrence of group `group_scope`.
    ///
    /// Emits an `Error`-severity issue for each occurrence found.
    pub fn forbid_segment_in_group(
        self,
        group_scope: &'static str,
        tag: &'static str,
        rule_id: impl Into<Arc<str>>,
    ) -> Self {
        let id: Arc<str> = rule_id.into();
        self.with_scoped_group_rule_fn(
            group_scope,
            id.clone(),
            move |_group, segs, _ctx, issues| {
                for (occ, _s) in segs.iter().filter(|s| s.tag == tag).enumerate() {
                    issues.push(
                        ValidationIssue::new(
                            ValidationSeverity::Error,
                            format!("segment {tag} must not appear in group {group_scope}"),
                        )
                        .with_segment(tag)
                        .with_segment_occurrence(u16::try_from(occ).unwrap_or(u16::MAX))
                        .with_rule_id(id.as_ref()),
                    );
                }
            },
        )
    }

    /// Assert qualifier `qualifier` at `(element, component)` in segment `tag` is
    /// present in every occurrence of group `group_scope`.
    pub fn require_qualifier_in_group(
        self,
        group_scope: &'static str,
        tag: &'static str,
        element: u8,
        component: u8,
        qualifier: &'static str,
        rule_id: impl Into<Arc<str>>,
    ) -> Self {
        let id: Arc<str> = rule_id.into();
        self.with_scoped_group_rule_fn(
            group_scope,
            id.clone(),
            move |_group, segs, _ctx, issues| {
                for (occ, s) in segs.iter().filter(|s| s.tag == tag).enumerate() {
                    let actual = s
                        .get_element(element as usize)
                        .and_then(|e| e.get_component(component as usize));
                    if actual != Some(qualifier) {
                        issues.push(
                            ValidationIssue::new(
                                ValidationSeverity::Error,
                                format!(
                                    "segment {tag} element {element} component {component} must be \
                                 {qualifier:?} in group {group_scope}, found {:?}",
                                    actual.unwrap_or("<absent>")
                                ),
                            )
                            .with_segment(tag)
                            .with_element_index(element)
                            .with_component_index(component)
                            .with_segment_occurrence(u16::try_from(occ).unwrap_or(u16::MAX))
                            .with_rule_id(id.as_ref()),
                        );
                    }
                }
            },
        )
    }

    /// Return the number of group-scoped rules in this pack.
    pub fn group_rule_count(&self) -> usize {
        self.group_rules.len()
    }

    // ── Private group validation engine ────────────────────────────────────

    /// Recursively walk the segment-group tree and evaluate group-scoped rules.
    ///
    /// Called internally by [`Validator::validate_group_batch`].
    fn walk_group_tree(
        &self,
        group: &SegmentGroupIndexed,
        all_segments: &[Segment<'_>],
        report: &mut ValidationReport,
        context: &ValidationRuleContext<'_>,
    ) {
        let group_segs = all_segments.get(group.total_span.clone()).unwrap_or(&[]);
        let mut rule_issues: Vec<ValidationIssue> = Vec::new();

        for named in &self.group_rules {
            // Skip if this rule is scoped to a different group name.
            if let Some(scope) = named.group_scope {
                if group.definition != scope {
                    continue;
                }
            }
            let errors_before = report.errors.len();
            (named.rule)(group, group_segs, context, &mut rule_issues);
            for mut issue in rule_issues.drain(..) {
                // Auto-stamp the group name if the rule didn't set it explicitly.
                if issue.segment_group.is_none() {
                    issue = issue.with_segment_group(group.definition);
                }
                match issue.severity {
                    ValidationSeverity::Critical | ValidationSeverity::Error => {
                        report.add_error(issue);
                    }
                    ValidationSeverity::Warning => {
                        report.add_warning(issue);
                    }
                    ValidationSeverity::Info => {
                        report.add_info(issue);
                    }
                }
            }
            if self.bail_on_first_error && report.errors.len() > errors_before {
                return;
            }
        }

        for child in &group.children {
            let errors_before_child = report.errors.len();
            self.walk_group_tree(child, all_segments, report, context);
            if self.bail_on_first_error && report.errors.len() > errors_before_child {
                return;
            }
        }
    }

    /// Add a rule that implements [`ProfileRule`].
    pub fn with_rule(mut self, rule: impl ProfileRule + 'static) -> Self {
        self.rules.push(NamedRule {
            id: None,
            rule: Arc::new(rule),
        });
        self
    }

    /// Add a named rule that implements [`ProfileRule`].
    pub fn with_named_rule(
        mut self,
        id: impl Into<Arc<str>>,
        rule: impl ProfileRule + 'static,
    ) -> Self {
        self.rules.push(NamedRule {
            id: Some(id.into()),
            rule: Arc::new(rule),
        });
        self
    }

    /// Prepend all rules from `base` to this pack.
    ///
    /// Rules from `base` are shared (via [`Arc`] cloning) and run first.
    /// Message-type restrictions from `base` are also merged.  The resulting
    /// release scope must be compatible with both packs.
    ///
    /// # Errors
    ///
    /// Returns [`EdifactError::IncompatibleReleaseScopes`] if both packs specify
    /// different release scopes.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let base = ProfileRulePack::new("MIG-UTILMD-BASE")
    ///     .with_stateless_rule_fn(/* mandatory segment rules */);
    ///
    /// let ahb_11001 = ProfileRulePack::new("AHB-11001")
    ///     .extend_from(&base)?
    ///     .with_stateless_rule_fn(/* 11001-specific rules */);
    /// ```
    pub fn extend_from(mut self, base: &ProfileRulePack) -> Result<Self, EdifactError> {
        let mut combined = base.rules.clone();
        combined.append(&mut self.rules);
        self.rules = combined;
        // Prepend group rules from base too.
        let mut combined_group = base.group_rules.clone();
        combined_group.append(&mut self.group_rules);
        self.group_rules = combined_group;
        for mt in &base.message_types {
            self.message_types.insert(mt.clone());
        }
        self.release = merge_release_scopes(self.release.take(), base.release.clone())?;
        Ok(self)
    }

    /// Merge `other` into `self`, with `other` taking precedence for any rule
    /// whose id already exists in `self`.
    ///
    /// - Rules in `other` that have a stable id matching a rule in `self` **replace**
    ///   the rule at the same position in `self`.
    /// - Rules in `other` with no id, or with an id not present in `self`, are
    ///   **appended** to `self`.
    /// - Rules present only in `self` (no matching override in `other`) are
    ///   **retained unchanged**.
    ///
    /// # Errors
    ///
    /// Returns [`EdifactError::IncompatibleReleaseScopes`] if both packs specify
    /// different release scopes.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let base = ProfileRulePack::new("UTILMD-5.4")
    ///     .with_named_stateless_rule_fn("AHB-11001-BGM-M", |segs, _issues| { /* old */ });
    ///
    /// let delta = ProfileRulePack::new("UTILMD-5.5-delta")
    ///     .with_named_stateless_rule_fn("AHB-11001-BGM-M", |segs, _issues| { /* updated */ });
    ///
    /// // `result` runs the updated BGM-M rule only once:
    /// let result = base.merge_with_override(delta)?;
    /// assert_eq!(result.rule_count(), 1);
    /// ```
    pub fn merge_with_override(mut self, mut other: Self) -> Result<Self, EdifactError> {
        let mut id_to_index: std::collections::HashMap<Arc<str>, usize> = Default::default();
        for (idx, rule) in self.rules.iter().enumerate() {
            if let Some(id) = &rule.id {
                id_to_index.insert(id.clone(), idx);
            }
        }

        let mut replacements: Vec<(usize, NamedRule)> = Vec::new();
        let mut to_append = Vec::new();

        for other_rule in other.rules.drain(..) {
            if let Some(id) = &other_rule.id {
                if let Some(&idx) = id_to_index.get(id) {
                    replacements.push((idx, other_rule));
                } else {
                    to_append.push(other_rule);
                }
            } else {
                to_append.push(other_rule);
            }
        }

        for (idx, rule) in replacements {
            if idx < self.rules.len() {
                self.rules[idx] = rule;
            }
        }

        self.rules.append(&mut to_append);
        self.message_types.append(&mut other.message_types);
        // Merge group rules: named overrides replace matching entries; others are appended.
        let mut group_id_to_index: std::collections::HashMap<Arc<str>, usize> = Default::default();
        for (idx, rule) in self.group_rules.iter().enumerate() {
            if let Some(id) = &rule.id {
                group_id_to_index.insert(id.clone(), idx);
            }
        }
        let mut group_replacements: Vec<(usize, NamedGroupRule)> = Vec::new();
        let mut group_to_append = Vec::new();
        for other_rule in other.group_rules.drain(..) {
            if let Some(id) = &other_rule.id {
                if let Some(&idx) = group_id_to_index.get(id) {
                    group_replacements.push((idx, other_rule));
                } else {
                    group_to_append.push(other_rule);
                }
            } else {
                group_to_append.push(other_rule);
            }
        }
        for (idx, rule) in group_replacements {
            if idx < self.group_rules.len() {
                self.group_rules[idx] = rule;
            }
        }
        self.group_rules.append(&mut group_to_append);
        self.release = merge_release_scopes(self.release.take(), other.release.take())?;
        Ok(self)
    }
}

pub(super) fn merge_release_scopes(
    current: Option<String>,
    incoming: Option<String>,
) -> Result<Option<String>, EdifactError> {
    match (current, incoming) {
        (Some(x), Some(y)) if x != y => Err(EdifactError::IncompatibleReleaseScopes {
            current: x,
            incoming: y,
        }),
        (Some(x), Some(_)) => Ok(Some(x)),
        (Some(x), None) => Ok(Some(x)),
        (None, incoming) => Ok(incoming),
    }
}

impl Validator for ProfileRulePack {
    fn validate_batch(
        &self,
        segments: &[Segment<'_>],
        report: &mut ValidationReport,
        context: &ValidationRuleContext<'_>,
    ) {
        // Use the pre-extracted message type from the rule context when available
        // (set by ValidationContext to avoid per-pack O(n) UNH scans, F-017).
        let unh_e1_storage;
        let unh_e1: Option<&crate::model::Element<'_>> = if context.message_type.is_some() {
            None
        } else {
            unh_e1_storage = segments
                .iter()
                .find(|s| s.tag == "UNH")
                .and_then(|s| s.get_element(1));
            unh_e1_storage
        };

        let message_type = context
            .message_type
            .or_else(|| unh_e1.and_then(|e| e.get_component(0)));

        if !self.message_types.is_empty()
            && !message_type.is_some_and(|mt| self.message_types.contains(mt))
        {
            return;
        }

        if let Some(bound_release) = &self.release {
            let msg_association = segments
                .iter()
                .find(|s| s.tag == "UNH")
                .and_then(|s| s.get_element(1))
                .and_then(|e| e.get_component(4));
            if msg_association != Some(bound_release.as_str()) {
                return;
            }
        }

        let mut rule_issues: Vec<ValidationIssue> = Vec::new();

        for named in &self.rules {
            let errors_before = report.errors.len();
            named.rule.evaluate(segments, context, &mut rule_issues);
            for issue in rule_issues.drain(..) {
                match issue.severity {
                    ValidationSeverity::Critical | ValidationSeverity::Error => {
                        report.add_error(issue);
                    }
                    ValidationSeverity::Warning => {
                        report.add_warning(issue);
                    }
                    ValidationSeverity::Info => {
                        report.add_info(issue);
                    }
                }
            }
            if self.bail_on_first_error && report.errors.len() > errors_before {
                return;
            }
        }
    }

    fn validate_group_batch(
        &self,
        root: &SegmentGroupIndexed,
        all_segments: &[Segment<'_>],
        report: &mut ValidationReport,
        context: &ValidationRuleContext<'_>,
    ) {
        if self.group_rules.is_empty() {
            return;
        }

        // Apply message-type and release scope filters (same as validate_batch).
        let unh_e1_storage;
        let unh_e1: Option<&crate::model::Element<'_>> = if context.message_type.is_some() {
            None
        } else {
            unh_e1_storage = all_segments
                .iter()
                .find(|s| s.tag == "UNH")
                .and_then(|s| s.get_element(1));
            unh_e1_storage
        };
        let message_type = context
            .message_type
            .or_else(|| unh_e1.and_then(|e| e.get_component(0)));

        if !self.message_types.is_empty()
            && !message_type.is_some_and(|mt| self.message_types.contains(mt))
        {
            return;
        }

        if let Some(bound_release) = &self.release {
            let msg_association = all_segments
                .iter()
                .find(|s| s.tag == "UNH")
                .and_then(|s| s.get_element(1))
                .and_then(|e| e.get_component(4));
            if msg_association != Some(bound_release.as_str()) {
                return;
            }
        }

        self.walk_group_tree(root, all_segments, report, context);
    }

    fn fork(&self) -> Box<dyn Validator + Send + Sync> {
        Box::new(self.clone())
    }
}

impl Clone for ProfileRulePack {
    fn clone(&self) -> Self {
        Self {
            name: self.name.clone(),
            message_types: self.message_types.clone(),
            release: self.release.clone(),
            rules: self.rules.clone(),
            group_rules: self.group_rules.clone(),
            bail_on_first_error: self.bail_on_first_error,
        }
    }
}

impl std::fmt::Debug for ProfileRulePack {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProfileRulePack")
            .field("name", &self.name)
            .field("message_types", &self.message_types)
            .field("release", &self.release)
            .field("rule_count", &self.rules.len())
            .field("group_rule_count", &self.group_rules.len())
            .field("bail_on_first_error", &self.bail_on_first_error)
            .finish()
    }
}

/// `Arc<ProfileRulePack>` can be plugged directly into a [`super::context::ValidationContext`].
///
/// Forking (for `fork_with_message_ref`) only increments the reference count — no
/// deep copy of the rule vec is performed.  This is the zero-allocation path for
/// downstream code that caches packs in a `LazyLock` or `OnceLock`.
///
/// # Example
///
/// ```rust,ignore
/// use std::sync::{Arc, LazyLock};
/// use edifact_rs::{ProfileRulePack, ValidationContext};
///
/// static ORDERS_PACK: LazyLock<Arc<ProfileRulePack>> = LazyLock::new(|| {
///     Arc::new(
///         ProfileRulePack::new("ORDERS-MIG")
///             .for_message_type("ORDERS")
///             .require_segment("BGM", "MIG-BGM-M"),
///     )
/// });
///
/// let ctx = ValidationContext::builder()
///     .with_profile_pack_arc(Arc::clone(&ORDERS_PACK))
///     .build();
/// ```
impl Validator for Arc<ProfileRulePack> {
    fn validate_batch(
        &self,
        segments: &[Segment<'_>],
        report: &mut ValidationReport,
        context: &ValidationRuleContext<'_>,
    ) {
        self.as_ref().validate_batch(segments, report, context);
    }

    fn validate_group_batch(
        &self,
        root: &SegmentGroupIndexed,
        all_segments: &[Segment<'_>],
        report: &mut ValidationReport,
        context: &ValidationRuleContext<'_>,
    ) {
        self.as_ref()
            .validate_group_batch(root, all_segments, report, context);
    }

    fn fork(&self) -> Box<dyn Validator + Send + Sync> {
        Box::new(Arc::clone(self))
    }
}