edi-energy 0.15.0

EDI@Energy EDIFACT parser and validator for the German energy market
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! [`UtiltsBuilder`] — fluent type-safe builder for UTILTS messages.

use std::marker::PhantomData;

use edifact_rs::Writer;

use crate::AgencyCode;
use crate::{Error, Release};

use super::{Set, Unset, bytes_to_segments};

// ── UTILTS-specific DTM helpers (format 303) ──────────────────────────────────
//
// These return only the DE 2380 datetime value; qualifier and format code are
// separate components at the emit site.

fn midnight_303(date: &str) -> String {
    format!("{date}0000+0000")
}

fn now_303() -> String {
    let now = time::OffsetDateTime::now_utc();
    let (y, mo, d, h, m) = (
        now.year(),
        now.month() as u8,
        now.day(),
        now.hour(),
        now.minute(),
    );
    format!("{y:04}{mo:02}{d:02}{h:02}{m:02}+0000")
}

// ── Body structs (public, re-exported from mod.rs) ────────────────────────────

/// A single usage-period block (SG6 `RFF+Z49/Z53` + `DTM+Z25` + optional `DTM+Z26`).
#[derive(Debug, Clone)]
pub struct UtiltsUsagePeriod {
    qualifier: String,
    period_id: u32,
    usage_from: String,
    usage_to: Option<String>,
}

impl UtiltsUsagePeriod {
    /// Create a usage-period block.
    ///
    /// - `period_id`: sequence number starting at 1 (oldest period first).
    /// - `usage_from`: format-303 datetime string, e.g. `"202710012200+00"`.
    pub fn new(period_id: u32, usage_from: impl Into<String>) -> Self {
        Self {
            qualifier: "Z49".to_owned(),
            period_id,
            usage_from: usage_from.into(),
            usage_to: None,
        }
    }

    /// Set qualifier to `"Z53"` (Viertelstundenwerte).  Defaults to `"Z49"`.
    pub fn qualifier(mut self, q: impl Into<String>) -> Self {
        self.qualifier = q.into();
        self
    }

    /// Set `DTM+Z26` (Verwendung der Daten bis) for this period.
    pub fn usage_to(mut self, datetime: impl Into<String>) -> Self {
        self.usage_to = Some(datetime.into());
        self
    }
}

/// A reference to an energy-amount result (SG8 `SEQ+Z36`).
#[derive(Debug, Clone)]
pub struct UtiltsEnergyAmountRef {
    time_period_id: u32,
    final_step_id: u32,
}

impl UtiltsEnergyAmountRef {
    /// Create a reference to the energy-amount result.
    ///
    /// - `time_period_id`: Zeitraum-ID (DE 1154 in `RFF+Z46`).
    /// - `final_step_id`: Rechenschrittidentifikator (DE 1154 in `RFF+Z23`).
    #[must_use]
    pub fn new(time_period_id: u32, final_step_id: u32) -> Self {
        Self {
            time_period_id,
            final_step_id,
        }
    }
}

/// One calculation-step component (SG8 `SEQ+Z37+{step_id}`).
///
/// # Example
///
/// ```no_run
/// use edi_energy::builders::UtiltsCalcStep;
/// let step = UtiltsCalcStep::new(1)
///     .time_period(1)
///     .messlokation("DE00000456789012345678900000000003D")
///     .operator("Z69")
///     .energy_direction("Z71");
/// ```
#[derive(Debug, Clone)]
pub struct UtiltsCalcStep {
    step_id: u32,
    time_period_id: Option<u32>,
    messlokation_id: Option<String>,
    ref_calc_step_id: Option<u32>,
    operator: Option<String>,
    energy_direction: Option<String>,
    loss_factor_trafo: Option<String>,
    loss_factor_line: Option<String>,
    split_factor: Option<String>,
}

impl UtiltsCalcStep {
    /// Create a calculation-step component.
    ///
    /// `step_id` is the Rechenschrittidentifikator (DE 1050 in `SEQ+Z37`).
    #[must_use]
    pub fn new(step_id: u32) -> Self {
        Self {
            step_id,
            time_period_id: None,
            messlokation_id: None,
            ref_calc_step_id: None,
            operator: None,
            energy_direction: None,
            loss_factor_trafo: None,
            loss_factor_line: None,
            split_factor: None,
        }
    }

