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
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
use std::sync::Arc;
use thiserror::Error;
/// Wrapper around [`std::io::Error`] that implements [`PartialEq`] by comparing [`std::io::ErrorKind`].
///
/// This allows `EdifactError` to derive `PartialEq` without requiring `std::io::Error: PartialEq`.
#[derive(Debug)]
pub struct IoError(pub(crate) std::io::Error);
impl IoError {
/// Returns a reference to the underlying [`std::io::Error`].
pub fn inner(&self) -> &std::io::Error {
&self.0
}
}
impl PartialEq for IoError {
/// Equality is determined by [`std::io::ErrorKind`] only.
///
/// Two `IoError` values with the same kind but different OS-level error codes
/// (or different messages) will compare as equal. This is a deliberate
/// limitation: `std::io::Error` is not `PartialEq`, so kind-based comparison
/// is the only practical option that lets `EdifactError` derive `PartialEq`.
fn eq(&self, other: &Self) -> bool {
self.0.kind() == other.0.kind()
}
}
impl std::fmt::Display for IoError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for IoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.source()
}
}
impl From<std::io::Error> for IoError {
fn from(e: std::io::Error) -> Self {
Self(e)
}
}
/// All errors produced by `edifact-rs`.
///
/// # Error Variants
///
/// All variants that include an offset carry byte position information from the input stream.
/// This data enables precise error location reporting in diagnostics.
#[derive(Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum EdifactError {
/// Unexpected end of input while parsing.
///
/// This typically occurs when a segment terminator or expected delimiter
/// is not found before the end of the input stream.
#[error("unexpected end of input at byte offset {offset}")]
UnexpectedEof {
/// Byte offset where the parser exhausted input.
offset: usize,
},
/// Invalid byte encountered in a delimiter context.
///
/// Delimiters must be precisely ASCII characters from the UNA service string advice.
/// Any other byte is invalid in delimiter position.
#[error("invalid delimiter byte 0x{byte:02X} at offset {offset}")]
InvalidDelimiter {
/// Unexpected delimiter byte.
byte: u8,
/// Byte offset where the delimiter was observed.
offset: usize,
},
/// Invalid UTF-8 sequence in parsed text.
///
/// While EDIFACT operates on bytes, segments and elements are expected to contain
/// valid UTF-8 text. Non-UTF-8 sequences are rejected at parse time.
#[error("invalid EDIFACT text at byte offset {offset}")]
InvalidText {
/// Byte offset where invalid UTF-8 text starts.
offset: usize,
},
/// Invalid release-character escape sequence in parsed text.
///
/// The release character (`?` by default) must be followed by one escaped byte.
/// A trailing release character without a following byte is malformed.
#[error("invalid release sequence at byte offset {offset}: dangling release character")]
InvalidReleaseSequence {
/// Byte offset of the dangling release character.
offset: usize,
},
/// UNZ interchange message count does not match the number of UNH/UNT pairs found.
///
/// The `UNZ` segment declares the number of messages in the interchange,
/// but the actual number of `UNH`/`UNT` pairs observed differs.
#[error("interchange message count mismatch: UNZ declared {expected}, found {actual}")]
MessageCountMismatch {
/// Message count declared in the UNZ segment.
expected: u32,
/// Actual number of UNH/UNT pairs observed.
actual: u32,
},
/// UNT segment count does not match the actual number of segments in the message.
///
/// The `UNT` segment declares the number of segments in the message (including `UNH`/`UNT`),
/// but the actual count differs.
#[error(
"segment count mismatch in message {message_ref}: UNT declared {expected}, found {actual}"
)]
SegmentCountMismatch {
/// Segment count declared in the UNT segment.
expected: u32,
/// Actual number of segments observed.
actual: u32,
/// Message reference from the UNH segment.
message_ref: String,
},
/// Invalid or malformed segment tag.
///
/// Segment tags must be exactly 3 ASCII uppercase letters.
#[error("invalid segment tag {0:?}")]
InvalidSegmentTag(String),
/// Invalid UNA service string advice.
///
/// If present, the UNA segment must be exactly 9 bytes: `"UNA"` followed by
/// 6 service characters. The four active characters (element separator,
/// component separator, release character, and segment terminator) must be
/// mutually distinct and must not be ASCII whitespace. The decimal mark and
/// repetition separator characters are not validated by this check.
#[error("invalid UNA service string advice")]
InvalidUna,
/// Missing required element in a segment.
///
/// Certain segments require specific elements to be present. This error indicates
/// a mandatory element was not found.
#[error("missing required element {element_index} in segment {tag}")]
MissingRequiredElement {
/// Segment tag containing the missing element.
tag: String,
/// Zero-based required element index.
element_index: usize,
},
/// Missing required component in a composite element.
///
/// The element is present, but the required component at the given index is absent or empty.
#[error(
"missing required component {component_index} in element {element_index} of segment {tag}"
)]
MissingRequiredComponent {
/// Segment tag containing the composite element.
tag: String,
/// Zero-based element index of the composite.
element_index: usize,
/// Zero-based component index that was absent.
component_index: usize,
},
/// Output serialization produced invalid UTF-8.
///
/// This is an internal consistency error; the writer should never produce non-UTF-8 output.
/// If this occurs, it indicates a bug in the serialization logic.
#[error("serialized output contains invalid UTF-8")]
InvalidUtf8,
/// I/O error from reading or writing.
#[error(transparent)]
Io(#[from] IoError),
// ── validation variants (E010–E020) ────────────────────────────────────
/// Segment is not valid for the current message type.
///
/// Structural validation found a segment that should not appear in this message.
#[error("segment {tag} is not valid for message type {message_type}")]
InvalidSegmentForMessage {
/// Segment tag that is not allowed for the message type.
tag: String,
/// Message type used for structural validation.
message_type: String,
/// Segment tag byte offset.
offset: usize,
},
/// Element count in segment exceeds or falls short of directory definition.
///
/// Validation against directory metadata found an element count mismatch.
#[error("segment {tag} has {actual} elements, expected between {min} and {max}")]
InvalidElementCount {
/// Segment tag with wrong arity.
tag: String,
/// Minimum allowed element count.
min: usize,
/// Maximum allowed element count.
max: usize,
/// Actual element count found.
actual: usize,
/// Segment start byte offset.
offset: usize,
},
/// Component count in a composite element is invalid.
///
/// A composite data element does not have the expected number of components.
#[error("segment {tag} element {element_index} has {actual} components, expected {expected}")]
InvalidComponentCount {
/// Segment tag containing the composite.
tag: String,
/// Zero-based element index of the composite.
element_index: usize,
/// Expected component count.
expected: u8,
/// Actual component count found.
actual: u8,
/// Segment start byte offset.
offset: usize,
},
/// Code-list value is not valid.
///
/// The value appears in a field that should contain a code from a specific code list,
/// but the value is not in that code list.
#[error(
"segment {tag} element {element_index}: '{value}' is not a valid code (code list {code_list})"
)]
InvalidCodeValue {
/// Segment tag containing the invalid value.
tag: String,
/// Zero-based element index containing the invalid code.
element_index: usize,
/// Invalid code value observed.
value: String,
/// Data element code list identifier.
code_list: String,
/// Segment start byte offset.
offset: usize,
/// Optional remediation suggestion from the code-list lookup function.
suggestion: Option<&'static str>,
},
/// A required segment is missing from the message.
///
/// Structural validation found that a mandatory segment is absent.
#[error("required segment {tag} is missing from message (position {expected_position})")]
MissingSegment {
/// Missing segment tag.
tag: String,
/// Human-readable position hint.
expected_position: String,
},
/// Qualifier does not match expected value for segment.
///
/// A qualified segment (e.g., NAD+MS) has a qualifier that does not match expected.
#[error("segment {tag} has qualifier '{actual}', expected '{expected}'")]
QualifierMismatch {
/// Segment tag whose qualifier mismatched.
tag: String,
/// Actual qualifier found.
actual: String,
/// Expected qualifier value.
expected: String,
/// Segment start byte offset.
offset: usize,
},
/// Conditional requirement not met.
///
/// A segment or element is conditionally required based on another element's value,
/// but the condition was not satisfied.
#[error("segment {tag} element {element_index}: conditional requirement not met ({condition})")]
ConditionalRequirementNotMet {
/// Segment tag that violated a conditional rule.
tag: String,
/// Zero-based element index governed by the condition.
element_index: usize,
/// Condition text describing the rule.
condition: String,
/// Segment start byte offset.
offset: usize,
},
/// Validation failed and the full [`ValidationReport`] is preserved.
///
/// Returned by validation helpers when errors are found. Provides programmatic
/// access to all issues, warnings, and infos.
///
/// # Example
///
/// ```rust,ignore
/// match my_fn() {
/// Err(EdifactError::ValidationErrors { report, .. }) => {
/// for issue in report.errors() {
/// eprintln!("{}", issue);
/// }
/// }
/// other => { /* ... */ }
/// }
/// ```
#[error("validation failed with {error_count} error(s)")]
ValidationErrors {
/// Number of error-severity issues in the report.
error_count: usize,
/// Full report with all errors, warnings, and infos.
report: Box<ValidationReport>,
},
/// Segment exceeded the configured maximum byte length.
///
/// Returned by reader-based parsers when an unterminated segment accumulates more
/// bytes than the configured `max_segment_bytes` limit in [`ReaderConfig`]. This
/// prevents resource exhaustion on adversarially crafted or truncated input that
/// never emits a segment terminator.
///
/// [`ReaderConfig`]: crate::ReaderConfig
#[error("segment starting at byte offset {offset} exceeded maximum length of {limit} bytes")]
SegmentTooLong {
/// Byte offset where the overlong segment started.
offset: usize,
/// Configured maximum segment byte length.
limit: usize,
},
/// No handler was registered in [`crate::MessageDispatch`] for this message type.
///
/// Returned by [`crate::MessageDispatch::dispatch`] when the message-type
/// extracted from the `UNH` segment does not match any registered handler
/// and no fallback was configured.
#[error("no handler registered for message type {message_type}")]
UnexpectedMessageType {
/// The unhandled message type string from the `UNH` segment.
message_type: String,
},
/// An interchange or message contains more segments or messages than can be
/// represented in a `u32` counter (> 4 294 967 295).
///
/// This is effectively unreachable in practice — no real-world EDIFACT
/// interchange has billions of segments — but the parser returns this error
/// rather than silently saturating or wrapping the counter.
#[error("interchange too large: count {count} exceeds u32::MAX")]
InterchangeTooLarge {
/// The count that could not be represented as `u32`.
count: u64,
},
/// An [`crate::EventEmitter`] received events in an invalid sequence.
///
/// This indicates a programming error in the caller's serialization code:
/// for example, emitting an [`crate::EdifactEvent::Element`] without a prior
/// [`crate::EdifactEvent::StartSegment`], or emitting
/// [`crate::EdifactEvent::ComponentElement`] without a preceding
/// [`crate::EdifactEvent::Element`].
#[error("invalid event sequence: {message}")]
InvalidEventSequence {
/// Description of the protocol violation.
message: &'static str,
},
/// An [`crate::OwnedElementRef`] has `position = 0`, which is never valid.
///
/// Element positions are one-based: position 1 refers to the first element
/// slot. Position 0 is reserved and invalid. Use [`crate::OwnedElementRef::new`]
/// to catch this at construction time.
#[error("element definition contains invalid position 0; positions must be >= 1 (one-based)")]
InvalidElementPosition,
/// Two [`crate::ProfileRulePack`] values with incompatible release scopes were composed.
///
/// When composing packs via [`crate::ProfileRulePack::extend_from`] or
/// [`crate::ProfileRulePack::merge_with_override`], both packs must either
/// share the same release scope or at most one may carry a scope.
#[error("incompatible release scopes: cannot compose {current:?} with {incoming:?}")]
#[non_exhaustive]
IncompatibleReleaseScopes {
/// Release scope of the pack being composed into.
current: String,
/// Release scope of the pack being composed in.
incoming: String,
},
/// A field value failed semantic validation (e.g. wrong format, out-of-range).
///
/// Distinct from [`InvalidCodeValue`][Self::InvalidCodeValue] which is for
/// code-list membership checks. Use this variant when a free-text or numeric
/// field contains a value that is structurally invalid for its purpose.
#[error("segment {tag} element {element_index}: invalid field value {value:?}")]
InvalidFieldValue {
/// Segment tag that contains the invalid field.
tag: String,
/// Zero-based element index of the invalid field.
element_index: usize,
/// The invalid value that was observed.
value: String,
},
/// A data or component element token appeared before the first segment tag.
///
/// EDIFACT syntax requires that every data element follows a segment tag.
/// A data element token encountered before any tag (e.g. after a stray
/// separator at the start of the stream) is a protocol violation.
///
/// Unlike stray segment terminators (which are tolerated as blank lines),
/// stray data tokens indicate encoding corruption or a partial write.
#[error("unexpected data token at byte offset {offset}: data element before segment tag")]
UnexpectedDataToken {
/// Byte offset of the stray token.
offset: usize,
},
/// The input contains EDIFACT functional group segments (`UNG`/`UNE`).
///
/// Functional groups are defined in ISO 9735 but are rarely used in practice
/// and are not supported by this library. Strip `UNG`/`UNE` wrappers before
/// calling `validate_envelope`, or process the interchange as raw segments.
#[error(
"functional group segments (UNG/UNE) at byte offset {offset} are not supported; \
strip them before calling validate_envelope"
)]
FunctionalGroupNotSupported {
/// Byte offset of the first `UNG` or `UNE` segment found.
offset: usize,
},
}
impl From<std::io::Error> for EdifactError {
fn from(e: std::io::Error) -> Self {
Self::Io(IoError(e))
}
}
impl EdifactError {
/// Stable diagnostic code for this error variant.
#[must_use]
pub const fn stable_code(&self) -> &'static str {
match self {
Self::UnexpectedEof { .. } => "E001",
Self::InvalidDelimiter { .. } => "E002",
Self::InvalidText { .. } => "E003",
Self::MessageCountMismatch { .. } => "E004",
Self::SegmentCountMismatch { .. } => "E005",
Self::InvalidSegmentTag(_) => "E006",
Self::InvalidUna => "E007",
Self::MissingRequiredElement { .. } => "E008",
Self::InvalidUtf8 => "E009",
Self::Io(_) => "E010",
Self::InvalidSegmentForMessage { .. } => "E011",
Self::InvalidElementCount { .. } => "E012",
Self::InvalidComponentCount { .. } => "E013",
Self::InvalidCodeValue { .. } => "E014",
Self::MissingSegment { .. } => "E015",
Self::QualifierMismatch { .. } => "E016",
Self::ConditionalRequirementNotMet { .. } => "E017",
// E018 is permanently retired (was ValidationFailed, removed in 0.8.0)
Self::InvalidReleaseSequence { .. } => "E019",
Self::SegmentTooLong { .. } => "E020",
Self::MissingRequiredComponent { .. } => "E021",
Self::UnexpectedMessageType { .. } => "E022",
Self::InterchangeTooLarge { .. } => "E023",
Self::InvalidEventSequence { .. } => "E024",
Self::InvalidElementPosition => "E025",
Self::IncompatibleReleaseScopes { .. } => "E026",
Self::InvalidFieldValue { .. } => "E027",
Self::UnexpectedDataToken { .. } => "E028",
Self::FunctionalGroupNotSupported { .. } => "E029",
Self::ValidationErrors { .. } => "E030",
}
}
/// Stable recovery hint for common malformed input and validation cases.
#[must_use]
pub fn recovery_hint(&self) -> Option<&'static str> {
match self {
Self::UnexpectedEof { .. } => {
Some("Ensure every segment ends with the configured segment terminator")
}
Self::InvalidDelimiter { .. } => {
Some("Check UNA service string advice and delimiter bytes in the payload")
}
Self::InvalidText { .. } => {
Some("Input must be valid UTF-8 text for segment and element values")
}
Self::InvalidReleaseSequence { .. } => {
Some("Release character must escape one following byte; trailing '?' is invalid")
}
Self::InvalidSegmentTag(_) => Some("Segment tags must be 3 ASCII uppercase letters"),
Self::InvalidUna => Some(
"UNA must be exactly 9 bytes: 'UNA' followed by 6 distinct, non-whitespace service characters",
),
Self::MissingRequiredElement { .. } => {
Some("Provide all mandatory elements for the segment per directory rules")
}
Self::MissingRequiredComponent { .. } => Some(
"Provide all mandatory components for the composite element per directory rules",
),
Self::InvalidSegmentForMessage { .. } => {
Some("Remove unsupported segment or switch to the correct message type")
}
Self::InvalidElementCount { .. } => {
Some("Adjust the segment element count to the allowed min/max range")
}
Self::InvalidComponentCount { .. } => {
Some("Fix composite element arity to match the expected component count")
}
Self::InvalidCodeValue { .. } => {
Some("Use a value from the referenced code list for this element")
}
Self::MissingSegment { .. } => {
Some("Insert the required segment at the expected position")
}
Self::QualifierMismatch { .. } => {
Some("Set the segment qualifier to the expected value")
}
Self::ConditionalRequirementNotMet { .. } => {
Some("When the condition is met, include the conditionally required element")
}
Self::SegmentTooLong { limit, .. } => {
let _ = limit; // used in the error message; hint is generic
Some("Increase max_segment_bytes in ReaderConfig or reject the input as malformed")
}
Self::InvalidEventSequence { .. } => {
Some("Emit StartSegment before Element, and Element before ComponentElement")
}
Self::InvalidElementPosition => Some(
"Set element position to a value >= 1; positions are one-based (1 = first element slot)",
),
Self::IncompatibleReleaseScopes { .. } => Some(
"Only compose ProfileRulePack values that share the same release scope, or where at most one has a release scope set",
),
Self::InvalidFieldValue { .. } => Some(
"Correct the field value to match the expected format or range for this element",
),
Self::UnexpectedDataToken { .. } => Some(
"A data element appeared before any segment tag; check for partial writes or encoding corruption",
),
Self::FunctionalGroupNotSupported { .. } => Some(
"Strip UNG/UNE segments before calling validate_envelope, or process the interchange as raw segments",
),
Self::ValidationErrors { .. }
| Self::MessageCountMismatch { .. }
| Self::SegmentCountMismatch { .. }
| Self::UnexpectedMessageType { .. }
| Self::InterchangeTooLarge { .. }
| Self::InvalidUtf8
| Self::Io(_) => None,
}
}
}
#[cfg(feature = "diagnostics")]
#[cfg_attr(docsrs, doc(cfg(feature = "diagnostics")))]
impl miette::Diagnostic for EdifactError {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new(self.stable_code()))
}
fn severity(&self) -> Option<miette::Severity> {
match self {
Self::InvalidCodeValue { .. }
| Self::InvalidComponentCount { .. }
| Self::QualifierMismatch { .. } => Some(miette::Severity::Warning),
_ => Some(miette::Severity::Error),
}
}
fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
match self {
// Static text — no allocation needed.
Self::InvalidUna => Some(Box::new(
"UNA segment must be exactly 9 bytes: 'UNA' + 6 service characters. See EDIFACT spec",
)),
Self::InvalidUtf8 => Some(Box::new(
"Internal error: serialized output contains invalid UTF-8. Please report this as a bug",
)),
// Dynamic help text.
Self::UnexpectedEof { offset } => Some(Box::new(format!(
"Check that all segments are terminated with the segment terminator (usually '). \
Reached end at offset {offset}",
))),
Self::InvalidDelimiter { byte, offset } => Some(Box::new(format!(
"The byte 0x{byte:02X} at offset {offset} is not a valid delimiter. \
Check UNA configuration",
))),
Self::InvalidText { offset } => Some(Box::new(format!(
"The byte sequence at offset {offset} contains invalid UTF-8. \
Ensure input is valid UTF-8",
))),
Self::InvalidReleaseSequence { offset } => Some(Box::new(format!(
"Release character at offset {offset} is dangling. \
Ensure '?' is followed by an escaped byte",
))),
Self::MessageCountMismatch { expected, actual } => Some(Box::new(format!(
"UNZ declares {expected} message(s) but {actual} UNH/UNT pair(s) were found. \
Check the UNZ message count",
))),
Self::SegmentCountMismatch {
expected,
actual,
message_ref,
} => Some(Box::new(format!(
"UNT for message {message_ref} declares {expected} segment(s) but {actual} were found. \
Check the UNT segment count",
))),
Self::InvalidSegmentTag(tag) => Some(Box::new(format!(
"Segment tag '{tag}' must be exactly 3 ASCII uppercase letters",
))),
Self::MissingRequiredElement { tag, element_index } => Some(Box::new(format!(
"Segment {tag} requires element at index {element_index}",
))),
Self::MissingRequiredComponent {
tag,
element_index,
component_index,
} => Some(Box::new(format!(
"Segment {tag} element {element_index} requires component at index {component_index}",
))),
Self::Io(e) => Some(Box::new(format!("I/O error: {e}"))),
Self::InvalidSegmentForMessage {
tag, message_type, ..
} => Some(Box::new(format!(
"Segment {tag} should not appear in a {message_type} message. \
Check the directory definition",
))),
Self::InvalidElementCount {
tag,
min,
max,
actual,
..
} => Some(Box::new(format!(
"Segment {tag} should have between {min} and {max} elements, but has {actual}. \
Check segment structure",
))),
Self::InvalidComponentCount {
tag,
element_index,
expected,
actual,
..
} => Some(Box::new(format!(
"In segment {tag}, element {element_index} should have {expected} components \
but has {actual}. Check element structure",
))),
Self::InvalidCodeValue {
tag,
element_index,
value,
code_list,
..
} => Some(Box::new(format!(
"Value '{value}' in segment {tag} element {element_index} is not in the \
{code_list} code list. Check the directory for valid codes",
))),
Self::MissingSegment {
tag,
expected_position,
} => Some(Box::new(format!(
"Segment {tag} is required at position {expected_position} but is missing. \
Add this segment to the message",
))),
Self::QualifierMismatch {
tag,
actual,
expected,
..
} => Some(Box::new(format!(
"Segment {tag} has qualifier '{actual}' but expected '{expected}'. \
Check the segment's first component",
))),
Self::ConditionalRequirementNotMet {
tag,
element_index,
condition,
..
} => Some(Box::new(format!(
"In segment {tag}, element {element_index} is conditionally required when: \
{condition}. Check if the condition is met",
))),
Self::SegmentTooLong { offset, limit } => Some(Box::new(format!(
"Segment starting at byte offset {offset} exceeds the {limit}-byte limit. \
Use ReaderConfig::max_segment_bytes to adjust the limit if needed, \
or verify the input for a missing segment terminator",
))),
Self::UnexpectedMessageType { message_type } => Some(Box::new(format!(
"No handler was registered for message type '{message_type}'. \
Register a handler with MessageDispatch::on(\"{message_type}\", ...)",
))),
Self::InterchangeTooLarge { count } => Some(Box::new(format!(
"Interchange contains {count} items which exceeds the u32::MAX limit. \
This is an extremely unusual input; verify the message is not corrupted.",
))),
Self::InvalidEventSequence { message } => Some(Box::new(format!(
"Event sequence violation: {message}. \
Check that StartSegment is emitted before Element, and Element before ComponentElement.",
))),
Self::InvalidElementPosition => Some(Box::new(
"Element positions must be >= 1 (one-based). \
Ensure no OwnedElementRef is constructed with position == 0",
)),
Self::IncompatibleReleaseScopes { current, incoming } => Some(Box::new(format!(
"Release scope {current:?} and {incoming:?} are incompatible. \
Only compose ProfileRulePack values that share the same release scope, \
or where at most one carries a release scope",
))),
Self::InvalidFieldValue {
tag,
element_index,
value,
} => Some(Box::new(format!(
"Segment {tag} element {element_index} has invalid value '{value}'. \
Check the expected format or range for this field",
))),
Self::UnexpectedDataToken { offset } => Some(Box::new(format!(
"Data element at offset {offset} appeared before any segment tag. \
Check for partial writes or encoding corruption",
))),
Self::FunctionalGroupNotSupported { offset } => Some(Box::new(format!(
"Functional group segment (UNG/UNE) found at offset {offset}. \
Strip UNG/UNE wrappers before calling validate_envelope",
))),
Self::ValidationErrors { error_count, .. } => Some(Box::new(format!(
"Validation found {error_count} error(s). Inspect the ValidationReport for details",
))),
}
}
}
// ── validation report ─────────────────────────────────────────────────────────
/// Priority level for a validation error or warning.
///
/// Marked `#[non_exhaustive]` so that adding new severity levels in future
/// releases is not a breaking change for downstream match arms.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ValidationSeverity {
/// Structural parse failure; processing cannot continue.
Critical,
/// Structural validation failed; message is invalid.
Error,
/// Data validation warning (e.g., code-list mismatch); message may be usable.
Warning,
/// Informational note; message is valid but noteworthy.
Info,
}
impl ValidationSeverity {
/// Return a lowercase ASCII string for this severity level.
///
/// Stable for the four known variants. Because the enum is
/// `#[non_exhaustive]`, new variants added in future releases are
/// handled by a catch-all arm that returns `"unknown"` so that
/// existing code keeps compiling and serialising gracefully.
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Critical => "critical",
Self::Error => "error",
Self::Warning => "warning",
Self::Info => "info",
#[allow(unreachable_patterns)]
_ => "unknown",
}
}
/// Return a numeric priority for this severity level.
///
/// Higher values indicate higher severity: `Critical = 3`, `Error = 2`,
/// `Warning = 1`, `Info = 0`.
#[must_use]
pub fn numeric_level(self) -> u8 {
match self {
Self::Info => 0,
Self::Warning => 1,
Self::Error => 2,
Self::Critical => 3,
}
}
}
impl std::fmt::Display for ValidationSeverity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
/// A structured validation issue.
///
/// Marked `#[non_exhaustive]` so that new diagnostic fields (e.g. `segment_group`)
/// can be added in future releases without breaking downstream code that constructs
/// issues via struct literals. Always use [`ValidationIssue::new`] + builder
/// methods (`with_*`) rather than constructing directly.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ValidationIssue {
/// Stable error code, if known.
pub error_code: Option<&'static str>,
/// The severity of this issue.
pub severity: ValidationSeverity,
/// The error or warning message.
pub message: String,
/// Byte offset in the source (if available).
pub offset: Option<usize>,
/// Segment tag involved (if known).
pub segment_tag: Option<String>,
/// Profile/MIG rule identifier, if applicable.
pub rule_id: Option<String>,
/// Element index (0-based), if known.
///
/// `u8` is sufficient: EDIFACT segments have at most 99 data elements per
/// the UN/EDIFACT standard, so an index fits comfortably in one byte.
pub element_index: Option<u8>,
/// Component index (0-based), if known.
///
/// `u8` is sufficient: composite data elements have at most 99 components
/// per the UN/EDIFACT standard.
pub component_index: Option<u8>,
/// Zero-based occurrence index among segments with the same tag in the message.
///
/// When multiple segments share the same tag (e.g. repeated `DTM` lines),
/// this field indicates which occurrence (0 = first) was the source of
/// this issue. `None` when occurrence tracking is not available for this rule.
pub segment_occurrence: Option<u16>,
/// Message reference (`UNH` element 0, DE 0062) that this issue belongs to.
///
/// Populated automatically when the context was built with
/// `ValidationContextBuilder::with_message_ref`. Useful in batch processing
/// where many messages are validated and issues from different messages must
/// be correlated back to the originating `UNH`/`UNT` envelope.
pub message_ref: Option<String>,
/// Suggested remediation (if available).
pub suggestion: Option<String>,
/// Segment group (e.g. `"SG6"`) in which the issue occurred, if known.
///
/// Populated by group-aware rule functions when they evaluate sub-slices of a
/// [`crate::group::SegmentGroupIndexed`] tree. `None` for flat-segment rules
/// that do not have group context.
pub segment_group: Option<Arc<str>>,
}
impl ValidationIssue {
/// Create a new validation issue.
pub fn new(severity: ValidationSeverity, message: impl Into<String>) -> Self {
Self {
error_code: None,
severity,
message: message.into(),
offset: None,
segment_tag: None,
rule_id: None,
element_index: None,
component_index: None,
segment_occurrence: None,
message_ref: None,
suggestion: None,
segment_group: None,
}
}
/// Set stable error code metadata.
pub fn with_error_code(mut self, code: &'static str) -> Self {
self.error_code = Some(code);
self
}
/// Set the offset for this issue.
pub fn with_offset(mut self, offset: usize) -> Self {
self.offset = Some(offset);
self
}
/// Set the segment tag for this issue.
pub fn with_segment(mut self, tag: impl Into<String>) -> Self {
self.segment_tag = Some(tag.into());
self
}
/// Set the profile/MIG rule identifier for this issue.
pub fn with_rule_id(mut self, rule_id: impl Into<String>) -> Self {
self.rule_id = Some(rule_id.into());
self
}
/// Set the element index (0-based) for this issue.
pub fn with_element_index(mut self, element_index: u8) -> Self {
self.element_index = Some(element_index);
self
}
/// Set the component index (0-based) for this issue.
pub fn with_component_index(mut self, component_index: u8) -> Self {
self.component_index = Some(component_index);
self
}
/// Set a suggestion for resolving this issue.
pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
self.suggestion = Some(suggestion.into());
self
}
/// Set the zero-based occurrence index for this issue.
///
/// Use this when the same segment tag appears multiple times in a message
/// and you want to identify which occurrence is affected.
pub fn with_segment_occurrence(mut self, occurrence: u16) -> Self {
self.segment_occurrence = Some(occurrence);
self
}
/// Set the message reference (`UNH` element 0) for this issue.
///
/// Use this to correlate an issue back to a specific message in a
/// multi-message interchange.
pub fn with_message_ref(mut self, message_ref: impl Into<String>) -> Self {
self.message_ref = Some(message_ref.into());
self
}
/// Set the segment group (e.g. `"SG6"`) in which this issue occurred.
///
/// Use this from group-aware rule functions that evaluate a sub-slice of a
/// [`crate::group::SegmentGroupIndexed`] tree so that consumers can identify
/// the exact group occurrence without re-reading the raw message.
pub fn with_segment_group(mut self, group: impl Into<Arc<str>>) -> Self {
self.segment_group = Some(group.into());
self
}
/// Short label for the severity level, suitable for display.
#[must_use]
pub fn severity_label(&self) -> &'static str {
match self.severity {
ValidationSeverity::Critical => "CRITICAL",
ValidationSeverity::Error => "ERROR",
ValidationSeverity::Warning => "WARNING",
ValidationSeverity::Info => "INFO",
}
}
// ── Getters ───────────────────────────────────────────────────────────────
/// Stable error code, if available.
#[must_use]
#[inline]
pub fn error_code(&self) -> Option<&'static str> {
self.error_code
}
/// Byte offset in the source, if available.
#[must_use]
#[inline]
pub fn offset(&self) -> Option<usize> {
self.offset
}
/// Segment tag involved in this issue, if known.
#[must_use]
#[inline]
pub fn segment_tag(&self) -> Option<&str> {
self.segment_tag.as_deref()
}
/// Profile/MIG rule identifier, if applicable.
#[must_use]
#[inline]
pub fn rule_id(&self) -> Option<&str> {
self.rule_id.as_deref()
}
/// Zero-based element index, if known.
#[must_use]
#[inline]
pub fn element_index(&self) -> Option<u8> {
self.element_index
}
/// Zero-based component index, if known.
#[must_use]
#[inline]
pub fn component_index(&self) -> Option<u8> {
self.component_index
}
/// Zero-based occurrence index among same-tag segments, if known.
#[must_use]
#[inline]
pub fn segment_occurrence(&self) -> Option<u16> {
self.segment_occurrence
}
/// Message reference (`UNH` element 0), if set.
#[must_use]
#[inline]
pub fn message_ref(&self) -> Option<&str> {
self.message_ref.as_deref()
}
/// Suggested remediation, if available.
#[must_use]
#[inline]
pub fn suggestion(&self) -> Option<&str> {
self.suggestion.as_deref()
}
/// Segment group (e.g. `"SG6"`) in which the issue occurred, if known.
#[must_use]
#[inline]
pub fn segment_group(&self) -> Option<&str> {
self.segment_group.as_deref()
}
}
impl std::fmt::Display for ValidationIssue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}] {}", self.severity_label(), self.message)
}
}
impl std::error::Error for ValidationIssue {}
/// A collection of validation results: errors, warnings, and info.
///
/// Enables batch validation where all issues are collected instead of failing on the first error.
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ValidationReport {
/// Critical and error-level issues.
pub(crate) errors: Vec<ValidationIssue>,
/// Warning-level issues.
pub(crate) warnings: Vec<ValidationIssue>,
/// Informational notes.
pub(crate) infos: Vec<ValidationIssue>,
}
impl ValidationReport {
/// Returns all error-level [`ValidationIssue`]s in this report.
pub fn errors(&self) -> &[ValidationIssue] {
&self.errors
}
/// Returns all error-level [`ValidationIssue`]s mutably.
pub fn errors_mut(&mut self) -> &mut [ValidationIssue] {
&mut self.errors
}
/// Returns all warning-level [`ValidationIssue`]s in this report.
pub fn warnings(&self) -> &[ValidationIssue] {
&self.warnings
}
/// Returns all warning-level [`ValidationIssue`]s mutably.
pub fn warnings_mut(&mut self) -> &mut [ValidationIssue] {
&mut self.warnings
}
/// Returns all informational [`ValidationIssue`]s in this report.
pub fn infos(&self) -> &[ValidationIssue] {
&self.infos
}
/// Returns all informational [`ValidationIssue`]s mutably.
pub fn infos_mut(&mut self) -> &mut [ValidationIssue] {
&mut self.infos
}
/// Add an error to the report.
pub fn add_error(&mut self, issue: ValidationIssue) {
self.errors.push(issue);
}
/// Add a warning to the report.
pub fn add_warning(&mut self, issue: ValidationIssue) {
self.warnings.push(issue);
}
/// Add an info message to the report.
pub fn add_info(&mut self, issue: ValidationIssue) {
self.infos.push(issue);
}
/// Check if the report has any errors.
pub fn has_errors(&self) -> bool {
!self.errors().is_empty()
}
/// Check if the report has any warnings.
pub fn has_warnings(&self) -> bool {
!self.warnings().is_empty()
}
/// Get the total count of all issues.
pub fn total_issues(&self) -> usize {
self.errors().len() + self.warnings().len() + self.infos().len()
}
/// Check if the validation passed (no errors, but may have warnings).
pub fn is_valid(&self) -> bool {
self.errors().is_empty()
}
/// Convert to a `Result`.
///
/// Returns `Ok(self)` when there are no errors. Returns `Err(self)` when
/// there is at least one error-level issue, **preserving warnings and infos**
/// in the `Err` variant so callers can inspect the full report.
pub fn result(self) -> Result<Self, Self> {
if self.is_valid() { Ok(self) } else { Err(self) }
}
/// Iterate over all issues in severity buckets: errors, warnings, then infos.
pub fn iter_issues(&self) -> impl Iterator<Item = &ValidationIssue> {
self.errors()
.iter()
.chain(self.warnings().iter())
.chain(self.infos().iter())
}
/// Return `true` if the report contains any issues (errors, warnings, or infos).
pub fn has_any_issues(&self) -> bool {
!self.errors().is_empty() || !self.warnings().is_empty() || !self.infos().is_empty()
}
/// Drain all issues from `other` into `self`.
///
/// Issues are appended in severity order: errors, warnings, infos.
/// `other` is left empty after this call.
pub fn merge(&mut self, mut other: ValidationReport) {
self.errors.append(&mut other.errors);
self.warnings.append(&mut other.warnings);
self.infos.append(&mut other.infos);
}
/// Iterate over all issues matching an exact profile/MIG rule identifier.
///
/// Searches errors, warnings, and infos in that order. Returns a lazy
/// iterator; collect into `Vec` if you need random access.
pub fn issues_for_rule_id<'a>(
&'a self,
rule_id: &'a str,
) -> impl Iterator<Item = &'a ValidationIssue> + 'a {
self.iter_issues()
.filter(move |issue| issue.rule_id.as_deref() == Some(rule_id))
}
/// Return a cloned report filtered by `pred`.
fn filter_report<F>(&self, pred: F) -> Self
where
F: Fn(&ValidationIssue) -> bool,
{
Self {
errors: self.errors().iter().filter(|i| pred(i)).cloned().collect(),
warnings: self
.warnings()
.iter()
.filter(|i| pred(i))
.cloned()
.collect(),
infos: self.infos().iter().filter(|i| pred(i)).cloned().collect(),
}
}
/// Return a cloned report containing only issues with an exact rule identifier.
pub fn filter_by_rule_id(&self, rule_id: &str) -> Self {
self.filter_report(|issue| issue.rule_id.as_deref() == Some(rule_id))
}
/// Return a cloned report containing only issues whose rule identifier starts with `prefix`.
pub fn filter_by_rule_prefix(&self, prefix: &str) -> Self {
self.filter_report(|issue| {
issue
.rule_id
.as_deref()
.is_some_and(|id| id.starts_with(prefix))
})
}
/// Return a cloned report containing only issues that reference `segment_tag`.
///
/// Issues whose `segment_tag` field does not match are dropped; the severity
/// buckets (errors / warnings / infos) are preserved.
///
/// # Example
///
/// ```rust
/// use edifact_rs::{ValidationReport, ValidationIssue, ValidationSeverity};
///
/// let mut report = ValidationReport::default();
/// report.add_error(
/// ValidationIssue::new(ValidationSeverity::Error, "BGM missing")
/// .with_segment("BGM"),
/// );
/// report.add_error(
/// ValidationIssue::new(ValidationSeverity::Error, "NAD missing")
/// .with_segment("NAD"),
/// );
/// let bgm_issues = report.for_segment("BGM");
/// assert_eq!(bgm_issues.errors().len(), 1);
/// assert_eq!(bgm_issues.errors()[0].segment_tag.as_deref(), Some("BGM"));
/// ```
pub fn for_segment(&self, segment_tag: &str) -> Self {
self.filter_report(|issue| issue.segment_tag.as_deref() == Some(segment_tag))
}
/// Return a deterministic, stable text representation for snapshots and logs.
pub fn render_deterministic(&self) -> String {
fn sorted_refs(issues: &[ValidationIssue]) -> Vec<&ValidationIssue> {
let mut refs: Vec<&ValidationIssue> = issues.iter().collect();
refs.sort_by(|left, right| {
left.offset
.unwrap_or(usize::MAX)
.cmp(&right.offset.unwrap_or(usize::MAX))
.then_with(|| {
left.segment_tag
.as_deref()
.unwrap_or("")
.cmp(right.segment_tag.as_deref().unwrap_or(""))
})
.then_with(|| {
left.rule_id
.as_deref()
.unwrap_or("")
.cmp(right.rule_id.as_deref().unwrap_or(""))
})
.then_with(|| {
left.element_index
.unwrap_or(u8::MAX)
.cmp(&right.element_index.unwrap_or(u8::MAX))
})
.then_with(|| {
left.component_index
.unwrap_or(u8::MAX)
.cmp(&right.component_index.unwrap_or(u8::MAX))
})
.then_with(|| {
left.error_code
.unwrap_or("")
.cmp(right.error_code.unwrap_or(""))
})
.then_with(|| left.message.cmp(&right.message))
});
refs
}
fn render_issue_line(out: &mut String, issue: &ValidationIssue) {
use std::fmt::Write as _;
out.push_str(" - ");
out.push_str(&issue.message);
if let Some(code) = issue.error_code {
out.push_str(" [");
out.push_str(code);
out.push(']');
}
if let Some(seg) = &issue.segment_tag {
out.push_str(" [segment=");
out.push_str(seg);
out.push(']');
}
if let Some(rule_id) = &issue.rule_id {
out.push_str(" [rule=");
out.push_str(rule_id);
out.push(']');
}
if let Some(element_index) = issue.element_index {
write!(out, " [element={element_index}]").ok();
}
if let Some(component_index) = issue.component_index {
write!(out, " [component={component_index}]").ok();
}
if let Some(offset) = issue.offset {
write!(out, " [offset={offset}]").ok();
}
if let Some(suggestion) = &issue.suggestion {
out.push_str(" [hint=");
out.push_str(suggestion);
out.push(']');
}
}
use std::fmt::Write as _;
let mut out = String::from("Validation Report:");
let errors = sorted_refs(self.errors());
let warnings = sorted_refs(self.warnings());
let infos = sorted_refs(self.infos());
if !errors.is_empty() {
write!(out, "\n Errors ({})", errors.len()).ok();
for issue in &errors {
out.push('\n');
render_issue_line(&mut out, issue);
}
}
if !warnings.is_empty() {
write!(out, "\n Warnings ({})", warnings.len()).ok();
for issue in &warnings {
out.push('\n');
render_issue_line(&mut out, issue);
}
}
if !infos.is_empty() {
write!(out, "\n Info ({})", infos.len()).ok();
for issue in &infos {
out.push('\n');
render_issue_line(&mut out, issue);
}
}
out
}
}
#[cfg(feature = "diagnostics")]
impl miette::Diagnostic for ValidationReport {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new("VALIDATION"))
}
fn severity(&self) -> Option<miette::Severity> {
if self.has_errors() {
Some(miette::Severity::Error)
} else if self.has_warnings() {
Some(miette::Severity::Warning)
} else {
Some(miette::Severity::Advice)
}
}
fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
let msg = format!(
"Validation found {} error(s), {} warning(s), {} info(s)",
self.errors().len(),
self.warnings().len(),
self.infos().len()
);
Some(Box::new(msg))
}
}
impl std::fmt::Display for ValidationReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.render_deterministic())
}
}
impl std::error::Error for ValidationReport {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validation_report_collects_errors() {
let mut report = ValidationReport::default();
report.add_error(
ValidationIssue::new(ValidationSeverity::Error, "Test error")
.with_segment("BGM")
.with_offset(42),
);
report.add_warning(ValidationIssue::new(
ValidationSeverity::Warning,
"Test warning",
));
assert!(report.has_errors());
assert!(report.has_warnings());
assert_eq!(report.total_issues(), 2);
assert!(!report.is_valid());
}
#[test]
fn validation_report_result_conversion() {
let mut report = ValidationReport::default();
report.add_error(ValidationIssue::new(
ValidationSeverity::Error,
"Critical issue",
));
let result = report.result();
assert!(result.is_err());
}
#[test]
fn validation_report_passes_when_no_errors() {
let mut report = ValidationReport::default();
report.add_warning(ValidationIssue::new(
ValidationSeverity::Warning,
"Just a warning",
));
assert!(report.is_valid());
assert!(report.result().is_ok());
}
#[test]
fn validation_issue_builder() {
let issue = ValidationIssue::new(ValidationSeverity::Warning, "test message")
.with_error_code("E013")
.with_offset(100)
.with_segment("NAD")
.with_rule_id("DEMO-P001")
.with_element_index(1)
.with_component_index(2)
.with_suggestion("Check element count");
assert_eq!(issue.error_code, Some("E013"));
assert_eq!(issue.message, "test message");
assert_eq!(issue.offset, Some(100));
assert_eq!(issue.segment_tag, Some("NAD".to_owned()));
assert_eq!(issue.rule_id, Some("DEMO-P001".to_owned()));
assert_eq!(issue.element_index, Some(1));
assert_eq!(issue.component_index, Some(2));
assert_eq!(issue.suggestion, Some("Check element count".to_owned()));
}
#[test]
fn validation_report_display() {
let mut report = ValidationReport::default();
report.add_error(
ValidationIssue::new(ValidationSeverity::Error, "Error 1")
.with_error_code("E011")
.with_offset(8),
);
report.add_warning(ValidationIssue::new(
ValidationSeverity::Warning,
"Warning 1",
));
report.add_info(ValidationIssue::new(ValidationSeverity::Info, "Info 1"));
let display_str = format!("{}", report);
assert!(display_str.contains("Errors (1)"));
assert!(display_str.contains("Warnings (1)"));
assert!(display_str.contains("Info (1)"));
assert!(display_str.contains("[E011]"));
}
#[test]
fn validation_report_render_is_deterministic() {
let mut report = ValidationReport::default();
report.add_error(
ValidationIssue::new(ValidationSeverity::Error, "later")
.with_segment("BGM")
.with_offset(20),
);
report.add_error(
ValidationIssue::new(ValidationSeverity::Error, "earlier")
.with_segment("UNH")
.with_offset(1),
);
let rendered = report.render_deterministic();
let first = rendered.find("earlier").expect("missing first issue");
let second = rendered.find("later").expect("missing second issue");
assert!(first < second, "expected deterministic sort by offset");
}
#[test]
fn recovery_hint_exists_for_common_malformed_cases() {
let err = EdifactError::InvalidReleaseSequence { offset: 10 };
assert!(err.recovery_hint().is_some());
let err = EdifactError::InvalidCodeValue {
tag: "BGM".to_owned(),
element_index: 0,
value: "X".to_owned(),
code_list: "1001".to_owned(),
offset: 0,
suggestion: None,
};
assert!(err.recovery_hint().is_some());
}
#[test]
fn validation_report_can_filter_by_rule_id() {
let mut report = ValidationReport::default();
report.add_error(
ValidationIssue::new(ValidationSeverity::Error, "orders policy blocked")
.with_rule_id("ORDERS-P001"),
);
report.add_warning(
ValidationIssue::new(ValidationSeverity::Warning, "invoic policy warning")
.with_rule_id("INVOIC-P001"),
);
report.add_info(
ValidationIssue::new(ValidationSeverity::Info, "orders policy info")
.with_rule_id("ORDERS-P002"),
);
let only_orders_block = report.filter_by_rule_id("ORDERS-P001");
assert_eq!(only_orders_block.errors().len(), 1);
assert!(only_orders_block.warnings().is_empty());
assert!(only_orders_block.infos().is_empty());
let orders_family = report.filter_by_rule_prefix("ORDERS-");
assert_eq!(orders_family.total_issues(), 2);
assert!(orders_family.has_errors());
assert!(!orders_family.has_warnings());
let exact: Vec<_> = report.issues_for_rule_id("INVOIC-P001").collect();
assert_eq!(exact.len(), 1);
assert_eq!(exact[0].message, "invoic policy warning");
}
}