automapper-validation 0.50.3

AHB condition expression parsing, evaluation, and EDIFACT validation
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
//! What a message must keep consistent within itself, beyond what its AHB
//! conditions state: the APERAK errors Z34 and Z21.
//!
//! - **Z34, "Zeitintervall negativ oder Null":** a period whose end is not
//!   after its beginning. A period is a pair of DTM qualifiers that name its
//!   two ends ([`INTERVALS`]), sent side by side in one segment group.
//! - **Z21, "Geschäftsvorfallinterne Referenzierung fehlerhaft":** a UTILMD
//!   SG8 or SG12 `RFF` that refers to a location of its transaction
//!   ("Zur Referenzierung auf eine ID einer Messlokation vom SG5 LOC+Z17",
//!   UTILMD MIG) naming none ([`REFERENCES`], [`REFERENCES_GAS`]); or an SG8
//!   `RFF` that refers to a device of its transaction (RFF+Z14 "Referenz auf
//!   das Smartmeter-Gateway") naming none ([`device_reference_targets`]).
//!
//! Both are judged only where the message itself settles them. A period
//! whose ends are sent in different formats, and a reference in a
//! transaction without any location of the kind it refers to, are left
//! alone: no error is reported rather than a wrong one.

use mig_types::segment::OwnedSegment;

use crate::validator::{IssueKind, SegmentPosition, Severity, ValidationIssue};

/// DTM 2005 pairs naming a period's beginning and end, and whether the
/// period may be empty (end = beginning).
///
/// "Frühestes … spätestes" (469/472) bound a window that may close on the
/// day it opens; the others are periods, empty if they end as they begin.
pub const INTERVALS: &[(&str, &str, bool)] = &[
    ("92", "93", false),   // Vertragsbeginn / Vertragsende
    ("155", "156", false), // Rechnungsperiode Beginn / Ende
    ("158", "159", false), // Bilanzierungsbeginn / -ende
    ("163", "164", false), // Verarbeitung Beginn / Ende
    ("Z15", "Z16", false), // Bilanzierungsbeginn / -ende der beteiligten Marktrolle
    ("Z25", "Z26", false), // Verwendung der Daten ab / bis
    ("Z34", "Z35", false), // Gültigkeitsbeginn / -ende
    ("Z42", "Z43", false), // vorläufiger Abrechnungszeitraum Beginn / Ende
    ("469", "472", true),  // frühester / spätester Zeitpunkt
];

/// UTILMD Strom RFF 1153 qualifiers that refer to a location of the
/// transaction, and the LOC 3227 qualifiers they may name (UTILMD MIG Strom,
/// RFF descriptions: "Zur Referenzierung auf eine ID einer Messlokation vom
/// SG5 LOC+Z17").
pub const REFERENCES: &[(&str, &[&str])] = &[
    ("Z18", &["Z16", "Z21", "Z22"]), // Marktlokation, Tranche, ruhende Marktlokation
    ("Z19", &["Z17"]),               // Messlokation
    ("Z32", &["Z18"]),               // Netzlokation
    ("Z37", &["Z20"]),               // Technische Ressource
    ("Z38", &["Z19"]),               // Steuerbare Ressource
    // Tranche ("vom SG5 LOC+Z21"); in SEQ+VY "Marktlokation oder Tranche".
    ("Z20", &["Z21", "Z16"]),
];

/// The same for UTILMD Gas, whose transactions name one location kind, the
/// Meldepunkt: "Zur Referenzierung auf eine ID einer Messlokation vom SG5
/// LOC+172" (UTILMD MIG Gas). Its Marktlokation reference (RFF+Z18, "die im
/// Vorgang behandelte Marktlokation") names no LOC, so it is not judged.
pub const REFERENCES_GAS: &[(&str, &[&str])] = &[("Z19", &["172"])];

