imferno-core 3.0.1

SMPTE ST 2067 IMF parser and validator
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
//! IMF validation finding model and report types.
//!
//! Cross-cutting types used by all spec modules to return findings.

use crate::assetmap::ImfUuid;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::path::PathBuf;

/// Severity level of validation issues
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Severity {
    /// Informational - Best practice suggestions
    Info,
    /// Warning - Should fix but not critical
    Warning,
    /// Error - Must fix for compliance
    Error,
    /// Critical - Prevents package from being usable
    Critical,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Severity::Info => write!(f, "INFO"),
            Severity::Warning => write!(f, "WARNING"),
            Severity::Error => write!(f, "ERROR"),
            Severity::Critical => write!(f, "CRITICAL"),
        }
    }
}

/// Category of validation issue
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Category {
    /// XML structure and syntax issues
    Structure,
    /// SMPTE schema compliance
    Schema,
    /// UUID and reference resolution
    Reference,
    /// Asset file availability and integrity
    Asset,
    /// Timing, frame rate, and duration issues
    Timing,
    /// Codec-level encoding issues (J2K profiles, JPEG-XS, AAC params,
    /// PCM bit depth, etc.). For *container*-level concerns (MXF
    /// wrapping, partition layout) use `Container`.
    Encoding,
    /// MXF / wrapping container constraints — distinct from `Encoding`
    /// which covers the codec carried inside the container.
    Container,
    /// Audio configuration issues
    Audio,
    /// Video configuration issues
    Video,
    /// Subtitle and caption issues
    Subtitle,
    /// Data essence (ISXD, dynamic metadata sidecars, ancillary data
    /// tracks). NOT for *metadata about* essence — that's `Metadata`.
    Data,
    /// Metadata and labeling issues
    Metadata,
    /// Security and DRM issues
    Security,
    /// Studio-specific requirements
    StudioSpecific(String),
}

impl fmt::Display for Category {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Category::Structure => write!(f, "Structure"),
            Category::Schema => write!(f, "Schema"),
            Category::Reference => write!(f, "Reference"),
            Category::Asset => write!(f, "Asset"),
            Category::Timing => write!(f, "Timing"),
            Category::Encoding => write!(f, "Encoding"),
            Category::Container => write!(f, "Container"),
            Category::Audio => write!(f, "Audio"),
            Category::Video => write!(f, "Video"),
            Category::Subtitle => write!(f, "Subtitle"),
            Category::Data => write!(f, "Data"),
            Category::Metadata => write!(f, "Metadata"),
            Category::Security => write!(f, "Security"),
            Category::StudioSpecific(studio) => write!(f, "{} Specific", studio),
        }
    }
}

/// Which authority produced a validation finding.
///
/// Encodes the "prose vs XSD" provenance distinction that SMPTE ST 2067-3
/// §5.1 calls out ("the prose document takes precedence on conflict"),
/// so callers can filter, group, or apply per-source severity overrides
/// without inspecting the code string.
///
/// Currently *inferred* from the code prefix on a `ValidationIssue`
/// (see `ValidationIssue::source`) — no engine code needs to set it
/// explicitly. If inference ever becomes ambiguous (e.g. a non-XSD
/// engine internal rule that wants the `XSD/` prefix for catalogue
/// reasons), we can add a `with_source()` builder without changing
/// the public shape.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IssueSource {
    /// Came from the runtime XSD validator (uppsala). Schema-layer:
    /// the structural subset of the spec that the XSD DSL can express.
    XsdLayer,
    /// Came from a hand-rolled prose-cited rule. Semantic/cross-field/
    /// value-set checks that XSD can't express; the rule's code
    /// typically cites a SMPTE prose section (e.g. `ST2067-2:2020:6.4.2`).
    ProseRule,
    /// Came from imferno engine internals — parse failures, package
    /// structure checks, manifest issues — not directly traceable to
    /// a single SMPTE spec section.
    EngineInternal,
}

impl fmt::Display for IssueSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IssueSource::XsdLayer => write!(f, "XSD"),
            IssueSource::ProseRule => write!(f, "Prose"),
            IssueSource::EngineInternal => write!(f, "Engine"),
        }
    }
}

impl IssueSource {
    /// Infer the source authority from a catalogue code string.
    ///
    /// Inference rules (checked top-to-bottom):
    /// - `XSD/...` → XsdLayer
    /// - `IMFERNO:...` or `IMFERNO/...` → EngineInternal
    /// - everything else → ProseRule (most catalogue codes cite a spec §)
    pub fn from_code(code: &str) -> Self {
        if code.starts_with("XSD/") {
            IssueSource::XsdLayer
        } else if code.starts_with("IMFERNO:") || code.starts_with("IMFERNO/") {
            IssueSource::EngineInternal
        } else {
            IssueSource::ProseRule
        }
    }
}

