edifact_types/d95b/mod.rs
1use crate::util::Parser;
2use edifact_types_macros::DisplayEdifact;
3use nom::{combinator::opt, IResult, Parser as _};
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7const VERSION: &str = "D95B";
8
9mod element;
10mod message;
11mod segment;
12mod types;
13
14// Re-Export on root level to keep compatibility
15pub use element::*;
16// pub use message::baplie::*;
17pub use message::coprar::*;
18pub use segment::*;
19pub use types::*;
20
21#[cfg(test)]
22mod test_segment;
23
24/// from: [official info](https://unece.org/fileadmin/DAM/trade/edifact/untdid/d422_s.htm)
25/// 6.1 Interchange structure
26///
27/// The Service String Advice, UNA, and the service segments UNB
28/// to UNZ shall appear in the below stated order in an
29/// interchange. There may be several functional groups or
30/// messages within an interchange and several messages in a
31/// functional group. A message consists of segments. The
32/// structures for segments and for data elements therein are
33/// shown in 6.2 and 6.3. The contents of the service segments
34/// are shown annex B. See also figure 1.
35///
36/// An interchange consists of:
37///
38/// x | x | x | x | Name | Abbr. | Req.
39/// --- | --- | --- | --- | --- | --- | ---
40/// o | o | o | o | Service String Advice | UNA | Conditional
41/// _ | _ | _ | _ | Interchange Header | UNB | Mandatory
42/// \| | _ | _ | _ | Functional Group Header | UNG | Conditional
43/// \| | \| | _ | _ | Message Header | UNH | Mandatory
44/// \| | \| | \| | | User Data Segments | | As required
45/// \| | \| | \| | _ | Message Trailer | UNT | Mandatory
46/// \| | \| | _ | _ | Functional Group Trailer | UNE | Conditional
47/// \| | _ | _ | _ | Interchange Trailer | UNZ | Mandatory
48///
49/// In addition to the above service segments, the service
50/// segment UNS can, when required, be used to divide a message
51/// into sections. See annex B (NOT IMPLEMENTED).
52#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, DisplayEdifact)]
53pub struct Interchange<T>
54where
55 T: std::fmt::Display,
56{
57 pub una: Option<UNA>,
58 pub unb: UNB,
59 pub segment: T,
60 pub unz: UNZ,
61}
62
63impl<'a, T: Default + Parser<&'a str, T, nom::error::Error<&'a str>> + std::fmt::Display>
64 Parser<&'a str, Interchange<T>, nom::error::Error<&'a str>> for Interchange<T>
65{
66 fn parse(input: &'a str) -> IResult<&'a str, Interchange<T>> {
67 let mut output = Interchange::default();
68 let (input, obj) = opt(UNA::parse).parse(input)?;
69 output.una = obj;
70 let (input, obj) = UNB::parse(input)?;
71 output.unb = obj;
72 let (input, t_obj) = T::parse(input)?;
73 output.segment = t_obj;
74 let (input, obj) = UNZ::parse(input)?;
75 output.unz = obj;
76 Ok((input, output))
77 }
78}