Skip to main content

edifact_rs/
ser.rs

1//! Custom serialization trait for EDIFACT.
2//!
3//! [`EdifactSerialize`] emits typed EDIFACT events rather than the generic
4//! key/value tokens of standard `serde`.  This matches EDIFACT's positional,
5//! qualifier-based data model — see `docs/writing.md` for the design rationale.
6
7use crate::EdifactError;
8use crate::event::{EdifactEvent, EventEmitter, WriterEmitter};
9use std::io::Write;
10
11// ── trait ─────────────────────────────────────────────────────────────────────
12
13/// Types that can serialize themselves to an EDIFACT event stream.
14///
15/// Implement manually or derive with `#[derive(EdifactSerialize)]` from the
16/// `edifact-rs-derive` crate.
17pub trait EdifactSerialize {
18    /// Serialize `self` by emitting events into `emitter`.
19    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError>;
20}
21
22/// Types that can serialize themselves as a composite EDIFACT element.
23///
24/// Implement this for custom composite structs used with
25/// `#[edifact(composite)]` in derive macros.
26pub trait EdifactCompositeSerialize {
27    /// Serialize `self` as one composite element into `emitter`.
28    fn edifact_serialize_composite<E: EventEmitter>(
29        &self,
30        emitter: &mut E,
31    ) -> Result<(), EdifactError>;
32}
33
34impl EdifactCompositeSerialize for Vec<String> {
35    fn edifact_serialize_composite<E: EventEmitter>(
36        &self,
37        emitter: &mut E,
38    ) -> Result<(), EdifactError> {
39        if self.is_empty() {
40            return emitter.emit(EdifactEvent::Element { value: "" });
41        }
42
43        emitter.emit(EdifactEvent::Element { value: &self[0] })?;
44        for component in self.iter().skip(1) {
45            emitter.emit(EdifactEvent::ComponentElement { value: component })?;
46        }
47        Ok(())
48    }
49}
50
51// ── blanket impls for scalar types ────────────────────────────────────────────
52
53impl EdifactSerialize for str {
54    #[inline]
55    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
56        emitter.emit(EdifactEvent::Element { value: self })
57    }
58}
59
60impl EdifactSerialize for String {
61    #[inline]
62    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
63        emitter.emit(EdifactEvent::Element {
64            value: self.as_str(),
65        })
66    }
67}
68
69/// `None` → empty element `""`; `Some(v)` → `v.edifact_serialize(emitter)`.
70impl<T: EdifactSerialize> EdifactSerialize for Option<T> {
71    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
72        match self {
73            Some(v) => v.edifact_serialize(emitter),
74            None => emitter.emit(EdifactEvent::Element { value: "" }),
75        }
76    }
77}
78
79/// Each element is serialized independently (repeated segments for groups).
80impl<T: EdifactSerialize> EdifactSerialize for Vec<T> {
81    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
82        for item in self {
83            item.edifact_serialize(emitter)?;
84        }
85        Ok(())
86    }
87}
88
89/// Each element is serialized independently (repeated segments for groups).
90impl<T: EdifactSerialize> EdifactSerialize for [T] {
91    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
92        for item in self {
93            item.edifact_serialize(emitter)?;
94        }
95        Ok(())
96    }
97}
98
99macro_rules! impl_serialize_int {
100    ($($t:ty),+ $(,)?) => {
101        $(
102            impl EdifactSerialize for $t {
103                fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
104                    // 42-byte buffer: i128::MIN is 40 chars; 2 spare bytes as safety margin.
105                    use std::io::Write as _;
106                    let mut buf = [0u8; 42];
107                    let mut w: &mut [u8] = &mut buf;
108                    if write!(w, "{self}").is_ok() {
109                        let written = 42 - w.len();
110                        // Display output for all integer/bool types is ASCII-only.
111                        let s = std::str::from_utf8(&buf[..written]).map_err(|_| EdifactError::InvalidUtf8)?;
112                        emitter.emit(EdifactEvent::Element { value: s })
113                    } else {
114                        // Extraordinary case: fall back to heap to avoid any panic.
115                        let s = format!("{self}");
116                        emitter.emit(EdifactEvent::Element { value: &s })
117                    }
118                }
119            }
120        )+
121    };
122}
123
124// Boolean is also bounded (max "false" = 5 bytes).
125impl_serialize_int!(
126    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, bool
127);
128
129// ── decimal-mark-aware float wrapper ─────────────────────────────────────────
130
131/// Decimal-mark-aware wrapper for `f32` or `f64` serialization.
132///
133/// Rust's [`Display`][std::fmt::Display] for `f32`/`f64` always uses `.` as the
134/// decimal separator.  EDIFACT interchanges can declare a different decimal mark
135/// in the UNA service string — `,` is the common alternative.
136///
137/// # Required for float serialization
138///
139/// `edifact-rs` intentionally provides **no** blanket `EdifactSerialize` impl for
140/// `f32`/`f64`.  This forces callers to make the decimal-mark intent explicit:
141///
142/// ```
143/// use edifact_rs::ser::DecimalFloat;
144/// use edifact_rs::{EdifactSerialize, VecEmitter, OwnedEdifactEvent};
145///
146/// let mut emitter = VecEmitter::default();
147/// DecimalFloat(12.5_f64).edifact_serialize(&mut emitter).unwrap();
148/// assert!(matches!(&emitter.events[0], OwnedEdifactEvent::Element { value } if value == "12.5"));
149/// ```
150///
151/// When the emitter's [`EventEmitter::decimal_mark`] is `b','`, the output will be `"12,5"`.
152///
153/// # Supported inner types
154///
155/// `DecimalFloat<f32>`, `DecimalFloat<f64>`. For any type that implements
156/// [`std::fmt::Display`], use [`DecimalFloatDisplay`] instead.
157#[derive(Debug, Clone, Copy, PartialEq)]
158pub struct DecimalFloat<T>(pub T);
159
160/// Decimal-mark-aware serializer for any [`std::fmt::Display`] value.
161///
162/// Like [`DecimalFloat`] but works with any type whose `Display` uses `.` as a
163/// decimal point (e.g., `rust_decimal::Decimal`, `bigdecimal::BigDecimal`).
164#[derive(Debug, Clone, Copy, PartialEq)]
165pub struct DecimalFloatDisplay<T: std::fmt::Display>(pub T);
166
167fn serialize_with_decimal_mark<E: EventEmitter>(
168    display: &dyn std::fmt::Display,
169    emitter: &mut E,
170) -> Result<(), EdifactError> {
171    use std::io::Write as _;
172    let mark = emitter.decimal_mark();
173
174    // Fast path: standard interchange — avoid any string manipulation.
175    if mark == b'.' {
176        let mut buf = [0u8; 320];
177        let mut w: &mut [u8] = &mut buf;
178        if write!(w, "{display}").is_ok() {
179            let written = 320 - w.len();
180            // INVARIANT: float Display output is ASCII-only.
181            let s = std::str::from_utf8(&buf[..written]).map_err(|_| EdifactError::InvalidUtf8)?;
182            return emitter.emit(EdifactEvent::Element { value: s });
183        }
184        // Buffer overflow fallback (extraordinarily large exponent).
185        let s = format!("{display}");
186        return emitter.emit(EdifactEvent::Element { value: &s });
187    }
188
189    // Non-standard decimal mark: format as string then replace '.'.
190    // INVARIANT: `mark` is ASCII (validated by ServiceStringAdvice::is_valid()).
191    let s = format!("{display}");
192    if s.contains('.') {
193        // Encode `mark` as a 1–4 byte UTF-8 slice on the stack; no heap allocation.
194        let mut mark_buf = [0u8; 4];
195        let mark_str = (mark as char).encode_utf8(&mut mark_buf);
196        let replaced = s.replace('.', mark_str);
197        emitter.emit(EdifactEvent::Element { value: &replaced })
198    } else {
199        emitter.emit(EdifactEvent::Element { value: &s })
200    }
201}
202
203/// Reject `NaN` and infinity before they reach the wire.
204///
205/// `Display` renders them as `NaN`, `inf`, and `-inf` — text that is not an
206/// EDIFACT numeric data element (ISO 9735-1 §7 allows digits, an optional sign,
207/// and the decimal mark), that no receiver can parse, and that this crate's own
208/// reader would hand back as a string rather than a number. Emitting it would
209/// turn a calculation bug into a wire-format bug discovered days later.
210#[inline]
211fn reject_non_finite(value: f64) -> Result<(), EdifactError> {
212    if value.is_finite() {
213        return Ok(());
214    }
215    Err(EdifactError::NonFiniteNumber {
216        value: value.to_string(),
217    })
218}
219
220impl EdifactSerialize for DecimalFloat<f32> {
221    #[inline]
222    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
223        reject_non_finite(f64::from(self.0))?;
224        serialize_with_decimal_mark(&self.0, emitter)
225    }
226}
227
228impl EdifactSerialize for DecimalFloat<f64> {
229    #[inline]
230    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
231        reject_non_finite(self.0)?;
232        serialize_with_decimal_mark(&self.0, emitter)
233    }
234}
235
236impl<T: std::fmt::Display> EdifactSerialize for DecimalFloatDisplay<T> {
237    #[inline]
238    fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
239        serialize_with_decimal_mark(&self.0, emitter)
240    }
241}
242
243/// Emit one segment from `(element_index, component_index, value)` triples.
244///
245/// Values may arrive in any order and need not cover every slot: the triples
246/// are sorted, gaps are filled with empty elements and components, and the
247/// result is a single well-formed segment.
248///
249/// This exists because a derive that addresses fields by UN/EDIFACT data element
250/// identifier (`#[edifact(element = "3055")]`) only learns the numeric slots
251/// during *const evaluation*, after the macro has already expanded — so it
252/// cannot lay out the emit order at expansion time the way a positional derive
253/// can.  Handing the resolved slots to this function moves the ordering to
254/// runtime, where they are known.
255///
256/// `parts` is taken by mutable reference because it is sorted in place; the
257/// caller keeps the allocation.
258///
259/// If two triples claim the same slot the **first** one wins and the rest are
260/// dropped — emitting both would shift every following component by one and
261/// silently corrupt the segment's positions. (The derive cannot produce a
262/// duplicate: it rejects colliding slots at compile time.)
263///
264/// # Example
265///
266/// ```rust
267/// use edifact_rs::{VecEmitter, emit_sparse_segment};
268/// use std::borrow::Cow;
269///
270/// let mut parts = vec![
271///     (1, 2, Cow::Borrowed("293")),
272///     (0, 0, Cow::Borrowed("MS")),
273///     (1, 0, Cow::Borrowed("9900112233445")),
274/// ];
275/// let mut emitter = VecEmitter::default();
276/// emit_sparse_segment(&mut emitter, "NAD", &mut parts)?;
277/// // Slot (1, 1) was never supplied, so it is emitted as an empty component:
278/// // NAD+MS+9900112233445::293'
279/// assert_eq!(emitter.events.len(), 6);
280/// # Ok::<(), edifact_rs::EdifactError>(())
281/// ```
282///
283/// # Errors
284///
285/// Propagates any error returned by the emitter.
286pub fn emit_sparse_segment<E: EventEmitter>(
287    emitter: &mut E,
288    tag: &str,
289    parts: &mut [(usize, usize, std::borrow::Cow<'_, str>)],
290) -> Result<(), EdifactError> {
291    parts.sort_by_key(|(element, component, _)| (*element, *component));
292
293    emitter.emit(EdifactEvent::StartSegment { tag })?;
294
295    // `parts` is sorted, so the last entry carries the highest element index and
296    // one linear walk covers every slot.
297    let mut cursor = 0usize;
298    let element_count = parts.last().map_or(0, |(element, _, _)| *element + 1);
299    for element in 0..element_count {
300        // Component 0 opens the element; every later component extends it.
301        let mut next_component = 0usize;
302        let mut opened = false;
303        while cursor < parts.len() && parts[cursor].0 == element {
304            let (_, component, value) = &parts[cursor];
305            if *component < next_component {
306                // A slot already emitted: first value wins.  Emitting this one
307                // too would push every later component one position right.
308                cursor += 1;
309                continue;
310            }
311            // Fill any skipped component slots so positions stay meaningful.
312            while next_component < *component {
313                let event = if opened {
314                    EdifactEvent::ComponentElement { value: "" }
315                } else {
316                    EdifactEvent::Element { value: "" }
317                };
318                emitter.emit(event)?;
319                opened = true;
320                next_component += 1;
321            }
322            let event = if opened {
323                EdifactEvent::ComponentElement { value }
324            } else {
325                EdifactEvent::Element { value }
326            };
327            emitter.emit(event)?;
328            opened = true;
329            next_component += 1;
330            cursor += 1;
331        }
332        if !opened {
333            // No value for this element at all — emit an empty placeholder so
334            // the following elements keep their positions.
335            emitter.emit(EdifactEvent::Element { value: "" })?;
336        }
337    }
338
339    emitter.emit(EdifactEvent::EndSegment)
340}
341
342/// Serialize `value` to the given [`Write`] implementation.
343pub fn to_writer<T, W>(inner: W, value: &T) -> Result<(), EdifactError>
344where
345    T: EdifactSerialize,
346    W: Write,
347{
348    let mut emitter = WriterEmitter::new(inner);
349    value.edifact_serialize(&mut emitter)?;
350    emitter.finish().map(|_| ())
351}
352
353/// Serialize `value` to an owned `Vec<u8>`.
354pub fn to_bytes<T: EdifactSerialize>(value: &T) -> Result<Vec<u8>, EdifactError> {
355    let mut buf = Vec::new();
356    to_writer(&mut buf, value)?;
357    Ok(buf)
358}
359
360/// Serialize `value` to a UTF-8 `String`.
361///
362/// # Allocations
363///
364/// Allocates one `Vec<u8>` via [`to_bytes`].  The subsequent conversion to
365/// `String` reuses that allocation in-place via [`String::from_utf8`] — no
366/// second allocation occurs.  When you only need raw bytes (e.g. for a network
367/// write), prefer [`to_bytes`] directly.
368pub fn to_edifact_string<T: EdifactSerialize>(value: &T) -> Result<String, EdifactError> {
369    let bytes = to_bytes(value)?;
370    String::from_utf8(bytes).map_err(|_| EdifactError::InvalidUtf8)
371}
372
373#[cfg(test)]
374mod tests {
375    use super::*;
376    use crate::event::{OwnedEdifactEvent, VecEmitter};
377    use std::borrow::Cow;
378
379    /// Render a sparse-emit result to wire bytes, which is what the shape of
380    /// the event stream actually has to produce.
381    fn sparse_to_wire(parts: &mut [(usize, usize, Cow<'_, str>)], tag: &str) -> String {
382        let mut buf = Vec::new();
383        {
384            let mut emitter = crate::WriterEmitter::new(&mut buf);
385            emit_sparse_segment(&mut emitter, tag, parts).expect("emit");
386            emitter.finish().expect("finish");
387        }
388        String::from_utf8(buf).expect("utf-8")
389    }
390
391    #[test]
392    fn emit_sparse_segment_fills_gaps_in_elements_and_components() {
393        let mut parts = vec![
394            (1, 2, Cow::Borrowed("293")),
395            (0, 0, Cow::Borrowed("MS")),
396            (3, 1, Cow::Borrowed("late")),
397        ];
398        // Element 2 is absent entirely, element 3 skips component 0, and
399        // element 1 skips component 1 — every gap holds its position.
400        assert_eq!(sparse_to_wire(&mut parts, "NAD"), "NAD+MS+::293++:late'");
401    }
402
403    #[test]
404    fn emit_sparse_segment_first_value_wins_on_a_duplicate_slot() {
405        // Emitting both would push `293` from component 2 to component 3 and
406        // silently corrupt every later position.
407        let mut parts = vec![
408            (0, 0, Cow::Borrowed("MS")),
409            (1, 0, Cow::Borrowed("first")),
410            (1, 0, Cow::Borrowed("second")),
411            (1, 2, Cow::Borrowed("293")),
412        ];
413        assert_eq!(sparse_to_wire(&mut parts, "NAD"), "NAD+MS+first::293'");
414    }
415
416    #[test]
417    fn emit_sparse_segment_with_no_parts_writes_a_bare_tag() {
418        assert_eq!(sparse_to_wire(&mut [], "UNS"), "UNS'");
419    }
420
421    struct BgmSegment {
422        doc_name_code: String,
423        pruef_id: String,
424        msg_function: Option<String>,
425    }
426
427    impl EdifactSerialize for BgmSegment {
428        fn edifact_serialize<E: EventEmitter>(&self, emitter: &mut E) -> Result<(), EdifactError> {
429            emitter.emit(EdifactEvent::StartSegment { tag: "BGM" })?;
430            emitter.emit(EdifactEvent::Element {
431                value: &self.doc_name_code,
432            })?;
433            emitter.emit(EdifactEvent::Element {
434                value: &self.pruef_id,
435            })?;
436            self.msg_function.edifact_serialize(emitter)?;
437            emitter.emit(EdifactEvent::EndSegment)?;
438            Ok(())
439        }
440    }
441
442    #[test]
443    fn vec_emitter_captures_segment_events() {
444        let seg = BgmSegment {
445            doc_name_code: "E03".to_owned(),
446            pruef_id: "11042".to_owned(),
447            msg_function: None,
448        };
449        let mut emitter = VecEmitter::default();
450        seg.edifact_serialize(&mut emitter).unwrap();
451
452        assert_eq!(
453            emitter.events[0],
454            OwnedEdifactEvent::StartSegment {
455                tag: "BGM".to_owned()
456            }
457        );
458        assert_eq!(emitter.events.last(), Some(&OwnedEdifactEvent::EndSegment));
459    }
460
461    #[test]
462    fn to_bytes_produces_valid_edifact() {
463        let seg = BgmSegment {
464            doc_name_code: "E03".to_owned(),
465            pruef_id: "11042".to_owned(),
466            msg_function: Some("9".to_owned()),
467        };
468        let bytes = to_bytes(&seg).unwrap();
469        assert_eq!(std::str::from_utf8(&bytes).unwrap(), "BGM+E03+11042+9'");
470    }
471
472    #[test]
473    fn option_none_emits_empty_element() {
474        let val: Option<String> = None;
475        let mut emitter = VecEmitter::default();
476        val.edifact_serialize(&mut emitter).unwrap();
477        assert_eq!(
478            emitter.events[0],
479            OwnedEdifactEvent::Element {
480                value: String::new()
481            }
482        );
483    }
484
485    #[test]
486    fn option_some_emits_value() {
487        let val: Option<String> = Some("TEST".to_owned());
488        let mut emitter = VecEmitter::default();
489        val.edifact_serialize(&mut emitter).unwrap();
490        assert_eq!(
491            emitter.events[0],
492            OwnedEdifactEvent::Element {
493                value: "TEST".to_owned()
494            }
495        );
496    }
497
498    #[test]
499    fn integer_types_serialize_without_alloc() {
500        let mut emitter = VecEmitter::default();
501        42u32.edifact_serialize(&mut emitter).unwrap();
502        assert_eq!(
503            emitter.events[0],
504            OwnedEdifactEvent::Element {
505                value: "42".to_owned()
506            }
507        );
508        // i128::MIN should fit exactly in the 40-byte buffer
509        let mut emitter2 = VecEmitter::default();
510        i128::MIN.edifact_serialize(&mut emitter2).unwrap();
511        assert_eq!(
512            emitter2.events[0],
513            OwnedEdifactEvent::Element {
514                value: "-170141183460469231731687303715884105728".to_owned()
515            }
516        );
517    }
518
519    #[test]
520    fn float_extremes_do_not_panic() {
521        use super::DecimalFloat;
522        // Rust Display for f64 picks the shortest round-trip form; a 320-byte buffer covers all values.
523        let mut emitter = VecEmitter::default();
524        DecimalFloat(f64::MAX)
525            .edifact_serialize(&mut emitter)
526            .unwrap();
527        let s = match &emitter.events[0] {
528            OwnedEdifactEvent::Element { value } => value.clone(),
529            _ => panic!("expected Element event"),
530        };
531        assert!(!s.is_empty());
532        // f32::MAX too
533        let mut emitter2 = VecEmitter::default();
534        DecimalFloat(f32::MAX)
535            .edifact_serialize(&mut emitter2)
536            .unwrap();
537        assert!(matches!(
538            &emitter2.events[0],
539            OwnedEdifactEvent::Element { .. }
540        ));
541    }
542
543    #[test]
544    fn vec_serializes_each_item() {
545        let segments = vec![
546            BgmSegment {
547                doc_name_code: "E03".to_owned(),
548                pruef_id: "11042".to_owned(),
549                msg_function: None,
550            },
551            BgmSegment {
552                doc_name_code: "E01".to_owned(),
553                pruef_id: "11043".to_owned(),
554                msg_function: None,
555            },
556        ];
557        let bytes = to_bytes(&segments).unwrap();
558        let s = std::str::from_utf8(&bytes).unwrap();
559        assert!(s.contains("BGM+E03+11042"));
560        assert!(s.contains("BGM+E01+11043"));
561    }
562}