/// Location where the issue was found
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Location {
    /// File path if applicable
    pub file: Option<PathBuf>,
    /// CPL UUID if applicable
    pub cpl_id: Option<ImfUuid>,
    /// CPL filename if applicable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpl_filename: Option<String>,
    /// CPL content title if applicable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpl_title: Option<String>,
    /// Segment index (0-based)
    pub segment: Option<usize>,
    /// Sequence UUID if applicable
    pub sequence_id: Option<String>,
    /// Resource UUID if applicable
    pub resource_id: Option<String>,
    /// Timecode if applicable
    pub timecode: Option<String>,
    /// Line number in XML file
    pub line: Option<usize>,
    /// XPath or field path
    pub path: Option<String>,
}

impl Location {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_file(mut self, file: PathBuf) -> Self {
        self.file = Some(file);
        self
    }

    pub fn with_cpl(mut self, cpl_id: ImfUuid) -> Self {
        self.cpl_id = Some(cpl_id);
        self
    }

    pub fn with_cpl_filename(mut self, filename: impl Into<String>) -> Self {
        self.cpl_filename = Some(filename.into());
        self
    }

    pub fn with_cpl_title(mut self, title: impl Into<String>) -> Self {
        self.cpl_title = Some(title.into());
        self
    }

    pub fn with_segment(mut self, segment: usize) -> Self {
        self.segment = Some(segment);
        self
    }

    pub fn with_resource(mut self, resource: usize) -> Self {
        self.resource_id = Some(resource.to_string());
        self
    }

    pub fn with_sequence(mut self, sequence_id: String) -> Self {
        self.sequence_id = Some(sequence_id);
        self
    }

    pub fn with_path(mut self, path: String) -> Self {
        self.path = Some(path);
        self
    }
}

impl fmt::Display for Location {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut parts = Vec::new();

        if let Some(ref file) = self.file {
            parts.push(format!("{}", file.display()));
        }
        if let Some(ref cpl_id) = self.cpl_id {
            let s = cpl_id.to_string();
            parts.push(format!("CPL:{}", &s[..8.min(s.len())]));
        }
        if let Some(segment) = self.segment {
            parts.push(format!("Segment:{}", segment + 1));
        }
        if let Some(ref sequence_id) = self.sequence_id {
            parts.push(format!("Seq:{}", &sequence_id[..8.min(sequence_id.len())]));
        }
        if let Some(line) = self.line {
            parts.push(format!("Line:{}", line));
        }
        if let Some(ref path) = self.path {
            parts.push(path.to_string());
        }

        write!(f, "{}", parts.join(", "))
    }
}

/// A single validation issue
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationIssue {
    /// Severity level
    pub severity: Severity,
    /// Category of issue
    pub category: Category,
    /// Authority that emitted this issue (XSD layer, prose rule, engine
    /// internal). Inferred from `code` at construction time so the field
    /// is always consistent with the code prefix — JS consumers can
    /// filter on `issue.source` directly without re-implementing the
    /// inference. Deserialised with a default so old reports without the
    /// field round-trip cleanly.
    #[serde(default = "default_source_for_deserialize")]
    pub source: IssueSource,
    /// Location where issue was found
    pub location: Location,
    /// Error code (e.g., "ST2067-2:2020:8.3/FileNotFound")
    pub code: String,
    /// Human-readable message
    pub message: String,
    /// Suggestion for how to fix
    pub suggestion: Option<String>,
    /// Additional context
    pub context: HashMap<String, String>,
    /// Other `Location`s when this issue represents an aggregation of
    /// multiple identical-code occurrences (see
    /// [`ValidationReport::aggregate`]). Empty for fresh, un-aggregated
    /// issues. Skipped during serialisation when empty so the on-wire
    /// shape stays back-compatible for callers that don't aggregate.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub additional_instances: Vec<Location>,
}

impl ValidationIssue {
    pub fn new(
        severity: Severity,
        category: Category,
        code: impl Into<String>,
        message: impl Into<String>,
    ) -> Self {
        let code: String = code.into();
        let source = IssueSource::from_code(&code);
        Self {
            severity,
            category,
            source,
            location: Location::new(),
            code,
            message: message.into(),
            suggestion: None,
            context: HashMap::new(),
            additional_instances: Vec::new(),
        }
    }

    /// Build a `ValidationIssue` directly from a typed `ValidationCode`,
    /// reading severity + category from the catalogue rather than the
    /// caller. Use this for any rule whose code lives in one of the
    /// `*_codes` modules — the catalogue is the single source of truth
    /// for severity and category. Pass the bare enum value:
    ///
    /// ```ignore
    /// use imferno_core::mxf::codes::St2067_2_2016;
    /// let issue = ValidationIssue::from_code(
    ///     St2067_2_2016::AudioSampleRateUnsupported,
    ///     format!("got {hz} Hz"),
    /// );
    /// ```
    pub fn from_code<C: codes::ValidationCode>(code: C, message: impl Into<String>) -> Self {
        // `code` is consumed only via `&self`-taking methods; we never
        // need to move it. The string code itself comes from
        // `code.code()` which returns `&'static str`, so no `Into<String>`
        // bound on `C` is required.
        Self::new(
            code.default_severity(),
            code.category(),
            code.code(),
            message,
        )
    }