/// The SG10 CCI 7037 codes of the devices an SG8 `RFF` with `qualifier`
/// may name in an SG8 of `seq` (SEQ 1229), the preferred kind first; `None`
/// for a qualifier that names no device. A device's number is DE7110 of the
/// `CAV+Z30` beside that CCI ("Identifikation / Nummer des Gerätes").
///
/// UTILMD MIG, SG8 RFF descriptions: RFF+Z14 is "Referenz auf das
/// Smartmeter-Gateway" (CCI+++Z75). RFF+MG names "die Gerätenummer des
/// Zählers im DE7110 im SG8 SEQ+Z03 CCI+++E13 CAV+Z30" in the Wandler,
/// technische Steuereinrichtung and Mengenumwerter data (SEQ+Z04, Z06, Z09
/// and their variants); in the Kommunikationseinrichtung (Z05) and the OBIS
/// data (Z20) also a Mengenumwerter or Smartmeter-Gateway. RFF+Z11, in UTILMD
/// Gas's OBIS data, is the "Gerätenummer des Mengenumwerters" (CCI+++Z64).
pub fn device_reference_targets(qualifier: &str, seq: &str) -> Option<&'static [&'static str]> {
    match (qualifier, seq) {
        ("Z14", _) => Some(&["Z75"]),
        // UTILMD Gas SEQ+Z20: "Gerätenummer des Mengenumwerters" (SEQ+Z09).
        ("Z11", _) => Some(&["Z64"]),
        ("MG", "Z04" | "Z06" | "Z09" | "ZB9" | "ZC0" | "ZC1" | "ZC2" | "ZF7" | "ZF9") => {
            Some(&["E13"])
        }
        ("MG", _) => Some(&["E13", "Z64", "Z75"]),
        _ => None,
    }
}

/// The consistency errors of one message (`segments`, UNH…UNT) of
/// `message_type` (a variant, `UTILMD_Strom`, or a message type).
pub fn check_consistency(segments: &[OwnedSegment], message_type: &str) -> Vec<ValidationIssue> {
    let mut out = intervals(segments);
    match message_type {
        "UTILMD_Strom" => {
            out.extend(references(segments, REFERENCES));
            out.extend(device_references(segments));
        }
        "UTILMD_Gas" => {
            out.extend(references(segments, REFERENCES_GAS));
            out.extend(device_references(segments));
        }
        _ => {}
    }
    out.sort_by_key(|i| i.segment_position.map(|p| p.segment_number));
    out
}

