1use nom::bytes::complete::tag;
2use nom::bytes::complete::take_until;
3use nom::bytes::complete::take_while;
4use nom::character::complete::newline;
5use nom::combinator::opt;
6use nom::multi::separated_list0;
7use nom::sequence::delimited;
8use nom::IResult;
9use nom::Parser as _;
10
11pub mod dt;
12pub mod tm;
13
14#[cfg(feature = "v004010")]
19pub fn is_equal_payload<T: PartialEq>(
20 src: &crate::v004010::Transmission<T>,
21 target: &crate::v004010::Transmission<T>,
22) -> bool {
23 let Some(target_first) = target.functional_group.first() else {
24 return src.functional_group.is_empty();
25 };
26 src.functional_group
27 .iter()
28 .all(|item| item.eq(target_first))
29}
30
31pub fn parse_line<'a>(input: &'a str, segment_name: &str) -> IResult<&'a str, Vec<&'a str>> {
32 let tag_name = format!("{segment_name}*");
33 let (rest, vars) = delimited(tag(tag_name.as_str()), take_until("~"), tag("~")).parse(input)?;
34 let (_, vars) = separated_list0(
35 tag("*"),
36 take_while(|x: char| {
37 x != '*' && (x.is_alphanumeric() || x.is_whitespace() || x.is_ascii_punctuation())
38 }),
39 )
40 .parse(vars)?;
41 let (rest, _) = opt(newline).parse(rest)?;
43 Ok((rest, vars))
44}
45
46pub trait Parser<I, O, E> {
47 fn parse(str: I) -> IResult<I, O>;
48}
49
50pub fn unborrow_string(input: &&str) -> String {
51 input.to_string()
52}
53
54#[macro_export]
60macro_rules! num_element {
61 ($(#[$meta:meta])* $name:ident) => {
62 $(#[$meta])*
63 #[derive(Clone, Default, Debug, PartialEq, Eq)]
64 pub struct $name {
65 raw: ::std::string::String,
66 }
67
68 impl $name {
69 pub fn raw(&self) -> &str {
71 &self.raw
72 }
73 pub fn as_f64(&self) -> ::core::option::Option<f64> {
75 self.raw.parse().ok()
76 }
77 pub fn as_i64(&self) -> ::core::option::Option<i64> {
79 self.raw.parse().ok()
80 }
81 }
82
83 impl ::core::fmt::Display for $name {
84 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
85 f.write_str(&self.raw)
86 }
87 }
88
89 impl $crate::util::X12Element for $name {
90 fn from_x12(s: &str) -> Self {
91 Self { raw: s.to_string() }
92 }
93 }
94
95 impl ::serde::Serialize for $name {
96 fn serialize<S: ::serde::Serializer>(&self, ser: S) -> ::core::result::Result<S::Ok, S::Error> {
97 ser.serialize_str(&self.raw)
98 }
99 }
100
101 impl<'de> ::serde::Deserialize<'de> for $name {
102 fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> ::core::result::Result<Self, D::Error> {
103 let raw = <::std::string::String as ::serde::Deserialize>::deserialize(d)?;
104 ::core::result::Result::Ok(Self { raw })
105 }
106 }
107 };
108}
109
110#[macro_export]
115macro_rules! time_element {
116 ($(#[$meta:meta])* $name:ident) => {
117 $(#[$meta])*
118 #[derive(Clone, Default, Debug, PartialEq, Eq)]
119 pub struct $name {
120 raw: ::std::string::String,
121 }
122
123 impl $name {
124 pub fn raw(&self) -> &str {
126 &self.raw
127 }
128 pub fn time(&self) -> ::core::option::Option<::chrono::NaiveTime> {
130 ::chrono::NaiveTime::parse_from_str(&self.raw, "%H%M%S")
131 .or_else(|_| ::chrono::NaiveTime::parse_from_str(&self.raw, "%H%M"))
132 .ok()
133 }
134 }
135
136 impl ::core::fmt::Display for $name {
137 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
138 f.write_str(&self.raw)
139 }
140 }
141
142 impl $crate::util::X12Element for $name {
143 fn from_x12(s: &str) -> Self {
144 Self { raw: s.to_string() }
145 }
146 }
147
148 impl ::serde::Serialize for $name {
149 fn serialize<S: ::serde::Serializer>(&self, ser: S) -> ::core::result::Result<S::Ok, S::Error> {
150 ser.serialize_str(&self.raw)
151 }
152 }
153
154 impl<'de> ::serde::Deserialize<'de> for $name {
155 fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> ::core::result::Result<Self, D::Error> {
156 let raw = <::std::string::String as ::serde::Deserialize>::deserialize(d)?;
157 ::core::result::Result::Ok(Self { raw })
158 }
159 }
160 };
161}
162
163pub trait X12Element {
171 fn from_x12(s: &str) -> Self;
172}
173
174impl X12Element for String {
175 fn from_x12(s: &str) -> Self {
176 s.to_string()
177 }
178}
179
180#[macro_export]
191macro_rules! code_enum {
192 (
193 $(#[$meta:meta])*
194 $name:ident {
195 $($(#[$vmeta:meta])* $code:literal => $variant:ident),* $(,)?
196 }
197 ) => {
198 $(#[$meta])*
199 #[derive(Clone, Debug, PartialEq, Eq)]
200 pub enum $name {
201 $($(#[$vmeta])* $variant,)*
202 Unknown(String),
205 }
206
207 impl ::core::fmt::Display for $name {
208 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
209 let s = match self {
210 $(Self::$variant => $code,)*
211 Self::Unknown(s) => s.as_str(),
212 };
213 f.write_str(s)
214 }
215 }
216
217 impl $crate::util::X12Element for $name {
218 fn from_x12(s: &str) -> Self {
219 match s {
220 $($code => Self::$variant,)*
221 other => Self::Unknown(other.to_string()),
222 }
223 }
224 }
225
226 impl ::core::default::Default for $name {
230 fn default() -> Self {
231 Self::Unknown(::std::string::String::new())
232 }
233 }
234
235 impl ::serde::Serialize for $name {
236 fn serialize<S: ::serde::Serializer>(&self, ser: S) -> ::core::result::Result<S::Ok, S::Error> {
237 ser.serialize_str(&::std::string::ToString::to_string(self))
238 }
239 }
240
241 impl<'de> ::serde::Deserialize<'de> for $name {
242 fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> ::core::result::Result<Self, D::Error> {
243 let s = <::std::string::String as ::serde::Deserialize>::deserialize(d)?;
244 ::core::result::Result::Ok(<Self as $crate::util::X12Element>::from_x12(&s))
245 }
246 }
247 };
248}
249
250#[macro_export]
256macro_rules! date_element {
257 ($(#[$meta:meta])* $name:ident) => {
258 $(#[$meta])*
259 #[derive(Clone, Default, Debug, PartialEq, Eq)]
260 pub struct $name {
261 raw: ::std::string::String,
262 }
263
264 impl $name {
265 pub fn raw(&self) -> &str {
267 &self.raw
268 }
269 pub fn date(&self) -> ::core::option::Option<::chrono::NaiveDate> {
272 ::chrono::NaiveDate::parse_from_str(&self.raw, "%Y%m%d").ok()
273 }
274 pub fn from_date(d: ::chrono::NaiveDate) -> Self {
276 Self { raw: d.format("%Y%m%d").to_string() }
277 }
278 }
279
280 impl ::core::fmt::Display for $name {
281 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
282 f.write_str(&self.raw)
283 }
284 }
285
286 impl $crate::util::X12Element for $name {
287 fn from_x12(s: &str) -> Self {
288 Self { raw: s.to_string() }
289 }
290 }
291
292 impl ::serde::Serialize for $name {
293 fn serialize<S: ::serde::Serializer>(&self, ser: S) -> ::core::result::Result<S::Ok, S::Error> {
294 ser.serialize_str(&self.raw)
295 }
296 }
297
298 impl<'de> ::serde::Deserialize<'de> for $name {
299 fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> ::core::result::Result<Self, D::Error> {
300 let raw = <::std::string::String as ::serde::Deserialize>::deserialize(d)?;
301 ::core::result::Result::Ok(Self { raw })
302 }
303 }
304 };
305}