edifact 0.20.0

EDIFACT (ISO 9735) for Rust: zero-copy parser, envelope-computing writer, typed derive, layered validation, DTM dates and CONTRL
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Event model for EDIFACT serialization.
//!
//! [`EdifactEvent`] carries its text as a [`Cow`], so one type serves both
//! streaming emission (every value borrows from the value being serialized) and
//! [`VecEmitter`], which has to keep events past the borrow that produced them.

use crate::EdifactError;
use std::borrow::Cow;
use std::io::Write;

// ── event types ───────────────────────────────────────────────────────────────

/// A borrowed EDIFACT event emitted during serialization.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum EdifactEvent<'a> {
    /// Beginning of a new segment (e.g. `"BGM"`, `"NAD"`).
    StartSegment {
        /// Segment tag.
        tag: Cow<'a, str>,
    },
    /// A data element value — first (or only) component of a new element.
    Element {
        /// Element text value.
        value: Cow<'a, str>,
    },
    /// An additional component within the current element.
    ComponentElement {
        /// Component text value.
        value: Cow<'a, str>,
    },
    /// The first component of a further occurrence of the current data element.
    ///
    /// The write-side mirror of [`Token::RepeatElement`][crate::Token::RepeatElement]:
    /// the parser splits repeating data elements, so the serializer has to be
    /// able to produce them, or a value that round-trips through a typed struct
    /// comes back collapsed into one occurrence.
    ///
    /// Requires an active repetition separator — see
    /// [`ServiceStringAdvice::is_repetition_active`][crate::ServiceStringAdvice::is_repetition_active].
    /// Without one there is no byte to separate occurrences with, so
    /// [`WriterEmitter`] returns
    /// [`EdifactError::RepetitionSeparatorNotDeclared`] rather than emitting
    /// output that reads back as a single occurrence.
    RepeatElement {
        /// First component of the new occurrence.
        value: Cow<'a, str>,
    },
    /// End of the current segment.
    EndSegment,
}

impl<'a> EdifactEvent<'a> {
    /// A [`StartSegment`][Self::StartSegment] event for `tag`.
    #[inline]
    #[must_use]
    pub fn start(tag: impl Into<Cow<'a, str>>) -> Self {
        Self::StartSegment { tag: tag.into() }
    }

    /// An [`Element`][Self::Element] event carrying `value`.
    #[inline]
    #[must_use]
    pub fn element(value: impl Into<Cow<'a, str>>) -> Self {
        Self::Element {
            value: value.into(),
        }
    }

    /// A [`ComponentElement`][Self::ComponentElement] event carrying `value`.
    #[inline]
    #[must_use]
    pub fn component(value: impl Into<Cow<'a, str>>) -> Self {
        Self::ComponentElement {
            value: value.into(),
        }
    }

    /// A [`RepeatElement`][Self::RepeatElement] event carrying `value`.
    #[inline]
    #[must_use]
    pub fn repeat(value: impl Into<Cow<'a, str>>) -> Self {
        Self::RepeatElement {
            value: value.into(),
        }
    }
}

impl EdifactEvent<'_> {
    /// Detach this event from the value it borrows from, cloning its text.
    #[must_use]
    pub fn into_owned(self) -> EdifactEvent<'static> {
        match self {
            Self::StartSegment { tag } => EdifactEvent::start(Cow::Owned(tag.into_owned())),
            Self::Element { value } => EdifactEvent::element(Cow::Owned(value.into_owned())),
            Self::ComponentElement { value } => {
                EdifactEvent::component(Cow::Owned(value.into_owned()))
            }
            Self::RepeatElement { value } => EdifactEvent::repeat(Cow::Owned(value.into_owned())),
            Self::EndSegment => EdifactEvent::EndSegment,
        }
    }
}

// ── emitter trait ─────────────────────────────────────────────────────────────