    /// Number of occurrences this issue represents. `1` for a fresh
    /// issue, `1 + additional_instances.len()` after aggregation.
    ///
    /// JS consumers can compute this themselves as
    /// `1 + issue.additionalInstances.length` — the field is the source
    /// of truth, this method is a Rust-side convenience.
    pub fn instance_count(&self) -> usize {
        1 + self.additional_instances.len()
    }

    pub fn with_location(mut self, location: Location) -> Self {
        self.location = location;
        self
    }

    pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
        self.suggestion = Some(suggestion.into());
        self
    }

    pub fn with_context(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.context.insert(key.into(), value.into());
        self
    }
}

/// Serde default for `ValidationIssue::source` — used only when
/// deserialising a payload that pre-dates the field. Returns
/// `IssueSource::EngineInternal` as a safe fallback; the proper value
/// will be re-derived if the issue is re-emitted, since `::new` always
/// computes from the code.
fn default_source_for_deserialize() -> IssueSource {
    IssueSource::EngineInternal
}

impl fmt::Display for ValidationIssue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{}] {} ({}): {}",
            self.severity, self.category, self.code, self.message
        )?;

        if !self.location.to_string().is_empty() {
            write!(f, "\n  Location: {}", self.location)?;
        }

        if let Some(ref suggestion) = self.suggestion {
            write!(f, "\n  Suggestion: {}", suggestion)?;
        }

        let count = self.instance_count();
        if count > 1 {
            write!(f, "\n  Occurrences: {}", count)?;
        }

        Ok(())
    }
}

/// Comprehensive validation report
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ValidationReport {
    /// Critical issues that prevent usage
    pub critical: Vec<ValidationIssue>,
    /// Errors that must be fixed for compliance
    pub errors: Vec<ValidationIssue>,
    /// Warnings that should be addressed
    pub warnings: Vec<ValidationIssue>,
    /// Informational issues
    pub info: Vec<ValidationIssue>,
    /// Issues that were suppressed by a `RuleSeverity::Off` override.
    /// These are not counted toward `is_playable`/`is_compliant` or
    /// surfaced by `has_errors`/`summary` — they exist so operators can
    /// debug their `RulesConfig` (`--show-suppressed`) without re-running
    /// validation. Each carries a `context["suppressed_by"]` annotation
    /// naming the rule key that matched.
    ///
    /// Skipped during serialisation when empty so reports without
    /// suppressed rules keep the previous on-wire shape.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub suppressed: Vec<ValidationIssue>,
    /// Whether the package is playable despite issues
    pub is_playable: bool,
    /// Whether the package is compliant with base SMPTE standards
    pub is_compliant: bool,
    /// Validation profile used
    pub profile: ValidationProfile,
    /// Timestamp of validation
    pub timestamp: String,
}

impl ValidationReport {
    pub fn new(profile: ValidationProfile) -> Self {
        Self {
            critical: Vec::new(),
            errors: Vec::new(),
            warnings: Vec::new(),
            info: Vec::new(),
            suppressed: Vec::new(),
            is_playable: true,
            is_compliant: true,
            profile,
            timestamp: chrono::Utc::now().to_rfc3339(),
        }
    }

    pub fn add(&mut self, issue: ValidationIssue) {
        match issue.severity {
            Severity::Critical => {
                self.critical.push(issue);
                self.is_playable = false;
                self.is_compliant = false;
            }
            Severity::Error => {
                self.errors.push(issue);
                self.is_compliant = false;
            }
            Severity::Warning => self.warnings.push(issue),
            Severity::Info => self.info.push(issue),
        }
    }

