edifact_rs/error.rs
1use crate::model::Span;
2use thiserror::Error;
3
4/// Wrapper around [`std::io::Error`] that implements [`PartialEq`] by comparing [`std::io::ErrorKind`].
5///
6/// This allows `EdifactError` to derive `PartialEq` without requiring `std::io::Error: PartialEq`.
7#[derive(Debug)]
8pub struct IoError(pub(crate) std::io::Error);
9
10impl IoError {
11 /// Returns a reference to the underlying [`std::io::Error`].
12 pub fn inner(&self) -> &std::io::Error {
13 &self.0
14 }
15}
16
17impl PartialEq for IoError {
18 /// Equality is determined by [`std::io::ErrorKind`] only.
19 ///
20 /// Two `IoError` values with the same kind but different OS-level error codes
21 /// (or different messages) will compare as equal. This is a deliberate
22 /// limitation: `std::io::Error` is not `PartialEq`, so kind-based comparison
23 /// is the only practical option that lets `EdifactError` derive `PartialEq`.
24 fn eq(&self, other: &Self) -> bool {
25 self.0.kind() == other.0.kind()
26 }
27}
28
29impl std::fmt::Display for IoError {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 self.0.fmt(f)
32 }
33}
34
35impl std::error::Error for IoError {
36 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37 self.0.source()
38 }
39}
40
41impl From<std::io::Error> for IoError {
42 fn from(e: std::io::Error) -> Self {
43 Self(e)
44 }
45}
46
47/// All errors produced by `edifact-rs`.
48///
49/// # Positional data
50///
51/// Two kinds of position information appear in this enum, and the distinction is
52/// deliberate:
53///
54/// * **`offset: usize`** — a single byte position in the input stream. Used by
55/// the lexical variants (`UnexpectedEof`, `InvalidDelimiter`, `InvalidText`,
56/// `InvalidReleaseSequence`, `SegmentTooLong`, `UnexpectedDataToken`), where
57/// the fault is a *point* in the byte stream and no meaningful end position
58/// exists.
59/// * **`span: Span`** — a half-open byte range. Used by every variant produced
60/// while validating an already-parsed [`Segment`][crate::Segment], where the
61/// exact source range is known. A [`ValidationIssue`][crate::ValidationIssue]
62/// built from such an error carries the full range, so `miette` and LSP
63/// tooling can underline the offending segment rather than place a
64/// zero-width caret.
65///
66/// Use `span.start` when only the start position is needed.
67#[derive(Debug, Error, PartialEq)]
68#[non_exhaustive]
69pub enum EdifactError {
70 /// Unexpected end of input while parsing.
71 ///
72 /// This typically occurs when a segment terminator or expected delimiter
73 /// is not found before the end of the input stream.
74 #[error("unexpected end of input at byte offset {offset}")]
75 UnexpectedEof {
76 /// Byte offset where the parser exhausted input.
77 offset: usize,
78 },
79
80 /// Invalid byte encountered in a delimiter context.
81 ///
82 /// Delimiters must be precisely ASCII characters from the UNA service string advice.
83 /// Any other byte is invalid in delimiter position.
84 #[error("invalid delimiter byte 0x{byte:02X} at offset {offset}")]
85 InvalidDelimiter {
86 /// Unexpected delimiter byte.
87 byte: u8,
88 /// Byte offset where the delimiter was observed.
89 offset: usize,
90 },
91
92 /// Invalid UTF-8 sequence in parsed text.
93 ///
94 /// While EDIFACT operates on bytes, segments and elements are expected to contain
95 /// valid UTF-8 text. Non-UTF-8 sequences are rejected at parse time.
96 #[error("invalid EDIFACT text at byte offset {offset}")]
97 InvalidText {
98 /// Byte offset where invalid UTF-8 text starts.
99 offset: usize,
100 },
101
102 /// Invalid release-character escape sequence in parsed text.
103 ///
104 /// The release character (`?` by default) must be followed by one escaped byte.
105 /// A trailing release character without a following byte is malformed.
106 #[error("invalid release sequence at byte offset {offset}: dangling release character")]
107 InvalidReleaseSequence {
108 /// Byte offset of the dangling release character.
109 offset: usize,
110 },
111
112 /// UNZ interchange message count does not match the number of UNH/UNT pairs found.
113 ///
114 /// The `UNZ` segment declares the number of messages in the interchange,
115 /// but the actual number of `UNH`/`UNT` pairs observed differs.
116 #[error("interchange message count mismatch: UNZ declared {expected}, found {actual}")]
117 MessageCountMismatch {
118 /// Message count declared in the UNZ segment.
119 expected: u32,
120 /// Actual number of UNH/UNT pairs observed.
121 actual: u32,
122 },
123
124 /// UNT segment count does not match the actual number of segments in the message.
125 ///
126 /// The `UNT` segment declares the number of segments in the message (including `UNH`/`UNT`),
127 /// but the actual count differs.
128 #[error(
129 "segment count mismatch in message {message_ref}: UNT declared {expected}, found {actual}"
130 )]
131 SegmentCountMismatch {
132 /// Segment count declared in the UNT segment.
133 expected: u32,
134 /// Actual number of segments observed.
135 actual: u32,
136 /// Message reference from the UNH segment.
137 message_ref: String,
138 },
139
140 /// Invalid or malformed segment tag.
141 ///
142 /// Segment tags must be exactly 3 ASCII uppercase letters.
143 #[error("invalid segment tag {0:?}")]
144 InvalidSegmentTag(String),
145
146 /// Invalid UNA service string advice.
147 ///
148 /// If present, the UNA segment must be exactly 9 bytes: `"UNA"` followed by
149 /// 6 service characters. The five active characters (`element_sep`,
150 /// `component_sep`, `decimal_mark`, `release_char`, and `segment_term`) must
151 /// all be mutually distinct and printable, non-alphanumeric ASCII. The
152 /// **repetition separator** (UNA byte 7) is validated on the same terms
153 /// unless it is a space, the conventional "not used" sentinel.
154 #[error("invalid UNA service string advice")]
155 InvalidUna,
156
157 /// Missing required element in a segment.
158 ///
159 /// Certain segments require specific elements to be present. This error indicates
160 /// a mandatory element was not found.
161 #[error("missing required element {element_index} in segment {tag}")]
162 MissingRequiredElement {
163 /// Segment tag containing the missing element.
164 tag: String,
165 /// Zero-based required element index.
166 element_index: usize,
167 },
168
169 /// Missing required component in a composite element.
170 ///
171 /// The element is present, but the required component at the given index is absent or empty.
172 #[error(
173 "missing required component {component_index} in element {element_index} of segment {tag}"
174 )]
175 MissingRequiredComponent {
176 /// Segment tag containing the composite element.
177 tag: String,
178 /// Zero-based element index of the composite.
179 element_index: usize,
180 /// Zero-based component index that was absent.
181 component_index: usize,
182 },
183
184 /// Output serialization produced invalid UTF-8.
185 ///
186 /// This is an internal consistency error; the writer should never produce non-UTF-8 output.
187 /// If this occurs, it indicates a bug in the serialization logic.
188 #[error("serialized output contains invalid UTF-8")]
189 InvalidUtf8,
190
191 /// I/O error from reading or writing.
192 #[error(transparent)]
193 Io(#[from] IoError),
194
195 // ── validation variants (E010–E020) ────────────────────────────────────
196 /// Segment is not valid for the current message type.
197 ///
198 /// Structural validation found a segment that should not appear in this message.
199 #[error("segment {tag} is not valid for message type {message_type}")]
200 InvalidSegmentForMessage {
201 /// Segment tag that is not allowed for the message type.
202 tag: String,
203 /// Message type used for structural validation.
204 message_type: String,
205 /// Byte range of the offending segment tag.
206 span: Span,
207 },
208
209 /// Element count in segment exceeds or falls short of directory definition.
210 ///
211 /// Validation against directory metadata found an element count mismatch.
212 #[error("segment {tag} has {actual} elements, expected between {min} and {max}")]
213 InvalidElementCount {
214 /// Segment tag with wrong arity.
215 tag: String,
216 /// Minimum allowed element count.
217 min: usize,
218 /// Maximum allowed element count.
219 max: usize,
220 /// Actual element count found.
221 actual: usize,
222 /// Byte range of the offending segment.
223 span: Span,
224 },
225
226 /// Component count in a composite element is invalid.
227 ///
228 /// A composite data element does not have the expected number of components.
229 #[error("segment {tag} element {element_index} has {actual} components, expected {expected}")]
230 InvalidComponentCount {
231 /// Segment tag containing the composite.
232 tag: String,
233 /// Zero-based element index of the composite.
234 element_index: usize,
235 /// Expected component count.
236 expected: u8,
237 /// Actual component count found.
238 actual: u8,
239 /// Byte range of the offending composite element.
240 span: Span,
241 },
242
243 /// Code-list value is not valid.
244 ///
245 /// The value appears in a field that should contain a code from a specific code list,
246 /// but the value is not in that code list.
247 #[error(
248 "segment {tag} element {element_index}: '{value}' is not a valid code (code list {code_list})"
249 )]
250 InvalidCodeValue {
251 /// Segment tag containing the invalid value.
252 tag: String,
253 /// Zero-based element index containing the invalid code.
254 element_index: usize,
255 /// Invalid code value observed.
256 value: String,
257 /// Data element code list identifier.
258 code_list: String,
259 /// Byte range of the offending value.
260 span: Span,
261 /// Optional remediation suggestion from the code-list lookup function.
262 suggestion: Option<&'static str>,
263 },
264
265 /// A required segment is missing from the message.
266 ///
267 /// Structural validation found that a mandatory segment is absent.
268 #[error("required segment {tag} is missing from message (position {expected_position})")]
269 MissingSegment {
270 /// Missing segment tag.
271 tag: String,
272 /// Human-readable position hint.
273 expected_position: String,
274 },
275
276 /// Qualifier does not match expected value for segment.
277 ///
278 /// A qualified segment (e.g., NAD+MS) has a qualifier that does not match expected.
279 #[error("segment {tag} has qualifier '{actual}', expected '{expected}'")]
280 QualifierMismatch {
281 /// Segment tag whose qualifier mismatched.
282 tag: String,
283 /// Actual qualifier found.
284 actual: String,
285 /// Expected qualifier value.
286 expected: String,
287 /// Byte range of the offending segment.
288 span: Span,
289 },
290
291 /// Conditional requirement not met.
292 ///
293 /// A segment or element is conditionally required based on another element's value,
294 /// but the condition was not satisfied.
295 #[error("segment {tag} element {element_index}: conditional requirement not met ({condition})")]
296 ConditionalRequirementNotMet {
297 /// Segment tag that violated a conditional rule.
298 tag: String,
299 /// Zero-based element index governed by the condition.
300 element_index: usize,
301 /// Condition text describing the rule.
302 condition: String,
303 /// Byte range of the offending segment.
304 span: Span,
305 },
306
307 /// Validation failed and the full [`ValidationReport`] is preserved.
308 ///
309 /// Returned by validation helpers when errors are found. Provides programmatic
310 /// access to all issues, warnings, and infos.
311 ///
312 /// # Example
313 ///
314 /// ```rust,ignore
315 /// match my_fn() {
316 /// Err(EdifactError::ValidationErrors { report, .. }) => {
317 /// for issue in report.errors() {
318 /// eprintln!("{}", issue);
319 /// }
320 /// }
321 /// other => { /* ... */ }
322 /// }
323 /// ```
324 #[error("validation failed with {error_count} error(s)")]
325 ValidationErrors {
326 /// Number of error-severity issues in the report.
327 error_count: usize,
328 /// Full report with all errors, warnings, and infos.
329 report: Box<ValidationReport>,
330 },
331
332 /// Segment exceeded the configured maximum byte length.
333 ///
334 /// Returned by reader-based parsers when an unterminated segment accumulates more
335 /// bytes than the configured `max_segment_bytes` limit in [`ReaderConfig`]. This
336 /// prevents resource exhaustion on adversarially crafted or truncated input that
337 /// never emits a segment terminator.
338 ///
339 /// [`ReaderConfig`]: crate::ReaderConfig
340 #[error("segment starting at byte offset {offset} exceeded maximum length of {limit} bytes")]
341 SegmentTooLong {
342 /// Byte offset where the overlong segment started.
343 offset: usize,
344 /// Configured maximum segment byte length.
345 limit: usize,
346 },
347
348 /// No handler was registered in [`crate::MessageDispatch`] for this message type.
349 ///
350 /// Returned by [`crate::MessageDispatch::dispatch`] when the message-type
351 /// extracted from the `UNH` segment does not match any registered handler
352 /// and no fallback was configured.
353 #[error("no handler registered for message type {message_type}")]
354 UnexpectedMessageType {
355 /// The unhandled message type string from the `UNH` segment.
356 message_type: String,
357 },
358
359 /// An interchange or message contains more segments or messages than can be
360 /// represented in a `u32` counter (> 4 294 967 295).
361 ///
362 /// This is effectively unreachable in practice — no real-world EDIFACT
363 /// interchange has billions of segments — but the parser returns this error
364 /// rather than silently saturating or wrapping the counter.
365 #[error("interchange too large: count {count} exceeds u32::MAX")]
366 InterchangeTooLarge {
367 /// The count that could not be represented as `u32`.
368 count: u64,
369 },
370
371 /// An [`crate::EventEmitter`] received events in an invalid sequence.
372 ///
373 /// This indicates a programming error in the caller's serialization code:
374 /// for example, emitting an [`crate::EdifactEvent::Element`] without a prior
375 /// [`crate::EdifactEvent::StartSegment`], or emitting
376 /// [`crate::EdifactEvent::ComponentElement`] without a preceding
377 /// [`crate::EdifactEvent::Element`].
378 #[error("invalid event sequence: {message}")]
379 InvalidEventSequence {
380 /// Description of the protocol violation.
381 message: &'static str,
382 },
383
384 /// An [`crate::OwnedElementRef`] has `position = 0`, which is never valid.
385 ///
386 /// Element positions are one-based: position 1 refers to the first element
387 /// slot. Position 0 is reserved and invalid. Use [`crate::OwnedElementRef::try_new`]
388 /// to get a `Result` instead of a panic.
389 #[error("element definition contains invalid position 0; positions must be >= 1 (one-based)")]
390 InvalidElementPosition,
391
392 /// Two [`crate::ProfileRulePack`] values with incompatible release scopes were composed.
393 ///
394 /// When composing packs via [`crate::ProfileRulePack::extend_from`] or
395 /// [`crate::ProfileRulePack::merge_with_override`], both packs must either
396 /// share the same release scope or at most one may carry a scope.
397 #[error("incompatible release scopes: cannot compose {current:?} with {incoming:?}")]
398 IncompatibleReleaseScopes {
399 /// Release scope of the pack being composed into.
400 current: String,
401 /// Release scope of the pack being composed in.
402 incoming: String,
403 },
404
405 /// A field value failed semantic validation (e.g. wrong format, out-of-range).
406 ///
407 /// Distinct from [`InvalidCodeValue`][Self::InvalidCodeValue] which is for
408 /// code-list membership checks. Use this variant when a free-text or numeric
409 /// field contains a value that is structurally invalid for its purpose.
410 #[error("segment {tag} element {element_index}: invalid field value {value:?}")]
411 InvalidFieldValue {
412 /// Segment tag that contains the invalid field.
413 tag: String,
414 /// Zero-based element index of the invalid field.
415 element_index: usize,
416 /// The invalid value that was observed.
417 value: String,
418 },
419
420 /// A data or component element token appeared before the first segment tag.
421 ///
422 /// EDIFACT syntax requires that every data element follows a segment tag.
423 /// A data element token encountered before any tag (e.g. after a stray
424 /// separator at the start of the stream) is a protocol violation.
425 ///
426 /// Unlike stray segment terminators (which are tolerated as blank lines),
427 /// stray data tokens indicate encoding corruption or a partial write.
428 #[error("unexpected data token at byte offset {offset}: data element before segment tag")]
429 UnexpectedDataToken {
430 /// Byte offset of the stray token.
431 offset: usize,
432 },
433
434 /// The interchange syntax identifier (UNB DE 0001) is not a recognised ISO 9735-1 value.
435 ///
436 /// Valid syntax identifiers are: `UNOA`, `UNOB`, `UNOC`, `UNOD`, `UNOE`, `UNOF`, and
437 /// `KECA` (Korean EDI Centre A). Any other value indicates a non-standard generator
438 /// or a corrupted UNB header.
439 #[error(
440 "unrecognised syntax identifier '{0}': expected UNOA/UNOB/UNOC/UNOD/UNOE/UNOF (or KECA)"
441 )]
442 UnrecognisedSyntaxIdentifier(String),
443
444 /// A control reference was reused within the scope that requires it to be unique.
445 ///
446 /// ISO 9735-1 requires the message reference number (`UNH` DE 0062) to be
447 /// unique within an interchange, and the group reference number (`UNG`
448 /// DE 0048) to be unique within an interchange. Duplicates make a message
449 /// unaddressable: a receiver keying on the reference silently processes one
450 /// occurrence and drops the rest.
451 #[error("duplicate {tag} reference '{reference}' at bytes {span}")]
452 DuplicateReference {
453 /// Segment tag that carries the duplicated reference (`UNH` or `UNG`).
454 tag: String,
455 /// The reference value that appeared more than once.
456 reference: String,
457 /// Byte range of the duplicate occurrence.
458 span: Span,
459 },
460
461 /// A UN/EDIFACT data element code was not found in the segment definition.
462 ///
463 /// Produced by the code-addressed accessors
464 /// ([`Segment::value_by_code`][crate::Segment::value_by_code] and friends)
465 /// when the requested data element identifier does not appear anywhere in
466 /// the supplied [`SegmentLayout`][crate::SegmentLayout]. This is the error
467 /// that turns a mistyped or stale DE reference into a loud failure instead
468 /// of a silent off-by-one read of the wrong element.
469 #[error("segment {tag} has no data element {data_element} in its definition")]
470 UnknownDataElement {
471 /// Segment tag whose definition was searched.
472 tag: String,
473 /// The data element identifier that was not found.
474 data_element: String,
475 },
476
477 /// A UN/EDIFACT data element code appears more than once in a segment definition.
478 ///
479 /// Code-addressed access requires an unambiguous target. When a directory
480 /// genuinely repeats a code (e.g. the same DE used at two positions), address
481 /// it positionally with [`Segment::element_str`][crate::Segment::element_str]
482 /// or split the definition.
483 #[error("segment {tag} defines data element {data_element} at more than one position")]
484 AmbiguousDataElement {
485 /// Segment tag whose definition was searched.
486 tag: String,
487 /// The data element identifier that resolved to multiple positions.
488 data_element: String,
489 },
490
491 /// A configured [`ReaderConfig`][crate::ReaderConfig] resource limit was exceeded.
492 ///
493 /// Raised by the parsing iterators when the input carries more segments,
494 /// messages, or bytes than the caller allowed. The limit is reported rather
495 /// than silently applied: a budget that ends the iterator without an error is
496 /// indistinguishable from a clean end of input, so the caller would accept a
497 /// **truncated** interchange as complete.
498 ///
499 /// [`SegmentTooLong`][Self::SegmentTooLong] covers the per-segment size
500 /// guard; this variant covers the whole-input budgets.
501 #[error("input exceeded the configured {limit} limit of {max}")]
502 LimitExceeded {
503 /// Name of the limit that tripped: `"max_segments"`, `"max_messages"`,
504 /// or `"max_input_bytes"`.
505 limit: &'static str,
506 /// The configured ceiling.
507 max: u64,
508 },
509
510 /// A repeating data element was written under a service string advice that
511 /// declares no repetition separator.
512 ///
513 /// ISO 9735-4 §3.1 repetitions can only be expressed when `UNA` position 7
514 /// carries a real separator. With the space "not used" sentinel there is no
515 /// byte to write between occurrences, and joining them anyway would emit
516 /// output that reads back as a single occurrence — silent data corruption.
517 /// Construct the writer with [`Writer::with_una`][crate::Writer::with_una]
518 /// and a service string advice whose `repetition_sep` is set.
519 #[error(
520 "cannot write a repeating data element: the active service string advice declares no repetition separator"
521 )]
522 RepetitionSeparatorNotDeclared,
523
524 /// A [`SegmentLayout`][crate::SegmentLayout] was applied to a segment with a different tag.
525 ///
526 /// Passing the `NAD` definition to a `DTM` segment would resolve codes
527 /// against the wrong table, which is exactly the class of mistake that
528 /// code-addressed access exists to prevent — so it is rejected up front.
529 #[error("segment layout is for {expected}, but the segment is {actual}")]
530 SegmentLayoutMismatch {
531 /// Tag the layout describes.
532 expected: String,
533 /// Tag of the segment the layout was applied to.
534 actual: String,
535 },
536}
537
538impl From<std::io::Error> for EdifactError {
539 fn from(e: std::io::Error) -> Self {
540 Self::Io(IoError(e))
541 }
542}
543
544impl EdifactError {
545 /// Stable diagnostic code for this error variant.
546 #[must_use]
547 pub const fn stable_code(&self) -> &'static str {
548 match self {
549 Self::UnexpectedEof { .. } => "E001",
550 Self::InvalidDelimiter { .. } => "E002",
551 Self::InvalidText { .. } => "E003",
552 Self::MessageCountMismatch { .. } => "E004",
553 Self::SegmentCountMismatch { .. } => "E005",
554 Self::InvalidSegmentTag(_) => "E006",
555 Self::InvalidUna => "E007",
556 Self::MissingRequiredElement { .. } => "E008",
557 Self::InvalidUtf8 => "E009",
558 Self::Io(_) => "E010",
559 Self::InvalidSegmentForMessage { .. } => "E011",
560 Self::InvalidElementCount { .. } => "E012",
561 Self::InvalidComponentCount { .. } => "E013",
562 Self::InvalidCodeValue { .. } => "E014",
563 Self::MissingSegment { .. } => "E015",
564 Self::QualifierMismatch { .. } => "E016",
565 Self::ConditionalRequirementNotMet { .. } => "E017",
566 // E018 is permanently retired (was ValidationFailed, removed in 0.8.0)
567 Self::InvalidReleaseSequence { .. } => "E019",
568 Self::SegmentTooLong { .. } => "E020",
569 Self::MissingRequiredComponent { .. } => "E021",
570 Self::UnexpectedMessageType { .. } => "E022",
571 Self::InterchangeTooLarge { .. } => "E023",
572 Self::InvalidEventSequence { .. } => "E024",
573 Self::InvalidElementPosition => "E025",
574 Self::IncompatibleReleaseScopes { .. } => "E026",
575 Self::InvalidFieldValue { .. } => "E027",
576 Self::UnexpectedDataToken { .. } => "E028",
577 // E029 is permanently retired (was FunctionalGroupNotSupported, removed when
578 // full UNG/UNE support was added — functional groups are now parsed natively)
579 Self::ValidationErrors { .. } => "E030",
580 Self::UnrecognisedSyntaxIdentifier(_) => "E031",
581 Self::DuplicateReference { .. } => "E032",
582 Self::UnknownDataElement { .. } => "E033",
583 Self::AmbiguousDataElement { .. } => "E034",
584 Self::SegmentLayoutMismatch { .. } => "E035",
585 Self::LimitExceeded { .. } => "E036",
586 Self::RepetitionSeparatorNotDeclared => "E037",
587 }
588 }
589
590 /// Stable recovery hint for common malformed input and validation cases.
591 #[must_use]
592 pub fn recovery_hint(&self) -> Option<&'static str> {
593 match self {
594 Self::UnexpectedEof { .. } => {
595 Some("Ensure every segment ends with the configured segment terminator")
596 }
597 Self::InvalidDelimiter { .. } => {
598 Some("Check UNA service string advice and delimiter bytes in the payload")
599 }
600 Self::InvalidText { .. } => {
601 Some("Input must be valid UTF-8 text for segment and element values")
602 }
603 Self::InvalidReleaseSequence { .. } => {
604 Some("Release character must escape one following byte; trailing '?' is invalid")
605 }
606 Self::InvalidSegmentTag(_) => Some("Segment tags must be 3 ASCII uppercase letters"),
607 Self::InvalidUna => Some(
608 "UNA must be exactly 9 bytes: 'UNA' followed by 6 distinct, non-whitespace service characters",
609 ),
610 Self::MissingRequiredElement { .. } => {
611 Some("Provide all mandatory elements for the segment per directory rules")
612 }
613 Self::MissingRequiredComponent { .. } => Some(
614 "Provide all mandatory components for the composite element per directory rules",
615 ),
616 Self::InvalidSegmentForMessage { .. } => {
617 Some("Remove unsupported segment or switch to the correct message type")
618 }
619 Self::InvalidElementCount { .. } => {
620 Some("Adjust the segment element count to the allowed min/max range")
621 }
622 Self::InvalidComponentCount { .. } => {
623 Some("Fix composite element arity to match the expected component count")
624 }
625 Self::InvalidCodeValue { .. } => {
626 Some("Use a value from the referenced code list for this element")
627 }
628 Self::MissingSegment { .. } => {
629 Some("Insert the required segment at the expected position")
630 }
631 Self::QualifierMismatch { .. } => {
632 Some("Set the segment qualifier to the expected value")
633 }
634 Self::ConditionalRequirementNotMet { .. } => {
635 Some("When the condition is met, include the conditionally required element")
636 }
637 Self::SegmentTooLong { limit, .. } => {
638 let _ = limit; // used in the error message; hint is generic
639 Some("Increase max_segment_bytes in ReaderConfig or reject the input as malformed")
640 }
641 Self::InvalidEventSequence { .. } => {
642 Some("Emit StartSegment before Element, and Element before ComponentElement")
643 }
644 Self::InvalidElementPosition => Some(
645 "Set element position to a value >= 1; positions are one-based (1 = first element slot)",
646 ),
647 Self::IncompatibleReleaseScopes { .. } => Some(
648 "Only compose ProfileRulePack values that share the same release scope, or where at most one has a release scope set",
649 ),
650 Self::InvalidFieldValue { .. } => Some(
651 "Correct the field value to match the expected format or range for this element",
652 ),
653 Self::UnexpectedDataToken { .. } => Some(
654 "A data element appeared before any segment tag; check for partial writes or encoding corruption",
655 ),
656 Self::DuplicateReference { .. } => Some(
657 "Assign a unique control reference to every UNH (DE 0062) and UNG (DE 0048) within an interchange",
658 ),
659 Self::UnknownDataElement { .. } => Some(
660 "Check the data element identifier against the segment definition; the directory is the source of truth",
661 ),
662 Self::AmbiguousDataElement { .. } => Some(
663 "The code appears at more than one position; address the element positionally instead",
664 ),
665 Self::SegmentLayoutMismatch { .. } => {
666 Some("Resolve codes against the segment definition whose tag matches the segment")
667 }
668 Self::LimitExceeded { .. } => {
669 Some("Raise the corresponding ReaderConfig limit, or reject the input as oversized")
670 }
671 Self::RepetitionSeparatorNotDeclared => Some(
672 "Build the writer with Writer::with_una and a ServiceStringAdvice whose repetition_sep is set",
673 ),
674 Self::ValidationErrors { .. }
675 | Self::MessageCountMismatch { .. }
676 | Self::SegmentCountMismatch { .. }
677 | Self::UnexpectedMessageType { .. }
678 | Self::InterchangeTooLarge { .. }
679 | Self::UnrecognisedSyntaxIdentifier(_)
680 | Self::InvalidUtf8
681 | Self::Io(_) => None,
682 }
683 }
684}
685
686#[cfg(feature = "diagnostics")]
687#[cfg_attr(docsrs, doc(cfg(feature = "diagnostics")))]
688impl miette::Diagnostic for EdifactError {
689 fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
690 Some(Box::new(self.stable_code()))
691 }
692
693 fn severity(&self) -> Option<miette::Severity> {
694 match self {
695 Self::InvalidCodeValue { .. }
696 | Self::InvalidComponentCount { .. }
697 | Self::QualifierMismatch { .. } => Some(miette::Severity::Warning),
698 _ => Some(miette::Severity::Error),
699 }
700 }
701
702 fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
703 match self {
704 // Static text — no allocation needed.
705 Self::InvalidUna => Some(Box::new(
706 "UNA segment must be exactly 9 bytes: 'UNA' + 6 service characters. See EDIFACT spec",
707 )),
708 Self::InvalidUtf8 => Some(Box::new(
709 "Internal error: serialized output contains invalid UTF-8. Please report this as a bug",
710 )),
711 // Dynamic help text.
712 Self::UnexpectedEof { offset } => Some(Box::new(format!(
713 "Check that all segments are terminated with the segment terminator (usually '). \
714 Reached end at offset {offset}",
715 ))),
716 Self::InvalidDelimiter { byte, offset } => Some(Box::new(format!(
717 "The byte 0x{byte:02X} at offset {offset} is not a valid delimiter. \
718 Check UNA configuration",
719 ))),
720 Self::InvalidText { offset } => Some(Box::new(format!(
721 "The byte sequence at offset {offset} contains invalid UTF-8. \
722 Ensure input is valid UTF-8",
723 ))),
724 Self::InvalidReleaseSequence { offset } => Some(Box::new(format!(
725 "Release character at offset {offset} is dangling. \
726 Ensure '?' is followed by an escaped byte",
727 ))),
728 Self::MessageCountMismatch { expected, actual } => Some(Box::new(format!(
729 "UNZ declares {expected} message(s) but {actual} UNH/UNT pair(s) were found. \
730 Check the UNZ message count",
731 ))),
732 Self::SegmentCountMismatch {
733 expected,
734 actual,
735 message_ref,
736 } => Some(Box::new(format!(
737 "UNT for message {message_ref} declares {expected} segment(s) but {actual} were found. \
738 Check the UNT segment count",
739 ))),
740 Self::InvalidSegmentTag(tag) => Some(Box::new(format!(
741 "Segment tag '{tag}' must be exactly 3 ASCII uppercase letters",
742 ))),
743 Self::MissingRequiredElement { tag, element_index } => Some(Box::new(format!(
744 "Segment {tag} requires element at index {element_index}",
745 ))),
746 Self::MissingRequiredComponent {
747 tag,
748 element_index,
749 component_index,
750 } => Some(Box::new(format!(
751 "Segment {tag} element {element_index} requires component at index {component_index}",
752 ))),
753 Self::Io(e) => Some(Box::new(format!("I/O error: {e}"))),
754 Self::InvalidSegmentForMessage {
755 tag, message_type, ..
756 } => Some(Box::new(format!(
757 "Segment {tag} should not appear in a {message_type} message. \
758 Check the directory definition",
759 ))),
760 Self::InvalidElementCount {
761 tag,
762 min,
763 max,
764 actual,
765 ..
766 } => Some(Box::new(format!(
767 "Segment {tag} should have between {min} and {max} elements, but has {actual}. \
768 Check segment structure",
769 ))),
770 Self::InvalidComponentCount {
771 tag,
772 element_index,
773 expected,
774 actual,
775 ..
776 } => Some(Box::new(format!(
777 "In segment {tag}, element {element_index} should have {expected} components \
778 but has {actual}. Check element structure",
779 ))),
780 Self::InvalidCodeValue {
781 tag,
782 element_index,
783 value,
784 code_list,
785 ..
786 } => Some(Box::new(format!(
787 "Value '{value}' in segment {tag} element {element_index} is not in the \
788 {code_list} code list. Check the directory for valid codes",
789 ))),
790 Self::MissingSegment {
791 tag,
792 expected_position,
793 } => Some(Box::new(format!(
794 "Segment {tag} is required at position {expected_position} but is missing. \
795 Add this segment to the message",
796 ))),
797 Self::QualifierMismatch {
798 tag,
799 actual,
800 expected,
801 ..
802 } => Some(Box::new(format!(
803 "Segment {tag} has qualifier '{actual}' but expected '{expected}'. \
804 Check the segment's first component",
805 ))),
806 Self::ConditionalRequirementNotMet {
807 tag,
808 element_index,
809 condition,
810 ..
811 } => Some(Box::new(format!(
812 "In segment {tag}, element {element_index} is conditionally required when: \
813 {condition}. Check if the condition is met",
814 ))),
815 Self::SegmentTooLong { offset, limit } => Some(Box::new(format!(
816 "Segment starting at byte offset {offset} exceeds the {limit}-byte limit. \
817 Use ReaderConfig::max_segment_bytes to adjust the limit if needed, \
818 or verify the input for a missing segment terminator",
819 ))),
820 Self::UnexpectedMessageType { message_type } => Some(Box::new(format!(
821 "No handler was registered for message type '{message_type}'. \
822 Register a handler with MessageDispatch::on(\"{message_type}\", ...)",
823 ))),
824 Self::InterchangeTooLarge { count } => Some(Box::new(format!(
825 "Interchange contains {count} items which exceeds the u32::MAX limit. \
826 This is an extremely unusual input; verify the message is not corrupted.",
827 ))),
828 Self::InvalidEventSequence { message } => Some(Box::new(format!(
829 "Event sequence violation: {message}. \
830 Check that StartSegment is emitted before Element, and Element before ComponentElement.",
831 ))),
832 Self::InvalidElementPosition => Some(Box::new(
833 "Element positions must be >= 1 (one-based). \
834 Ensure no OwnedElementRef is constructed with position == 0",
835 )),
836 Self::IncompatibleReleaseScopes { current, incoming } => Some(Box::new(format!(
837 "Release scope {current:?} and {incoming:?} are incompatible. \
838 Only compose ProfileRulePack values that share the same release scope, \
839 or where at most one carries a release scope",
840 ))),
841 Self::InvalidFieldValue {
842 tag,
843 element_index,
844 value,
845 } => Some(Box::new(format!(
846 "Segment {tag} element {element_index} has invalid value '{value}'. \
847 Check the expected format or range for this field",
848 ))),
849 Self::UnexpectedDataToken { offset } => Some(Box::new(format!(
850 "Data element at offset {offset} appeared before any segment tag. \
851 Check for partial writes or encoding corruption",
852 ))),
853 Self::ValidationErrors { error_count, .. } => Some(Box::new(format!(
854 "Validation found {error_count} error(s). Inspect the ValidationReport for details",
855 ))),
856 Self::UnrecognisedSyntaxIdentifier(id) => Some(Box::new(format!(
857 "Syntax identifier '{id}' is not defined in ISO 9735-1. \
858 Valid values are UNOA, UNOB, UNOC, UNOD, UNOE, UNOF (or KECA for KEC-A profile)",
859 ))),
860 Self::DuplicateReference { tag, reference, .. } => Some(Box::new(format!(
861 "Reference '{reference}' is used by more than one {tag} in this interchange; \
862 each must be unique so receivers can address messages unambiguously",
863 ))),
864 Self::UnknownDataElement { tag, data_element } => Some(Box::new(format!(
865 "Segment {tag} does not define data element {data_element}. \
866 Check the identifier against the directory definition for {tag}",
867 ))),
868 Self::AmbiguousDataElement { tag, data_element } => Some(Box::new(format!(
869 "Segment {tag} defines data element {data_element} at more than one position, \
870 so code-addressed access cannot pick one; use a positional accessor",
871 ))),
872 Self::SegmentLayoutMismatch { expected, actual } => Some(Box::new(format!(
873 "The supplied layout describes segment {expected} but was applied to {actual}. \
874 Look up the definition by the segment's own tag",
875 ))),
876 Self::RepetitionSeparatorNotDeclared => Some(Box::new(
877 "UNA position 7 holds the space \"not used\" sentinel, so repeating data \
878 elements cannot be expressed. Use Writer::with_una with a repetition_sep",
879 )),
880 Self::LimitExceeded { limit, max } => Some(Box::new(format!(
881 "The input exceeds the configured {limit} limit of {max}. \
882 Raise it via ReaderConfig if the input is legitimate, or reject the input",
883 ))),
884 }
885 }
886}
887
888// ── validation report ─────────────────────────────────────────────────────────
889
890pub use crate::report::ValidationReport;
891
892#[cfg(test)]
893mod tests {
894 use super::*;
895
896 #[test]
897 fn recovery_hint_exists_for_common_malformed_cases() {
898 let err = EdifactError::InvalidReleaseSequence { offset: 10 };
899 assert!(err.recovery_hint().is_some());
900
901 let err = EdifactError::InvalidCodeValue {
902 tag: "BGM".to_owned(),
903 element_index: 0,
904 value: "X".to_owned(),
905 code_list: "1001".to_owned(),
906 span: Span::new(0, 9),
907 suggestion: None,
908 };
909 assert!(err.recovery_hint().is_some());
910 }
911}