    /// Set the Zeitraum-ID reference (`RFF+Z46:{id}`).
    #[must_use]
    pub fn time_period(mut self, id: u32) -> Self {
        self.time_period_id = Some(id);
        self
    }

    /// Set the Messlokations-ID reference (`RFF+Z19:{id}`).
    pub fn messlokation(mut self, id: impl Into<String>) -> Self {
        self.messlokation_id = Some(id.into());
        self
    }

    /// Set a reference to another calculation step (`RFF+Z23:{id}`).
    #[must_use]
    pub fn ref_calc_step(mut self, id: u32) -> Self {
        self.ref_calc_step_id = Some(id);
        self
    }

    /// Set the mathematical operator (`CAV` in `SG9 CCI+++Z86`).
    ///
    /// Codes: `Z69` (Addition), `Z70` (Subtraktion), `Z80`–`Z85` (other ops).
    pub fn operator(mut self, code: impl Into<String>) -> Self {
        self.operator = Some(code.into());
        self
    }

    /// Set the energy flow direction (`CAV` in `SG9 CCI+++Z87`).
    ///
    /// - `Z71` — Verbrauch, `Z72` — Erzeugung.
    pub fn energy_direction(mut self, code: impl Into<String>) -> Self {
        self.energy_direction = Some(code.into());
        self
    }

    /// Set the transformer loss factor (`SG9 CCI+++Z16`, `CAV+Z28:::{value}`).
    pub fn loss_factor_trafo(mut self, value: impl Into<String>) -> Self {
        self.loss_factor_trafo = Some(value.into());
        self
    }

    /// Set the line loss factor (`SG9 CCI+++ZB2`, `CAV+Z28:::{value}`).
    pub fn loss_factor_line(mut self, value: impl Into<String>) -> Self {
        self.loss_factor_line = Some(value.into());
        self
    }

    /// Set the energy split factor (`SG9 CCI+++ZG6`, `CAV+ZH6:::{value}`).
    pub fn split_factor(mut self, value: impl Into<String>) -> Self {
        self.split_factor = Some(value.into());
        self
    }
}

/// A time-switching / load-curve / time-registration definition block (SG8 `SEQ+Z42` etc.).
///
/// # Example
///
/// ```no_run
/// use edi_energy::builders::UtiltsDefinitionBlock;
/// let block = UtiltsDefinitionBlock::new("Z42")
///     .definition_code("ZZ1")
///     .frequency("Z33")
///     .transmissibility("Z23");
/// ```
#[derive(Debug, Clone)]
pub struct UtiltsDefinitionBlock {
    seq_qualifier: String,
    change_time: Option<String>,
    register_code: Option<String>,
    ref_definition_code: Option<String>,
    definition_code: Option<String>,
    frequency: Option<String>,
    transmissibility: Option<String>,
    peak_load_detection: Option<String>,
    orderable: Option<String>,
    switching_action: Option<String>,
}

impl UtiltsDefinitionBlock {
    /// Create a definition block.
    ///
    /// `seq_qualifier`: `Z42` (Zählzeitübersicht), `Z43` (ausgerollt), `Z41` (Register),
    /// `Z69` (Schaltzeitübersicht), `Z70`/`Z74` (Leistungskurve).
    pub fn new(seq_qualifier: impl Into<String>) -> Self {
        Self {
            seq_qualifier: seq_qualifier.into(),
            change_time: None,
            register_code: None,
            ref_definition_code: None,
            definition_code: None,
            frequency: None,
            transmissibility: None,
            peak_load_detection: None,
            orderable: None,
            switching_action: None,
        }
    }

    /// Set the change-time for this definition (DTM+Z33/Z44/Z45, format-303).
    pub fn change_time(mut self, datetime: impl Into<String>) -> Self {
        self.change_time = Some(datetime.into());
        self
    }