    /// Collapse repeat-offender diagnostics within each severity bucket.
    ///
    /// When the same `code` fires N times, the first-encountered issue
    /// is kept as the canonical instance and the rest contribute only
    /// their `Location` to its [`ValidationIssue::additional_instances`].
    /// All other fields (`message`, `suggestion`, `context`, `severity`,
    /// `category`) are taken from the first occurrence; later instances'
    /// `additional_instances` (if any — handles repeated aggregation
    /// idempotently) are merged in.
    ///
    /// Aggregation stays within severity buckets, since rules emit at
    /// a fixed severity per code. Order across the report is preserved
    /// (first-seen wins) so output remains reproducible.
    ///
    /// Idempotent and order-independent with [`Self::apply_rules`] —
    /// safe to call either before or after.
    pub fn aggregate(mut self) -> Self {
        fn collapse(bucket: &mut Vec<ValidationIssue>) {
            if bucket.len() < 2 {
                return;
            }
            let mut seen: HashMap<String, usize> = HashMap::with_capacity(bucket.len());
            let mut out: Vec<ValidationIssue> = Vec::with_capacity(bucket.len());
            for issue in bucket.drain(..) {
                match seen.get(&issue.code) {
                    Some(&i) => {
                        out[i].additional_instances.push(issue.location);
                        out[i]
                            .additional_instances
                            .extend(issue.additional_instances);
                    }
                    None => {
                        seen.insert(issue.code.clone(), out.len());
                        out.push(issue);
                    }
                }
            }
            *bucket = out;
        }
        collapse(&mut self.critical);
        collapse(&mut self.errors);
        collapse(&mut self.warnings);
        collapse(&mut self.info);
        self
    }

    /// Merge another report's issues into this one.
    ///
    /// The source report's `profile` and `timestamp` are discarded;
    /// only `self`'s values are retained. The `is_playable` and
    /// `is_compliant` flags are combined via logical AND.
    pub fn merge(&mut self, other: ValidationReport) {
        self.critical.extend(other.critical);
        self.errors.extend(other.errors);
        self.warnings.extend(other.warnings);
        self.info.extend(other.info);
        self.suppressed.extend(other.suppressed);
        self.is_playable = self.is_playable && other.is_playable;
        self.is_compliant = self.is_compliant && other.is_compliant;
    }

    pub fn total_issues(&self) -> usize {
        self.critical.len() + self.errors.len() + self.warnings.len() + self.info.len()
    }

    pub fn has_critical(&self) -> bool {
        !self.critical.is_empty()
    }

    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }

    pub fn summary(&self) -> String {
        format!(
            "Validation Report: {} critical, {} errors, {} warnings, {} info",
            self.critical.len(),
            self.errors.len(),
            self.warnings.len(),
            self.info.len()
        )
    }
}

impl fmt::Display for ValidationReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "IMF Package Validation Report")?;
        writeln!(f, "=============================")?;
        writeln!(f, "Profile: {}", self.profile)?;
        writeln!(f, "Timestamp: {}", self.timestamp)?;
        writeln!(
            f,
            "Playable: {}",
            if self.is_playable {
                "✅ YES"
            } else {
                "❌ NO"
            }
        )?;
        writeln!(
            f,
            "Compliant: {}",
            if self.is_compliant {
                "✅ YES"
            } else {
                "❌ NO"
            }
        )?;
        writeln!(f)?;

        if !self.critical.is_empty() {
            writeln!(f, "CRITICAL ISSUES ({}):", self.critical.len())?;
            for issue in &self.critical {
                writeln!(f, "{}", issue)?;
            }
            writeln!(f)?;
        }

        if !self.errors.is_empty() {
            writeln!(f, "ERRORS ({}):", self.errors.len())?;
            for issue in &self.errors {
                writeln!(f, "{}", issue)?;
            }
            writeln!(f)?;
        }

        if !self.warnings.is_empty() {
            writeln!(f, "WARNINGS ({}):", self.warnings.len())?;
            for issue in &self.warnings {
                writeln!(f, "{}", issue)?;
            }
            writeln!(f)?;
        }

        if !self.info.is_empty() {
            writeln!(f, "INFO ({}):", self.info.len())?;
            for issue in &self.info {
                writeln!(f, "{}", issue)?;
            }
        }

        Ok(())
    }
}

/// Validation profile determining strictness
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ValidationProfile {
    /// Minimal validation - just check if playable
    Minimal,
    /// Standard SMPTE compliance
    #[default]
    SMPTE,
    /// Custom profile with specific rules
    Custom,
}

impl fmt::Display for ValidationProfile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ValidationProfile::Minimal => write!(f, "Minimal"),
            ValidationProfile::SMPTE => write!(f, "SMPTE"),
            ValidationProfile::Custom => write!(f, "Custom"),
        }
    }
}

/// Typed validation-code catalogue with per-spec enums.
///
/// Every normative code emitted by the imf-rs validators is defined here,
/// grouped by the SMPTE specification that defines it.  See [`codes`] for
/// the full API.
pub mod codes;

/// ESLint-style per-rule severity overrides for `ValidationReport`.
pub mod rules;
pub use rules::{RuleSeverity, RulesConfig};

/// Result type for parsing operations that can accumulate errors
pub type ParseResult<T> = Result<(T, ValidationReport), CriticalError>;

/// Critical errors that prevent any further processing
#[derive(Debug)]
pub struct CriticalError {
    pub message: String,
    pub cause: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl fmt::Display for CriticalError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Critical Error: {}", self.message)?;
        if let Some(ref cause) = self.cause {
            write!(f, "\nCaused by: {}", cause)?;
        }
        Ok(())
    }
}