/// Trait for any sink that can consume [`EdifactEvent`]s.
pub trait EventEmitter {
    /// Consume one event.
    fn emit(&mut self, event: EdifactEvent<'_>) -> Result<(), EdifactError>;

    /// Return the decimal-mark byte used by the interchange (`b'.'` by default).
    ///
    /// Serializers that format numeric values (e.g. [`crate::ser::DecimalFloat`])
    /// call this to discover whether to emit `12.5` or `12,5`.
    ///
    /// The default implementation returns `b'.'`, which is correct for standard
    /// EDIFACT interchanges that do not declare a UNA service string or that use
    /// the ISO 9735 default.  Override this in emitters backed by a
    /// [`crate::Writer`] with a custom [`crate::ServiceStringAdvice`].
    #[inline]
    fn decimal_mark(&self) -> u8 {
        b'.'
    }
}

// ── VecEmitter ────────────────────────────────────────────────────────────────

/// Collects events into a `Vec<EdifactEvent<'static>>`.
///
/// Useful for testing and introspection.  Does not leak memory.
#[derive(Debug, Default)]
pub struct VecEmitter {
    /// Collected owned events.
    pub events: Vec<EdifactEvent<'static>>,
}

impl EventEmitter for VecEmitter {
    fn emit(&mut self, event: EdifactEvent<'_>) -> Result<(), EdifactError> {
        self.events.push(event.into_owned());
        Ok(())
    }
}

// ── WriterEmitter ─────────────────────────────────────────────────────────────

/// Internal protocol-state machine for [`WriterEmitter`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EmitterState {
    /// Between segments: no open segment.
    Idle,
    /// A [`EdifactEvent::StartSegment`] has been emitted; no element written yet.
    InSegment,
    /// An [`EdifactEvent::Element`] has been emitted; `ComponentElement` is valid.
    InElement,
}

/// Writes EDIFACT events directly to any [`Write`] implementation.
///
/// Each event is written to the underlying writer immediately, escaping in
/// place, so the emitter allocates nothing per event (asserted by the
/// `allocation_budget` test `writer_emitter_allocates_nothing_per_event`). Two
/// exceptions: a bound repertoire ([`with_charset`][Self::with_charset])
/// transcodes non-ASCII text into a temporary buffer, and the underlying
/// `Write` may allocate on its own (a `Vec<u8>` grows).
///
/// # Protocol
///
/// Events must arrive in the order produced by [`crate::EdifactSerialize`]:
/// `StartSegment` → zero or more (`Element` → zero or more `ComponentElement`) → `EndSegment`.
///
/// Any violation of this protocol returns
/// [`EdifactError::InvalidEventSequence`] immediately.  Violations are
/// detected in both debug and release builds.
///
/// # Errors and poisoning
///
/// The unit is one event. An event refused before its first byte — a protocol
/// violation, an invalid tag, an undeclared repetition separator — writes
/// nothing and leaves the emitter where it was. Any other failure poisons the
/// underlying [`Writer`][crate::Writer]: every later `emit` and
/// [`finish`][Self::finish] returns [`EdifactError::WriterPoisoned`].
pub struct WriterEmitter<W: Write> {
    writer: crate::Writer<W>,
    state: EmitterState,
}

impl<W: Write> WriterEmitter<W> {
    /// Create a new `WriterEmitter` with default EDIFACT delimiters.
    pub fn new(inner: W) -> Self {
        Self {
            writer: crate::Writer::new(inner),
            state: EmitterState::Idle,
        }
    }

    /// Create a new `WriterEmitter` with custom delimiters, writing a UNA header first.
    ///
    /// # Errors
    ///
    /// Returns [`EdifactError::InvalidUna`] when `ssa.is_valid()` is false.
    pub fn with_una(
        inner: W,
        ssa: crate::tokenizer::ServiceStringAdvice,
    ) -> Result<Self, crate::EdifactError> {
        Ok(Self {
            writer: crate::Writer::with_una(inner, ssa)?,
            state: EmitterState::Idle,
        })
    }

