Skip to main content

edifact_rs/
de.rs

1//! Custom deserialization trait for EDIFACT.
2//!
3//! [`EdifactDeserialize`] maps a slice of parsed [`Segment`]s to a Rust value.
4//! [`EdifactSegmentTag`] is a companion trait that carries the segment tag and
5//! optional qualifier at the type level, enabling the blanket
6//! `impl EdifactDeserialize for Vec<T>`.
7
8use crate::{EdifactError, Segment};
9use std::borrow::Cow;
10use std::io::Read;
11use std::str::FromStr;
12
13// ── traits ────────────────────────────────────────────────────────────────────
14
15/// Types that can be deserialized from a slice of EDIFACT segments.
16///
17/// Implement manually or derive with `#[derive(EdifactDeserialize)]` from the
18/// `edifact-rs-derive` crate.
19pub trait EdifactDeserialize: Sized {
20    /// Deserialize `Self` from the provided segment slice.
21    ///
22    /// The slice may contain any number of segments; implementations extract
23    /// only the ones they care about and ignore the rest.
24    fn edifact_deserialize(segments: &[Segment<'_>]) -> Result<Self, EdifactError>;
25
26    /// Deserialize `Self` from a slice of owned EDIFACT segments.
27    ///
28    /// # Default implementation
29    ///
30    /// Converts each [`crate::OwnedSegment`] to its borrowed form via
31    /// [`crate::OwnedSegment::as_borrowed`] and delegates to
32    /// [`edifact_deserialize`][Self::edifact_deserialize].  This incurs one
33    /// `Vec<Segment<'_>>` allocation per call.
34    ///
35    /// # Warning: allocation overhead
36    ///
37    /// This default implementation incurs one [`Vec<Segment<'_>>`](Vec)
38    /// allocation per call.  Types generated by `#[derive(EdifactDeserialize)]`
39    /// automatically override this method to work directly on the owned data
40    /// without the intermediate allocation.  Manual implementations should also
41    /// override when used in the high-throughput reader-streaming path
42    /// ([`deserialize_first_from_reader`], [`deserialize_all_from_reader`],
43    /// [`deserialize_messages_from_reader`]) to avoid the per-message allocation.
44    fn edifact_deserialize_owned(segments: &[crate::OwnedSegment]) -> Result<Self, EdifactError> {
45        let borrowed: Vec<Segment<'_>> = segments.iter().map(|s| s.as_borrowed()).collect();
46        Self::edifact_deserialize(&borrowed)
47    }
48}
49
50/// Types that can be deserialized from a composite EDIFACT element.
51///
52/// Implement this for custom composite structs used with
53/// `#[edifact(composite)]` in derive macros.
54pub trait EdifactCompositeDeserialize: Sized {
55    /// Deserialize `Self` from a composite element.
56    fn edifact_deserialize_composite(composite: CompositeElement<'_>)
57    -> Result<Self, EdifactError>;
58}
59
60impl EdifactCompositeDeserialize for Vec<String> {
61    fn edifact_deserialize_composite(
62        composite: CompositeElement<'_>,
63    ) -> Result<Self, EdifactError> {
64        Ok(composite.iter().map(str::to_owned).collect())
65    }
66}
67
68/// Companion trait that declares a type's segment tag (and optional qualifier).
69///
70/// Required for the `Vec<T>` blanket impl and for finding the right segment in
71/// a message-level struct deserialization.
72pub trait EdifactSegmentTag {
73    /// The 3-character EDIFACT segment tag (e.g. `"BGM"`, `"NAD"`).
74    const SEGMENT_TAG: &'static str;
75
76    /// Optional qualifier pattern to further constrain segment matching.
77    ///
78    /// Examples:
79    /// - `Some("MS")` for exact qualifier matching.
80    /// - `Some("M*")` for wildcard prefix matching (matches `"MS"`, `"MR"`, etc.).
81    const QUALIFIER_PATTERN: Option<&'static str> = None;
82
83    /// Return `true` if `seg`'s qualifier matches this type's qualifier pattern.
84    fn matches_qualifier(seg: &Segment<'_>) -> bool {
85        match Self::QUALIFIER_PATTERN {
86            Some(pattern) => seg
87                .element_str(0)
88                .is_some_and(|q| qualifier_matches_pattern(q, pattern)),
89            None => true,
90        }
91    }
92
93    /// Return `true` if `seg` is the segment this type maps to.
94    ///
95    /// Default: `seg.tag == Self::SEGMENT_TAG`.  Override to also match on a
96    /// qualifier (e.g. `NAD+BY` — element 0 = `"BY"`).
97    fn matches_segment(seg: &Segment<'_>) -> bool {
98        seg.tag == Self::SEGMENT_TAG && Self::matches_qualifier(seg)
99    }
100
101    /// Like [`matches_segment`][Self::matches_segment] but works directly on an
102    /// [`crate::OwnedSegment`] without incurring the `Vec` allocation of
103    /// [`crate::OwnedSegment::as_borrowed`].
104    fn matches_owned_segment(seg: &crate::OwnedSegment) -> bool {
105        if seg.tag != Self::SEGMENT_TAG {
106            return false;
107        }
108        match Self::QUALIFIER_PATTERN {
109            None => true,
110            Some(pattern) => {
111                let q = seg
112                    .elements
113                    .first()
114                    .and_then(|e| e.components.first())
115                    .map(|(c, _)| c.as_str())
116                    .unwrap_or("");
117                qualifier_matches_pattern(q, pattern)
118            }
119        }
120    }
121}
122
123// ── blanket impl for Vec<T> ───────────────────────────────────────────────────
124
125/// Deserializes each segment matching `T::matches_segment` as an independent
126/// single-segment slice, collecting the results.
127impl<T> EdifactDeserialize for Vec<T>
128where
129    T: EdifactDeserialize + EdifactSegmentTag,
130{
131    fn edifact_deserialize(segments: &[Segment<'_>]) -> Result<Self, EdifactError> {
132        segments
133            .iter()
134            .filter(|s| T::matches_segment(s))
135            .map(|seg| T::edifact_deserialize(std::slice::from_ref(seg)))
136            .collect()
137    }
138
139    fn edifact_deserialize_owned(segments: &[crate::OwnedSegment]) -> Result<Self, EdifactError> {
140        segments
141            .iter()
142            .filter(|s| T::matches_owned_segment(s))
143            .map(|seg| T::edifact_deserialize_owned(std::slice::from_ref(seg)))
144            .collect()
145    }
146}
147
148// ── public API ────────────────────────────────────────────────────────────────
149
150/// Deserialize a value of type `T` from EDIFACT bytes.
151///
152/// Unlike [`crate::from_bytes`], which parses bytes into raw [`Segment`]s, this
153/// function fully deserializes the payload into a typed Rust value via [`EdifactDeserialize`].
154///
155/// # Memory
156///
157/// This function buffers **all** parsed segments into a `Vec<Segment<'_>>` before
158/// calling `T::edifact_deserialize`.  For large interchanges — or when only the first
159/// matching segment is needed — prefer [`deserialize_first_streaming`] or
160/// [`deserialize_all_streaming`] to avoid holding the entire input in memory.
161/// For reader-based I/O with bounded memory use, see [`deserialize_first_from_reader`]
162/// and [`deserialize_all_from_reader`].
163pub fn deserialize<T: EdifactDeserialize>(input: &[u8]) -> Result<T, EdifactError> {
164    let segments: Vec<Segment<'_>> = crate::from_bytes(input).collect::<Result<_, _>>()?;
165    T::edifact_deserialize(&segments)
166}
167
168/// Stream-parse EDIFACT bytes and deserialize the first matching segment as `T`.
169///
170/// This avoids allocating a full `Vec<Segment>` and is intended for low-memory
171/// extraction of segment-scoped types.
172pub fn deserialize_first_streaming<T>(input: &[u8]) -> Result<T, EdifactError>
173where
174    T: EdifactDeserialize + EdifactSegmentTag,
175{
176    for segment in crate::from_bytes(input) {
177        let segment = segment?;
178        if T::matches_segment(&segment) {
179            return T::edifact_deserialize(std::slice::from_ref(&segment));
180        }
181    }
182
183    Err(EdifactError::MissingSegment {
184        tag: T::SEGMENT_TAG.to_owned(),
185        expected_position: "any position in input".to_owned(),
186    })
187}
188
189/// Stream-parse EDIFACT bytes and deserialize all matching segments as `Vec<T>`.
190///
191/// This avoids buffering non-matching segments in memory.
192pub fn deserialize_all_streaming<T>(input: &[u8]) -> Result<Vec<T>, EdifactError>
193where
194    T: EdifactDeserialize + EdifactSegmentTag,
195{
196    let mut out = Vec::new();
197    for segment in crate::from_bytes(input) {
198        let segment = segment?;
199        if T::matches_segment(&segment) {
200            out.push(T::edifact_deserialize(std::slice::from_ref(&segment))?);
201        }
202    }
203    Ok(out)
204}
205
206/// Stream-parse EDIFACT from a reader and deserialize the first matching segment as `T`.
207///
208/// This is the low-memory typed path for large payloads read from I/O streams.
209pub fn deserialize_first_from_reader<T, R>(reader: R) -> Result<T, EdifactError>
210where
211    T: EdifactDeserialize + EdifactSegmentTag,
212    R: Read,
213{
214    for segment in crate::from_reader_iter(reader) {
215        let segment = segment?;
216        // O(1) tag + qualifier check before paying for as_borrowed().
217        if !T::matches_owned_segment(&segment) {
218            continue;
219        }
220        return T::edifact_deserialize_owned(std::slice::from_ref(&segment));
221    }
222
223    Err(EdifactError::MissingSegment {
224        tag: T::SEGMENT_TAG.to_owned(),
225        expected_position: "any position in input".to_owned(),
226    })
227}
228
229/// Stream-parse EDIFACT from a reader and deserialize all matching segments as `Vec<T>`.
230pub fn deserialize_all_from_reader<T, R>(reader: R) -> Result<Vec<T>, EdifactError>
231where
232    T: EdifactDeserialize + EdifactSegmentTag,
233    R: Read,
234{
235    let mut out = Vec::new();
236    for segment in crate::from_reader_iter(reader) {
237        let segment = segment?;
238        // O(1) tag + qualifier check before paying for as_borrowed().
239        if !T::matches_owned_segment(&segment) {
240            continue;
241        }
242        out.push(T::edifact_deserialize_owned(std::slice::from_ref(
243            &segment,
244        ))?);
245    }
246    Ok(out)
247}
248
249/// Deserialize a value of type `T` from an EDIFACT string.
250pub fn deserialize_str<T: EdifactDeserialize>(input: &str) -> Result<T, EdifactError> {
251    deserialize(input.as_bytes())
252}
253
254// ── helper functions ──────────────────────────────────────────────────────────
255
256/// Find the first segment with the given tag.
257pub fn find_segment<'s, 'd>(segments: &'s [Segment<'d>], tag: &str) -> Option<&'s Segment<'d>> {
258    segments.iter().find(|s| s.tag == tag)
259}
260
261/// Iterate over all segments with the given tag without allocating a `Vec`.
262pub fn find_segments_iter<'s, 'd: 's>(
263    segments: &'s [Segment<'d>],
264    tag: &'s str,
265) -> impl Iterator<Item = &'s Segment<'d>> {
266    segments.iter().filter(move |s| s.tag == tag)
267}
268
269/// Find the first segment matching `tag` whose element 0 equals `qualifier`.
270pub fn find_qualified_segment<'s, 'd>(
271    segments: &'s [Segment<'d>],
272    tag: &str,
273    qualifier: &str,
274) -> Option<&'s Segment<'d>> {
275    segments
276        .iter()
277        .find(|s| s.tag == tag && s.element_str(0).unwrap_or("") == qualifier)
278}
279
280/// Find the first segment by type-level qualifier pattern.
281pub fn find_segment_typed<'s, 'd, T>(segments: &'s [Segment<'d>]) -> Option<&'s Segment<'d>>
282where
283    T: EdifactSegmentTag,
284{
285    segments.iter().find(|s| T::matches_segment(s))
286}
287
288/// Iterate over all segments by type-level qualifier pattern.
289pub fn find_segments_typed<'s, 'd: 's, T>(
290    segments: &'s [Segment<'d>],
291) -> impl Iterator<Item = &'s Segment<'d>>
292where
293    T: EdifactSegmentTag,
294{
295    segments.iter().filter(|s| T::matches_segment(s))
296}
297
298/// Collect contiguous groups of segments that match `T`.
299///
300/// Each group is a borrowed slice of the original `segments` array.
301/// Use [`contiguous_groups_iter`] to avoid the outer `Vec` allocation.
302pub fn contiguous_groups_by_qualifier<'s, 'd, T>(
303    segments: &'s [Segment<'d>],
304) -> Vec<&'s [Segment<'d>]>
305where
306    T: EdifactSegmentTag,
307{
308    let mut groups = Vec::new();
309    let mut idx = 0;
310    while idx < segments.len() {
311        if T::matches_segment(&segments[idx]) {
312            let start = idx;
313            idx += 1;
314            while idx < segments.len() && T::matches_segment(&segments[idx]) {
315                idx += 1;
316            }
317            groups.push(&segments[start..idx]);
318        } else {
319            idx += 1;
320        }
321    }
322    groups
323}
324
325/// Iterate lazily over contiguous groups of segments that match `T`.
326///
327/// Each yielded item is a borrowed slice `&[Segment<'_>]` that forms one
328/// contiguous run of `T`-matching segments.  No outer `Vec` is allocated —
329/// the caller can break early or collect only as many groups as needed.
330///
331/// This function uses separate lifetimes for the slice reference (`'s`) and
332/// the segment data (`'d`), matching the signature of
333/// [`contiguous_groups_by_qualifier`].
334///
335/// # Example
336/// ```rust,ignore
337/// for group in contiguous_groups_iter::<UnaSegment>(&segments) {
338///     process_group(group);
339/// }
340/// ```
341pub fn contiguous_groups_iter<'s, 'd, T>(
342    segments: &'s [Segment<'d>],
343) -> impl Iterator<Item = &'s [Segment<'d>]> + 's
344where
345    T: EdifactSegmentTag,
346{
347    let mut idx = 0;
348    let len = segments.len();
349    std::iter::from_fn(move || {
350        // Skip non-matching segments
351        while idx < len && !T::matches_segment(&segments[idx]) {
352            idx += 1;
353        }
354        if idx >= len {
355            return None;
356        }
357        let start = idx;
358        idx += 1;
359        while idx < len && T::matches_segment(&segments[idx]) {
360            idx += 1;
361        }
362        Some(&segments[start..idx])
363    })
364}
365
366/// Return `true` if all segments matching `T` are in one contiguous block.
367pub fn groups_are_contiguous_by_qualifier<T>(segments: &[Segment<'_>]) -> bool
368where
369    T: EdifactSegmentTag,
370{
371    let mut seen_match = false;
372    let mut seen_gap_after_match = false;
373
374    for seg in segments {
375        if T::matches_segment(seg) {
376            if seen_gap_after_match {
377                return false;
378            }
379            seen_match = true;
380        } else if seen_match {
381            seen_gap_after_match = true;
382        }
383    }
384
385    true
386}
387
388/// Match a qualifier value against an exact or wildcard pattern.
389///
390/// Rules:
391/// - If `pattern` contains `*`, it is treated as a glob wildcard (e.g. `"M*"` matches `"MS"`, `"MR"`).
392/// - If no wildcard is present, exact match is required.
393///
394/// Prefix matching without an explicit `*` was deliberately removed: `"M"` matches only `"M"`,
395/// not `"MS"` or `"MR"`.  Use `"M*"` for prefix semantics.
396///
397/// Patterns with more than 3 wildcard segments (i.e. 4 or more `*` characters) are rejected
398/// immediately with `false` to guard against pathological O(n·m) matching.
399pub fn qualifier_matches_pattern(value: &str, pattern: &str) -> bool {
400    if pattern.is_empty() {
401        return value.is_empty();
402    }
403
404    if !pattern.contains('*') {
405        return value == pattern;
406    }
407
408    // Fast path: single wildcard (dominant case — e.g. "M*" or "*:MS")
409    if let Some((prefix, suffix)) = pattern.split_once('*') {
410        // Only one wildcard — prefix and suffix cannot overlap in a second split.
411        if !pattern[prefix.len() + 1..].contains('*') {
412            return value.len() >= prefix.len() + suffix.len()
413                && value.starts_with(prefix)
414                && value.ends_with(suffix)
415                && {
416                    // Ensure prefix and suffix don't overlap.
417                    let mid_start = prefix.len();
418                    let mid_end = value.len().saturating_sub(suffix.len());
419                    mid_start <= mid_end
420                };
421        }
422    }
423
424    // General multi-wildcard path.
425    let parts: smallvec::SmallVec<[&str; 4]> = pattern.split('*').collect();
426
427    // Guard against pathological O(n·m) matching on adversarial patterns.
428    // EDIFACT qualifier patterns use at most 1–2 wildcards; 4 is a generous
429    // ceiling. Anything beyond is almost certainly a programming error or
430    // adversarial input — reject immediately.
431    if parts.len() > 4 {
432        return false;
433    }
434
435    let prefix = parts[0];
436    let suffix = parts[parts.len() - 1];
437
438    if !value.starts_with(prefix) || !value.ends_with(suffix) {
439        return false;
440    }
441
442    let mid_start = prefix.len();
443    let mid_end = value.len().saturating_sub(suffix.len());
444
445    if mid_start > mid_end {
446        return parts[1..parts.len() - 1].iter().all(|p| p.is_empty());
447    }
448
449    let mut remaining = &value[mid_start..mid_end];
450
451    for part in &parts[1..parts.len() - 1] {
452        if part.is_empty() {
453            continue;
454        }
455        match remaining.find(part) {
456            Some(idx) => remaining = &remaining[idx + part.len()..],
457            None => return false,
458        }
459    }
460
461    true
462}
463
464/// Extract the string value of element `idx` from `seg`, or `""` if absent.
465#[inline]
466pub fn element_str<'s>(seg: &'s Segment<'_>, idx: usize) -> &'s str {
467    seg.element_str(idx).unwrap_or("")
468}
469
470// ── segment accessor helpers ───────────────────────────────────────────────────
471
472/// Extract a required text element from a segment.
473///
474/// Returns the element's first component, or an error if absent or empty.
475///
476/// # Empty-string semantics
477///
478/// EDIFACT allows elements to be syntactically present but carry an empty
479/// string value (e.g., `SEG++'`). This function treats an empty string as
480/// *absent* — it returns [`EdifactError::MissingRequiredElement`] in that
481/// case, matching the EDIFACT rule that mandatory data elements must carry
482/// a non-empty value.
483///
484/// Delegates to [`SegmentAccessor::text_element`].
485pub fn required_element<'a>(seg: &'a Segment<'_>, idx: usize) -> Result<&'a str, EdifactError> {
486    seg.text_element(idx)
487}
488
489/// Extract an optional text element from a segment.
490///
491/// Returns the element's first component, or None if absent or empty.
492///
493/// Delegates to [`SegmentAccessor::optional_element`].
494pub fn optional_element<'a>(seg: &'a Segment<'_>, idx: usize) -> Option<&'a str> {
495    SegmentAccessor::optional_element(seg, idx)
496}
497
498/// Extract a required component from a segment element.
499///
500/// Returns the component value, or an error if the element or component is absent.
501///
502/// # Empty-string semantics
503///
504/// Like [`required_element`], an empty string component value is treated as
505/// *absent*.  A component that is syntactically present as `''` (two
506/// consecutive component separators) will cause this function to return
507/// [`EdifactError::MissingRequiredComponent`].
508///
509/// # Failure modes
510///
511/// - [`EdifactError::MissingRequiredElement`] — element `elem_idx` is absent.
512/// - [`EdifactError::MissingRequiredComponent`] — element is present but component `comp_idx` is absent or empty.
513///
514/// Delegates to [`SegmentAccessor::required_composite`].
515pub fn required_component<'a>(
516    seg: &'a Segment<'_>,
517    elem_idx: usize,
518    comp_idx: usize,
519) -> Result<&'a str, EdifactError> {
520    seg.required_composite(elem_idx, comp_idx)
521}
522
523/// Extract an optional component from a segment element.
524///
525/// Returns the component value, or None if absent or empty.
526///
527/// Delegates to [`SegmentAccessor::get_component`].
528pub fn optional_component<'a>(
529    seg: &'a Segment<'_>,
530    elem_idx: usize,
531    comp_idx: usize,
532) -> Option<&'a str> {
533    SegmentAccessor::get_component(seg, elem_idx, comp_idx)
534}
535
536/// Iterate over all components of an element without allocating a `Vec`.
537///
538/// Yields an empty iterator if the element is absent.
539pub fn get_components_iter<'a>(seg: &'a Segment<'_>, idx: usize) -> impl Iterator<Item = &'a str> {
540    seg.elements
541        .get(idx)
542        .into_iter()
543        .flat_map(|elem| elem.components.iter().map(|(c, _)| c.as_ref()))
544}
545
546/// A composite data element wrapper for clearer ergonomics.
547///
548/// Holds borrowed `&'a str` references to the underlying data — no string
549/// copies are made.  Up to 4 component pointers are stored inline (via
550/// [`SmallVec`]) so the common case is fully allocation-free.
551///
552/// The lifetime `'a` represents the underlying data lifetime.
553///
554/// [`SmallVec`]: smallvec::SmallVec
555pub struct CompositeElement<'a> {
556    components: smallvec::SmallVec<[&'a str; 4]>,
557}
558
559impl<'a> CompositeElement<'a> {
560    /// Create a `CompositeElement` from a pre-existing `Cow` component slice.
561    ///
562    /// Used internally by generated owned-deserialization code.
563    pub fn from_slice(components: &'a [std::borrow::Cow<'a, str>]) -> Self {
564        Self {
565            components: components.iter().map(|c| c.as_ref()).collect(),
566        }
567    }
568
569    /// Crate-private constructor for direct `&str` components.
570    pub(crate) fn from_strs(components: smallvec::SmallVec<[&'a str; 4]>) -> Self {
571        Self { components }
572    }
573
574    /// Get the component at index `i`, or None if absent.
575    pub fn get(&self, i: usize) -> Option<&'a str> {
576        self.components.get(i).copied()
577    }
578
579    /// Get the component at index `i`, or empty string if absent.
580    pub fn get_or_empty(&self, i: usize) -> &'a str {
581        self.get(i).unwrap_or("")
582    }
583
584    /// Get the number of components.
585    pub fn len(&self) -> usize {
586        self.components.len()
587    }
588
589    /// Check if the composite is empty.
590    pub fn is_empty(&self) -> bool {
591        self.components.is_empty()
592    }
593
594    /// Iterate over all component string values.
595    pub fn iter(&self) -> impl Iterator<Item = &'a str> + '_ {
596        self.components.iter().copied()
597    }
598}
599
600/// Get a composite element from a segment with clearer ergonomics.
601pub fn composite_element<'a, 'd: 'a>(
602    seg: &'a Segment<'d>,
603    idx: usize,
604) -> Option<CompositeElement<'a>> {
605    // `.collect()` into `SmallVec<[&str; 4]>` keeps ≤4-component elements
606    // fully on the stack (no heap allocation for the common case).
607    seg.elements.get(idx).map(|elem| {
608        CompositeElement::from_strs(elem.components.iter().map(|(c, _)| c.as_ref()).collect())
609    })
610}
611
612/// Find the first [`OwnedSegment`] with the given tag.
613///
614/// Zero-allocation counterpart of [`find_segment`] for use in
615/// [`EdifactDeserialize::edifact_deserialize_owned`] implementations.
616///
617/// [`OwnedSegment`]: crate::OwnedSegment
618pub fn find_segment_owned<'s>(
619    segments: &'s [crate::OwnedSegment],
620    tag: &str,
621) -> Option<&'s crate::OwnedSegment> {
622    segments.iter().find(|s| s.tag == tag)
623}
624
625/// Find the first [`OwnedSegment`] with the given tag **and** qualifier.
626///
627/// The qualifier is compared against the first component of element 0.
628/// Zero-allocation counterpart of [`find_qualified_segment`] for use in
629/// [`EdifactDeserialize::edifact_deserialize_owned`] implementations.
630///
631/// [`OwnedSegment`]: crate::OwnedSegment
632pub fn find_qualified_segment_owned<'s>(
633    segments: &'s [crate::OwnedSegment],
634    tag: &str,
635    qualifier: &str,
636) -> Option<&'s crate::OwnedSegment> {
637    segments
638        .iter()
639        .find(|s| s.tag == tag && s.element_str(0).unwrap_or("") == qualifier)
640}
641
642/// Segment accessor trait for ergonomic typed extraction.
643pub trait SegmentAccessor<'a> {
644    /// Get non-empty element text at index `idx`.
645    fn get_element(&'a self, idx: usize) -> Option<&'a str>;
646    /// Get non-empty component text at element/component indexes.
647    fn get_component(&'a self, elem: usize, comp: usize) -> Option<&'a str>;
648    /// Get a composite wrapper for element `idx`.
649    fn get_composite(&'a self, idx: usize) -> Option<CompositeElement<'a>>;
650
651    /// Get required non-empty element text.
652    fn text_element(&'a self, idx: usize) -> Result<&'a str, EdifactError>;
653    /// Get optional non-empty element text.
654    fn optional_element(&'a self, idx: usize) -> Option<&'a str>;
655    /// Parse a typed code value from a required element.
656    fn code_element<T: FromStr>(&'a self, idx: usize) -> Result<T, EdifactError>;
657    /// Get required non-empty composite component.
658    fn required_composite(&'a self, elem: usize, comp: usize) -> Result<&'a str, EdifactError>;
659    /// Get `count` required components starting at `start_idx` from element `elem`.
660    ///
661    /// Allocates a `Vec`.  For a zero-alloc alternative, use
662    /// [`repeating_components_iter`][Self::repeating_components_iter] and
663    /// consume the iterator directly without collecting.
664    fn repeating_components(
665        &'a self,
666        elem: usize,
667        start_idx: usize,
668        count: usize,
669    ) -> Result<Vec<&'a str>, EdifactError> {
670        // Default implementation delegates to the zero-alloc iterator and
671        // collects.  Implementors that can do better should override this.
672        self.repeating_components_iter(elem, start_idx, count)
673            .collect()
674    }
675
676    /// Iterate over `count` required components starting at `start_idx` from element `elem`.
677    ///
678    /// Allocation-free alternative to [`repeating_components`][Self::repeating_components];
679    /// the caller supplies the iteration budget and consumes results on the fly.
680    fn repeating_components_iter(
681        &'a self,
682        elem: usize,
683        start_idx: usize,
684        count: usize,
685    ) -> impl Iterator<Item = Result<&'a str, EdifactError>> + 'a;
686}
687
688impl<'s, 'd> SegmentAccessor<'s> for Segment<'d>
689where
690    'd: 's,
691{
692    fn get_element(&'s self, idx: usize) -> Option<&'s str> {
693        self.element_str(idx).filter(|s| !s.is_empty())
694    }
695
696    fn get_component(&'s self, elem: usize, comp: usize) -> Option<&'s str> {
697        self.elements
698            .get(elem)
699            .and_then(|e| e.get_component(comp))
700            .filter(|s| !s.is_empty())
701    }
702
703    fn get_composite(&'s self, idx: usize) -> Option<CompositeElement<'s>> {
704        composite_element(self, idx)
705    }
706
707    fn text_element(&'s self, idx: usize) -> Result<&'s str, EdifactError> {
708        <Self as SegmentAccessor>::get_element(self, idx).ok_or_else(|| {
709            EdifactError::MissingRequiredElement {
710                tag: self.tag.to_owned(),
711                element_index: idx,
712            }
713        })
714    }
715
716    fn optional_element(&'s self, idx: usize) -> Option<&'s str> {
717        <Self as SegmentAccessor>::get_element(self, idx)
718    }
719
720    fn code_element<T: FromStr>(&'s self, idx: usize) -> Result<T, EdifactError> {
721        let raw = self.text_element(idx)?;
722        raw.parse::<T>().map_err(|_| EdifactError::InvalidText {
723            offset: self
724                .element_span(idx)
725                .map(|s| s.start)
726                .unwrap_or(self.span.start),
727        })
728    }
729
730    fn required_composite(&'s self, elem: usize, comp: usize) -> Result<&'s str, EdifactError> {
731        match self.elements.get(elem) {
732            None => Err(EdifactError::MissingRequiredElement {
733                tag: self.tag.to_owned(),
734                element_index: elem,
735            }),
736            Some(e) => e
737                .get_component(comp)
738                .filter(|s| !s.is_empty())
739                .ok_or_else(|| EdifactError::MissingRequiredComponent {
740                    tag: self.tag.to_owned(),
741                    element_index: elem,
742                    component_index: comp,
743                }),
744        }
745    }
746
747    fn repeating_components_iter(
748        &'s self,
749        elem: usize,
750        start_idx: usize,
751        count: usize,
752    ) -> impl Iterator<Item = Result<&'s str, EdifactError>> + 's {
753        let tag = self.tag;
754        let element_exists = self.elements.get(elem).is_some();
755        let components = self
756            .elements
757            .get(elem)
758            .map(|e| e.components.as_slice())
759            .unwrap_or(&[]);
760        (start_idx..start_idx + count).map(move |idx| {
761            components
762                .get(idx)
763                .map(|(c, _)| c.as_ref())
764                .filter(|s| !s.is_empty())
765                .ok_or_else(|| {
766                    if element_exists {
767                        EdifactError::MissingRequiredComponent {
768                            tag: tag.to_owned(),
769                            element_index: elem,
770                            component_index: idx,
771                        }
772                    } else {
773                        EdifactError::MissingRequiredElement {
774                            tag: tag.to_owned(),
775                            element_index: elem,
776                        }
777                    }
778                })
779        })
780    }
781}
782
783// ── message-window streaming ──────────────────────────────────────────────────
784
785/// A complete `UNH..UNT` message window that borrows from the original input.
786///
787/// Produced by [`MessageWindowsSliceIter`] / [`message_windows_bytes`].
788/// The `message_type` and `association_code` fields are extracted from the
789/// `UNH` segment at construction time, so callers do not need to traverse the
790/// segment list themselves.
791///
792/// `segments` contains the full window including the `UNH` and `UNT` service
793/// segments so that envelope-aware consumers have access to them.
794///
795/// # Accessing segments
796///
797/// ```rust,ignore
798/// for window in message_windows_bytes(input) {
799///     let window = window?;
800///     println!("type={:?} code={:?}", window.message_type, window.association_code);
801///     let bgm = window.segments.iter().find(|s| s.tag == "BGM");
802/// }
803/// ```
804#[derive(Debug)]
805pub struct MessageWindow<'a> {
806    /// EDIFACT message type extracted from `UNH` element 1, component 0.
807    ///
808    /// Borrowed when the component can be referenced directly, owned when
809    /// release-character unescaping requires allocation.
810    pub message_type: Option<Cow<'a, str>>,
811    /// Association-assigned code (DE 0057) from `UNH` element 1, component 4.
812    ///
813    /// Borrowed when the component can be referenced directly, owned when
814    /// release-character unescaping requires allocation.
815    pub association_code: Option<Cow<'a, str>>,
816    /// All segments in this window, from `UNH` through `UNT` (inclusive).
817    pub segments: Vec<crate::Segment<'a>>,
818}
819
820impl<'a> MessageWindow<'a> {
821    /// Build a `MessageWindow` from a completed segment buffer.
822    ///
823    /// Extracts `message_type` and `association_code` from the leading `UNH`
824    /// segment.  Metadata extraction is allocation-free for borrowed components;
825    /// release-character unescaping may allocate owned strings when necessary.
826    fn from_segments(segments: Vec<crate::Segment<'a>>) -> Self {
827        let message_type = segments
828            .first()
829            .filter(|s| s.tag == "UNH")
830            .and_then(|unh| unh_component(unh, 0));
831        let association_code = segments
832            .first()
833            .filter(|s| s.tag == "UNH")
834            .and_then(|unh| unh_component(unh, 4));
835        Self {
836            message_type,
837            association_code,
838            segments,
839        }
840    }
841}
842
843/// Extract a non-empty string component from UNH element 1, preserving the
844/// component's borrowed/owned state.
845///
846/// By using two distinct lifetime parameters (`'b` for the borrow of `seg`,
847/// `'a` for the segment data), we tell the borrow checker that the returned
848/// `&'a str` lives independently of how long we hold `&seg`, which lets callers
849/// move `seg` into a containing struct after this call returns.
850fn unh_component<'a, 'b>(seg: &'b crate::Segment<'a>, comp_idx: usize) -> Option<Cow<'a, str>>
851where
852    'a: 'b,
853{
854    seg.elements
855        .get(1)
856        .and_then(|e| e.components.get(comp_idx))
857        .and_then(|(c, _)| if c.is_empty() { None } else { Some(c.clone()) })
858}
859
860/// An owned, heap-allocated `UNH..UNT` message window.
861///
862/// Produced by [`MessageWindowsIter`] / [`message_windows_from_reader`].
863/// Equivalent to [`MessageWindow`] but with all data owned, so it outlives
864/// the original reader.
865///
866/// `segments` contains the full window including the `UNH` and `UNT` service
867/// segments.
868#[derive(Debug, Clone)]
869pub struct OwnedMessageWindow {
870    /// EDIFACT message type extracted from `UNH` element 1, component 0.
871    pub message_type: Option<String>,
872    /// Association-assigned code (DE 0057) from `UNH` element 1, component 4.
873    pub association_code: Option<String>,
874    /// All segments in this window, from `UNH` through `UNT` (inclusive).
875    pub segments: Vec<crate::OwnedSegment>,
876}
877
878impl OwnedMessageWindow {
879    fn from_segments(segments: Vec<crate::OwnedSegment>) -> Self {
880        let unh = segments.first().filter(|s| s.tag == "UNH");
881        let message_type = unh
882            .and_then(|s| s.elements.get(1))
883            .and_then(|e| e.components.first())
884            .map(|(c, _)| c.as_str())
885            .filter(|s| !s.is_empty())
886            .map(str::to_owned);
887        let association_code = unh
888            .and_then(|s| s.elements.get(1))
889            .and_then(|e| e.components.get(4))
890            .map(|(c, _)| c.as_str())
891            .filter(|s| !s.is_empty())
892            .map(str::to_owned);
893        Self {
894            message_type,
895            association_code,
896            segments,
897        }
898    }
899}
900
901/// An iterator that groups borrowed EDIFACT segments into per-message windows.
902///
903/// Zero-copy counterpart to [`MessageWindowsIter`] for in-memory byte slices.
904/// Text content borrows from the original input; segment structure allocates
905/// element vectors during parsing. Release-character unescaping may further
906/// allocate owned strings when escape sequences are present. Envelope segments
907/// outside a `UNH..UNT` pair are silently skipped.
908///
909/// Obtain this via [`message_windows_bytes`].
910pub struct MessageWindowsSliceIter<'a> {
911    inner: crate::FromBytesIter<'a>,
912    buf: Vec<crate::Segment<'a>>,
913    in_message: bool,
914    done: bool,
915}
916
917impl<'a> MessageWindowsSliceIter<'a> {
918    fn new(inner: crate::FromBytesIter<'a>) -> Self {
919        Self {
920            inner,
921            buf: Vec::new(),
922            in_message: false,
923            done: false,
924        }
925    }
926}
927
928impl<'a> Iterator for MessageWindowsSliceIter<'a> {
929    type Item = Result<MessageWindow<'a>, EdifactError>;
930
931    fn next(&mut self) -> Option<Self::Item> {
932        if self.done {
933            return None;
934        }
935        loop {
936            let segment = match self.inner.next() {
937                Some(Ok(s)) => s,
938                Some(Err(e)) => {
939                    self.done = true;
940                    return Some(Err(e));
941                }
942                None => {
943                    self.done = true;
944                    if self.in_message && !self.buf.is_empty() {
945                        self.in_message = false;
946                        let offset = self.buf.last().map(|s| s.span.end).unwrap_or(0);
947                        return Some(Err(EdifactError::UnexpectedEof { offset }));
948                    }
949                    return None;
950                }
951            };
952
953            match segment.tag {
954                "UNH" => {
955                    if self.in_message {
956                        self.buf.clear();
957                        self.in_message = false;
958                        self.done = true;
959                        let offset = segment.span.start;
960                        return Some(Err(EdifactError::InvalidSegmentForMessage {
961                            tag: "UNH".to_owned(),
962                            message_type: "ENVELOPE".to_owned(),
963                            offset,
964                        }));
965                    }
966                    self.buf.clear();
967                    self.in_message = true;
968                    self.buf.push(segment);
969                }
970                "UNT" if self.in_message => {
971                    self.buf.push(segment);
972                    self.in_message = false;
973                    let segments = std::mem::take(&mut self.buf);
974                    return Some(Ok(MessageWindow::from_segments(segments)));
975                }
976                _ if self.in_message => {
977                    self.buf.push(segment);
978                }
979                _ => {
980                    // Envelope segment outside a window — skip.
981                }
982            }
983        }
984    }
985}
986
987/// An iterator that groups owned EDIFACT segments into per-message windows.
988///
989/// Each yielded item is an [`OwnedMessageWindow`] containing the segments for one
990/// complete `UNH..UNT` message, inclusive of both service segments.
991/// Envelope-level segments (`UNB`, `UNG`, `UNZ`, `UNE`) that sit outside any
992/// `UNH..UNT` pair are silently skipped.
993///
994/// # Errors
995///
996/// - An inner-iterator error is forwarded immediately and iteration stops.
997/// - A `UNH` seen while a prior window is still open (missing `UNT`) is an error.
998/// - Input that ends while a `UNH` window is open (stream truncation) yields
999///   `Err(EdifactError::UnexpectedEof { … })` before returning `None`.
1000///
1001/// # Construction
1002///
1003/// Use [`message_windows_from_reader`] or [`message_windows_bytes`] to
1004/// obtain a `MessageWindowsIter` directly.  For fully custom sources, call
1005/// [`MessageWindowsIter::new`] with any `Iterator<Item = Result<OwnedSegment,
1006/// EdifactError>>`.
1007pub struct MessageWindowsIter<I> {
1008    inner: I,
1009    buf: Vec<crate::OwnedSegment>,
1010    in_message: bool,
1011    /// Set to `true` after any terminal condition (error or clean EOF) so that
1012    /// subsequent `next()` calls immediately return `None`.
1013    done: bool,
1014}
1015
1016impl<I: Iterator<Item = Result<crate::OwnedSegment, EdifactError>>> MessageWindowsIter<I> {
1017    /// Wrap any owned-segment iterator as a message-window iterator.
1018    pub fn new(inner: I) -> Self {
1019        Self {
1020            inner,
1021            buf: Vec::new(),
1022            in_message: false,
1023            done: false,
1024        }
1025    }
1026}
1027
1028impl<I: Iterator<Item = Result<crate::OwnedSegment, EdifactError>>> Iterator
1029    for MessageWindowsIter<I>
1030{
1031    type Item = Result<OwnedMessageWindow, EdifactError>;
1032
1033    fn next(&mut self) -> Option<Self::Item> {
1034        if self.done {
1035            return None;
1036        }
1037        loop {
1038            let segment = match self.inner.next() {
1039                Some(Ok(s)) => s,
1040                Some(Err(e)) => {
1041                    self.done = true;
1042                    return Some(Err(e));
1043                }
1044                None => {
1045                    self.done = true;
1046                    // A window that opened (UNH seen) but never closed (no UNT)
1047                    // means the stream was truncated — surface as an error.
1048                    if self.in_message && !self.buf.is_empty() {
1049                        self.in_message = false;
1050                        let offset = self.buf.last().map(|s| s.span.end).unwrap_or(0);
1051                        return Some(Err(EdifactError::UnexpectedEof { offset }));
1052                    }
1053                    return None;
1054                }
1055            };
1056
1057            match segment.tag.as_str() {
1058                "UNH" => {
1059                    if self.in_message {
1060                        // Malformed: new UNH without closing the prior UNT.
1061                        self.buf.clear();
1062                        self.in_message = false;
1063                        self.done = true;
1064                        let offset = segment.span.start;
1065                        return Some(Err(EdifactError::InvalidSegmentForMessage {
1066                            tag: "UNH".to_owned(),
1067                            message_type: "ENVELOPE".to_owned(),
1068                            offset,
1069                        }));
1070                    }
1071                    self.buf.clear();
1072                    self.in_message = true;
1073                    self.buf.push(segment);
1074                }
1075                "UNT" if self.in_message => {
1076                    self.buf.push(segment);
1077                    self.in_message = false;
1078                    let segments = std::mem::take(&mut self.buf);
1079                    return Some(Ok(OwnedMessageWindow::from_segments(segments)));
1080                }
1081                _ if self.in_message => {
1082                    self.buf.push(segment);
1083                }
1084                _ => {
1085                    // Envelope segment outside a window — skip.
1086                }
1087            }
1088        }
1089    }
1090}
1091
1092/// Stream-parse EDIFACT bytes into an iterator of per-message windows.
1093///
1094/// Each yielded [`MessageWindow`] spans one `UNH..UNT` pair, with segments
1095/// borrowing from `input` for their text content. Segment assembly is
1096/// zero-copy for borrowed input bytes; release-character unescaping may
1097/// allocate owned component strings when necessary.
1098/// Envelope segments (`UNB`, `UNZ`, …) are skipped automatically.
1099///
1100/// The `message_type` and `association_code` fields are populated directly from
1101/// the `UNH` segment so that routing logic does not need to traverse `segments`.
1102///
1103/// # Example
1104/// ```
1105/// use edifact_rs::from_bytes_windows;
1106/// let input = b"UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'\
1107///               UNH+1+ORDERS:D:96A:UN'\
1108///               BGM+220+PO-001+9'\
1109///               UNT+3+1'\
1110///               UNZ+1+1'";
1111///
1112/// let windows: Vec<_> = from_bytes_windows(input)
1113///     .collect::<Result<_, _>>()
1114///     .unwrap();
1115/// assert_eq!(windows.len(), 1);
1116/// assert_eq!(windows[0].message_type.as_deref(), Some("ORDERS"));
1117/// assert_eq!(windows[0].segments[0].tag, "UNH");
1118/// assert_eq!(windows[0].segments.last().unwrap().tag, "UNT");
1119/// ```
1120pub fn message_windows_bytes(input: &[u8]) -> MessageWindowsSliceIter<'_> {
1121    MessageWindowsSliceIter::new(crate::from_bytes(input))
1122}
1123
1124/// Stream-parse EDIFACT from a reader into an iterator of per-message windows.
1125///
1126/// Each yielded [`OwnedMessageWindow`] spans one `UNH..UNT` pair.
1127/// This variant reads lazily — only enough input to complete one window is
1128/// consumed per [`Iterator::next`] call.
1129pub fn message_windows_from_reader<R: Read>(
1130    reader: R,
1131) -> MessageWindowsIter<crate::FromReaderIter<R>> {
1132    MessageWindowsIter::new(crate::from_reader_iter(reader))
1133}
1134
1135/// Stream typed messages from a reader by deserializing each `UNH..UNT` window.
1136///
1137/// This is the highest-level streaming API: it returns one `T` per message,
1138/// reading only as much data as needed to complete each window.
1139///
1140/// Each message window is deserialized via
1141/// [`EdifactDeserialize::edifact_deserialize_owned`], which avoids the
1142/// intermediate `Vec<Segment<'_>>` allocation incurred by the slice-based path.
1143/// Types derived with `#[derive(EdifactDeserialize)]` provide an efficient
1144/// override; manual implementations fall back to [`crate::OwnedSegment::as_borrowed`].
1145///
1146/// # Example
1147/// ```ignore
1148/// // Assuming `OrdersMessage` implements `EdifactDeserialize`:
1149/// let messages: Vec<OrdersMessage> =
1150///     deserialize_messages_from_reader::<OrdersMessage, _>(reader)
1151///         .collect::<Result<_, _>>()?;
1152/// ```
1153pub fn deserialize_messages_from_reader<T, R>(
1154    reader: R,
1155) -> impl Iterator<Item = Result<T, EdifactError>>
1156where
1157    T: EdifactDeserialize,
1158    R: Read,
1159{
1160    message_windows_from_reader(reader).map(|window| {
1161        let window = window?;
1162        T::edifact_deserialize_owned(&window.segments)
1163    })
1164}
1165
1166/// Stream typed messages from a byte slice by deserializing each `UNH..UNT` window.
1167pub fn deserialize_messages_bytes<T>(
1168    input: &[u8],
1169) -> impl Iterator<Item = Result<T, EdifactError>> + '_
1170where
1171    T: EdifactDeserialize,
1172{
1173    message_windows_bytes(input).map(|window| {
1174        let window = window?;
1175        T::edifact_deserialize(&window.segments)
1176    })
1177}
1178
1179// ── MessageDispatch ───────────────────────────────────────────────────────────
1180
1181/// A type-erased deserialized message produced by [`MessageDispatch`].
1182pub struct DispatchedMessage {
1183    /// The EDIFACT message type string extracted from the `UNH` segment.
1184    pub message_type: String,
1185    value: Box<dyn std::any::Any + Send + Sync>,
1186}
1187
1188impl DispatchedMessage {
1189    /// Attempt to downcast the inner value to `T`.
1190    ///
1191    /// Returns `None` if the stored type does not match `T`.
1192    pub fn downcast<T: std::any::Any + Send + Sync + 'static>(&self) -> Option<&T> {
1193        self.value.downcast_ref::<T>()
1194    }
1195}
1196
1197impl std::fmt::Debug for DispatchedMessage {
1198    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1199        f.debug_struct("DispatchedMessage")
1200            .field("message_type", &self.message_type)
1201            .finish_non_exhaustive()
1202    }
1203}
1204
1205type DispatchHandlerFn = Box<
1206    dyn for<'a> Fn(&[Segment<'a>]) -> Result<Box<dyn std::any::Any + Send + Sync>, EdifactError>
1207        + Send
1208        + Sync,
1209>;
1210
1211type FallbackHandlerFn = Box<
1212    dyn for<'a> Fn(
1213            &[Segment<'a>],
1214            &str,
1215        ) -> Result<Box<dyn std::any::Any + Send + Sync>, EdifactError>
1216        + Send
1217        + Sync,
1218>;
1219
1220/// Type-based dispatcher for mixed-message EDIFACT streams.
1221///
1222/// Register one handler per message type with [`on`][Self::on], then call
1223/// [`dispatch`][Self::dispatch] on each message window.  If no handler matches
1224/// and a [`fallback`][Self::fallback] was registered it is invoked instead;
1225/// otherwise an [`EdifactError::UnexpectedMessageType`] is returned.
1226///
1227/// # Example
1228///
1229/// ```rust,ignore
1230/// let dispatch = MessageDispatch::new()
1231///     .on("ORDERS",  |segs| Orders::edifact_deserialize(segs))
1232///     .on("INVOIC",  |segs| Invoice::edifact_deserialize(segs));
1233///
1234/// for window in message_windows_bytes(input) {
1235///     let window = window?;
1236///     let msg = dispatch.dispatch(&window)?;
1237///     match msg.message_type.as_str() {
1238///         "ORDERS"  => { let o = msg.downcast::<Orders>().unwrap(); /* … */ }
1239///         "INVOIC"  => { let i = msg.downcast::<Invoice>().unwrap(); /* … */ }
1240///         _         => unreachable!(),
1241///     }
1242/// }
1243/// ```
1244pub struct MessageDispatch {
1245    handlers: Vec<(String, DispatchHandlerFn)>,
1246    fallback: Option<FallbackHandlerFn>,
1247}
1248
1249impl Default for MessageDispatch {
1250    fn default() -> Self {
1251        Self::new()
1252    }
1253}
1254
1255impl MessageDispatch {
1256    /// Create an empty dispatcher.
1257    pub fn new() -> Self {
1258        Self {
1259            handlers: Vec::new(),
1260            fallback: None,
1261        }
1262    }
1263
1264    /// Register a handler for `message_type`.
1265    ///
1266    /// The closure receives the full message window and returns a typed value
1267    /// that is boxed and stored inside [`DispatchedMessage`].
1268    pub fn on<T, F>(mut self, message_type: &str, handler: F) -> Self
1269    where
1270        T: std::any::Any + Send + Sync + 'static,
1271        F: for<'a> Fn(&[Segment<'a>]) -> Result<T, EdifactError> + Send + Sync + 'static,
1272    {
1273        let erased: DispatchHandlerFn = Box::new(move |segs| {
1274            let val = handler(segs)?;
1275            Ok(Box::new(val) as Box<dyn std::any::Any + Send + Sync>)
1276        });
1277        self.handlers.push((message_type.to_owned(), erased));
1278        self
1279    }
1280
1281    /// Register a fallback handler for unrecognised message types.
1282    ///
1283    /// The closure receives the segment window **and** the unknown message-type
1284    /// string.
1285    pub fn fallback<T, F>(mut self, handler: F) -> Self
1286    where
1287        T: std::any::Any + Send + Sync + 'static,
1288        F: for<'a> Fn(&[Segment<'a>], &str) -> Result<T, EdifactError> + Send + Sync + 'static,
1289    {
1290        let erased: FallbackHandlerFn = Box::new(move |segs, mt| {
1291            let val = handler(segs, mt)?;
1292            Ok(Box::new(val) as Box<dyn std::any::Any + Send + Sync>)
1293        });
1294        self.fallback = Some(erased);
1295        self
1296    }
1297
1298    /// Dispatch a single message window to the appropriate handler.
1299    ///
1300    /// The message type is extracted from the `UNH` segment.  If no `UNH` is
1301    /// present, [`EdifactError::MissingSegment`] is returned.
1302    pub fn dispatch(&self, window: &[Segment<'_>]) -> Result<DispatchedMessage, EdifactError> {
1303        let message_type = window
1304            .iter()
1305            .find(|s| s.tag == "UNH")
1306            .and_then(|unh| unh.get_element(1))
1307            .and_then(|e| e.get_component(0))
1308            .map(|s| s.to_owned())
1309            .ok_or_else(|| EdifactError::MissingSegment {
1310                tag: "UNH".to_owned(),
1311                expected_position: "first segment of message window".to_owned(),
1312            })?;
1313
1314        for (mt, handler) in &self.handlers {
1315            if *mt == message_type {
1316                let value = handler(window)?;
1317                return Ok(DispatchedMessage {
1318                    message_type,
1319                    value,
1320                });
1321            }
1322        }
1323
1324        if let Some(fallback) = &self.fallback {
1325            let value = fallback(window, &message_type)?;
1326            return Ok(DispatchedMessage {
1327                message_type,
1328                value,
1329            });
1330        }
1331
1332        Err(EdifactError::UnexpectedMessageType { message_type })
1333    }
1334
1335    /// Dispatch all messages from a byte reader.
1336    ///
1337    /// Each message window is extracted and dispatched in order.  The returned
1338    /// iterator is lazy — errors are yielded as `Err` items.
1339    pub fn dispatch_all_from_bytes<'a>(
1340        &'a self,
1341        input: &'a [u8],
1342    ) -> impl Iterator<Item = Result<DispatchedMessage, EdifactError>> + 'a {
1343        message_windows_bytes(input).map(move |window| {
1344            let window = window?;
1345            self.dispatch(&window.segments)
1346        })
1347    }
1348
1349    /// Dispatch all messages from a reader.
1350    ///
1351    /// Parses the stream into message windows and dispatches each.  The
1352    /// returned iterator yields owned [`DispatchedMessage`] values lazily:
1353    /// each window is fully buffered in memory (as `Vec<OwnedSegment>`) before
1354    /// dispatch, but windows are processed one at a time rather than all at once.
1355    pub fn dispatch_all_from_reader<R: Read + 'static>(
1356        &self,
1357        reader: R,
1358    ) -> impl Iterator<Item = Result<DispatchedMessage, EdifactError>> + '_ {
1359        message_windows_from_reader(reader).map(|window| {
1360            let window = window?;
1361            let borrowed: Vec<Segment<'_>> =
1362                window.segments.iter().map(|s| s.as_borrowed()).collect();
1363            self.dispatch(&borrowed)
1364        })
1365    }
1366}
1367
1368#[cfg(test)]
1369mod tests {
1370    use super::*;
1371
1372    // ── manual test impl ──────────────────────────────────────────────────────
1373    #[derive(Debug, PartialEq)]
1374    struct BgmSegment {
1375        doc_name_code: String,
1376        pruef_id: String,
1377        msg_function: Option<String>,
1378    }
1379
1380    impl EdifactSegmentTag for BgmSegment {
1381        const SEGMENT_TAG: &'static str = "BGM";
1382    }
1383
1384    struct NadM;
1385
1386    impl EdifactSegmentTag for NadM {
1387        const SEGMENT_TAG: &'static str = "NAD";
1388        const QUALIFIER_PATTERN: Option<&'static str> = Some("M*");
1389    }
1390
1391    struct NadWildcard;
1392
1393    impl EdifactSegmentTag for NadWildcard {
1394        const SEGMENT_TAG: &'static str = "NAD";
1395        const QUALIFIER_PATTERN: Option<&'static str> = Some("M*");
1396    }
1397
1398    impl EdifactDeserialize for BgmSegment {
1399        fn edifact_deserialize(segments: &[Segment<'_>]) -> Result<Self, EdifactError> {
1400            let seg = find_segment(segments, "BGM").ok_or_else(|| {
1401                EdifactError::MissingRequiredElement {
1402                    tag: "BGM".to_owned(),
1403                    element_index: 0,
1404                }
1405            })?;
1406            Ok(Self {
1407                doc_name_code: element_str(seg, 0).to_owned(),
1408                pruef_id: element_str(seg, 1).to_owned(),
1409                msg_function: seg
1410                    .element_str(2)
1411                    .filter(|s| !s.is_empty())
1412                    .map(str::to_owned),
1413            })
1414        }
1415    }
1416
1417    #[test]
1418    fn deserialize_single_segment() {
1419        let input = b"BGM+E03+11042+9'";
1420        let bgm: BgmSegment = deserialize(input).unwrap();
1421        assert_eq!(bgm.doc_name_code, "E03");
1422        assert_eq!(bgm.pruef_id, "11042");
1423        assert_eq!(bgm.msg_function, Some("9".to_owned()));
1424    }
1425
1426    #[test]
1427    fn streaming_deserialize_first_from_bytes() {
1428        let input = b"UNH+1+ORDERS:D:11A:UN'BGM+E03+11042+9'UNT+3+1'";
1429        let bgm: BgmSegment = deserialize_first_streaming(input).unwrap();
1430        assert_eq!(bgm.pruef_id, "11042");
1431    }
1432
1433    #[test]
1434    fn streaming_deserialize_all_from_bytes() {
1435        let input = b"BGM+E03+11042+9'RFF+AA:1'BGM+E01+11043+9'";
1436        let bgms: Vec<BgmSegment> = deserialize_all_streaming(input).unwrap();
1437        assert_eq!(bgms.len(), 2);
1438        assert_eq!(bgms[0].pruef_id, "11042");
1439        assert_eq!(bgms[1].pruef_id, "11043");
1440    }
1441
1442    #[test]
1443    fn streaming_deserialize_first_from_reader() {
1444        let input =
1445            std::io::Cursor::new(b"UNH+1+ORDERS:D:11A:UN'BGM+E03+11042+9'UNT+3+1'".to_vec());
1446        let bgm: BgmSegment = deserialize_first_from_reader(input).unwrap();
1447        assert_eq!(bgm.pruef_id, "11042");
1448    }
1449
1450    #[test]
1451    fn streaming_deserialize_all_from_reader() {
1452        let input = std::io::Cursor::new(b"BGM+E03+11042+9'BGM+E01+11043+9'".to_vec());
1453        let bgms: Vec<BgmSegment> = deserialize_all_from_reader(input).unwrap();
1454        assert_eq!(bgms.len(), 2);
1455        assert_eq!(bgms[0].pruef_id, "11042");
1456        assert_eq!(bgms[1].pruef_id, "11043");
1457    }
1458
1459    #[test]
1460    fn missing_segment_returns_error() {
1461        let input = b"DTM+137:20230401:102'";
1462        let result: Result<BgmSegment, _> = deserialize(input);
1463        assert!(result.is_err());
1464    }
1465
1466    #[test]
1467    fn vec_collects_all_matching_segments() {
1468        let input = b"DTM+137:20230401:102'BGM+E03+11042+9'BGM+E01+11043+9'";
1469        let bgms: Vec<BgmSegment> = deserialize(input).unwrap();
1470        assert_eq!(bgms.len(), 2);
1471        assert_eq!(bgms[0].pruef_id, "11042");
1472        assert_eq!(bgms[1].pruef_id, "11043");
1473    }
1474
1475    #[test]
1476    fn find_qualified_segment_matches_qualifier() {
1477        let input = b"NAD+MS+9900001+293'NAD+MR+9900002+293'";
1478        let segments: Vec<Segment<'_>> =
1479            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1480        let nad_ms = find_qualified_segment(&segments, "NAD", "MS");
1481        let nad_mr = find_qualified_segment(&segments, "NAD", "MR");
1482        assert!(nad_ms.is_some());
1483        assert!(nad_mr.is_some());
1484        assert_eq!(element_str(nad_ms.unwrap(), 0), "MS");
1485        assert_eq!(element_str(nad_mr.unwrap(), 0), "MR");
1486    }
1487
1488    #[test]
1489    fn round_trip_str_api() {
1490        let input = "BGM+E03+11042+9'";
1491        let bgm: BgmSegment = deserialize_str(input).unwrap();
1492        assert_eq!(bgm.pruef_id, "11042");
1493    }
1494
1495    #[test]
1496    fn required_element_extraction() {
1497        let input = b"BGM+E03+11042+9'";
1498        let segments: Vec<Segment<'_>> =
1499            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1500        let seg = &segments[0];
1501
1502        assert_eq!(required_element(seg, 0).unwrap(), "E03");
1503        assert_eq!(required_element(seg, 1).unwrap(), "11042");
1504        // Element 5 doesn't exist
1505        assert!(required_element(seg, 5).is_err());
1506    }
1507
1508    #[test]
1509    fn optional_element_extraction() {
1510        let input = b"BGM+E03+11042+9'BGM+E01++absent'";
1511        let segments: Vec<Segment<'_>> =
1512            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1513
1514        // First segment
1515        assert_eq!(optional_element(&segments[0], 0), Some("E03"));
1516        assert_eq!(optional_element(&segments[0], 1), Some("11042"));
1517        assert_eq!(optional_element(&segments[0], 5), None);
1518
1519        // Second segment with empty element
1520        assert_eq!(optional_element(&segments[1], 1), None);
1521    }
1522
1523    #[test]
1524    fn component_extraction() {
1525        let input = b"UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'";
1526        let segments: Vec<Segment<'_>> =
1527            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1528        let seg = &segments[0];
1529
1530        assert_eq!(required_component(seg, 0, 0).unwrap(), "UNOA");
1531        assert_eq!(required_component(seg, 0, 1).unwrap(), "1");
1532        // Non-existent component
1533        assert!(required_component(seg, 0, 5).is_err());
1534    }
1535
1536    #[test]
1537    fn composite_element_helper() {
1538        let input = b"UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'";
1539        let segments: Vec<Segment<'_>> =
1540            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1541        let seg = &segments[0];
1542
1543        let comp = composite_element(seg, 0).unwrap();
1544        assert_eq!(comp.len(), 2);
1545        assert_eq!(comp.get(0), Some("UNOA"));
1546        assert_eq!(comp.get(1), Some("1"));
1547        assert_eq!(comp.get(5), None);
1548        assert_eq!(comp.get_or_empty(5), "");
1549    }
1550
1551    #[test]
1552    fn get_all_components() {
1553        // UNB has composite element: UNOA:1
1554        let input = b"UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'";
1555        let segments: Vec<Segment<'_>> =
1556            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1557        let seg = &segments[0];
1558
1559        let comps: Vec<&str> = get_components_iter(seg, 0).collect(); // First element is UNOA:1
1560        assert!(!comps.is_empty(), "Expected components but got empty");
1561        assert_eq!(comps.len(), 2);
1562        assert_eq!(comps[0], "UNOA");
1563        assert_eq!(comps[1], "1");
1564    }
1565
1566    #[test]
1567    fn qualifier_pattern_matching_supports_exact_and_wildcard() {
1568        // Exact match (no wildcard)
1569        assert!(qualifier_matches_pattern("MS", "MS"));
1570        assert!(!qualifier_matches_pattern("MS", "M")); // Not a prefix match after R-003
1571        // Wildcard patterns
1572        assert!(qualifier_matches_pattern("MS", "M*"));
1573        assert!(qualifier_matches_pattern("MRY", "M*Y"));
1574        assert!(!qualifier_matches_pattern("AB", "M*"));
1575    }
1576
1577    /// Comprehensive table-driven tests for `qualifier_matches_pattern`.
1578    #[test]
1579    fn qualifier_matches_pattern_table() {
1580        // (value, pattern, expected)
1581        let cases: &[(&str, &str, bool)] = &[
1582            // ── empty inputs ────────────────────────────────────────────────
1583            ("", "", true),   // empty matches empty
1584            ("", "*", true),  // wildcard matches empty string
1585            ("A", "", false), // non-empty does not match empty pattern
1586            ("", "A", false), // empty does not match non-empty literal
1587            // ── literal (no wildcard) ────────────────────────────────────────
1588            ("MS", "MS", true),
1589            ("BY", "BY", true),
1590            ("ms", "MS", false),  // case-sensitive
1591            ("MSX", "MS", false), // prefix is NOT a match without wildcard
1592            ("M", "MS", false),   // too short
1593            // ── single wildcard at the end (prefix match) ────────────────────
1594            ("MS", "M*", true),
1595            ("MULTI", "MUL*", true),
1596            ("AB", "M*", false),
1597            ("", "M*", false), // empty does not start with 'M'
1598            // ── single wildcard at the start (suffix match) ──────────────────
1599            ("MSG", "*G", true),
1600            ("G", "*G", true),
1601            ("MSG", "*X", false),
1602            ("", "*G", false),
1603            // ── wildcard in the middle ───────────────────────────────────────
1604            ("MRY", "M*Y", true),
1605            ("MAY", "M*Y", true),
1606            ("MY", "M*Y", true),    // zero-width wildcard: "M" + "" + "Y"
1607            ("MYY", "M*Y", true),   // last 'Y' matches, wildcard = 'Y'
1608            ("MAYZ", "M*Y", false), // does not end with 'Y'
1609            ("AB", "M*Y", false),
1610            // ── bare wildcard (match-all) ────────────────────────────────────
1611            ("*", "*", true), // literal '*' value vs wildcard pattern
1612            ("anything", "*", true),
1613            ("", "*", true),
1614            // ── multiple wildcards ────────────────────────────────────────────
1615            ("ABCDE", "A*C*E", true),
1616            ("ACE", "A*C*E", true), // zero-width wildcards
1617            ("AXCYE", "A*C*E", true),
1618            ("ABCDF", "A*C*E", false),
1619            // ── wildcard with empty segment between stars ─────────────────────
1620            ("AB", "A**B", true), // "A**B" → parts ["A", "", "B"] → ends_with_wildcard?
1621            // ── pattern longer than value ─────────────────────────────────────
1622            ("AB", "A*B*C", false),
1623            // ── value contains pattern as substring but must anchor start ─────
1624            ("XMS", "MS", false),
1625        ];
1626
1627        for (value, pattern, expected) in cases {
1628            let got = qualifier_matches_pattern(value, pattern);
1629            assert_eq!(
1630                got, *expected,
1631                "qualifier_matches_pattern({value:?}, {pattern:?}) expected {expected} but got {got}"
1632            );
1633        }
1634    }
1635
1636    #[test]
1637    fn typed_qualifier_helpers_work() {
1638        let input = b"NAD+MS+9900001+293'NAD+MR+9900002+293'";
1639        let segments: Vec<Segment<'_>> =
1640            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1641
1642        let first = find_segment_typed::<NadM>(&segments).unwrap();
1643        assert_eq!(first.element_str(0), Some("MS"));
1644
1645        let all: Vec<_> = find_segments_typed::<NadWildcard>(&segments).collect();
1646        assert_eq!(all.len(), 2);
1647    }
1648
1649    #[test]
1650    fn segment_accessor_trait_methods_work() {
1651        let input = b"UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'";
1652        let segments: Vec<Segment<'_>> =
1653            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1654        let seg = &segments[0];
1655
1656        assert_eq!(SegmentAccessor::get_element(seg, 1), Some("SENDER"));
1657        assert_eq!(SegmentAccessor::required_composite(seg, 0, 1).unwrap(), "1");
1658        let parsed: i32 = SegmentAccessor::code_element(seg, 4).unwrap();
1659        assert_eq!(parsed, 1);
1660        let reps = SegmentAccessor::repeating_components(seg, 3, 0, 2).unwrap();
1661        assert_eq!(reps, vec!["200101", "0900"]);
1662    }
1663
1664    #[test]
1665    fn group_helpers_detect_contiguity() {
1666        struct NadAny;
1667        impl EdifactSegmentTag for NadAny {
1668            const SEGMENT_TAG: &'static str = "NAD";
1669        }
1670
1671        let contiguous_input = b"NAD+MS+1'NAD+MR+2'RFF+AA:1'";
1672        let contiguous_segments: Vec<Segment<'_>> = crate::from_bytes(contiguous_input)
1673            .collect::<Result<_, _>>()
1674            .unwrap();
1675        assert!(groups_are_contiguous_by_qualifier::<NadAny>(
1676            &contiguous_segments
1677        ));
1678
1679        let non_contiguous_input = b"NAD+MS+1'RFF+AA:1'NAD+MR+2'";
1680        let non_contiguous_segments: Vec<Segment<'_>> = crate::from_bytes(non_contiguous_input)
1681            .collect::<Result<_, _>>()
1682            .unwrap();
1683        assert!(!groups_are_contiguous_by_qualifier::<NadAny>(
1684            &non_contiguous_segments
1685        ));
1686    }
1687
1688    #[test]
1689    fn group_helpers_collect_contiguous_groups() {
1690        struct NadAny;
1691        impl EdifactSegmentTag for NadAny {
1692            const SEGMENT_TAG: &'static str = "NAD";
1693        }
1694
1695        let input = b"NAD+MS+1'NAD+MR+2'RFF+AA:1'NAD+BY+3'";
1696        let segments: Vec<Segment<'_>> =
1697            crate::from_bytes(input).collect::<Result<_, _>>().unwrap();
1698        let groups = contiguous_groups_by_qualifier::<NadAny>(&segments);
1699
1700        assert_eq!(groups.len(), 2);
1701        assert_eq!(groups[0].len(), 2);
1702        assert_eq!(groups[1].len(), 1);
1703    }
1704
1705    // ── MessageWindowsIter tests ──────────────────────────────────────────────
1706
1707    #[test]
1708    fn message_windows_bytes_yields_complete_windows() {
1709        let input = b"UNB+UNOA:1+S+R+200101:0900+1'\
1710                      UNH+1+ORDERS:D:96A:UN'\
1711                      BGM+220+PO-001+9'\
1712                      UNT+3+1'\
1713                      UNZ+1+1'";
1714        let windows: Vec<_> = message_windows_bytes(input)
1715            .collect::<Result<_, _>>()
1716            .unwrap();
1717        assert_eq!(windows.len(), 1);
1718        assert_eq!(windows[0].segments[0].tag, "UNH");
1719        assert_eq!(windows[0].segments.last().unwrap().tag, "UNT");
1720        assert_eq!(windows[0].message_type.as_deref(), Some("ORDERS"));
1721        assert_eq!(windows[0].association_code.as_deref(), None);
1722    }
1723
1724    #[test]
1725    fn message_windows_bytes_preserves_owned_unh_metadata() {
1726        let input = b"UNB+UNOA:1+S+R+200101:0900+1'\
1727                      UNH+1+ORD?ERS:D:96A:UN:5??5??3a'\
1728                      BGM+220+PO-001+9'\
1729                      UNT+3+1'\
1730                      UNZ+1+1'";
1731        let windows: Vec<_> = message_windows_bytes(input)
1732            .collect::<Result<_, _>>()
1733            .unwrap();
1734
1735        assert_eq!(windows.len(), 1);
1736        assert_eq!(windows[0].message_type.as_deref(), Some("ORDERS"));
1737        assert_eq!(windows[0].association_code.as_deref(), Some("5?5?3a"));
1738    }
1739
1740    #[test]
1741    fn message_windows_truncated_stream_returns_error() {
1742        // Stream ends after UNH and BGM but without UNT — truncation must be an error
1743        let input = b"UNH+1+ORDERS:D:96A:UN'BGM+220+PO-001+9'";
1744        let results: Vec<_> = message_windows_bytes(input).collect();
1745        assert_eq!(results.len(), 1);
1746        assert!(
1747            matches!(results[0], Err(EdifactError::UnexpectedEof { .. })),
1748            "expected UnexpectedEof for truncated window, got: {:?}",
1749            results[0]
1750        );
1751    }
1752
1753    #[test]
1754    fn message_windows_subsequent_calls_return_none_after_truncation() {
1755        let input = b"UNH+1+ORDERS:D:96A:UN'BGM+220+PO-001+9'";
1756        let mut iter = message_windows_bytes(input);
1757        assert!(matches!(
1758            iter.next(),
1759            Some(Err(EdifactError::UnexpectedEof { .. }))
1760        ));
1761        // After the error, the iterator must be fused (done = true)
1762        assert!(iter.next().is_none());
1763    }
1764
1765    #[test]
1766    fn message_windows_unh_without_unt_before_next_unh_returns_error() {
1767        let input = b"UNH+1+ORDERS:D:96A:UN'BGM+220+PO-001+9'\
1768                      UNH+2+ORDERS:D:96A:UN'BGM+220+PO-002+9'UNT+3+2'";
1769        let results: Vec<_> = message_windows_bytes(input).collect();
1770        // First item must be an error (UNH before UNT — missing closer)
1771        assert!(
1772            matches!(
1773                results[0],
1774                Err(EdifactError::InvalidSegmentForMessage { ref tag, .. }) if tag == "UNH"
1775            ),
1776            "expected InvalidSegmentForMessage(UNH), got: {:?}",
1777            results[0]
1778        );
1779    }
1780
1781    // ── SegmentAccessor unit tests ─────────────────────────────────────────────
1782
1783    fn parse_one(input: &str) -> crate::OwnedSegment {
1784        crate::from_reader_collect(std::io::Cursor::new(input.as_bytes()))
1785            .expect("parse failed")
1786            .into_iter()
1787            .next()
1788            .expect("at least one segment")
1789    }
1790
1791    #[test]
1792    fn segment_accessor_get_element_returns_value() {
1793        let owned = parse_one("BGM+220+PO-001+9'");
1794        let seg = owned.as_borrowed();
1795        assert_eq!(SegmentAccessor::get_element(&seg, 0), Some("220"));
1796        assert_eq!(SegmentAccessor::get_element(&seg, 1), Some("PO-001"));
1797        assert_eq!(SegmentAccessor::get_element(&seg, 2), Some("9"));
1798        assert_eq!(
1799            SegmentAccessor::get_element(&seg, 9),
1800            None,
1801            "out-of-bounds must return None"
1802        );
1803    }
1804
1805    #[test]
1806    fn segment_accessor_get_element_filters_empty() {
1807        let owned = parse_one("TST+++VALUE'");
1808        let seg = owned.as_borrowed();
1809        // elements 0 and 1 are empty; element 2 is "VALUE"
1810        assert_eq!(
1811            SegmentAccessor::get_element(&seg, 0),
1812            None,
1813            "empty element must return None"
1814        );
1815        assert_eq!(
1816            SegmentAccessor::get_element(&seg, 1),
1817            None,
1818            "empty element must return None"
1819        );
1820        assert_eq!(SegmentAccessor::get_element(&seg, 2), Some("VALUE"));
1821    }
1822
1823    #[test]
1824    fn segment_accessor_get_component_returns_value() {
1825        let owned = parse_one("UNH+1+ORDERS:D:96A:UN'");
1826        let seg = owned.as_borrowed();
1827        assert_eq!(seg.get_component(1, 0), Some("ORDERS"));
1828        assert_eq!(seg.get_component(1, 1), Some("D"));
1829        assert_eq!(seg.get_component(1, 2), Some("96A"));
1830        assert_eq!(seg.get_component(1, 3), Some("UN"));
1831        assert_eq!(
1832            seg.get_component(1, 9),
1833            None,
1834            "out-of-bounds must return None"
1835        );
1836    }
1837
1838    #[test]
1839    fn segment_accessor_text_element_errors_on_missing() {
1840        let owned = parse_one("BGM+'");
1841        let seg = owned.as_borrowed();
1842        // element 0 is empty — text_element must return an error
1843        let err = seg.text_element(0);
1844        assert!(
1845            matches!(err, Err(EdifactError::MissingRequiredElement { ref tag, element_index: 0 }) if tag == "BGM"),
1846            "expected MissingRequiredElement, got: {err:?}"
1847        );
1848    }
1849
1850    #[test]
1851    fn segment_accessor_required_composite_errors_on_missing() {
1852        let owned = parse_one("DTM+137'");
1853        let seg = owned.as_borrowed();
1854        // component 1 of element 0 is absent
1855        let err = seg.required_composite(0, 1);
1856        assert!(
1857            matches!(err, Err(EdifactError::MissingRequiredComponent { ref tag, element_index: 0, component_index: 1 }) if tag == "DTM"),
1858            "expected MissingRequiredComponent, got: {err:?}"
1859        );
1860    }
1861
1862    #[test]
1863    fn segment_accessor_code_element_parses_integer() {
1864        let owned = parse_one("QTY+21:100'");
1865        let seg = owned.as_borrowed();
1866        let qty: u32 = seg.code_element(0).expect("should parse qualifier as u32");
1867        assert_eq!(qty, 21);
1868    }
1869
1870    #[test]
1871    fn segment_accessor_optional_element_absent_returns_none() {
1872        let owned = parse_one("BGM+220'");
1873        let seg = owned.as_borrowed();
1874        assert_eq!(seg.optional_element(5), None);
1875    }
1876}