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
//! [`IftstaBuilder`] — fluent type-safe builder for IFTSTA messages.
use std::marker::PhantomData;
use edifact_rs::Writer;
use crate::AgencyCode;
use crate::{Error, Release};
use super::{Set, Unset, bytes_to_segments};
#[derive(Debug, Clone)]
struct IftstaBuilderInner {
release: Release,
sender_id: Option<String>,
receiver_id: Option<String>,
sender_agency: AgencyCode,
receiver_agency: AgencyCode,
message_ref: String,
document_code: String,
document_id: Option<String>,
document_date: Option<String>,
// WiM status (SG14/SG15) — only emitted when set (e.g. 21042 Umsetzungsstatus).
pruefidentifikator: Option<u32>,
sts_category: Option<String>,
sts_reason: Option<String>,
vorgangsnummer: Option<String>,
order_reference: Option<String>,
vertragsende: Option<String>,
}
/// Fluent builder for `IFTSTA` (International Multimodal Status Report) messages.
///
/// # Type-state
///
/// [`build`](IftstaBuilder::build) is only available once both
/// [`sender`](IftstaBuilder::sender) and [`receiver`](IftstaBuilder::receiver)
/// have been called.
///
/// # Example
///
/// ```rust,no_run
/// use edi_energy::Release;
/// use edi_energy::builders::IftstaBuilder;
///
/// let msg = IftstaBuilder::new(Release::new("2.0g"))
/// .sender("4012345000023")
/// .receiver("9900357000004")
/// .document_id("00021000")
/// .build()?;
///
/// assert_eq!(msg.sender().unwrap().party_id.as_deref(), Some("4012345000023"));
/// # Ok::<(), edi_energy::Error>(())
/// ```
#[derive(Debug, Clone)]
#[must_use = "Builder must be consumed via .build() or .serialize()"]
pub struct IftstaBuilder<S = Unset, R = Unset> {
_ph: PhantomData<fn() -> (S, R)>,
inner: IftstaBuilderInner,
}
impl IftstaBuilder<Unset, Unset> {
/// Create a builder targeting the given EDI@Energy release.
pub fn new(release: Release) -> Self {
Self {
_ph: PhantomData,
inner: IftstaBuilderInner {
release,
sender_id: None,
receiver_id: None,
sender_agency: AgencyCode::Bdew,
receiver_agency: AgencyCode::Bdew,
message_ref: "1".to_owned(),
document_code: "Z03".to_owned(),
document_id: None,
document_date: None,
pruefidentifikator: None,
sts_category: None,
sts_reason: None,
vorgangsnummer: None,
order_reference: None,
vertragsende: None,
},
}
}
}
impl<S, R> IftstaBuilder<S, R> {
fn transition<S2, R2>(self) -> IftstaBuilder<S2, R2> {
IftstaBuilder {
_ph: PhantomData,
inner: self.inner,
}
}
/// Set the message sender's market-participant identifier.
pub fn sender(mut self, id: impl Into<String>) -> IftstaBuilder<Set, R> {
self.inner.sender_id = Some(id.into());
self.transition()
}
/// Set the message recipient's market-participant identifier.
pub fn receiver(mut self, id: impl Into<String>) -> IftstaBuilder<S, Set> {
self.inner.receiver_id = Some(id.into());
self.transition()
}
/// Override the agency code for the sender's party identifier.
///
/// Default: [`AgencyCode::Bdew`] (`"293"`). Use [`AgencyCode::Etso`] (`"305"`)
/// for TSO/ÜNB parties that carry a 16-char EIC code.
pub fn sender_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.sender_agency = agency;
self
}
/// Override the agency code for the receiver's party identifier.
///
/// Default: [`AgencyCode::Bdew`] (`"293"`).
pub fn receiver_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.receiver_agency = agency;
self
}
/// Set the BGM document identifier (Dokumentennummer).
pub fn document_id(mut self, id: impl Into<String>) -> Self {
self.inner.document_id = Some(id.into());
self
}
/// Override the BGM document type code. Defaults to `"Z03"`.
pub fn document_code(mut self, code: impl Into<String>) -> Self {
self.inner.document_code = code.into();
self
}
/// Override the message reference number. Defaults to `"1"`.
pub fn message_ref(mut self, reference: impl Into<String>) -> Self {
self.inner.message_ref = reference.into();
self
}
/// Set the document date for DTM+137 (`YYYYMMDD`).
pub fn document_date(mut self, date: impl Into<String>) -> Self {
self.inner.document_date = Some(date.into());
self
}
/// Set the `WiM` Prüfidentifikator, emitted as SG15 `RFF+Z13:<pid>`
/// (e.g. `21042` — `WiM` / Umsetzungsstatus). Also stamped on the parsed
/// message so `detect_pruefidentifikator()` and AHB validation resolve it.
pub fn pruefidentifikator(mut self, pid: u32) -> Self {
self.inner.pruefidentifikator = Some(pid);
self
}
/// Set the SG15 `STS` status: DE9015 category + DE4405 reason
/// (e.g. `("Z21", "105")` = „Bestellung / beendet" for UC 4.4).
pub fn status(mut self, category: impl Into<String>, reason: impl Into<String>) -> Self {
self.inner.sts_category = Some(category.into());
self.inner.sts_reason = Some(reason.into());
self
}
/// Set the SG14 `CNI+<n>` Vorgangsnummer (mandatory for `WiM` status messages).
pub fn vorgangsnummer(mut self, n: impl Into<String>) -> Self {
self.inner.vorgangsnummer = Some(n.into());
self
}
/// Set the SG15 `RFF+AGI:<ref>` Beantragungsnummer — the Belegnummer of the
/// original Bestellung (ORDERS BGM) this status refers to.
pub fn order_reference(mut self, reference: impl Into<String>) -> Self {
self.inner.order_reference = Some(reference.into());
self
}
/// Set the SG15 `DTM+93` Datum Vertragsende (the Beendigung date, `YYYYMMDD`).
pub fn vertragsende(mut self, date: impl Into<String>) -> Self {
self.inner.vertragsende = Some(date.into());
self
}
fn to_bytes(&self) -> Result<Vec<u8>, Error> {
let dtm_val = self
.inner
.document_date
.as_deref()
.map_or_else(super::now_ccyymmddhhmm, str::to_owned);
let mut buf = Vec::new();
let mut w = Writer::new(&mut buf);
let doc_id = self.inner.document_id.as_deref().unwrap_or("");
emit_comp!(
w,
"UNH",
[&self.inner.message_ref],
["IFTSTA", "D", "18A", "UN", self.inner.release.as_str()]
);
emit_seg!(w, "BGM", &self.inner.document_code, doc_id);
// `DTM+137` Dokumentendatum. Every EDI@Energy AHB gives DE 2379 as
// `303` (`CCYYMMDDHHMMZZZ`) with condition `[931]` fixing the zone to
// `+00`; `[494]` requires the stamp to be the creation moment or
// earlier. There is no Anwendungsfall in any AHB that takes `102`.
emit_comp!(w, "DTM", ["137", &super::ccyymmddhhmm_utc(&dtm_val), "303"]);
if let Some(id) = &self.inner.sender_id {
emit_comp!(
w,
"NAD",
["MS"],
[id, "", self.inner.sender_agency.as_str()]
);
}
if let Some(id) = &self.inner.receiver_id {
emit_comp!(
w,
"NAD",
["MR"],
[id, "", self.inner.receiver_agency.as_str()]
);
}
// ── SG14 CNI — Vorgangsnummer ────────────────────────────────────────
if let Some(vn) = &self.inner.vorgangsnummer {
emit_seg!(w, "CNI", vn);
}
// ── SG15 STS — status category (DE9015) + reason (DE4405) ────────────
if let (Some(cat), Some(reason)) = (&self.inner.sts_category, &self.inner.sts_reason) {
emit_comp!(w, "STS", [cat], ["", reason]);
}
// ── SG15 RFF+Z13 — Prüfidentifikator ─────────────────────────────────
if let Some(pid) = self.inner.pruefidentifikator {
let pid_str = pid.to_string();
emit_comp!(w, "RFF", ["Z13", &pid_str]);
}
// ── SG15 RFF+AGI — Beantragungsnummer (ref to the Bestellung) ────────
if let Some(order_ref) = &self.inner.order_reference {
emit_comp!(w, "RFF", ["AGI", order_ref]);
}
// ── SG15 DTM+93 — Datum Vertragsende (Beendigung) ───────────────────
//
// IFTSTA AHB 2.1 gives DE 2379 as `303` here, like every other date in
// the message; `[931]` fixes the zone to `+00`.
if let Some(ende) = &self.inner.vertragsende {
emit_comp!(w, "DTM", ["93", &super::ccyymmddhhmm_utc(ende), "303"]);
}
w.finish_unt(&self.inner.message_ref)
.map_err(Error::Parse)?;
Ok(buf)
}
/// Build and serialize the message to EDIFACT bytes.
///
/// # Errors
///
/// Returns an [`Error`] if serialization fails.
pub fn serialize(self) -> Result<Vec<u8>, Error> {
self.to_bytes()
}
}
impl IftstaBuilder<Set, Set> {
/// Build and return a fully-parsed [`crate::messages::iftsta::IftstaMessage`].
///
/// # Errors
///
/// Returns an [`Error`] if EDIFACT serialization or parsing fails.
pub fn build(self) -> Result<crate::messages::iftsta::IftstaMessage, Error> {
let message_ref = self.inner.message_ref.clone();
let assoc_code = self.inner.release.as_str().to_owned();
let segments = bytes_to_segments(&self.to_bytes()?)?;
let pruefidentifikator = self.inner.pruefidentifikator;
Ok(crate::messages::iftsta::IftstaMessage::from_parts(
segments,
message_ref.as_str(),
assoc_code.as_str(),
pruefidentifikator,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::EdiEnergyMessage;
/// UC 4.4 „Beendigung durch MSB" (IFTSTA 21042) builds a message that
/// carries the Prüfidentifikator (SG15 RFF+Z13), the STS Z21/105 status,
/// the Vorgangsnummer (SG14 CNI) and passes AHB validation.
#[test]
fn builds_a_conformant_21042_beendigung() {
let msg = IftstaBuilder::new(Release::new("2.0g"))
.sender("4012345000023")
.receiver("9900555000005")
.document_code("Z09")
.document_id("00021042")
.pruefidentifikator(21042)
.status("Z21", "105")
.vorgangsnummer("1")
.order_reference("BEST-4711")
.vertragsende("20260801")
.build()
.expect("build 21042");
assert_eq!(msg.detect_pruefidentifikator().unwrap().as_u32(), 21042);
// The wire form carries the mandatory WiM status segments.
let wire = String::from_utf8(msg.serialize().expect("serialize")).unwrap();
assert!(
wire.contains("RFF+Z13:21042"),
"PID in SG15 RFF+Z13: {wire}"
);
assert!(
wire.contains("STS+Z21+:105"),
"STS Z21/105 „beendet\": {wire}"
);
assert!(wire.contains("CNI+1"), "Vorgangsnummer SG14 CNI: {wire}");
assert!(
wire.contains("RFF+AGI:BEST-4711"),
"order ref SG15 RFF+AGI: {wire}"
);
let report = msg.validate().unwrap();
assert!(report.is_valid(), "21042 must be AHB-conformant: {report}");
}
}