Skip to main content

edifact_rs/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3//! `edifact-rs` — zero-copy EDIFACT tokenizer, parser, writer, serde traits,
4//! validation engine, and extensible directory support.
5//!
6//! `edifact-rs` is the main entry point of this workspace. The core parsing,
7//! writing, and validation infrastructure is always available. Custom directory
8//! validators can be implemented by downstream crates or generated through
9//! external build tooling.
10//!
11//! # Quick start
12//! ```
13//! use edifact_rs::from_bytes;
14//! let input = b"UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'UNZ+0+1'";
15//! let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>().unwrap();
16//! assert_eq!(segments[0].tag, "UNB");
17//! ```
18//!
19//! # Crate features
20//!
21//! - `derive` (enabled by default): re-exports the derive macros from
22//!   `edifact-rs-derive`.
23//! - `diagnostics` (disabled by default): enables rich diagnostic output via `miette`.
24//!   When enabled, errors implement `miette::Diagnostic` for enhanced error reporting.
25//!   This feature adds an optional dependency and has no impact on parsing performance.
26//!
27//! The crate is expected to compile both with defaults and with
28//! `--no-default-features` for consumers who only want the core parsing and
29//! writing functionality.
30//!
31//! ## Feature matrix workflows
32//!
33//! - default features:
34//!   `cargo test -p edifact-rs`
35//! - no default features:
36//!   `cargo test -p edifact-rs --no-default-features`
37//! - all features:
38//!   `cargo test -p edifact-rs --all-features`
39//!
40//! # Diagnostic Feature
41//!
42//! When the `diagnostics` feature is enabled, [`EdifactError`] gains additional
43//! traits and methods that enable rich, human-readable error output:
44//!
45//! ```text
46//! Error: invalid delimiter byte 0xAB at offset 42
47//!
48//!  ╭─ input.edi:2:3
49//!  │
50//!  2 │ UNB+UNOA:1+....[invalid]...
51//!  │         ^^^ invalid byte here
52//!  │
53//! Error Code: E002
54//! Help: The byte 0xAB is not a valid delimiter. Check UNA configuration
55//! ```
56//!
57//! This feature is useful for CLI tools and error reporting, but is not required
58//! for applications that handle errors programmatically.
59//!
60//! # Parse And Text Contracts
61//!
62//! Parsing in `edifact-rs` is strict and deterministic:
63//!
64//! - Segment and element text must decode as UTF-8 (`E003` on failure).
65//! - Release characters must escape exactly one following byte.
66//!   A trailing `?` at end-of-input is rejected (`E019`).
67//! - Malformed delimiters and truncated segments are reported with stable
68//!   error codes rather than panicking.
69//!
70//! These contracts apply to both slice-based parsing (`from_bytes`) and
71//! reader-based parsing (`from_reader`).
72//!
73//! ```
74//! use edifact_rs::from_reader_collect;
75//! use std::io::Cursor;
76//!
77//! let input = b"UNA:;.? 'BGM;220;test?;value'";
78//! let segments = from_reader_collect(Cursor::new(&input[..])).unwrap();
79//! assert_eq!(segments.len(), 1);
80//! assert_eq!(segments[0].tag, "BGM");
81//! assert_eq!(segments[0].element_str(0), Some("220"));
82//! assert_eq!(segments[0].element_str(1), Some("test;value"));
83//! ```
84//!
85//! # Validation Quick Start
86//!
87//! The `Validator` trait and `ValidationContext` provide a flexible framework
88//! for building custom validators. Users can generate validators from official
89//! UNECE sources or implement their own.
90//!
91//! See the [`Validator`] trait documentation and the `cookbook_fixture_validation.rs`
92//! example for details on creating custom validators.
93//!
94//! # Custom Profile Packs
95//!
96//! `ProfileRulePack` is the extension point for downstream MIG/profile crates.
97//! Packs can be authored with public APIs only and plugged into a
98//! [`ValidationContext`]:
99//!
100//! ```
101//! use edifact_rs::{
102//!     from_bytes, ProfileRulePack, ValidationContext, ValidationIssue, ValidationSeverity,
103//! };
104//!
105//! let segments: Vec<_> = from_bytes(b"UNH+1+ORDERS:D:96A:UN'BGM+220+PO123+9'UNT+3+1'")
106//!     .collect::<Result<_, _>>()?;
107//!
108//! let pack = ProfileRulePack::new("ORDERS-DEMO")
109//!     .for_message_type("ORDERS")
110//!     .with_stateless_rule_fn(|segments, issues| {
111//!         if let Some(bgm) = segments.iter().find(|segment| segment.tag == "BGM") {
112//!             if let Some(code) = bgm.get_element(0).and_then(|e| e.get_component(0)) {
113//!                 if code == "220" {
114//!                     issues.push(
115//!                         ValidationIssue::new(
116//!                             ValidationSeverity::Warning,
117//!                             "demo pack rejects BGM 220 for illustration",
118//!                         )
119//!                         .with_rule_id("DEMO-P001")
120//!                         .with_segment("BGM")
121//!                         .with_element_index(0),
122//!                     );
123//!                 }
124//!             }
125//!         }
126//!     });
127//!
128//! let report = ValidationContext::builder()
129//!     .with_profile_pack(pack)
130//!     .build()
131//!     .validate_lenient(&segments);
132//!
133//! assert!(report.has_warnings());
134//! let partner_report = report.filter_by_rule_prefix("DEMO-");
135//! assert!(partner_report.total_issues() >= 1);
136//! # Ok::<(), edifact_rs::EdifactError>(())
137//! ```
138//!
139//! # Async Usage
140//!
141//! `edifact-rs` does not provide a native `async` API.  All parsing is
142//! synchronous and driven by the standard `std::io::Read` / `std::io::BufRead`
143//! traits.  The recommended integration pattern with async runtimes is:
144//!
145//! 1. Use your async runtime's read utilities to read the entire message into a
146//!    `Vec<u8>` (e.g. `tokio::io::AsyncReadExt::read_to_end`).
147//! 2. Parse the in-memory slice with [`from_bytes`].
148//!
149//! ```rust,no_run
150//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
151//! // With tokio:
152//! // let mut buf = Vec::new();
153//! // reader.read_to_end(&mut buf).await?;
154//! // let segments: Vec<_> = edifact_rs::from_bytes(&buf).collect::<Result<_, _>>()?;
155//! # Ok(())
156//! # }
157//! ```
158//!
159//! A native zero-copy streaming async API is tracked as a future roadmap item.
160// ── core modules ──────────────────────────────────────────────────────────────
161pub mod directory_validator;
162pub(crate) mod envelope;
163/// Error types and validation reporting primitives.
164pub(crate) mod error;
165pub mod group;
166/// Core zero-copy and owned EDIFACT data model types.
167pub(crate) mod model;
168pub(crate) mod parser;
169pub(crate) mod tokenizer;
170pub(crate) mod validator;
171pub(crate) mod writer;
172
173// ── typed serialization layer ─────────────────────────────────────────────────
174pub mod de;
175pub(crate) mod event;
176pub mod ser;
177
178// ── flat re-exports: core ─────────────────────────────────────────────────────
179pub use envelope::{
180    InterchangeEnvelope, MessageEnvelope, MessageIdentifier, parse_unh, validate_envelope,
181    validate_envelope_from_owned, validate_envelope_lenient, validate_envelope_lenient_from_owned,
182};
183pub use error::{EdifactError, IoError, ValidationIssue, ValidationReport, ValidationSeverity};
184pub use group::{
185    GroupDef, SegmentGroup, SegmentGroupIndexed, group_owned_segments,
186    group_owned_segments_indexed, group_segments, group_segments_indexed,
187};
188pub use model::{
189    BorrowedElement, BorrowedSegment, Element, OwnedElement, OwnedSegment, Segment, Span,
190};
191pub use parser::{
192    Parser, ReaderConfig, from_bufread, from_bufread_stream, from_bufread_stream_with_config,
193    from_reader_with_config,
194};
195pub use tokenizer::{ServiceStringAdvice, Tokenizer};
196pub use validator::{
197    EnvelopeValidator, ProfileRule, ProfileRulePack, ValidationContext, ValidationContextBuilder,
198    ValidationLayer, ValidationRuleContext, Validator, validate_each,
199};
200pub use writer::Writer;
201
202// ── flat re-exports: serde ────────────────────────────────────────────────────
203
204/// User-facing deserialization API.
205pub use de::{
206    CompositeElement, DispatchedMessage, EdifactCompositeDeserialize, EdifactDeserialize,
207    EdifactSegmentTag, MessageDispatch, MessageWindow, MessageWindowsIter, MessageWindowsSliceIter,
208    OwnedMessageWindow, SegmentAccessor, deserialize, deserialize_all_from_reader,
209    deserialize_all_streaming, deserialize_first_from_reader, deserialize_first_streaming,
210    deserialize_messages_bytes, deserialize_messages_from_reader, deserialize_str, element_str,
211    find_qualified_segment, find_segment, groups_are_contiguous_by_qualifier,
212    message_windows_from_reader, optional_element, required_element,
213};
214
215/// Splits a byte slice into [`MessageWindow`] views, one per `UNH`/`UNT` envelope,
216/// enabling parallel or lazy per-message processing without copying data.
217///
218/// # Example
219/// ```rust,ignore
220/// use edifact_rs::from_bytes_windows;
221/// let windows: Vec<_> = from_bytes_windows(input).collect();
222/// ```
223pub use de::message_windows_bytes as from_bytes_windows;
224
225// ── Proc-macro support ─────────────────────────────────────────────────────────
226
227/// Segment-navigation helpers for working with parsed EDIFACT segments.
228///
229/// These functions cover the most common patterns when extracting data from
230/// a parsed `&[Segment<'_>]` or `&[OwnedSegment]` slice.
231///
232/// ## Segment lookup
233///
234/// - [`find_segment`] — locate the first segment with a given tag.
235/// - [`find_qualified_segment`] — locate a segment by tag *and* qualifier (element 0).
236/// - [`helpers::find_qualified_segment_owned`] — owned-segment variant.
237/// - [`helpers::find_segment_owned`] — owned-segment variant of `find_segment`.
238/// - [`helpers::find_segment_typed`] — find a segment matching an `EdifactSegmentTag` implementor.
239/// - [`helpers::find_segments_typed`] — iterate all segments matching a tag type.
240/// - [`helpers::find_segments_iter`] — iterate all segments matching a tag string.
241///
242/// ## Element and component access
243///
244/// - [`element_str`] — extract the raw string value of an element.
245/// - [`required_element`] — extract a mandatory element, returning an error when absent.
246/// - [`optional_element`] — extract an optional element as `Option<&str>`.
247/// - [`helpers::required_component`] — extract a mandatory component within a composite element.
248/// - [`helpers::optional_component`] — extract an optional component within a composite element.
249/// - [`helpers::get_components_iter`] — iterate over the components of a composite element.
250/// - [`helpers::composite_element`] — retrieve a composite element as a [`crate::CompositeElement`].
251///
252/// ## Pattern matching
253///
254/// - [`helpers::qualifier_matches_pattern`] — test whether a qualifier value matches a
255///   wildcard pattern (e.g. `"E01*"` matches `"E010"`, `"E011"`, …).
256///
257/// ## Groups
258///
259/// - [`helpers::contiguous_groups_by_qualifier`] — collect contiguous groups of segments
260///   sharing the same qualifier value into a `Vec<Vec<…>>`.
261///
262/// # Example
263///
264/// ```rust,ignore
265/// use edifact_rs::helpers::{find_segment, required_element};
266///
267/// let bgm = find_segment(segments, "BGM").ok_or(/* … */)?;
268/// let doc_code = required_element(bgm, 0)?;
269/// ```
270pub mod helpers {
271    pub use crate::de::{
272        composite_element, contiguous_groups_by_qualifier, element_str, find_qualified_segment,
273        find_qualified_segment_owned, find_segment, find_segment_owned, find_segment_typed,
274        find_segments_iter, find_segments_typed, get_components_iter, optional_component,
275        optional_element, qualifier_matches_pattern, required_component, required_element,
276    };
277}
278pub use directory_validator::{
279    DirectoryValidator, DirectoryValidatorBuilder, ElementRef, OwnedElementRef, OwnedSegmentDef,
280    SegmentDefinition, Status,
281};
282#[cfg(feature = "derive")]
283#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
284pub use edifact_rs_derive::{EdifactDeserialize, EdifactSerialize};
285pub use event::{EdifactEvent, EventEmitter, OwnedEdifactEvent, VecEmitter, WriterEmitter};
286pub use ser::{
287    DecimalFloat, DecimalFloatDisplay, EdifactCompositeSerialize, EdifactSerialize, to_bytes,
288    to_edifact_string,
289};
290
291// ── core free functions ───────────────────────────────────────────────────────
292
293use std::io::{Read, Write};
294
295/// Iterator returned by [`from_bytes`].
296pub struct FromBytesIter<'a> {
297    parser: Option<parser::Parser<'a>>,
298    pending_error: Option<EdifactError>,
299    /// Remaining segment allowance (`None` = unlimited).
300    segments_remaining: Option<usize>,
301    /// Maximum byte budget (`None` = unlimited).
302    bytes_remaining: Option<u64>,
303    /// Byte offset of the start of the current parse position (approximated
304    /// as the sum of previously yielded segment spans — the borrowed tokenizer
305    /// does not expose a byte counter, so we track it from `Segment::span`).
306    bytes_consumed: u64,
307}
308
309/// Iterator returned by [`from_reader`].
310pub struct FromReaderIter<R: Read> {
311    inner: parser::OwnedSegmentStream<std::io::BufReader<R>>,
312}
313
314impl<R: Read> Iterator for FromReaderIter<R> {
315    type Item = Result<OwnedSegment, EdifactError>;
316
317    fn next(&mut self) -> Option<Self::Item> {
318        self.inner.next()
319    }
320}
321
322impl<'a> Iterator for FromBytesIter<'a> {
323    type Item = Result<Segment<'a>, EdifactError>;
324
325    fn next(&mut self) -> Option<Self::Item> {
326        if let Some(err) = self.pending_error.take() {
327            return Some(Err(err));
328        }
329        // max_segments guard
330        if let Some(ref mut remaining) = self.segments_remaining {
331            if *remaining == 0 {
332                self.parser = None;
333                return None;
334            }
335        }
336        // max_input_bytes guard — uses absolute byte offset from the input start.
337        // `bytes_consumed` holds `seg.span.end` of the last yielded segment, which
338        // is an absolute position in the input slice and therefore naturally includes
339        // the 9-byte UNA header and the segment terminator character.
340        if let Some(max) = self.bytes_remaining {
341            if self.bytes_consumed >= max {
342                self.parser = None;
343                return None;
344            }
345        }
346        let item = self.parser.as_mut()?.next();
347        if let Some(Ok(ref seg)) = item {
348            // Decrement segment allowance
349            if let Some(ref mut remaining) = self.segments_remaining {
350                *remaining = remaining.saturating_sub(1);
351            }
352            // Track the absolute input position at the end of this segment.
353            // `seg.span.end` is the byte offset just past the segment terminator —
354            // a monotonically increasing absolute cursor that automatically accounts
355            // for the UNA header, element/component separators, and terminators.
356            self.bytes_consumed = seg.span.end as u64;
357            if let Some(max) = self.bytes_remaining {
358                if self.bytes_consumed >= max {
359                    self.parser = None;
360                }
361            }
362        }
363        item
364    }
365}
366
367/// Parse `input` bytes into an iterator of [`Segment`]s.
368///
369/// Borrows directly from `input` — zero allocation for segment data.
370///
371/// # Segment-size limit
372///
373/// Applies a default 64 KiB per-segment limit, matching the reader-based path.
374/// Use [`from_bytes_with_config`] to override.
375pub fn from_bytes(input: &[u8]) -> FromBytesIter<'_> {
376    from_bytes_with_config(input, parser::ReaderConfig::default())
377}
378
379/// Parse `input` bytes into an iterator of [`Segment`]s with explicit configuration.
380///
381/// All three [`ReaderConfig`] limits are enforced:
382/// - `max_segment_bytes`: returns [`EdifactError::SegmentTooLong`] if a single segment
383///   exceeds the threshold.
384/// - `max_segments`: stops the iterator after this many segments have been yielded.
385/// - `max_input_bytes`: stops the iterator once this many bytes have been consumed.
386///   The byte count uses the absolute input position (i.e. `Segment::span.end` after
387///   each segment is yielded), so it correctly accounts for the 9-byte UNA header and
388///   segment terminators.  The last segment whose end position exceeds the limit is still
389///   returned; processing stops before fetching the next one.
390///
391/// Pass `ReaderConfig::default()` to use the default 64 KiB per-segment limit with
392/// no segment-count or byte-budget cap.
393///
394/// # Example
395///
396/// ```
397/// use edifact_rs::{ReaderConfig, from_bytes_with_config};
398///
399/// let cfg = ReaderConfig::default().max_segment_bytes(128);
400/// let result: Result<Vec<_>, _> = from_bytes_with_config(b"BGM+220+1+9'", cfg).collect();
401/// assert!(result.is_ok());
402/// ```
403pub fn from_bytes_with_config<'a>(
404    input: &'a [u8],
405    config: parser::ReaderConfig,
406) -> FromBytesIter<'a> {
407    let segments_remaining = config.max_segments;
408    let bytes_remaining = config.max_input_bytes;
409    match tokenizer::ServiceStringAdvice::from_bytes_strict(input) {
410        Ok(ssa) => {
411            let t = tokenizer::Tokenizer::with_limit(input, ssa, config.max_segment_bytes);
412            FromBytesIter {
413                parser: Some(parser::Parser::new(t)),
414                pending_error: None,
415                segments_remaining,
416                bytes_remaining,
417                bytes_consumed: 0,
418            }
419        }
420        Err(error) => FromBytesIter {
421            parser: None,
422            pending_error: Some(error),
423            segments_remaining,
424            bytes_remaining,
425            bytes_consumed: 0,
426        },
427    }
428}
429
430/// Parse a reader into a lazy iterator of [`OwnedSegment`]s.
431///
432/// Returns a [`FromReaderIter`] that parses and yields segments on demand,
433/// keeping memory bounded. Use [`from_reader_collect`] to eagerly materialise
434/// all segments into a `Vec`.
435///
436/// # Errors
437///
438/// Each `next()` call yields `Some(Ok(segment))` for a successfully parsed
439/// segment, `Some(Err(EdifactError))` for a parse or I/O failure, and `None`
440/// when the end of the stream has been reached.
441pub fn from_reader<R: Read>(reader: R) -> FromReaderIter<R> {
442    FromReaderIter {
443        inner: parser::from_reader_stream(reader),
444    }
445}
446
447/// Parse a reader into an owned `Vec` of all segments.
448///
449/// Eagerly collects the full interchange into memory. If you only need a
450/// subset of segments, prefer [`from_reader`] (lazy iterator) to avoid
451/// unnecessary allocations.
452///
453/// # Errors
454///
455/// Returns an error if the input contains malformed EDIFACT syntax,
456/// invalid UTF-8 segment text, dangling release sequences, or underlying I/O failures.
457pub fn from_reader_collect<R: Read>(reader: R) -> Result<Vec<OwnedSegment>, EdifactError> {
458    parser::from_reader(reader)
459}
460
461/// Parse `input` bytes eagerly into an iterator of [`OwnedSegment`]s.
462///
463/// Unlike [`from_bytes`] (which yields borrowed [`Segment`]s tied to the input
464/// lifetime), every segment returned here is fully owned.  This is convenient
465/// when you need to store or return segments without retaining a reference to
466/// the original byte slice.
467///
468/// # Example
469///
470/// ```
471/// let segs: Vec<edifact_rs::OwnedSegment> = edifact_rs::from_bytes_owned(b"BGM+220+1+9'")
472///     .collect::<Result<_, _>>()
473///     .unwrap();
474/// assert_eq!(segs[0].tag, "BGM");
475/// ```
476pub fn from_bytes_owned(
477    input: &[u8],
478) -> impl Iterator<Item = Result<OwnedSegment, EdifactError>> + '_ {
479    from_bytes(input).map(|r| r.map(OwnedSegment::from))
480}
481
482/// Parse `input` bytes eagerly into an iterator of [`OwnedSegment`]s with a
483/// custom [`ReaderConfig`].
484///
485/// Identical to [`from_bytes_owned`] but applies the limits and settings from
486/// `config` (e.g. `max_segment_bytes`, `max_segments`, `max_input_bytes`).
487///
488/// # Example
489///
490/// ```
491/// use edifact_rs::ReaderConfig;
492/// let config = ReaderConfig::default().max_segments(10);
493/// let segs: Vec<edifact_rs::OwnedSegment> = edifact_rs::from_bytes_owned_with_config(
494///     b"BGM+220+1+9'",
495///     config,
496/// )
497/// .collect::<Result<_, _>>()
498/// .unwrap();
499/// assert_eq!(segs[0].tag, "BGM");
500/// ```
501pub fn from_bytes_owned_with_config(
502    input: &[u8],
503    config: ReaderConfig,
504) -> impl Iterator<Item = Result<OwnedSegment, EdifactError>> + '_ {
505    from_bytes_with_config(input, config).map(|r| r.map(OwnedSegment::from))
506}
507
508/// Parse a reader into owned segments as a streaming iterator.
509///
510/// This keeps memory bounded by yielding segments incrementally instead of
511/// materializing the full interchange up front.
512///
513/// # Deprecation
514///
515/// Use [`from_reader`] instead — this function is an alias kept for internal use.
516pub(crate) fn from_reader_iter<R: Read>(reader: R) -> FromReaderIter<R> {
517    FromReaderIter {
518        inner: parser::from_reader_stream(reader),
519    }
520}
521
522/// Serialize `segments` to an [`std::io::Write`] implementation.
523///
524/// # Errors
525///
526/// Returns an error if writing fails or if segment serialization fails.
527pub fn to_writer<'a, 'b, W, I>(w: W, segments: I) -> Result<(), EdifactError>
528where
529    'b: 'a,
530    W: Write,
531    I: IntoIterator<Item = &'a Segment<'b>>,
532{
533    let mut wr = writer::Writer::new(w);
534    for seg in segments {
535        wr.write_segment(seg)?;
536    }
537    wr.finish().map(|_| ())
538}
539
540/// Serialize `segments` to an owned `Vec<u8>`.
541///
542/// # Errors
543///
544/// Returns an error if serialization fails.
545pub fn segments_to_bytes<'a, 'b, I>(segments: I) -> Result<Vec<u8>, EdifactError>
546where
547    'b: 'a,
548    I: IntoIterator<Item = &'a Segment<'b>>,
549{
550    let mut buf = Vec::new();
551    to_writer(&mut buf, segments)?;
552    Ok(buf)
553}
554
555/// Serialize a slice of [`OwnedSegment`]s to an owned `Vec<u8>`.
556///
557/// Convenience wrapper around [`to_writer`] that accepts owned segments
558/// directly.  Each segment is converted to its borrowed form on the fly
559/// and written immediately — no intermediate `Vec<Segment<'_>>` is
560/// allocated, so peak memory stays proportional to one segment at a time
561/// rather than the full slice.
562///
563/// # Errors
564///
565/// Returns an error if serialization fails.
566pub fn segments_to_bytes_owned(segments: &[OwnedSegment]) -> Result<Vec<u8>, EdifactError> {
567    let mut buf = Vec::new();
568    let mut wr = writer::Writer::new(&mut buf);
569    for seg in segments {
570        wr.write_segment(&seg.as_borrowed())?;
571    }
572    wr.finish()?;
573    Ok(buf)
574}
575
576/// Validate the envelope structure of an owned-segment slice.
577///
578/// Convenience wrapper that accepts `&[OwnedSegment]` without requiring a
579/// manual conversion to borrowed segments.  Unlike the previous implementation,
580/// no intermediate `Vec<Segment<'_>>` is allocated — segments are read directly.
581///
582/// # Errors
583///
584/// Returns an error if the envelope is structurally invalid.
585pub fn validate_envelope_owned(segments: &[OwnedSegment]) -> Result<(), EdifactError> {
586    envelope::validate_envelope_from_owned(segments).map(|_| ())
587}
588
589/// Lenient envelope validation over owned segments — collects all errors.
590///
591/// Convenience wrapper around [`validate_envelope_lenient_from_owned`] that accepts
592/// `&[OwnedSegment]` directly.  Returns an empty `Vec` when the envelope is valid.
593pub fn validate_envelope_lenient_owned(segments: &[OwnedSegment]) -> Vec<EdifactError> {
594    envelope::validate_envelope_lenient_from_owned(segments)
595}
596
597#[cfg(test)]
598mod tests {
599    use super::*;
600
601    #[test]
602    fn from_bytes_rejects_invalid_una() {
603        let err = from_bytes(b"UNA::.? 'BGM:220'")
604            .collect::<Result<Vec<_>, _>>()
605            .expect_err("invalid UNA should fail slice parsing");
606        assert!(matches!(err, EdifactError::InvalidUna));
607    }
608}