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
//! [`AperakBuilder`] — fluent type-safe builder for APERAK messages.
use std::marker::PhantomData;
use edifact_rs::Writer;
use crate::AgencyCode;
use crate::{Error, Pruefidentifikator, Release};
use super::{Set, Unset, bytes_to_segments};
#[derive(Debug, Clone)]
struct AperakBuilderInner {
release: Release,
sender_id: Option<String>,
receiver_id: Option<String>,
sender_agency: Option<AgencyCode>,
receiver_agency: Option<AgencyCode>,
message_ref: String,
document_code: String,
document_id: Option<String>,
acw_ref: Option<String>,
error_code: Option<String>,
error_text: Option<String>,
document_date: Option<String>,
}
/// Fluent builder for `APERAK` (Application Error and Acknowledgement) messages.
///
/// # Type-state
///
/// [`build`](AperakBuilder::build) is only available once both
/// [`sender`](AperakBuilder::sender) and [`receiver`](AperakBuilder::receiver)
/// have been called.
#[derive(Debug, Clone)]
#[must_use = "Builder must be consumed via .build() or .serialize()"]
pub struct AperakBuilder<S = Unset, R = Unset> {
_ph: PhantomData<fn() -> (S, R)>,
inner: AperakBuilderInner,
}
impl AperakBuilder<Unset, Unset> {
/// Create a builder targeting the given EDI@Energy release.
pub fn new(release: Release) -> Self {
Self {
_ph: PhantomData,
inner: AperakBuilderInner {
release,
sender_id: None,
receiver_id: None,
sender_agency: None,
receiver_agency: None,
message_ref: "1".to_owned(),
document_code: "1000".to_owned(),
document_id: None,
acw_ref: None,
error_code: None,
error_text: None,
document_date: None,
},
}
}
}
impl<S, R> AperakBuilder<S, R> {
/// Address this APERAK as the acknowledgement of a received message.
///
/// Mirrors the parties (the original receiver becomes the sender), carries
/// the acknowledged message's UNH reference into `RFF+ACW`, and adopts its
/// transmission date. Those three fields are what correlate an
/// acknowledgement with what it acknowledges, and deriving them from the
/// received message is the only way they cannot drift apart.
///
/// The release is the **APERAK's own**, set on [`new`](Self::new) — not the
/// release of the message being acknowledged, which is usually a different
/// message type on a different track.
///
/// ```rust,no_run
/// # #[cfg(all(feature = "aperak", feature = "utilmd"))]
/// # fn main() -> Result<(), edi_energy::Error> {
/// use edi_energy::{Platform, Release};
/// use edi_energy::builders::AperakBuilder;
///
/// # let wire: &[u8] = b"";
/// let received = Platform::with_all_profiles().parse_interchange_full(wire)?;
/// let ack = AperakBuilder::new(Release::new("2.1i"))
/// .for_receipt(&received.messages[0].receipt_context())
/// .error_code("Z10")
/// .serialize()?;
/// # Ok(())
/// # }
/// # #[cfg(not(all(feature = "aperak", feature = "utilmd")))]
/// # fn main() {}
/// ```
pub fn for_receipt(
mut self,
ctx: &crate::interchange::ReceiptContext<'_>,
) -> AperakBuilder<Set, Set> {
self.inner.sender_id = Some(ctx.original_receiver.to_owned());
self.inner.receiver_id = Some(ctx.original_sender.to_owned());
self.inner.acw_ref = Some(ctx.message_ref.to_owned());
if let Some(date) = ctx.transmission_date {
self.inner.document_date = Some(format!(
"{:04}{:02}{:02}",
date.year(),
date.month() as u8,
date.day()
));
}
self.transition()
}
fn transition<S2, R2>(self) -> AperakBuilder<S2, R2> {
AperakBuilder {
_ph: PhantomData,
inner: self.inner,
}
}
/// Set the message sender's market-participant identifier.
pub fn sender(mut self, id: impl Into<String>) -> AperakBuilder<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>) -> AperakBuilder<S, Set> {
self.inner.receiver_id = Some(id.into());
self.transition()
}
/// Override the agency code for the sender's party identifier.
///
/// Leave unset and the agency is derived from the MP-ID itself —
/// [`AgencyCode::for_mp_id`]: `99…` → BDEW `293`, `98…` → DVGW `332`, any
/// other 13-digit code → GS1 `9`. Override only for a party whose
/// registered code list differs from what its number implies.
pub fn sender_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.sender_agency = Some(agency);
self
}
/// Override the agency code for the receiver's party identifier.
///
/// Derived from the MP-ID when unset — see
/// [`sender_agency`](Self::sender_agency).
pub fn receiver_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.receiver_agency = Some(agency);
self
}
/// Set the Prüfidentifikator (BGM document identifier).
pub fn pruefidentifikator(mut self, pid: Pruefidentifikator) -> Self {
self.inner.document_id = Some(pid.as_u32().to_string());
self
}
/// Set the acknowledgement reference number (RFF+ACW).
pub fn acw_ref(mut self, reference: impl Into<String>) -> Self {
self.inner.acw_ref = Some(reference.into());
self
}
/// Set an application error code (ERC segment).
pub fn error_code(mut self, code: impl Into<String>) -> Self {
self.inner.error_code = Some(code.into());
self
}
/// Set a free-text error description (FTX+AAI).
pub fn error_text(mut self, text: impl Into<String>) -> Self {
self.inner.error_text = Some(text.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
}
/// Override the BGM document function code (DE1001).
///
/// The default is `"1000"` (standard EDIFACT APERAK code).
/// For BDEW-specific message classes:
/// - `"312"` — Anerkennungsmeldung (positive acknowledgement)
/// - `"313"` — Verarbeitbarkeitsfehlermeldung (processing error rejection,
/// BGM+313 per BDEW APERAK AHB 1.0 §2.1.1)
///
/// In most cases, the EDIFACT renderer in `makod` sets this
/// automatically based on whether an `error_code` is present.
pub fn document_code(mut self, code: impl Into<String>) -> Self {
self.inner.document_code = code.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],
["APERAK", "D", "07B", "UN", self.inner.release.as_str()]
);
emit_seg!(w, "BGM", &self.inner.document_code, doc_id, "9");
// `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, "", super::agency_for(self.inner.sender_agency, id)]
);
}
if let Some(id) = &self.inner.receiver_id {
emit_comp!(
w,
"NAD",
["MR"],
[id, "", super::agency_for(self.inner.receiver_agency, id)]
);
}
if let Some(r) = &self.inner.acw_ref {
emit_comp!(w, "RFF", ["ACW", r]);
}
if let Some(code) = &self.inner.error_code {
emit_seg!(w, "ERC", code);
}
if let Some(text) = &self.inner.error_text {
emit_comp!(w, "FTX", ["AAI"], [""], [""], [text]);
}
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 AperakBuilder<Set, Set> {
/// Build and return a fully-parsed [`crate::messages::aperak::AperakMessage`].
///
/// # Errors
///
/// Returns an [`Error`] if EDIFACT serialization or parsing fails.
pub fn build(self) -> Result<crate::messages::aperak::AperakMessage, Error> {
let pid = self
.inner
.document_id
.as_deref()
.and_then(|s| s.parse::<u32>().ok());
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()?)?;
Ok(crate::messages::aperak::AperakMessage::from_parts(
segments,
message_ref.as_str(),
assoc_code.as_str(),
pid,
))
}
}