edifact-rs-derive 0.4.0

Derive macros for EdifactSerialize / EdifactDeserialize (from edifact-rs)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# edifact-rs โšก

[![crates.io](https://img.shields.io/crates/v/edifact-rs.svg)](https://crates.io/crates/edifact-rs)
[![docs.rs](https://docs.rs/edifact-rs/badge.svg)](https://docs.rs/edifact-rs)
[![CI](https://github.com/hupe1980/edifact-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/hupe1980/edifact-rs/actions)
[![license](https://img.shields.io/crates/l/edifact-rs.svg)](LICENSE)
[![MSRV](https://img.shields.io/badge/rust-1.85%2B-blue.svg)](Cargo.toml)

**EDIFACT for Rust** โ€” zero-copy parsing, streaming deserialization, typed derive macros, composable validation, and rich diagnostics.

---

## โœจ Why edifact-rs?

| | edifact-rs |
|---|---|
| ๐Ÿš€ **Zero-copy parsing** | Borrows directly from the input `&[u8]` โ€” no intermediate allocations |
| ๐Ÿ”„ **Streaming I/O** | Reader-based APIs process gigabyte interchanges in constant memory |
| ๐ŸŽฏ **Typed mapping** | `#[derive(EdifactDeserialize, EdifactSerialize)]` for segments and messages |
| โœ… **Composable validation** | `ProfileRulePack` with multi-layer, rule-ID-filtered reporting |
| ๐Ÿฉบ **Rich diagnostics** | Optional `miette` integration for human-friendly error output |
| ๐Ÿ›ก๏ธ **DOS hardening** | Configurable `max_segment_bytes` guard enforced on all read paths |
| ๐ŸŽ๏ธ **Allocation-free hot paths** | `SmallVec`, eager `WriterEmitter`, and `edifact_deserialize_owned` |

---

## ๐Ÿ“ฆ Installation

```toml
[dependencies]
edifact-rs = "0.4"

# Optional: derive macros (included by default)
# edifact-rs = { version = "0.4", features = ["derive"] }

# Optional: rich miette diagnostics
# edifact-rs = { version = "0.4", features = ["diagnostics"] }
```

### Feature flags

| Feature | Default | Description |
|---|---|---|
| `derive` | โœ… yes | Re-exports `EdifactDeserialize` / `EdifactSerialize` derive macros |
| `diagnostics` | โŒ no | Adds `miette::Diagnostic` to `EdifactError` for human-readable output |

---

## ๐Ÿš€ Quick start

### Parse bytes (zero-copy)

```rust
use edifact_rs::from_bytes;

let input = b"UNA:+.? 'UNH+1+ORDERS:D:11A:UN'BGM+220+PO-4711+9'UNT+3+1'";
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>()?;

assert_eq!(segments[0].tag, "UNH");
let bgm = &segments[1];
assert_eq!(bgm.tag, "BGM");
assert_eq!(bgm.element_str(0), Some("220"));   // document code
assert_eq!(bgm.element_str(1), Some("PO-4711")); // document number
# Ok::<(), edifact_rs::EdifactError>(())
```

### Typed deserialization with derive

```rust
use edifact_rs::{EdifactDeserialize, EdifactSerialize, from_bytes};

#[derive(Debug, EdifactDeserialize, EdifactSerialize)]
#[edifact(segment = "BGM")]
struct Bgm {
    #[edifact(element = 0)]
    document_name_code: String,
    #[edifact(element = 1)]
    document_number: String,
    #[edifact(element = 2)]
    function_code: Option<String>,
}

let input = b"BGM+220+PO-4711+9'";
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>()?;
let bgm = Bgm::edifact_deserialize(&segments)?;

assert_eq!(bgm.document_name_code, "220");
assert_eq!(bgm.document_number, "PO-4711");
assert_eq!(bgm.function_code.as_deref(), Some("9"));
# Ok::<(), edifact_rs::EdifactError>(())
```

### Map a full message with qualifier-based fields

```rust
use edifact_rs::{EdifactDeserialize, EdifactSerialize, from_bytes};

#[derive(Debug, EdifactDeserialize, EdifactSerialize)]
#[edifact(segment = "NAD", qualifier_from = 0)]
struct Nad {
    #[edifact(element = 0)]
    qualifier: String,
    #[edifact(element = 1)]
    party_id: Option<String>,
}

#[derive(Debug, EdifactDeserialize)]
struct OrderMessage {
    bgm: Option<Bgm>,
    #[edifact(qualifier = "BY")]
    buyer: Option<Nad>,
    #[edifact(qualifier = "SU")]
    supplier: Option<Nad>,
}

let input = b"UNH+1+ORDERS:D:11A:UN'BGM+220+PO-4711+9'\
              NAD+BY+4000001000002::9'NAD+SU+4000001000001::9'UNT+5+1'";
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>()?;
let msg = OrderMessage::edifact_deserialize(&segments)?;

println!("buyer:    {:?}", msg.buyer.as_ref().and_then(|n| n.party_id.as_deref()));
println!("supplier: {:?}", msg.supplier.as_ref().and_then(|n| n.party_id.as_deref()));
# Ok::<(), edifact_rs::EdifactError>(())
```

### Serialize to wire format

```rust
use edifact_rs::ser;

# use edifact_rs::{EdifactSerialize};
# #[derive(EdifactSerialize)]
# #[edifact(segment = "BGM")]
# struct Bgm { #[edifact(element = 0)] doc_code: String }
let bgm = Bgm { doc_code: "220".into() };
let wire = ser::to_string(&bgm)?;
assert_eq!(wire, "BGM+220'");
# Ok::<(), edifact_rs::EdifactError>(())
```

---

## ๐Ÿ“ก Streaming APIs

### Low-memory typed extraction

Scan a large interchange and extract matching segments without buffering everything:

```rust
use edifact_rs::{EdifactDeserialize, deserialize_first_from_reader, deserialize_all_from_reader};

# #[derive(Debug, EdifactDeserialize)]
# #[edifact(segment = "BGM")]
# struct Bgm { #[edifact(element = 0)] doc_code: String, #[edifact(element = 1)] doc_id: String }
let input = std::io::Cursor::new(
    b"UNH+1+ORDERS:D:11A:UN'BGM+220+PO-001+9'BGM+231+PO-002+9'UNT+4+1'".to_vec()
);

// Stop after the first match โ€” O(1) memory:
let first: Bgm = deserialize_first_from_reader(input.clone())?;
assert_eq!(first.doc_id, "PO-001");

// Collect all matches โ€” only matching segments are kept:
let all: Vec<Bgm> = deserialize_all_from_reader(input)?;
assert_eq!(all.len(), 2);
# Ok::<(), edifact_rs::EdifactError>(())
```

### Message-window streaming (UNH..UNT)

Process multi-message interchanges one window at a time, with O(1) memory per message:

```rust
use edifact_rs::{message_windows_from_reader, deserialize_messages_from_reader, EdifactDeserialize};

# #[derive(Debug, EdifactDeserialize)]
# #[edifact(segment = "BGM")]
# struct Bgm { #[edifact(element = 0)] doc_code: String }
# #[derive(Debug, EdifactDeserialize)]
# struct OrderMessage { bgm: Option<Bgm> }
let interchange = std::io::Cursor::new(b"\
    UNB+UNOA:1+SENDER+RECEIVER+200101:0900+1'\
    UNH+1+ORDERS:D:96A:UN'BGM+220+PO-001+9'UNT+3+1'\
    UNH+2+ORDERS:D:96A:UN'BGM+220+PO-002+9'UNT+3+2'\
    UNZ+2+1'".to_vec());

// Iterate raw windows:
for window in message_windows_from_reader(interchange.clone()) {
    let segs = window?;
    println!("window: {} segments", segs.len());
}

// Or deserialize directly โ€” zero Vec<Segment> allocation per window:
let messages: Vec<OrderMessage> =
    deserialize_messages_from_reader::<OrderMessage, _>(interchange)
        .collect::<Result<_, _>>()?;
assert_eq!(messages.len(), 2);
# Ok::<(), edifact_rs::EdifactError>(())
```

> **Performance note**: `deserialize_messages_from_reader` calls
> `edifact_deserialize_owned`, a method generated by the derive macro that
> works directly on `&[OwnedSegment]` โ€” no intermediate `Vec<Segment<'_>>`
> is ever materialized.

---

## โœ… Validation

### Profile rule packs

Compose business-level validation rules with stable rule IDs that can be filtered and reported independently:

```rust
use edifact_rs::{
    ProfileRulePack, ValidationContext, ValidationIssue, ValidationSeverity, from_bytes,
};

let segments: Vec<_> =
    from_bytes(b"UNH+1+ORDERS:D:96A:UN'BGM+220+PO123+9'UNT+3+1'")
        .collect::<Result<_, _>>()?;

let document_pack = ProfileRulePack::builder("ORDERS-DOC")
    .for_message_type("ORDERS")
    .with_rule_fn(|segments| {
        let bgm = segments.iter().find(|s| s.tag == "BGM")?;
        let code = bgm.get_element(0)?.get_component(0)?;
        (code == "220").then(|| {
            ValidationIssue::new(ValidationSeverity::Warning, "code 220 requires special handling")
                .with_rule_id("ORDERS-DOC-P001")
                .with_segment("BGM")
                .with_element_index(0)
                .with_suggestion("Check your trading-partner agreement")
        })
    });

let report = ValidationContext::builder()
    .with_profile_pack(document_pack)
    .build()
    .validate_lenient(&segments);

// Filter by rule namespace:
let doc_issues = report.filter_by_rule_prefix("ORDERS-DOC-");
println!("{} issue(s) from ORDERS-DOC rules", doc_issues.total_issues());
# Ok::<(), edifact_rs::EdifactError>(())
```

### Multi-layer validation

Separate structure, code-list, and profile checks into distinct layers:

```rust
use edifact_rs::{Validator, ValidationContext, ValidationLayer, ValidationReport, Segment};

struct StructureValidator;
impl Validator for StructureValidator {
    fn validate_batch(&self, _segments: &[Segment<'_>], _report: &mut ValidationReport) {
        // check mandatory segments, ordering, ...
    }
    fn set_message_type(&mut self, _: Option<&str>) {}
}

let context = ValidationContext::builder()
    .with_message_type("ORDERS")
    .with_validator(ValidationLayer::Structure, StructureValidator)
    .build();
# Ok::<(), edifact_rs::EdifactError>(())
```

---

## ๐Ÿฉบ Diagnostics (optional feature)

Enable the `diagnostics` feature for human-readable, span-annotated error output powered by [`miette`](https://docs.rs/miette):

```toml
edifact-rs = { version = "0.4", features = ["diagnostics"] }
```

```
Error: invalid code value "999" at offset 42
  โ•ญโ”€ input.edi:2:5
  โ”‚
2 โ”‚ BGM+999+PO-4711+9'
  โ”‚     ^^^  code "999" is not in code list 1001
  โ”‚
Error Code: E007
Help: Use a valid document name code from UNTDID 1001
```

```rust,no_run
use edifact_rs::{from_bytes, ValidationContext};
// With `diagnostics` feature, errors implement miette::Diagnostic.
// Use miette's Report for pretty-printing to the terminal.
```

See [`cookbook_diagnostics.rs`](crates/edifact-rs/examples/cookbook_diagnostics.rs) for a complete example.

---

## ๐Ÿ—๏ธ Architecture

```
edifact-rs workspace
โ”‚
โ”œโ”€โ”€ edifact-rs              โ† core library
โ”‚   โ”œโ”€โ”€ tokenizer           zero-copy byte scanning, UNA handling
โ”‚   โ”œโ”€โ”€ parser              segment assembly, release-char resolution
โ”‚   โ”œโ”€โ”€ model               Segment / Element / OwnedSegment types
โ”‚   โ”œโ”€โ”€ writer              streaming wire-format serialization
โ”‚   โ”œโ”€โ”€ event               EdifactEvent / WriterEmitter (allocation-free)
โ”‚   โ”œโ”€โ”€ de                  EdifactDeserialize trait + free helpers
โ”‚   โ”œโ”€โ”€ ser                 EdifactSerialize trait
โ”‚   โ”œโ”€โ”€ envelope            UNB/UNH/UNT/UNZ validation
โ”‚   โ”œโ”€โ”€ validator           Validator / ValidationContext / ProfileRulePack
โ”‚   โ””โ”€โ”€ directory_validator SegmentDefinition / DirectoryValidator
โ”‚
โ””โ”€โ”€ edifact-rs-derive       proc-macro crate
    โ””โ”€โ”€ #[derive(EdifactDeserialize, EdifactSerialize)]
```

**Two parsing modes:**

| Mode | API | Allocation model |
|---|---|---|
| Zero-copy | `from_bytes(input: &[u8])` | Borrows from `input` โ€” no heap for segment data |
| Owned streaming | `from_reader_iter(reader)` | One `OwnedSegment` per segment; reader not buffered |

**Key types:**

| Type | Description |
|---|---|
| `Segment<'a>` | Zero-copy view with `tag: &'a str` and borrowed elements |
| `OwnedSegment` | Heap-owned copy; `.borrow()` returns O(1) `BorrowedSegment` |
| `BorrowedSegment<'a>` | Zero-allocation view of `OwnedSegment` |
| `EdifactError` | Stable error codes (E001โ€“E020) with byte offsets |
| `ValidationReport` | Collected issues with lenient/strict modes |
| `ProfileRulePack` | Composable, filterable business-rule bundles |

---

## ๐Ÿ”ง Low-level API

### Segment and element access

```rust
use edifact_rs::{from_bytes, find_qualified_segment};

let input = b"NAD+BY+4000001000002::9'NAD+SU+4000001000001::9'";
let segs: Vec<_> = from_bytes(input).collect::<Result<_, _>>()?;

let buyer = find_qualified_segment(&segs, "NAD", "BY").unwrap();
assert_eq!(buyer.element_str(0), Some("BY"));
assert_eq!(buyer.get_element(1).and_then(|e| e.get_component(0)), Some("4000001000002"));
# Ok::<(), edifact_rs::EdifactError>(())
```

### Reader with DOS guard

```rust
use edifact_rs::{ReaderConfig, from_bufread_stream_with_config};
use std::io::BufReader;

let config = ReaderConfig {
    max_segment_bytes: 8_192,
    ..Default::default()
};
let reader = BufReader::new(std::io::Cursor::new(b"BGM+220+test'"));
let segments = from_bufread_stream_with_config(reader, config)?;
# Ok::<(), edifact_rs::EdifactError>(())
```

### Write segments

```rust
use edifact_rs::{Writer, Segment, model::Element};

let mut buf: Vec<u8> = Vec::new();
let mut writer = Writer::new(&mut buf);
writer.write_segment(&Segment::new(
    "BGM",
    vec![Element::of(&["220"]), Element::of(&["PO-4711"])],
))?;
writer.finish()?;
assert_eq!(buf, b"BGM+220+PO-4711'");
# Ok::<(), edifact_rs::EdifactError>(())
```

---

## ๐ŸŒ Async / tokio integration

`edifact-rs` is intentionally synchronous โ€” EDIFACT parsing is CPU-bound and imposing an async runtime on all users would be wrong. Two clean patterns bridge to async:

**Pattern A** โ€” read into memory, then parse synchronously (recommended for < 1 MB):

```rust,ignore
let bytes = tokio::fs::read("message.edi").await?;
let windows: Vec<_> = edifact_rs::message_windows_bytes(&bytes)
    .collect::<Result<_, _>>()?;
```

**Pattern B** โ€” `spawn_blocking` for large files or blocking sources:

```rust,ignore
let messages = tokio::task::spawn_blocking(move || {
    let f = std::fs::File::open("large.edi")?;
    edifact_rs::message_windows_from_reader(f)
        .collect::<Result<Vec<_>, _>>()
}).await??;
```

---

## ๐Ÿ“š Examples

Run any example with `cargo run -p edifact-rs --example <name>`:

| Example | What it shows |
|---|---|
| [`cookbook_parse_map_validate_write`]crates/edifact-rs/examples/cookbook_parse_map_validate_write.rs | Parse โ†’ extract fields โ†’ validate โ†’ round-trip write |
| [`cookbook_typed_derive`]crates/edifact-rs/examples/cookbook_typed_derive.rs | Full derive workflow with qualifier-based NAD mapping |
| [`cookbook_typed_streaming`]crates/edifact-rs/examples/cookbook_typed_streaming.rs | All four streaming extraction APIs |
| [`cookbook_profile_packs`]crates/edifact-rs/examples/cookbook_profile_packs.rs | Composing and filtering profile rule packs |
| [`cookbook_streamed_progressive_validation`]crates/edifact-rs/examples/cookbook_streamed_progressive_validation.rs | Per-window validation over reader-based interchange |
| [`cookbook_fixture_validation`]crates/edifact-rs/examples/cookbook_fixture_validation.rs | Custom `Validator` implementation with fixture data |
| [`cookbook_diagnostics`]crates/edifact-rs/examples/cookbook_diagnostics.rs | Rich miette diagnostics (`--features diagnostics`) |

---

## ๐Ÿงช Testing

```bash
# Run all tests (unit + integration + doc-tests + derive UI tests):
cargo test --workspace --all-features

# Clippy (zero warnings policy):
cargo clippy --all-targets --all-features -- -D warnings

# Benchmarks (criterion + custom):
cargo bench -p edifact-rs

# Fuzz (requires cargo-bolero):
cargo bolero test -p edifact-rs fuzz_parse_write_parse_invariant_small_message
```

---

## ๐Ÿ“‹ Workspace layout

```
edifact-rs/
โ”œโ”€โ”€ crates/
โ”‚   โ”œโ”€โ”€ edifact-rs/          core library crate
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ examples/        runnable cookbooks
โ”‚   โ”‚   โ”œโ”€โ”€ tests/           integration + conformance tests
โ”‚   โ”‚   โ””โ”€โ”€ benches/         criterion benchmarks
โ”‚   โ””โ”€โ”€ edifact-rs-derive/   proc-macro crate
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ””โ”€โ”€ tests/ui/        trybuild compile-fail test suite
โ”œโ”€โ”€ scripts/                 UNECE source download helpers
โ”œโ”€โ”€ CHANGELOG.md
โ”œโ”€โ”€ CONCEPT.md
โ”œโ”€โ”€ FINDINGS.md              independent code-review findings (24/24 fixed โœ…)
โ””โ”€โ”€ RELEASE_POLICY.md
```

---

## โš™๏ธ MSRV and edition

- **Minimum Supported Rust Version**: 1.85
- **Edition**: 2024
- MSRV is enforced by CI on every push.

---

## ๐Ÿ“„ License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.