    /// Set the active register code (`RFF+Z28:{code}`).
    pub fn register_code(mut self, code: impl Into<String>) -> Self {
        self.register_code = Some(code.into());
        self
    }

    /// Set the parent-definition reference code (`RFF+Z28:{code}`) for Register blocks.
    pub fn ref_definition_code(mut self, code: impl Into<String>) -> Self {
        self.ref_definition_code = Some(code.into());
        self
    }

    /// Set the definition code emitted in `SG9 CCI+{type}++{code}`.
    pub fn definition_code(mut self, code: impl Into<String>) -> Self {
        self.definition_code = Some(code.into());
        self
    }

    /// Set the frequency code (`CAV+ZE0:::{code}`).  `Z33` or `Z34`.
    pub fn frequency(mut self, code: impl Into<String>) -> Self {
        self.frequency = Some(code.into());
        self
    }

    /// Set the transmissibility code (`CAV+ZD5:::{code}`).  `Z23` or `Z24`.
    pub fn transmissibility(mut self, code: impl Into<String>) -> Self {
        self.transmissibility = Some(code.into());
        self
    }

    /// Set the peak-load detection method (`CAV+ZD4:::{code}`).  `Z25` or `Z26`.
    pub fn peak_load_detection(mut self, code: impl Into<String>) -> Self {
        self.peak_load_detection = Some(code.into());
        self
    }

    /// Set the orderability code (`CAV+ZD7:::{code}`).  `Z27` or `Z28`.
    pub fn orderable(mut self, code: impl Into<String>) -> Self {
        self.orderable = Some(code.into());
        self
    }

    /// Set the switching action code for Schaltzeitdefinitionen (`SG9 CCI+Z58++{code}`).
    pub fn switching_action(mut self, code: impl Into<String>) -> Self {
        self.switching_action = Some(code.into());
        self
    }
}

// ── UtiltsVorgang ─────────────────────────────────────────────────────────────

/// A single Vorgang (SG5 transaction block) in a UTILTS message.
///
/// # Example
///
/// ```no_run
/// use edi_energy::builders::{UtiltsCalcStep, UtiltsEnergyAmountRef, UtiltsUsagePeriod, UtiltsVorgang};
/// let vorgang = UtiltsVorgang::new("V001", 25001)
///     .location("DE0000012345678907234567890123456789")
///     .formula_status("Z33", 1)
///     .add_usage_period(UtiltsUsagePeriod::new(1, "202710012200+00"))
///     .add_energy_ref(UtiltsEnergyAmountRef::new(1, 1))
///     .add_calc_step(
///         UtiltsCalcStep::new(1)
///             .time_period(1)
///             .messlokation("DE00000456789012345678900000000003D")
///             .operator("Z69")
///             .energy_direction("Z71"),
///     );
/// ```
#[derive(Debug, Clone)]
pub struct UtiltsVorgang {
    /// The Vorgangsnummer (DE 7402 in IDE+24).
    pub transaction_id: String,
    /// The Prüfidentifikator code (DE 1154 in SG6 RFF+Z13).
    pub pruefidentifikator: u32,
    location_id: Option<String>,
    definition_code: Option<String>,
    valid_from: Option<String>,
    formula_status_code: Option<String>,
    formula_status_period: Option<u32>,
    ref_transaction_id: Option<String>,
    usage_periods: Vec<UtiltsUsagePeriod>,
    energy_amount_refs: Vec<UtiltsEnergyAmountRef>,
    calc_steps: Vec<UtiltsCalcStep>,
    definition_blocks: Vec<UtiltsDefinitionBlock>,
}

impl UtiltsVorgang {
    /// Create a new Vorgang.
    ///
    /// - `transaction_id`: the Vorgangsnummer (globally unique, max 35 chars).
    /// - `pruefidentifikator`: the PID code (e.g. `25001`).
    pub fn new(transaction_id: impl Into<String>, pruefidentifikator: u32) -> Self {
        Self {
            transaction_id: transaction_id.into(),
            pruefidentifikator,
            location_id: None,
            definition_code: None,
            valid_from: None,
            formula_status_code: None,
            formula_status_period: None,
            ref_transaction_id: None,
            usage_periods: Vec::new(),
            energy_amount_refs: Vec::new(),
            calc_steps: Vec::new(),
            definition_blocks: Vec::new(),
        }
    }