    /// Bind this emitter's writer to a character repertoire.
    ///
    /// The typed-serialization counterpart of
    /// [`Writer::with_charset`][crate::Writer::with_charset]. Without it a
    /// `#[derive(EdifactSerialize)]` struct goes out as UTF-8, which a
    /// `UNOC`…`UNOK` partner reads as mojibake (`ü` becomes two characters)
    /// rather than as an error.
    ///
    /// # Example
    ///
    /// ```
    /// use edifact::{Charset, EdifactEvent, EventEmitter, WriterEmitter};
    ///
    /// let mut emitter = WriterEmitter::new(Vec::new()).with_charset(Charset::UnoC);
    /// emitter.emit(EdifactEvent::start("NAD"))?;
    /// emitter.emit(EdifactEvent::element("Müller"))?;
    /// emitter.emit(EdifactEvent::EndSegment)?;
    /// assert_eq!(emitter.finish()?, b"NAD+M\xFCller'".to_vec());
    /// # Ok::<(), edifact::EdifactError>(())
    /// ```
    #[must_use]
    pub fn with_charset(mut self, charset: crate::Charset) -> Self {
        self.writer = self.writer.with_charset(charset);
        self
    }

    /// Flush and consume the emitter, returning the underlying writer.
    ///
    /// # Errors
    ///
    /// [`EdifactError::WriterPoisoned`] after a failure,
    /// [`EdifactError::InvalidEventSequence`] while a segment is open (emit
    /// `EndSegment` first), and [`EdifactError::Io`] when the flush fails. Use
    /// [`into_inner`][Self::into_inner] to get the sink back regardless.
    ///
    /// ```
    /// use edifact::{EdifactError, EdifactEvent, EventEmitter, WriterEmitter};
    ///
    /// let mut emitter = WriterEmitter::new(Vec::new());
    /// emitter.emit(EdifactEvent::start("BGM"))?;
    /// emitter.emit(EdifactEvent::element("220"))?;
    /// // No `EndSegment`: the segment is incomplete.
    /// assert!(matches!(
    ///     emitter.finish(),
    ///     Err(EdifactError::InvalidEventSequence { .. })
    /// ));
    /// # Ok::<(), EdifactError>(())
    /// ```
    pub fn finish(self) -> Result<W, EdifactError> {
        self.writer.check_healthy()?;
        if self.state != EmitterState::Idle {
            return Err(EdifactError::InvalidEventSequence {
                message: "finish() called with a segment still open; emit EndSegment first",
            });
        }
        self.writer.finish()
    }

    /// Return the sink as it is: no flush, no checks, open segment or not.
    #[must_use]
    pub fn into_inner(self) -> W {
        self.writer.into_inner()
    }

    /// Number of complete segments written so far.
    pub fn segment_count(&self) -> u64 {
        self.writer.segment_count()
    }

    /// Return the active [`ServiceStringAdvice`][crate::ServiceStringAdvice].
    ///
    /// Callers can use this to format values (e.g., floats) using the correct
    /// decimal-mark character configured in the UNA header.
    pub fn service_string_advice(&self) -> crate::tokenizer::ServiceStringAdvice {
        self.writer.service_string_advice()
    }
}

impl<W: Write> EventEmitter for WriterEmitter<W> {
    #[inline]
    fn decimal_mark(&self) -> u8 {
        self.writer.service_string_advice().decimal_mark
    }