impl std::error::Error for CriticalError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.cause
            .as_ref()
            .map(|e| &**e as &(dyn std::error::Error + 'static))
    }
}

impl From<std::io::Error> for CriticalError {
    fn from(err: std::io::Error) -> Self {
        CriticalError {
            message: format!("IO Error: {}", err),
            cause: Some(Box::new(err)),
        }
    }
}

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

    #[test]
    fn test_validation_issue_creation() {
        let issue = ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "ST2067-2:2020:8.3/FileNotFound",
            "Missing required field 'EditRate' in Segment",
        )
        .with_location(
            Location::new()
                .with_cpl(ImfUuid::parse("urn:uuid:12345678-0000-0000-0000-000000000000").unwrap())
                .with_segment(0),
        )
        .with_suggestion("Add EditRate element with value like '24 1' or '24000 1001'");

        assert_eq!(issue.severity, Severity::Error);
        assert_eq!(issue.code, "ST2067-2:2020:8.3/FileNotFound");
        assert!(issue.suggestion.is_some());
    }

    #[test]
    fn issue_source_from_code_classifies_xsd_layer() {
        assert_eq!(
            IssueSource::from_code("XSD/TypeInvalid/IssueDate"),
            IssueSource::XsdLayer
        );
        assert_eq!(
            IssueSource::from_code("XSD/ElementMissing"),
            IssueSource::XsdLayer
        );
    }

    #[test]
    fn issue_source_from_code_classifies_engine_internal() {
        assert_eq!(
            IssueSource::from_code("IMFERNO:Package/UnreferencedAsset"),
            IssueSource::EngineInternal
        );
        assert_eq!(
            IssueSource::from_code("IMFERNO/Internal/ParseError"),
            IssueSource::EngineInternal
        );
    }

    #[test]
    fn issue_source_from_code_defaults_to_prose_rule() {
        // SMPTE prose-cited codes — explicit spec section in the prefix.
        assert_eq!(
            IssueSource::from_code("ST2067-2:2020:6.4.2/EssenceDescriptorList"),
            IssueSource::ProseRule
        );
        assert_eq!(
            IssueSource::from_code("ST2067-21:2023:7.1/AppIdMismatch"),
            IssueSource::ProseRule
        );
        // Catch-all: anything we don't recognise falls into ProseRule
        // (catalogue codes are overwhelmingly spec-cited).
        assert_eq!(
            IssueSource::from_code("dcml-UUID-Malformed"),
            IssueSource::ProseRule
        );
    }

    #[test]
    fn validation_issue_source_field_is_populated_from_code() {
        let xsd = ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "XSD/PatternInvalid/UUID",
            "uuid did not match pattern",
        );
        assert_eq!(xsd.source, IssueSource::XsdLayer);

        let prose = ValidationIssue::new(
            Severity::Warning,
            Category::Reference,
            "ST2067-3:2020:5.5.1.2/ContentKindUnknown",
            "unknown content kind",
        );
        assert_eq!(prose.source, IssueSource::ProseRule);

        let engine = ValidationIssue::new(
            Severity::Critical,
            Category::Structure,
            "IMFERNO:Package/ParseError",
            "could not parse CPL",
        );
        assert_eq!(engine.source, IssueSource::EngineInternal);
    }

    #[test]
    fn validation_issue_source_round_trips_through_serde() {
        let xsd = ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "XSD/PatternInvalid/UUID",
            "uuid did not match pattern",
        );
        let json = serde_json::to_string(&xsd).unwrap();
        assert!(json.contains("XsdLayer"), "source should serialise: {json}");
        let back: ValidationIssue = serde_json::from_str(&json).unwrap();
        assert_eq!(back.source, IssueSource::XsdLayer);
    }

    #[test]
    fn validation_issue_source_deserialise_defaults_when_missing() {
        // Older payload shape without the `source` field — must
        // round-trip into a sensible default rather than failing.
        let legacy = r#"{
            "severity": "Error",
            "category": "Schema",
            "location": {},
            "code": "XSD/PatternInvalid/UUID",
            "message": "uuid did not match pattern",
            "suggestion": null,
            "context": {}
        }"#;
        let issue: ValidationIssue = serde_json::from_str(legacy).unwrap();
        assert_eq!(issue.source, IssueSource::EngineInternal);
    }

    #[test]
    fn test_validation_report() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);

        report.add(ValidationIssue::new(
            Severity::Critical,
            Category::Asset,
            "ST2067-2:2020:8.3/FileNotFound",
            "Required MXF file not found",
        ));

        report.add(ValidationIssue::new(
            Severity::Warning,
            Category::Metadata,
            "META-001",
            "ContentKind not in recommended vocabulary",
        ));

        assert_eq!(report.total_issues(), 2);
        assert!(!report.is_playable);
        assert!(!report.is_compliant);
        assert!(report.has_critical());
    }

    #[test]
    fn test_location_formatting() {
        let location = Location::new()
            .with_file(std::path::PathBuf::from("ASSETMAP.xml"))
            .with_cpl(ImfUuid::parse("urn:uuid:12345678-0000-0000-0000-000000000000").unwrap())
            .with_segment(2)
            .with_path("/path/to/package".to_string());

        let formatted = format!("{}", location);
        assert!(formatted.contains("ASSETMAP.xml"));
        assert!(formatted.contains("1234-5678") || !formatted.is_empty());
        assert!(formatted.contains("2") || !formatted.is_empty());
    }

    #[test]
    fn test_severity_ordering() {
        // Test severity ordering for proper sorting
        assert!(Severity::Critical > Severity::Error);
        assert!(Severity::Error > Severity::Warning);
        assert!(Severity::Warning > Severity::Info);

        let severities = vec![
            Severity::Info,
            Severity::Critical,
            Severity::Warning,
            Severity::Error,
        ];
        let mut sorted = severities.clone();
        sorted.sort();
        sorted.reverse(); // Highest first

        assert_eq!(
            sorted,
            vec![
                Severity::Critical,
                Severity::Error,
                Severity::Warning,
                Severity::Info
            ]
        );
    }

    #[test]
    fn test_category_display() {
        assert_eq!(format!("{}", Category::Schema), "Schema");
        assert_eq!(format!("{}", Category::Asset), "Asset");
        assert_eq!(format!("{}", Category::Metadata), "Metadata");
        assert_eq!(format!("{}", Category::Timing), "Timing");
        assert_eq!(format!("{}", Category::Asset), "Asset");
        assert_eq!(format!("{}", Category::Structure), "Structure");
    }

    #[test]
    fn test_validation_issue_with_context() {
        let mut issue = ValidationIssue::new(
            Severity::Warning,
            Category::Metadata,
            "META-002",
            "ContentKind uses non-standard value",
        );

        issue = issue.with_context("element", "Found in MainMarker element");

        assert!(!issue.context.is_empty());
        assert!(issue.context.contains_key("element"));
    }

    #[test]
    fn test_validation_report_merge() {
        let mut report1 = ValidationReport::new(ValidationProfile::SMPTE);
        report1.add(ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "ST2067-2:2020:8.3/ChecksumMismatch",
            "Invalid type for EditRate",
        ));

        let mut report2 = ValidationReport::new(ValidationProfile::SMPTE);
        report2.add(ValidationIssue::new(
            Severity::Warning,
            Category::Metadata,
            "META-003",
            "Missing annotation",
        ));

        report1.merge(report2);

        assert_eq!(report1.total_issues(), 2);
        assert!(report1.has_errors());
    }

    #[test]
    fn test_validation_report_summary() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);

        report.add(ValidationIssue::new(
            Severity::Critical,
            Category::Asset,
            "ST2067-2:2020:8.3/FileNotFound",
            "Critical issue",
        ));

        report.add(ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "ST2067-2:2020:8.3/ChecksumMismatch",
            "Error issue",
        ));

        report.add(ValidationIssue::new(
            Severity::Warning,
            Category::Metadata,
            "META-004",
            "Warning issue",
        ));

        report.add(ValidationIssue::new(
            Severity::Info,
            Category::Structure,
            "INFO-001",
            "Info issue",
        ));

        let summary = report.summary();
        assert!(summary.contains("1 critical") || !summary.is_empty());
        assert!(summary.contains("issues") || summary.len() > 10);
        // Summary is a formatted string, check it contains useful information
        assert!(!summary.is_empty());
    }

    #[test]
    fn test_error_display() {
        let error = CriticalError {
            message: "Package not found".to_string(),
            cause: None,
        };

        let display = format!("{}", error);
        assert!(display.contains("Package not found"));
    }

    #[test]
    fn test_error_with_cause() {
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
        let critical_error = CriticalError::from(io_error);

        assert!(critical_error.message.contains("IO Error"));
        assert!(critical_error.cause.is_some());
    }

    #[test]
    fn test_validation_profile_display() {
        assert_eq!(format!("{}", ValidationProfile::SMPTE), "SMPTE");
        assert_eq!(format!("{}", ValidationProfile::SMPTE), "SMPTE");
        assert_eq!(format!("{}", ValidationProfile::Custom), "Custom");
        assert_eq!(format!("{}", ValidationProfile::Custom), "Custom");
    }

    #[test]
    fn test_location_edge_cases() {
        // Test empty location
        let empty_location = Location::new();
        let formatted = format!("{}", empty_location);
        assert!(formatted.is_empty());

        // Test location with only path
        let path_only = Location::new().with_path("/test/path".to_string());
        let formatted = format!("{}", path_only);
        assert!(formatted.contains("/test/path"));

        // Test location with only file
        let file_only = Location::new().with_file(std::path::PathBuf::from("test.xml"));
        let formatted = format!("{}", file_only);
        assert!(formatted.contains("test.xml"));
    }

    #[test]
    fn test_validation_issue_chaining() {
        let issue = ValidationIssue::new(
            Severity::Error,
            Category::Timing,
            "TL-001",
            "Timeline validation failed",
        )
        .with_location(Location::new().with_segment(5))
        .with_suggestion("Check segment timing")
        .with_context("phase", "During composition validation");

        assert!(issue.location.segment.is_some());
        assert!(issue.suggestion.is_some());
        assert!(!issue.context.is_empty());
    }

    #[test]
    fn test_validation_report_display() {
        let mut report = ValidationReport::new(ValidationProfile::Custom);

        report.add(ValidationIssue::new(
            Severity::Critical,
            Category::Asset,
            "ST2067-2:2020:8.3/FileNotFound",
            "Critical test issue",
        ));

        let display = format!("{}", report);
        assert!(display.contains("Critical"));
        assert!(
            display.contains("FILE_NOT_FOUND") || display.contains("Asset") || !display.is_empty()
        );
        assert!(display.contains("Critical test issue"));
    }

    #[test]
    fn test_error_codes() {
        // ValidationIssue stores whatever code string it is given.
        let issue = ValidationIssue::new(Severity::Error, Category::Asset, "A/Code", "msg");
        assert_eq!(issue.code, "A/Code");
        let issue2 = ValidationIssue::new(Severity::Error, Category::Asset, "B/Code", "msg");
        assert_ne!(issue.code, issue2.code);
    }

    #[test]
    fn location_cpl_id_serde_round_trip() {
        let uuid = ImfUuid::parse("urn:uuid:12345678-0000-0000-0000-000000000000").unwrap();
        let loc = Location::new().with_cpl(uuid);
        let json = serde_json::to_string(&loc).unwrap();
        let deserialized: Location = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.cpl_id, Some(uuid));
    }

    #[test]
    fn validation_issue_serde_round_trip() {
        let uuid = ImfUuid::parse("urn:uuid:abcdef00-1234-5678-9abc-def012345678").unwrap();
        let issue = ValidationIssue::new(
            Severity::Warning,
            Category::Structure,
            "TEST/Code",
            "test message",
        )
        .with_location(Location::new().with_cpl(uuid));

        let json = serde_json::to_string(&issue).unwrap();
        let deserialized: ValidationIssue = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.severity, Severity::Warning);
        assert_eq!(deserialized.location.cpl_id, Some(uuid));
    }

    /// FIX-12 regression: a ValidationReport carrying both a populated
    /// `suppressed` bucket and aggregated issues with non-empty
    /// `additional_instances` survives a serde JSON round-trip.
    #[test]
    fn validation_report_serde_round_trip_with_suppressed_and_aggregate() {
        let uuid1 = ImfUuid::parse("urn:uuid:00000000-0000-0000-0000-000000000001").unwrap();
        let uuid2 = ImfUuid::parse("urn:uuid:00000000-0000-0000-0000-000000000002").unwrap();
        let uuid3 = ImfUuid::parse("urn:uuid:00000000-0000-0000-0000-000000000003").unwrap();

        let mut report = ValidationReport::new(ValidationProfile::SMPTE);

        // Aggregated issue: 1 primary + 2 additional instances.
        let mut aggregated = ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "XSD/PatternInvalid/UUID",
            "uuid pattern violation",
        )
        .with_location(Location::new().with_cpl(uuid1));
        aggregated
            .additional_instances
            .push(Location::new().with_cpl(uuid2));
        aggregated
            .additional_instances
            .push(Location::new().with_cpl(uuid3));
        report.errors.push(aggregated);

        // Suppressed issue: demoted by a hypothetical rule key, annotated.
        let mut suppressed = ValidationIssue::new(
            Severity::Info,
            Category::Schema,
            "XSD/TypeInvalid/IssueDate",
            "issue date not a valid xs:dateTime",
        )
        .with_location(Location::new().with_cpl(uuid1));
        suppressed
            .context
            .insert("suppressed_by".to_string(), "source:XsdLayer".to_string());
        report.suppressed.push(suppressed);

        let json = serde_json::to_string(&report).expect("serialise");
        let back: ValidationReport = serde_json::from_str(&json).expect("deserialise");

        assert_eq!(back.errors.len(), 1);
        assert_eq!(back.errors[0].additional_instances.len(), 2);
        assert_eq!(back.errors[0].additional_instances[0].cpl_id, Some(uuid2));
        assert_eq!(back.errors[0].additional_instances[1].cpl_id, Some(uuid3));
        assert_eq!(back.errors[0].instance_count(), 3);

        assert_eq!(back.suppressed.len(), 1);
        assert_eq!(
            back.suppressed[0]
                .context
                .get("suppressed_by")
                .map(String::as_str),
            Some("source:XsdLayer")
        );
    }

    fn agg_issue(code: &str, severity: Severity, cpl_byte: u8) -> ValidationIssue {
        let uuid = ImfUuid::parse(&format!(
            "urn:uuid:00000000-0000-0000-0000-0000000000{:02x}",
            cpl_byte
        ))
        .unwrap();
        ValidationIssue::new(severity, Category::Schema, code, "test")
            .with_location(Location::new().with_cpl(uuid))
    }

    #[test]
    fn aggregate_collapses_repeat_codes_within_a_bucket() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(agg_issue("XSD/PatternInvalid/UUID", Severity::Error, 1));
        report.add(agg_issue("XSD/PatternInvalid/UUID", Severity::Error, 2));
        report.add(agg_issue("XSD/PatternInvalid/UUID", Severity::Error, 3));
        report.add(agg_issue("XSD/Other/X", Severity::Error, 4));
        let out = report.aggregate();
        // Two distinct codes → two issues in errors bucket.
        assert_eq!(out.errors.len(), 2);
        // The repeated-code issue carries the extra locations.
        let agg = out
            .errors
            .iter()
            .find(|i| i.code == "XSD/PatternInvalid/UUID")
            .unwrap();
        assert_eq!(agg.instance_count(), 3);
        assert_eq!(agg.additional_instances.len(), 2);
        // First-seen location is retained on the canonical issue.
        // Other code stays as a single-instance issue.
        let solo = out.errors.iter().find(|i| i.code == "XSD/Other/X").unwrap();
        assert_eq!(solo.instance_count(), 1);
    }

    #[test]
    fn aggregate_preserves_first_message_and_severity() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(
            ValidationIssue::new(Severity::Error, Category::Schema, "X/Y", "first message")
                .with_suggestion("first suggestion"),
        );
        report.add(ValidationIssue::new(
            Severity::Error,
            Category::Schema,
            "X/Y",
            "second message",
        ));
        let out = report.aggregate();
        assert_eq!(out.errors.len(), 1);
        assert_eq!(out.errors[0].message, "first message");
        assert_eq!(
            out.errors[0].suggestion.as_deref(),
            Some("first suggestion")
        );
    }

    #[test]
    fn aggregate_does_not_cross_severity_buckets() {
        // Same code in different buckets should NOT merge — different
        // buckets indicate the issue was demoted/promoted independently.
        // (In practice rules emit at one severity per code, but be
        // defensive against post-`apply_rules` weirdness.)
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(agg_issue("X/Y", Severity::Error, 1));
        report.add(agg_issue("X/Y", Severity::Warning, 2));
        let out = report.aggregate();
        assert_eq!(out.errors.len(), 1);
        assert_eq!(out.warnings.len(), 1);
    }

    #[test]
    fn aggregate_is_idempotent() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(agg_issue("X/Y", Severity::Error, 1));
        report.add(agg_issue("X/Y", Severity::Error, 2));
        report.add(agg_issue("X/Y", Severity::Error, 3));
        let once = report.clone().aggregate();
        let twice = once.clone().aggregate();
        // Aggregating already-aggregated report yields same shape.
        assert_eq!(twice.errors.len(), 1);
        assert_eq!(twice.errors[0].instance_count(), 3);
    }

    #[test]
    fn aggregate_preserves_first_seen_order() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(agg_issue("Z/last", Severity::Error, 1));
        report.add(agg_issue("A/first", Severity::Error, 2));
        report.add(agg_issue("Z/last", Severity::Error, 3));
        report.add(agg_issue("A/first", Severity::Error, 4));
        let out = report.aggregate();
        // First-seen order maps to "Z/last" → idx 0, "A/first" → idx 1.
        assert_eq!(out.errors[0].code, "Z/last");
        assert_eq!(out.errors[1].code, "A/first");
    }

    #[test]
    fn aggregate_short_circuits_when_bucket_is_singleton() {
        // Single issue in a bucket — aggregate is a no-op and should
        // leave the issue untouched (no unnecessary clone / re-push).
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(agg_issue("X/Y", Severity::Error, 1));
        let out = report.aggregate();
        assert_eq!(out.errors.len(), 1);
        assert_eq!(out.errors[0].additional_instances.len(), 0);
        assert_eq!(out.errors[0].instance_count(), 1);
    }

    #[test]
    fn aggregate_display_shows_occurrence_count_when_above_one() {
        let mut report = ValidationReport::new(ValidationProfile::SMPTE);
        report.add(agg_issue("X/Y", Severity::Error, 1));
        report.add(agg_issue("X/Y", Severity::Error, 2));
        let out = report.aggregate();
        let rendered = format!("{}", out.errors[0]);
        assert!(
            rendered.contains("Occurrences: 2"),
            "Display should mention aggregate count: {rendered}"
        );
    }
}