    /// Set the Meldepunkt ID for this Vorgang (`LOC+172`).
    pub fn location(mut self, malo_id: impl Into<String>) -> Self {
        self.location_id = Some(malo_id.into());
        self
    }

    /// Set the Code der Definition (`LOC+Z09+{code}`).
    pub fn definition_code(mut self, code: impl Into<String>) -> Self {
        self.definition_code = Some(code.into());
        self
    }

    /// Set the validity start time (`DTM+157:{datetime}:303`).
    pub fn valid_from(mut self, datetime: impl Into<String>) -> Self {
        self.valid_from = Some(datetime.into());
        self
    }

    /// Set the Berechnungsformel status (`STS+Z23+{code}+{period_id}`).
    pub fn formula_status(mut self, code: impl Into<String>, period_id: u32) -> Self {
        self.formula_status_code = Some(code.into());
        self.formula_status_period = Some(period_id);
        self
    }

    /// Set a reference to a previous Berechnungsformel Vorgang (`SG6 RFF+TN:{id}`).
    pub fn ref_transaction(mut self, id: impl Into<String>) -> Self {
        self.ref_transaction_id = Some(id.into());
        self
    }

    /// Add a usage-period block (SG6).
    #[must_use]
    pub fn add_usage_period(mut self, period: UtiltsUsagePeriod) -> Self {
        self.usage_periods.push(period);
        self
    }

    /// Add an energy-amount reference (SG8 `SEQ+Z36`).
    #[must_use]
    pub fn add_energy_ref(mut self, r: UtiltsEnergyAmountRef) -> Self {
        self.energy_amount_refs.push(r);
        self
    }

    /// Add a calculation-step component (SG8 `SEQ+Z37`).
    #[must_use]
    pub fn add_calc_step(mut self, step: UtiltsCalcStep) -> Self {
        self.calc_steps.push(step);
        self
    }

    /// Add a definition block (SG8 `SEQ+Z42`/`Z43`/`Z41`/`Z69`/`Z70`/`Z74`).
    #[must_use]
    pub fn add_definition_block(mut self, block: UtiltsDefinitionBlock) -> Self {
        self.definition_blocks.push(block);
        self
    }
}

// ── UtiltsBuilder ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
struct UtiltsBuilderInner {
    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>,
    vorgaenge: Vec<UtiltsVorgang>,
}

/// Fluent builder for `UTILTS` (Übertragung technischer Stammdaten) messages.
///
/// # Type-state
///
/// [`build`](UtiltsBuilder::build) is only available once both
/// [`sender`](UtiltsBuilder::sender) and [`receiver`](UtiltsBuilder::receiver)
/// have been called.
///
/// # Example
///
/// ```rust,no_run
/// use edi_energy::Release;
/// use edi_energy::builders::{UtiltsBuilder, UtiltsVorgang, UtiltsUsagePeriod,
///                             UtiltsEnergyAmountRef, UtiltsCalcStep};
///
/// let msg = UtiltsBuilder::new(Release::new("1.1e"))
///     .sender("9900259000002")
///     .receiver("9900259000001")
///     .document_id("MKIDI5422")
///     .add_vorgang(
///         UtiltsVorgang::new("V001", 25001)
///             .location("DE0000012345678907234567890123456789")
///             .formula_status("Z33", 1)
///             .add_usage_period(UtiltsUsagePeriod::new(1, "202710012200+00"))
///             .add_energy_ref(UtiltsEnergyAmountRef::new(1, 1))
///             .add_calc_step(
///                 UtiltsCalcStep::new(1)
///                     .time_period(1)
///                     .messlokation("DE00000456789012345678900000000003D")
///                     .operator("Z69")
///                     .energy_direction("Z71"),
///             ),
///     )
///     .build()?;
/// # Ok::<(), edi_energy::Error>(())
/// ```
#[derive(Debug, Clone)]
#[must_use = "Builder must be consumed via .build() or .serialize()"]
pub struct UtiltsBuilder<S = Unset, R = Unset> {
    _ph: PhantomData<fn() -> (S, R)>,
    inner: UtiltsBuilderInner,
}

