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;
161/// Core zero-copy and owned EDIFACT data model types.
162pub(crate) mod model;
163pub(crate) mod parser;
164pub(crate) mod tokenizer;
165pub(crate) mod validator;
166pub(crate) mod writer;
167pub mod group;
168
169// ── typed serialization layer ─────────────────────────────────────────────────
170pub mod de;
171pub(crate) mod event;
172pub mod ser;
173
174// ── flat re-exports: core ─────────────────────────────────────────────────────
175pub use envelope::{validate_envelope, InterchangeEnvelope, MessageEnvelope, MessageIdentifier, parse_unh};
176pub use error::{EdifactError, IoError, ValidationIssue, ValidationReport, ValidationSeverity};
177pub use model::{BorrowedElement, BorrowedSegment, Element, OwnedElement, OwnedSegment, Segment, Span};
178pub use parser::{
179 Parser, ReaderConfig, from_bufread, from_bufread_stream, from_bufread_stream_with_config,
180 from_reader_with_config,
181};
182pub use tokenizer::{ServiceStringAdvice, Tokenizer};
183pub use validator::{
184 ProfileRule, ProfileRulePack, ValidationContext, ValidationContextBuilder, ValidationLayer,
185 ValidationRuleContext, Validator, validate_each,
186};
187pub use writer::Writer;
188pub use group::{GroupDef, SegmentGroup, group_segments};
189
190// ── flat re-exports: serde ────────────────────────────────────────────────────
191
192/// User-facing deserialization API.
193pub use de::{
194 CompositeElement, EdifactCompositeDeserialize, EdifactDeserialize, EdifactSegmentTag,
195 MessageWindowsIter, MessageWindowsSliceIter, SegmentAccessor,
196 deserialize, deserialize_all_from_reader,
197 deserialize_all_streaming, deserialize_first_from_reader, deserialize_first_streaming,
198 deserialize_messages_bytes, deserialize_messages_from_reader, deserialize_str,
199 groups_are_contiguous_by_qualifier,
200 message_windows_bytes, message_windows_from_reader,
201 message_type_from_window, MessageDispatch, DispatchedMessage,
202};
203
204// ── Proc-macro support ─────────────────────────────────────────────────────────
205// Re-export helpers at root with doc(hidden) for macro-generated code compatibility.
206#[doc(hidden)]
207pub use de::{
208 composite_element, contiguous_groups_by_qualifier, element_str,
209 find_qualified_segment, find_qualified_segment_owned, find_segment, find_segment_owned,
210 find_segment_typed, find_segments_iter, find_segments_typed, get_components_iter,
211 optional_component, optional_element, qualifier_matches_pattern,
212 required_component, required_element,
213};
214#[cfg(feature = "derive")]
215#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
216pub use edifact_rs_derive::{EdifactDeserialize, EdifactSerialize};
217pub use event::{EdifactEvent, EventEmitter, OwnedEdifactEvent, VecEmitter, WriterEmitter};
218pub use ser::{EdifactCompositeSerialize, EdifactSerialize, to_bytes, to_edifact_string};
219pub use directory_validator::{DirectoryValidator, ElementRef, SegmentDefinition, Status};
220
221// ── core free functions ───────────────────────────────────────────────────────
222
223use std::io::{Read, Write};
224
225/// Iterator returned by [`from_bytes`].
226pub struct FromBytesIter<'a> {
227 parser: Option<parser::Parser<'a>>,
228 pending_error: Option<EdifactError>,
229}
230
231/// Iterator returned by [`from_reader_iter`].
232pub struct FromReaderIter<R: Read> {
233 inner: parser::OwnedSegmentStream<std::io::BufReader<R>>,
234}
235
236impl<R: Read> Iterator for FromReaderIter<R> {
237 type Item = Result<OwnedSegment, EdifactError>;
238
239 fn next(&mut self) -> Option<Self::Item> {
240 self.inner.next()
241 }
242}
243
244impl<'a> Iterator for FromBytesIter<'a> {
245 type Item = Result<Segment<'a>, EdifactError>;
246
247 fn next(&mut self) -> Option<Self::Item> {
248 if let Some(err) = self.pending_error.take() {
249 return Some(Err(err));
250 }
251 self.parser.as_mut()?.next()
252 }
253}
254
255/// Parse `input` bytes into an iterator of [`Segment`]s.
256///
257/// Borrows directly from `input` — zero allocation for segment data.
258///
259/// # Segment-size limit
260///
261/// Applies a default 64 KiB per-segment limit, matching the reader-based path.
262/// Use [`from_bytes_with_config`] to override.
263pub fn from_bytes(input: &[u8]) -> FromBytesIter<'_> {
264 from_bytes_with_config(input, parser::ReaderConfig::default())
265}
266
267/// Parse `input` bytes into an iterator of [`Segment`]s with explicit configuration.
268///
269/// The `config.max_segment_bytes` limit is enforced by the tokenizer, returning
270/// [`EdifactError::SegmentTooLong`] if a single segment exceeds the threshold.
271/// Pass `ReaderConfig::default().max_segment_bytes(usize::MAX)` to disable the limit.
272///
273/// # Example
274///
275/// ```
276/// use edifact_rs::{ReaderConfig, from_bytes_with_config};
277///
278/// let cfg = ReaderConfig::default().max_segment_bytes(128);
279/// let result: Result<Vec<_>, _> = from_bytes_with_config(b"BGM+220+1+9'", cfg).collect();
280/// assert!(result.is_ok());
281/// ```
282pub fn from_bytes_with_config<'a>(input: &'a [u8], config: parser::ReaderConfig) -> FromBytesIter<'a> {
283 match tokenizer::ServiceStringAdvice::from_bytes_strict(input) {
284 Ok(ssa) => {
285 let t = tokenizer::Tokenizer::with_limit(input, ssa, config.max_segment_bytes);
286 FromBytesIter {
287 parser: Some(parser::Parser::new(t)),
288 pending_error: None,
289 }
290 }
291 Err(error) => FromBytesIter {
292 parser: None,
293 pending_error: Some(error),
294 },
295 }
296}
297
298/// Parse a reader into owned segments.
299///
300/// # Errors
301///
302/// Returns an error if the input contains malformed EDIFACT syntax,
303/// invalid UTF-8 segment text, dangling release sequences, or underlying I/O failures.
304pub fn from_reader<R: Read>(reader: R) -> Result<Vec<OwnedSegment>, EdifactError> {
305 parser::from_reader(reader)
306}
307
308/// Parse a reader into owned segments as a streaming iterator.
309///
310/// This keeps memory bounded by yielding segments incrementally instead of
311/// materializing the full interchange up front.
312pub fn from_reader_iter<R: Read>(reader: R) -> FromReaderIter<R> {
313 FromReaderIter {
314 inner: parser::from_reader_stream(reader),
315 }
316}
317
318/// Serialize `segments` to an [`std::io::Write`] implementation.
319///
320/// # Errors
321///
322/// Returns an error if writing fails or if segment serialization fails.
323pub fn to_writer<'a, 'b, W, I>(w: W, segments: I) -> Result<(), EdifactError>
324where
325 'b: 'a,
326 W: Write,
327 I: IntoIterator<Item = &'a Segment<'b>>,
328{
329 let mut wr = writer::Writer::new(w);
330 for seg in segments {
331 wr.write_segment(seg)?;
332 }
333 wr.finish().map(|_| ())
334}
335
336/// Serialize `segments` to an owned `Vec<u8>`.
337///
338/// # Errors
339///
340/// Returns an error if serialization fails.
341pub fn segments_to_bytes<'a, 'b, I>(segments: I) -> Result<Vec<u8>, EdifactError>
342where
343 'b: 'a,
344 I: IntoIterator<Item = &'a Segment<'b>>,
345{
346 let mut buf = Vec::new();
347 to_writer(&mut buf, segments)?;
348 Ok(buf)
349}
350
351#[cfg(test)]
352mod tests {
353 use super::*;
354
355 #[test]
356 fn from_bytes_rejects_invalid_una() {
357 let err = from_bytes(b"UNA::.? 'BGM:220'")
358 .collect::<Result<Vec<_>, _>>()
359 .expect_err("invalid UNA should fail slice parsing");
360 assert!(matches!(err, EdifactError::InvalidUna));
361 }
362}