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
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::try_new`]
/// to get a `Result` instead of a panic.
#[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:?}")]
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 ─────────────────────────────────────────────────────────
pub use crate::report::ValidationReport;
#[cfg(test)]
mod tests {
use super::*;
#[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());
}
}