impl UtiltsBuilder<Unset, Unset> {
    /// Create a builder targeting the given EDI@Energy release.
    pub fn new(release: Release) -> Self {
        Self {
            _ph: PhantomData,
            inner: UtiltsBuilderInner {
                release,
                sender_id: None,
                receiver_id: None,
                sender_agency: AgencyCode::Bdew,
                receiver_agency: AgencyCode::Bdew,
                message_ref: "1".to_owned(),
                document_code: "Z36".to_owned(),
                document_id: None,
                document_date: None,
                vorgaenge: Vec::new(),
            },
        }
    }
}

impl<S, R> UtiltsBuilder<S, R> {
    fn transition<S2, R2>(self) -> UtiltsBuilder<S2, R2> {
        UtiltsBuilder {
            _ph: PhantomData,
            inner: self.inner,
        }
    }

    /// Set the message sender's market-participant identifier.
    pub fn sender(mut self, id: impl Into<String>) -> UtiltsBuilder<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>) -> UtiltsBuilder<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::Entso`] (`"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.
    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 `"Z36"` (Berechnungsformel).
    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 (`YYYYMMDD`) for DTM+137 (emitted in format 303, midnight UTC).
    pub fn document_date(mut self, date: impl Into<String>) -> Self {
        self.inner.document_date = Some(date.into());
        self
    }

    /// Set a full format-303 datetime (`CCYYMMDDHHMMZZZ`) for DTM+137.
    pub fn document_datetime(mut self, datetime: impl Into<String>) -> Self {
        self.inner.document_date = Some(format!("303:{}", datetime.into()));
        self
    }

    /// Add a Vorgang (SG5 transaction block) to the message.
    pub fn add_vorgang(mut self, vorgang: UtiltsVorgang) -> Self {
        self.inner.vorgaenge.push(vorgang);
        self
    }

    #[allow(clippy::too_many_lines)]
    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        let dtm_val = match self.inner.document_date.as_deref() {
            Some(d) if d.starts_with("303:") => d["303:".len()..].to_owned(),
            Some(d) => midnight_303(d),
            None => now_303(),
        };

        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],
            ["UTILTS", "D", "18A", "UN", self.inner.release.as_str()]
        );
        emit_seg!(w, "BGM", &self.inner.document_code, doc_id);
        emit_comp!(w, "DTM", ["137", &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()]
            );
        }

        for vorgang in &self.inner.vorgaenge {
            emit_seg!(w, "IDE", "24", &vorgang.transaction_id);
            if let Some(malo) = &vorgang.location_id {
                emit_seg!(w, "LOC", "172", malo);
            }
            if let Some(code) = &vorgang.definition_code {
                emit_seg!(w, "LOC", "Z09", code);
            }
            if let Some(vf) = &vorgang.valid_from {
                emit_comp!(w, "DTM", ["157", vf, "303"]);
            }
            if let (Some(code), Some(pid)) =
                (&vorgang.formula_status_code, &vorgang.formula_status_period)
            {
                emit_seg!(w, "STS", "Z23", code, &pid.to_string());
            }
            emit_comp!(w, "RFF", ["Z13", &vorgang.pruefidentifikator.to_string()]);
            if let Some(ref_id) = &vorgang.ref_transaction_id {
                emit_comp!(w, "RFF", ["TN", ref_id]);
            }
            for period in &vorgang.usage_periods {
                emit_comp!(
                    w,
                    "RFF",
                    [&period.qualifier, "", &period.period_id.to_string()]
                );
                emit_comp!(w, "DTM", ["Z25", &period.usage_from, "303"]);
                if let Some(to) = &period.usage_to {
                    emit_comp!(w, "DTM", ["Z26", to, "303"]);
                }
            }
            for er in &vorgang.energy_amount_refs {
                emit_seg!(w, "SEQ", "Z36");
                emit_comp!(w, "RFF", ["Z46", &er.time_period_id.to_string()]);
                emit_comp!(w, "RFF", ["Z23", &er.final_step_id.to_string()]);
            }
            for step in &vorgang.calc_steps {
                emit_seg!(w, "SEQ", "Z37", &step.step_id.to_string());
                if let Some(tp) = step.time_period_id {
                    emit_comp!(w, "RFF", ["Z46", &tp.to_string()]);
                }
                if let Some(malo) = &step.messlokation_id {
                    emit_comp!(w, "RFF", ["Z19", malo]);
                }
                if let Some(rs) = step.ref_calc_step_id {
                    emit_comp!(w, "RFF", ["Z23", &rs.to_string()]);
                }
                if let Some(op) = &step.operator {
                    emit_seg!(w, "CCI", "", "", "Z86");
                    emit_seg!(w, "CAV", op);
                }
                if let Some(dir) = &step.energy_direction {
                    emit_seg!(w, "CCI", "", "", "Z87");
                    emit_seg!(w, "CAV", dir);
                }
                if let Some(vt) = &step.loss_factor_trafo {
                    emit_seg!(w, "CCI", "", "", "Z16");
                    emit_comp!(w, "CAV", ["Z28", "", "", vt]);
                }
                if let Some(vl) = &step.loss_factor_line {
                    emit_seg!(w, "CCI", "", "", "ZB2");
                    emit_comp!(w, "CAV", ["Z28", "", "", vl]);
                }
                if let Some(af) = &step.split_factor {
                    emit_seg!(w, "CCI", "", "", "ZG6");
                    emit_comp!(w, "CAV", ["ZH6", "", "", af]);
                }
            }
            for block in &vorgang.definition_blocks {
                emit_seg!(w, "SEQ", &block.seq_qualifier);
                if let Some(ct) = &block.change_time {
                    let dtm_q = match block.seq_qualifier.as_str() {
                        "Z69" => "Z44",
                        "Z70" | "Z74" => "Z45",
                        _ => "Z33",
                    };
                    emit_comp!(w, "DTM", [dtm_q, ct, "303"]);
                }
                if let Some(rc) = &block.register_code {
                    emit_comp!(w, "RFF", ["Z28", rc]);
                }
                if let Some(rc) = &block.ref_definition_code {
                    emit_comp!(w, "RFF", ["Z28", rc]);
                }
                if let Some(def_code) = &block.definition_code {
                    let cci_type = match block.seq_qualifier.as_str() {
                        "Z69" => "Z52",
                        "Z70" | "Z74" => "Z53",
                        _ => "Z39",
                    };
                    emit_seg!(w, "CCI", cci_type, "", def_code);
                    if let Some(freq) = &block.frequency {
                        emit_comp!(w, "CAV", ["ZE0", "", "", freq]);
                    }
                    if let Some(tr) = &block.transmissibility {
                        emit_comp!(w, "CAV", ["ZD5", "", "", tr]);
                    }
                    if let Some(pl) = &block.peak_load_detection {
                        emit_comp!(w, "CAV", ["ZD4", "", "", pl]);
                    }
                    if let Some(ord) = &block.orderable {
                        emit_comp!(w, "CAV", ["ZD7", "", "", ord]);
                    }
                }
                if let Some(sa) = &block.switching_action {
                    emit_seg!(w, "CCI", "Z58", "", sa);
                }
            }
        }

        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 UtiltsBuilder<Set, Set> {
    /// Build and return a fully-parsed [`crate::messages::utilts::UtiltsMessage`].
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if EDIFACT serialization or parsing fails.
    pub fn build(self) -> Result<crate::messages::utilts::UtiltsMessage, Error> {
        let pid = self.inner.vorgaenge.first().map(|v| v.pruefidentifikator);
        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::utilts::UtiltsMessage::from_parts(
            segments,
            message_ref.as_str(),
            assoc_code.as_str(),
            pid,
        ))
    }
}