    fn emit(&mut self, event: EdifactEvent<'_>) -> Result<(), EdifactError> {
        self.writer.check_healthy()?;
        // Each arm refuses a protocol violation before writing, and moves the
        // state only after the write succeeded.
        let next = match event {
            EdifactEvent::StartSegment { tag } => {
                if self.state != EmitterState::Idle {
                    return Err(EdifactError::InvalidEventSequence {
                        message: "StartSegment emitted while a segment is already open; emit EndSegment first",
                    });
                }
                self.writer.emit_tag(&tag)?;
                EmitterState::InSegment
            }
            EdifactEvent::Element { value } => {
                if self.state == EmitterState::Idle {
                    return Err(EdifactError::InvalidEventSequence {
                        message: "Element emitted outside of a segment; emit StartSegment first",
                    });
                }
                self.writer.emit_element(&value)?;
                EmitterState::InElement
            }
            EdifactEvent::ComponentElement { value } => {
                if self.state != EmitterState::InElement {
                    return Err(EdifactError::InvalidEventSequence {
                        message: "ComponentElement emitted without a preceding Element in the same segment",
                    });
                }
                self.writer.emit_component(&value)?;
                EmitterState::InElement
            }
            EdifactEvent::RepeatElement { value } => {
                if self.state != EmitterState::InElement {
                    return Err(EdifactError::InvalidEventSequence {
                        message: "RepeatElement emitted without a preceding Element in the same segment",
                    });
                }
                self.writer.emit_repetition(&value)?;
                EmitterState::InElement
            }
            EdifactEvent::EndSegment => {
                if self.state == EmitterState::Idle {
                    return Err(EdifactError::InvalidEventSequence {
                        message: "EndSegment emitted while no segment is open; emit StartSegment first",
                    });
                }
                self.writer.emit_segment_end()?;
                EmitterState::Idle
            }
        };
        self.state = next;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn vec_emitter_no_memory_leak() {
        let mut e = VecEmitter::default();
        e.emit(EdifactEvent::start("BGM")).unwrap();
        e.emit(EdifactEvent::element("E03")).unwrap();
        e.emit(EdifactEvent::EndSegment).unwrap();
        assert_eq!(e.events[0], EdifactEvent::start("BGM".to_owned()));
        assert_eq!(e.events[1], EdifactEvent::element("E03".to_owned()));
    }

    #[test]
    fn writer_emitter_produces_valid_edifact() {
        let mut buf = Vec::new();
        {
            let mut e = WriterEmitter::new(&mut buf);
            e.emit(EdifactEvent::start("BGM")).unwrap();
            e.emit(EdifactEvent::element("E03")).unwrap();
            e.emit(EdifactEvent::element("11042")).unwrap();
            e.emit(EdifactEvent::EndSegment).unwrap();
            e.finish().unwrap();
        }
        assert_eq!(buf, b"BGM+E03+11042'");
    }

    #[test]
    fn writer_emitter_handles_components() {
        let mut buf = Vec::new();
        {
            let mut e = WriterEmitter::new(&mut buf);
            e.emit(EdifactEvent::start("NAD")).unwrap();
            e.emit(EdifactEvent::element("MS")).unwrap();
            e.emit(EdifactEvent::element("9900112233445")).unwrap();
            e.emit(EdifactEvent::component("")).unwrap();
            e.emit(EdifactEvent::component("293")).unwrap();
            e.emit(EdifactEvent::EndSegment).unwrap();
            e.finish().unwrap();
        }
        let s = std::str::from_utf8(&buf).unwrap();
        assert_eq!(s, "NAD+MS+9900112233445::293'");
    }

    #[test]
    fn repetitions_round_trip_through_the_event_layer() {
        // The parser splits repeating data elements, so the serializer has to be
        // able to produce them — otherwise a value that goes out through a typed
        // struct comes back collapsed into one occurrence.
        let ssa = crate::ServiceStringAdvice::from_bytes(b"UNA:+.?*'").unwrap();
        let mut buf = Vec::new();
        {
            let mut e = WriterEmitter::with_una(&mut buf, ssa).unwrap();
            e.emit(EdifactEvent::start("RFF")).unwrap();
            e.emit(EdifactEvent::element("ON")).unwrap();
            e.emit(EdifactEvent::component("1")).unwrap();
            e.emit(EdifactEvent::repeat("ON")).unwrap();
            e.emit(EdifactEvent::component("2")).unwrap();
            e.emit(EdifactEvent::EndSegment).unwrap();
            e.finish().unwrap();
        }
        assert_eq!(
            std::str::from_utf8(&buf).unwrap(),
            "UNA:+.?*'RFF+ON:1*ON:2'"
        );

        let segments: Vec<_> = crate::from_bytes(&buf)
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        let element = segments[0].get_element(0).unwrap();
        assert_eq!(element.repeat_count(), 2);
        assert_eq!(element.repetition(1).unwrap()[1].0, "2");
    }

    #[test]
    fn a_repetition_without_a_declared_separator_is_refused() {
        let mut e = WriterEmitter::new(Vec::<u8>::new());
        e.emit(EdifactEvent::start("RFF")).unwrap();
        e.emit(EdifactEvent::element("ON")).unwrap();
        let err = e.emit(EdifactEvent::repeat("ON")).unwrap_err();
        assert!(
            matches!(err, EdifactError::RepetitionSeparatorNotDeclared),
            "expected RepetitionSeparatorNotDeclared, got {err:?}"
        );
    }

    #[test]
    fn a_repetition_before_any_element_is_refused() {
        let ssa = crate::ServiceStringAdvice::from_bytes(b"UNA:+.?*'").unwrap();
        let mut e = WriterEmitter::with_una(Vec::<u8>::new(), ssa).unwrap();
        e.emit(EdifactEvent::start("RFF")).unwrap();
        let err = e.emit(EdifactEvent::repeat("ON")).unwrap_err();
        assert!(
            matches!(err, EdifactError::InvalidEventSequence { .. }),
            "expected InvalidEventSequence, got {err:?}"
        );
    }

    // ── protocol-violation tests (BUG 2.1) ───────────────────────────────────

    #[test]
    fn writer_emitter_element_before_start_segment_is_err() {
        let mut e = WriterEmitter::new(Vec::<u8>::new());
        let err = e.emit(EdifactEvent::element("X")).unwrap_err();
        assert!(
            matches!(err, crate::EdifactError::InvalidEventSequence { .. }),
            "expected InvalidEventSequence, got {err:?}"
        );
    }

    #[test]
    fn writer_emitter_component_before_element_is_err() {
        let mut e = WriterEmitter::new(Vec::<u8>::new());
        e.emit(EdifactEvent::start("BGM")).unwrap();
        let err = e.emit(EdifactEvent::component("X")).unwrap_err();
        assert!(
            matches!(err, crate::EdifactError::InvalidEventSequence { .. }),
            "expected InvalidEventSequence, got {err:?}"
        );
    }

    #[test]
    fn writer_emitter_double_start_segment_is_err() {
        let mut e = WriterEmitter::new(Vec::<u8>::new());
        e.emit(EdifactEvent::start("BGM")).unwrap();
        let err = e.emit(EdifactEvent::start("DTM")).unwrap_err();
        assert!(
            matches!(err, crate::EdifactError::InvalidEventSequence { .. }),
            "expected InvalidEventSequence, got {err:?}"
        );
    }

    #[test]
    fn writer_emitter_end_segment_without_start_is_err() {
        let mut e = WriterEmitter::new(Vec::<u8>::new());
        let err = e.emit(EdifactEvent::EndSegment).unwrap_err();
        assert!(
            matches!(err, crate::EdifactError::InvalidEventSequence { .. }),
            "expected InvalidEventSequence, got {err:?}"
        );
    }

    // ── honest finish and poisoning ──────────────────────────────────────────

    /// Fails every write from the `fail_at`-th (zero-based) on.
    struct FailingSink {
        buf: Vec<u8>,
        writes: usize,
        fail_at: usize,
    }

    impl Write for FailingSink {
        fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
            self.writes += 1;
            if self.writes > self.fail_at {
                return Err(std::io::Error::other("injected"));
            }
            self.buf.extend_from_slice(data);
            Ok(data.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn finish_with_open_segment_is_refused() {
        let mut buf = Vec::new();
        {
            let mut e = WriterEmitter::new(&mut buf);
            e.emit(EdifactEvent::start("BGM")).unwrap();
            e.emit(EdifactEvent::element("220")).unwrap();
            let err = e.finish().unwrap_err();
            assert!(
                matches!(err, EdifactError::InvalidEventSequence { .. }),
                "{err:?}"
            );
        }
        assert_eq!(buf, b"BGM+220");

        let mut e = WriterEmitter::new(Vec::new());
        e.emit(EdifactEvent::start("BGM")).unwrap();
        e.emit(EdifactEvent::element("220")).unwrap();
        assert_eq!(e.into_inner(), b"BGM+220");
    }

    #[test]
    fn refused_tag_does_not_advance_state() {
        let mut e = WriterEmitter::new(Vec::new());
        let err = e.emit(EdifactEvent::start("bad")).unwrap_err();
        assert!(matches!(err, EdifactError::InvalidSegmentTag(_)), "{err:?}");
        e.emit(EdifactEvent::start("BGM")).unwrap();
        e.emit(EdifactEvent::EndSegment).unwrap();
        assert_eq!(e.finish().unwrap(), b"BGM'");
    }

    #[test]
    fn refused_repeat_event_keeps_segment_completable() {
        let mut e = WriterEmitter::new(Vec::new());
        e.emit(EdifactEvent::start("RFF")).unwrap();
        e.emit(EdifactEvent::element("ON")).unwrap();
        assert!(matches!(
            e.emit(EdifactEvent::repeat("ON")),
            Err(EdifactError::RepetitionSeparatorNotDeclared)
        ));
        e.emit(EdifactEvent::element("1")).unwrap();
        e.emit(EdifactEvent::EndSegment).unwrap();
        assert!(!e.writer.is_poisoned());
        assert_eq!(e.finish().unwrap(), b"RFF+ON+1'");
    }

    #[test]
    fn emitter_is_poisoned_after_sink_failure() {
        let sink = FailingSink {
            buf: Vec::new(),
            writes: 0,
            fail_at: 1,
        };
        let mut e = WriterEmitter::new(sink);
        e.emit(EdifactEvent::start("BGM")).unwrap();
        let err = e.emit(EdifactEvent::element("220")).unwrap_err();
        assert!(matches!(err, EdifactError::Io(_)), "{err:?}");
        for event in [
            EdifactEvent::element("220"),
            EdifactEvent::EndSegment,
            EdifactEvent::start("DTM"),
        ] {
            let err = e.emit(event).unwrap_err();
            assert!(
                matches!(err, EdifactError::WriterPoisoned { .. }),
                "{err:?}"
            );
        }
        assert_eq!(e.into_inner().buf, b"BGM");
    }

    #[test]
    fn poisoned_emitter_refuses_to_finish() {
        let sink = FailingSink {
            buf: Vec::new(),
            writes: 0,
            fail_at: 0,
        };
        let mut e = WriterEmitter::new(sink);
        assert!(e.emit(EdifactEvent::start("BGM")).is_err());
        assert!(matches!(
            e.finish(),
            Err(EdifactError::WriterPoisoned { .. })
        ));
    }

    #[test]
    fn charset_failure_mid_event_poisons() {
        let mut e = WriterEmitter::new(Vec::new()).with_charset(crate::Charset::UnoC);
        e.emit(EdifactEvent::start("NAD")).unwrap();
        let err = e.emit(EdifactEvent::element("M€ller")).unwrap_err();
        assert!(
            matches!(err, EdifactError::CharacterNotInRepertoire { .. }),
            "{err:?}"
        );
        assert!(e.writer.is_poisoned());
        assert_eq!(e.into_inner(), b"NAD+");
    }
}