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;
75//! use std::io::Cursor;
76//!
77//! let input = b"UNA:;.? 'BGM;220;test?;value'";
78//! let segments = from_reader(Cursor::new(&input[..])).unwrap();
79//! assert_eq!(segments.len(), 1);
80//! assert_eq!(segments[0].tag, "BGM");
81//! assert_eq!(segments[0].elements[0].components[0], "220");
82//! assert_eq!(segments[0].elements[1].components[0], "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| {
111//!         let bgm = segments.iter().find(|segment| segment.tag == "BGM")?;
112//!         let document_code = bgm.get_element(0)?.get_component(0)?;
113//!         (document_code == "220").then(|| {
114//!             ValidationIssue::new(
115//!                 ValidationSeverity::Warning,
116//!                 "demo pack rejects BGM 220 for illustration",
117//!             )
118//!             .with_rule_id("DEMO-P001")
119//!             .with_segment("BGM")
120//!             .with_element_index(0)
121//!         })
122//!     });
123//!
124//! let report = ValidationContext::builder()
125//!     .with_profile_pack(pack)
126//!     .build()
127//!     .validate_lenient(&segments);
128//!
129//! assert!(report.has_warnings());
130//! let partner_report = report.filter_by_rule_prefix("DEMO-");
131//! assert!(partner_report.total_issues() >= 1);
132//! # Ok::<(), edifact_rs::EdifactError>(())
133//! ```
134//!
135//! # Async Usage
136//!
137//! `edifact-rs` does not provide a native `async` API.  All parsing is
138//! synchronous and driven by the standard `std::io::Read` / `std::io::BufRead`
139//! traits.  The recommended integration pattern with async runtimes is:
140//!
141//! 1. Use your async runtime's read utilities to read the entire message into a
142//!    `Vec<u8>` (e.g. `tokio::io::AsyncReadExt::read_to_end`).
143//! 2. Parse the in-memory slice with [`from_bytes`].
144//!
145//! ```rust,no_run
146//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
147//! // With tokio:
148//! // let mut buf = Vec::new();
149//! // reader.read_to_end(&mut buf).await?;
150//! // let segments: Vec<_> = edifact_rs::from_bytes(&buf).collect::<Result<_, _>>()?;
151//! # Ok(())
152//! # }
153//! ```
154//!
155//! A native zero-copy streaming async API is tracked as a future roadmap item.
156// ── core modules ──────────────────────────────────────────────────────────────
157pub mod directory_validator;
158pub(crate) mod envelope;
159/// Error types and validation reporting primitives.
160pub(crate) mod error;
161pub mod group;
162/// Core zero-copy and owned EDIFACT data model types.
163pub(crate) mod model;
164pub(crate) mod parser;
165pub(crate) mod tokenizer;
166pub(crate) mod validator;
167pub(crate) mod writer;
168
169// ── typed serialization layer ─────────────────────────────────────────────────
170pub mod de;
171pub(crate) mod event;
172pub mod ser;
173
174// ── flat re-exports: core ─────────────────────────────────────────────────────
175pub use envelope::{
176    InterchangeEnvelope, MessageEnvelope, MessageIdentifier, parse_unh, validate_envelope,
177};
178pub use error::{EdifactError, IoError, ValidationIssue, ValidationReport, ValidationSeverity};
179pub use group::{GroupDef, SegmentGroup, group_segments};
180pub use model::{
181    BorrowedElement, BorrowedSegment, Element, OwnedElement, OwnedSegment, Segment, Span,
182};
183pub use parser::{
184    Parser, ReaderConfig, from_bufread, from_bufread_stream, from_bufread_stream_with_config,
185    from_reader_with_config,
186};
187pub use tokenizer::{ServiceStringAdvice, Tokenizer};
188pub use validator::{
189    ProfileRule, ProfileRulePack, ValidationContext, ValidationContextBuilder, ValidationLayer,
190    ValidationRuleContext, Validator, validate_each,
191};
192pub use writer::Writer;
193
194// ── flat re-exports: serde ────────────────────────────────────────────────────
195
196/// User-facing deserialization API.
197pub use de::{
198    CompositeElement, DispatchedMessage, EdifactCompositeDeserialize, EdifactDeserialize,
199    EdifactSegmentTag, MessageDispatch, MessageWindow, MessageWindowsIter, MessageWindowsSliceIter,
200    OwnedMessageWindow, SegmentAccessor, deserialize, deserialize_all_from_reader,
201    deserialize_all_streaming, deserialize_first_from_reader, deserialize_first_streaming,
202    deserialize_messages_bytes, deserialize_messages_from_reader, deserialize_str,
203    groups_are_contiguous_by_qualifier, message_windows_bytes, message_windows_from_reader,
204};
205
206// ── Proc-macro support ─────────────────────────────────────────────────────────
207// Re-export helpers at root with doc(hidden) for macro-generated code compatibility.
208#[doc(hidden)]
209pub use de::{
210    composite_element, contiguous_groups_by_qualifier, element_str, find_qualified_segment,
211    find_qualified_segment_owned, find_segment, find_segment_owned, find_segment_typed,
212    find_segments_iter, find_segments_typed, get_components_iter, optional_component,
213    optional_element, qualifier_matches_pattern, required_component, required_element,
214};
215pub use directory_validator::{
216    DirectoryValidator, DirectoryValidatorBuilder, ElementRef, OwnedElementRef, OwnedSegmentDef,
217    SegmentDefinition, Status,
218};
219#[cfg(feature = "derive")]
220#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
221pub use edifact_rs_derive::{EdifactDeserialize, EdifactSerialize};
222pub use event::{EdifactEvent, EventEmitter, OwnedEdifactEvent, VecEmitter, WriterEmitter};
223pub use ser::{EdifactCompositeSerialize, EdifactSerialize, to_bytes, to_edifact_string};
224
225// ── core free functions ───────────────────────────────────────────────────────
226
227use std::io::{Read, Write};
228
229/// Iterator returned by [`from_bytes`].
230pub struct FromBytesIter<'a> {
231    parser: Option<parser::Parser<'a>>,
232    pending_error: Option<EdifactError>,
233}
234
235/// Iterator returned by [`from_reader_iter`].
236pub struct FromReaderIter<R: Read> {
237    inner: parser::OwnedSegmentStream<std::io::BufReader<R>>,
238}
239
240impl<R: Read> Iterator for FromReaderIter<R> {
241    type Item = Result<OwnedSegment, EdifactError>;
242
243    fn next(&mut self) -> Option<Self::Item> {
244        self.inner.next()
245    }
246}
247
248impl<'a> Iterator for FromBytesIter<'a> {
249    type Item = Result<Segment<'a>, EdifactError>;
250
251    fn next(&mut self) -> Option<Self::Item> {
252        if let Some(err) = self.pending_error.take() {
253            return Some(Err(err));
254        }
255        self.parser.as_mut()?.next()
256    }
257}
258
259/// Parse `input` bytes into an iterator of [`Segment`]s.
260///
261/// Borrows directly from `input` — zero allocation for segment data.
262///
263/// # Segment-size limit
264///
265/// Applies a default 64 KiB per-segment limit, matching the reader-based path.
266/// Use [`from_bytes_with_config`] to override.
267pub fn from_bytes(input: &[u8]) -> FromBytesIter<'_> {
268    from_bytes_with_config(input, parser::ReaderConfig::default())
269}
270
271/// Parse `input` bytes into an iterator of [`Segment`]s with explicit configuration.
272///
273/// The `config.max_segment_bytes` limit is enforced by the tokenizer, returning
274/// [`EdifactError::SegmentTooLong`] if a single segment exceeds the threshold.
275/// Pass `ReaderConfig::default().max_segment_bytes(usize::MAX)` to disable the limit.
276///
277/// # Example
278///
279/// ```
280/// use edifact_rs::{ReaderConfig, from_bytes_with_config};
281///
282/// let cfg = ReaderConfig::default().max_segment_bytes(128);
283/// let result: Result<Vec<_>, _> = from_bytes_with_config(b"BGM+220+1+9'", cfg).collect();
284/// assert!(result.is_ok());
285/// ```
286pub fn from_bytes_with_config<'a>(
287    input: &'a [u8],
288    config: parser::ReaderConfig,
289) -> FromBytesIter<'a> {
290    match tokenizer::ServiceStringAdvice::from_bytes_strict(input) {
291        Ok(ssa) => {
292            let t = tokenizer::Tokenizer::with_limit(input, ssa, config.max_segment_bytes);
293            FromBytesIter {
294                parser: Some(parser::Parser::new(t)),
295                pending_error: None,
296            }
297        }
298        Err(error) => FromBytesIter {
299            parser: None,
300            pending_error: Some(error),
301        },
302    }
303}
304
305/// Parse a reader into owned segments.
306///
307/// # Errors
308///
309/// Returns an error if the input contains malformed EDIFACT syntax,
310/// invalid UTF-8 segment text, dangling release sequences, or underlying I/O failures.
311pub fn from_reader<R: Read>(reader: R) -> Result<Vec<OwnedSegment>, EdifactError> {
312    parser::from_reader(reader)
313}
314
315/// Parse a reader into owned segments as a streaming iterator.
316///
317/// This keeps memory bounded by yielding segments incrementally instead of
318/// materializing the full interchange up front.
319pub fn from_reader_iter<R: Read>(reader: R) -> FromReaderIter<R> {
320    FromReaderIter {
321        inner: parser::from_reader_stream(reader),
322    }
323}
324
325/// Serialize `segments` to an [`std::io::Write`] implementation.
326///
327/// # Errors
328///
329/// Returns an error if writing fails or if segment serialization fails.
330pub fn to_writer<'a, 'b, W, I>(w: W, segments: I) -> Result<(), EdifactError>
331where
332    'b: 'a,
333    W: Write,
334    I: IntoIterator<Item = &'a Segment<'b>>,
335{
336    let mut wr = writer::Writer::new(w);
337    for seg in segments {
338        wr.write_segment(seg)?;
339    }
340    wr.finish().map(|_| ())
341}
342
343/// Serialize `segments` to an owned `Vec<u8>`.
344///
345/// # Errors
346///
347/// Returns an error if serialization fails.
348pub fn segments_to_bytes<'a, 'b, I>(segments: I) -> Result<Vec<u8>, EdifactError>
349where
350    'b: 'a,
351    I: IntoIterator<Item = &'a Segment<'b>>,
352{
353    let mut buf = Vec::new();
354    to_writer(&mut buf, segments)?;
355    Ok(buf)
356}
357
358#[cfg(test)]
359mod tests {
360    use super::*;
361
362    #[test]
363    fn from_bytes_rejects_invalid_una() {
364        let err = from_bytes(b"UNA::.? 'BGM:220'")
365            .collect::<Result<Vec<_>, _>>()
366            .expect_err("invalid UNA should fail slice parsing");
367        assert!(matches!(err, EdifactError::InvalidUna));
368    }
369}