ISO 9735 · Rust
EDIFACT for Rust that refuses to guess.
Zero-copy parsing, typed derive macros, layered validation and a writer that computes its own envelope. Malformed input is an error, never a plausible-looking wrong value.
cargo add edifact
Parse, address, validate
Borrow straight from the bytes
use edifact::from_bytes;
let input = b"UNA:+.? 'BGM+220+PO-4711+9'";
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>()?;
assert_eq!(segments[0].tag, "BGM");
assert_eq!(segments[0].element_str(1), Some("PO-4711"));
Address fields by identifier, not index
// A stale or mistyped identifier is a compile error,
// not a quiet read of the neighbouring element.
#[derive(EdifactDeserialize)]
#[edifact(segment = "NAD", layout = NAD_DEF)]
struct Nad {
#[edifact(element = "3035")] qualifier: String,
#[edifact(element = "3039")] party_id: String,
}
Why edifact
-
Refuses to guess
The slice and reader paths return the same result for every input, however it is chunked. An unknown repertoire, syntax version or service segment is an error with a stable code.
-
Bounded by default
64 KiB per segment, 16 MiB per message, 256 MiB per input, with no configuration. A limit raises
E036; it never ends iteration quietly. -
Interchanges built, not assembled
A typed
InterchangeHeaderis parsed and written.InterchangeBuilder,MessageBuilderand theWritercomputeUNTandUNZ. -
An honest writer
A message dropped without
finish()gets noUNT. After a failed write the writer is poisoned; a refusal writes nothing. -
Dates by format code
The
dtmfeature decodes, compares and buildsDTMvalues for 17 UNCL 2379 codes;dtm-timeconverts totime. -
Identifiers over indices
Address data elements by UN/EDIFACT identifier. The derive resolves them at compile time, so a wrong identifier fails the build.
Frequently asked
What is EDIFACT?
EDIFACT — Electronic Data Interchange For Administration, Commerce and Transport — is the UN standard for structured business messages, defined in ISO 9735. An interchange is flat text: segments separated by a terminator, each carrying data elements, optionally split into components. It is used in supply chain, logistics, energy, healthcare and banking. See Core Concepts.
How is this different from a split-on-delimiters parser?
The UNA can redefine every delimiter, any delimiter can
appear in a value when release-escaped, and syntax version 4 adds a
repetition separator. A naive split merges or truncates values in all
three cases.
Can edifact stream files larger than memory?
Yes. A reader iterator holds one segment (at most 64 KiB) and a read
buffer. A message window holds one UNH…UNT
message of at most max_message_bytes (16 MiB by default),
parsed into at most 128× that in heap.
See Streaming.
Is it safe against hostile input?
Every entry point is bounded by default; ReaderConfig::unbounded()
opts out for trusted input. Error text escapes control and bidirectional
characters. The crate denies unsafe and is property- and
fuzz-tested.
See the threat model.
Can the writer produce a broken interchange?
It computes UNT and UNZ from what it wrote and
validates the typed UNB header. A message dropped without
finish() gets no UNT; after a failed write the
writer is poisoned (E072).
See Writing.
Does it ship UN/EDIFACT directories?
No. The UNTDID licence forbids distributing modified copies of the Directory without UNECE's permission. The crate ships the validation engine, the table types and the ISO 9735 service segments; supply your directory definitions at compile time or load them at startup. See Validation.
Start reading
- Getting Started Install edifact, parse your first EDIFACT interchange, and choose the feature flags you need.
- Core Concepts The EDIFACT wire format — segments, data elements, components, the UNA service string advice, release characters, and repetition — mapped onto the Rust types.
- Character Sets Decode UNOA-UNOK and UNOY interchanges, encode them back, and validate a payload against the repertoire its own UNB declares.
- Parsing Zero-copy slice parsing, streaming readers, byte spans, UNA handling, and the ReaderConfig budgets that guard against oversized input.
- Writing Serialize segments back to the EDIFACT wire format with Writer, automatic delimiter escaping, custom UNA service strings, and repeating data elements.
- Dates and times Decode, compare, build and validate EDIFACT DTM values by their UNCL 2379 format code, with optional conversions to the time crate.