/// A DTM value as minutes since 0000-03-01 UTC, for formats 102
/// (`CCYYMMDD`) and 303 (`CCYYMMDDHHMMZZZ`); `None` for anything else.
fn instant(value: &str, format: &str) -> Option<i64> {
    let digits = |s: &str| -> Option<i64> {
        if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {
            return None;
        }
        s.parse().ok()
    };
    let (date, minutes, offset) = match format {
        "102" if value.len() == 8 => (value, 0, 0),
        "303" if value.len() == 15 => {
            let hours = digits(&value[8..10])?;
            let mins = digits(&value[10..12])?;
            let sign = match &value[12..13] {
                "+" => 1,
                "-" => -1,
                _ => return None,
            };
            (
                &value[..8],
                hours * 60 + mins,
                sign * digits(&value[13..15])? * 60,
            )
        }
        _ => return None,
    };
    let (y, m, d) = (
        digits(&date[..4])?,
        digits(&date[4..6])?,
        digits(&date[6..8])?,
    );
    if !(1..=12).contains(&m) || !(1..=31).contains(&d) {
        return None;
    }
    // Days since 0000-03-01 (H. Hinnant's days_from_civil, shifted).
    let y = if m <= 2 { y - 1 } else { y };
    let era = y.div_euclid(400);
    let yoe = y - era * 400;
    let mp = (m + 9) % 12;
    let doy = (153 * mp + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146_097 + doe;
    Some(days * 24 * 60 + minutes - offset)
}

fn intervals(segments: &[OwnedSegment]) -> Vec<ValidationIssue> {
    let mut out = Vec::new();
    // DTMs sent side by side belong to one group; a period's two ends are
    // always among them.
    for run in segments.split(|s| s.id != "DTM") {
        for &(begin, end, may_be_empty) in INTERVALS {
            let find = |q: &str| run.iter().find(|s| s.get_component(0, 0) == q);
            let (Some(b), Some(e)) = (find(begin), find(end)) else {
                continue;
            };
            let format = b.get_component(0, 2);
            if format != e.get_component(0, 2) {
                continue;
            }
            let (Some(from), Some(to)) = (
                instant(b.get_component(0, 1), format),
                instant(e.get_component(0, 1), format),
            ) else {
                continue;
            };
            if to < from || (to == from && !may_be_empty) {
                out.push(
                    ValidationIssue::new(
                        Severity::Error,
                        IssueKind::IntervalNotPositive {
                            begin: format!("DTM+{begin}"),
                            end: format!("DTM+{end}"),
                        },
                    )
                    .with_position(SegmentPosition::at(e.segment_number))
                    .with_field_path("DTM/C507/2380")
                    .with_field_position(2, Some(2))
                    .with_actual(e.get_component(0, 1))
                    .with_expected(format!("> {}", b.get_component(0, 1))),
                );
            }
        }
    }
    out
}

fn references(segments: &[OwnedSegment], table: &[(&str, &[&str])]) -> Vec<ValidationIssue> {
    let mut out = Vec::new();
    // One transaction (SG4) after another: its LOCs (SG5), then the
    // references of its SG8/SG12 groups.
    for transaction in segments.split(|s| s.id == "IDE").skip(1) {
        let mut locs: Vec<(&str, &str)> = Vec::new();
        let mut in_sg8_or_sg12 = false;
        for seg in transaction {
            match seg.id.as_str() {
                "LOC" if !in_sg8_or_sg12 => {
                    locs.push((seg.get_element(0), seg.get_component(1, 0)));
                }
                "SEQ" | "NAD" => in_sg8_or_sg12 = true,
                "RFF" if in_sg8_or_sg12 => {
                    let qualifier = seg.get_component(0, 0);
                    let id = seg.get_component(0, 1);
                    let Some((_, kinds)) = table.iter().find(|(q, _)| *q == qualifier) else {
                        continue;
                    };
                    let named: Vec<&str> = locs
                        .iter()
                        .filter(|(k, v)| kinds.contains(k) && !v.is_empty())
                        .map(|(_, v)| *v)
                        .collect();
                    if id.is_empty() || named.is_empty() || named.contains(&id) {
                        continue;
                    }
                    out.push(
                        ValidationIssue::new(
                            Severity::Error,
                            IssueKind::ReferenceNotFound {
                                reference: format!("RFF+{qualifier}"),
                                value: id.to_string(),
                                targets: kinds.iter().map(|k| format!("LOC+{k}")).collect(),
                            },
                        )
                        .with_position(SegmentPosition::at(seg.segment_number))
                        .with_field_path("RFF/C506/1154")
                        .with_field_position(2, Some(2))
                        .with_actual(id)
                        .with_expected(named.join(", ")),
                    );
                }
                _ => {}
            }
        }
    }
    out
}

/// The devices of one transaction (`segments` from its IDE on): the SG10
/// CCI 7037 code of each and its number (`CAV+Z30` DE7110).
pub fn transaction_devices(segments: &[OwnedSegment]) -> Vec<(String, String)> {
    let mut out = Vec::new();
    let mut device: Option<&str> = None;
    for seg in segments {
        match seg.id.as_str() {
            "SEQ" | "NAD" => device = None,
            "CCI" => device = Some(seg.get_component(2, 0)),
            "CAV" if seg.get_component(0, 0) == "Z30" => {
                let number = seg.get_component(0, 3);
                if let (Some(kind), false) = (device, number.is_empty()) {
                    out.push((kind.to_string(), number.to_string()));
                }
            }
            _ => {}
        }
    }
    out
}

/// The SG8 references of each transaction naming none of the devices
/// [`device_reference_targets`] lets them name: 55043's meter OBIS data
/// (SEQ+Z20) with an RFF+Z14 that names no SEQ+Z13's gateway. A transaction
/// without any device of the kind is left alone.
fn device_references(segments: &[OwnedSegment]) -> Vec<ValidationIssue> {
    let mut out = Vec::new();
    for transaction in segments.split(|s| s.id == "IDE").skip(1) {
        let devices = transaction_devices(transaction);
        let mut seq: Option<&str> = None;
        for seg in transaction {
            match seg.id.as_str() {
                "SEQ" => seq = Some(seg.get_element(0)),
                "NAD" => seq = None,
                "RFF" if seq.is_some() => {
                    let qualifier = seg.get_component(0, 0);
                    let id = seg.get_component(0, 1);
                    let Some(kinds) = device_reference_targets(qualifier, seq.unwrap_or_default())
                    else {
                        continue;
                    };
                    let named: Vec<&str> = devices
                        .iter()
                        .filter(|(k, _)| kinds.contains(&k.as_str()))
                        .map(|(_, n)| n.as_str())
                        .collect();
                    if id.is_empty() || named.is_empty() || named.contains(&id) {
                        continue;
                    }
                    out.push(
                        ValidationIssue::new(
                            Severity::Error,
                            IssueKind::ReferenceNotFound {
                                reference: format!("RFF+{qualifier}"),
                                value: id.to_string(),
                                targets: kinds
                                    .iter()
                                    .map(|k| format!("CCI+++{k} CAV+Z30"))
                                    .collect(),
                            },
                        )
                        .with_position(SegmentPosition::at(seg.segment_number))
                        .with_field_path("SG4/SG8/RFF/C506/1154")
                        .with_field_position(2, Some(2))
                        .with_actual(id)
                        .with_expected(named.join(", ")),
                    );
                }
                _ => {}
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_gateway_reference_names_a_gateway_of_its_transaction() {
        let msg = |reference: &str| {
            message(&format!(
                "UNH+1+UTILMD:D:11A:UN:S2.1'IDE+24+X'SEQ+Z13'CCI+++Z75'CAV+Z30:::GW1'\
                 SEQ+Z03'CCI+++E13'CAV+Z30:::M1'{reference}'\
                 SEQ+Z20'RFF+MG:M1'NAD+Z09'UNT+9+1'"
            ))
        };
        let found = |reference: &str| {
            check_consistency(&msg(reference), "UTILMD_Strom")
                .into_iter()
                .map(|i| format!("{:?}", i.kind))
                .collect::<Vec<_>>()
        };
        assert!(found("RFF+Z14:GW1").is_empty());
        let wrong = found("RFF+Z14:M1");
        assert_eq!(wrong.len(), 1, "{wrong:?}");
        assert!(wrong[0].contains("RFF+Z14") && wrong[0].contains("CCI+++Z75"));
        // A meter reference may name the meter, not an unknown device.
        assert_eq!(found("RFF+MG:M1").len(), 0);
        assert_eq!(found("RFF+MG:XX").len(), 1);
        // A Wandler's names the meter only; the OBIS data's also a gateway.
        let wandler = message(
            "UNH+1+UTILMD:D:11A:UN:S2.1'IDE+24+X'SEQ+Z13'CCI+++Z75'CAV+Z30:::GW1'\
             SEQ+Z03'CCI+++E13'CAV+Z30:::M1'SEQ+Z04'RFF+MG:GW1'SEQ+Z20'RFF+MG:GW1'UNT+9+1'",
        );
        let issues = check_consistency(&wandler, "UTILMD_Strom");
        assert_eq!(issues.len(), 1, "{issues:?}");
        assert_eq!(issues[0].expected_value.as_deref(), Some("M1"));
    }

    fn message(text: &str) -> Vec<OwnedSegment> {
        mig_assembly::tokenize::parse_to_segments(text.as_bytes()).unwrap()
    }

    #[test]
    fn instants_compare_across_offsets() {
        let a = instant("202501010000+00", "303").unwrap();
        let b = instant("202501010100+01", "303").unwrap();
        assert_eq!(a, b);
        assert!(instant("20250301", "102").unwrap() > instant("20250228", "102").unwrap());
        assert_eq!(instant("2025013", "102"), None);
        assert_eq!(instant("202501010000+00", "610"), None);
    }

    #[test]
    fn a_period_ending_as_or_before_it_begins_is_z34() {
        let issues = |dtm: &str| {
            check_consistency(
                &message(&format!(
                    "UNH+1+MSCONS:D:04B:UN:2.5'QTY+220:1'{dtm}UNT+5+1'"
                )),
                "MSCONS",
            )
        };
        let backwards = issues("DTM+163:202501020000?+00:303'DTM+164:202501010000?+00:303'");
        assert_eq!(backwards.len(), 1, "{backwards:?}");
        assert_eq!(backwards[0].code(), "CON001");
        assert_eq!(
            backwards[0].segment_position.map(|p| p.segment_number),
            Some(4)
        );
        assert_eq!(
            issues("DTM+163:202501010000?+00:303'DTM+164:202501010000?+00:303'").len(),
            1
        );
        assert!(issues("DTM+163:202501010000?+00:303'DTM+164:202501020000?+00:303'").is_empty());
        // Different formats, other groups: not judged.
        assert!(issues("DTM+163:20250102:102'DTM+164:202501010000?+00:303'").is_empty());
        assert!(
            issues("DTM+163:202501020000?+00:303'QTY+220:1'DTM+164:202501010000?+00:303'")
                .is_empty()
        );
        // A window may close as it opens.
        assert!(issues("DTM+469:202501010000?+00:303'DTM+472:202501010000?+00:303'").is_empty());
    }

    #[test]
    fn a_reference_to_no_location_of_its_transaction_is_z21() {
        let issues = |rff: &str| {
            check_consistency(
                &message(&format!(
                    "UNH+1+UTILMD:D:11A:UN:S2.2'IDE+24+X'LOC+Z17+DE00MELO'LOC+Z18+E1814774964'\
                     SEQ+Z03'{rff}UNT+7+1'"
                )),
                "UTILMD_Strom",
            )
        };
        let wrong = issues("RFF+Z19:DE00ANDERE'");
        assert_eq!(wrong.len(), 1, "{wrong:?}");
        assert_eq!(wrong[0].code(), "CON002");
        assert!(issues("RFF+Z19:DE00MELO'").is_empty());
        // No Technische Ressource in the transaction: not judged.
        assert!(issues("RFF+Z37:C123'").is_empty());
        // A Tranche reference names a Tranche (LOC+Z21) of the transaction.
        let tranche = |rff: &str| {
            check_consistency(
                &message(&format!(
                    "UNH+1+UTILMD:D:11A:UN:S2.2'IDE+24+X'LOC+Z16+76419669208'\
                     LOC+Z21+89889012345'SEQ+Z16'{rff}UNT+7+1'"
                )),
                "UTILMD_Strom",
            )
        };
        assert!(tranche("RFF+Z20:89889012345'").is_empty());
        assert_eq!(tranche("RFF+Z20:69206957749'").len(), 1);
        // Gas: a Messlokation is a Meldepunkt (LOC+172).
        let gas = |rff: &str| {
            check_consistency(
                &message(&format!(
                    "UNH+1+UTILMD:D:11A:UN:G1.2'IDE+24+X'LOC+172+DE00MELO'SEQ+Z03'{rff}UNT+6+1'"
                )),
                "UTILMD_Gas",
            )
        };
        assert_eq!(gas("RFF+Z19:DE00ANDERE'").len(), 1);
        assert!(gas("RFF+Z19:DE00MELO'").is_empty());
        // Another message type: not judged.
        assert!(check_consistency(
            &message("UNH+1+UTILTS:D:18A:UN:1.1e'IDE+24+X'LOC+Z17+A'SEQ+Z03'RFF+Z19:B'UNT+6+1'"),
            "UTILTS"
        )
        .is_empty());
    }
}