automapper-validation 0.30.18

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
446
447
448
449
450
// <auto-generated>
// Generated by automapper-generator generate-conditions
// AHB: xml-migs-and-ahbs/FV2604/COMDIS_AHB_1_0h_20251001.xml
// Generated: 2026-03-12T11:30:28Z
// </auto-generated>

// LLM-produced bodies aren't idiomatic Rust — silence stylistic lints at
// file scope so `cargo clippy -D warnings` stays green. Correctness,
// suspicious, and perf categories stay on so real bugs still surface.
#![allow(clippy::style, clippy::complexity)]

#[allow(unused_imports)]
use crate::eval::format_validators::*;
use crate::eval::{ConditionEvaluator, ConditionResult, EvaluationContext};
use mig_types::segment::OwnedSegment;

/// Generated condition evaluator for COMDIS FV2604.
pub struct ComdisConditionEvaluatorFV2604 {
    // External condition IDs that require runtime context.
    external_conditions: std::collections::HashSet<u32>,
}

impl Default for ComdisConditionEvaluatorFV2604 {
    fn default() -> Self {
        let mut external_conditions = std::collections::HashSet::new();
        external_conditions.insert(3);
        external_conditions.insert(23);
        external_conditions.insert(24);
        external_conditions.insert(25);
        external_conditions.insert(26);
        external_conditions.insert(27);
        external_conditions.insert(28);
        external_conditions.insert(29);
        external_conditions.insert(30);
        external_conditions.insert(492);
        external_conditions.insert(493);
        Self {
            external_conditions,
        }
    }
}

impl ConditionEvaluator for ComdisConditionEvaluatorFV2604 {
    fn message_type(&self) -> &str {
        "COMDIS"
    }

    fn format_version(&self) -> &str {
        "FV2604"
    }

    fn evaluate(&self, condition: u32, ctx: &EvaluationContext) -> ConditionResult {
        match condition {
            1 => self.evaluate_1(ctx),
            2 => self.evaluate_2(ctx),
            3 => self.evaluate_3(ctx),
            4 => self.evaluate_4(ctx),
            5 => self.evaluate_5(ctx),
            6 => self.evaluate_6(ctx),
            7 => self.evaluate_7(ctx),
            8 => self.evaluate_8(ctx),
            9 => self.evaluate_9(ctx),
            10 => self.evaluate_10(ctx),
            11 => self.evaluate_11(ctx),
            12 => self.evaluate_12(ctx),
            13 => self.evaluate_13(ctx),
            14 => self.evaluate_14(ctx),
            15 => self.evaluate_15(ctx),
            16 => self.evaluate_16(ctx),
            17 => self.evaluate_17(ctx),
            18 => self.evaluate_18(ctx),
            19 => self.evaluate_19(ctx),
            20 => self.evaluate_20(ctx),
            21 => self.evaluate_21(ctx),
            22 => self.evaluate_22(ctx),
            23 => self.evaluate_23(ctx),
            24 => self.evaluate_24(ctx),
            25 => self.evaluate_25(ctx),
            26 => self.evaluate_26(ctx),
            27 => self.evaluate_27(ctx),
            28 => self.evaluate_28(ctx),
            29 => self.evaluate_29(ctx),
            30 => self.evaluate_30(ctx),
            31 => self.evaluate_31(ctx),
            32 => self.evaluate_32(ctx),
            33 => self.evaluate_33(ctx),
            34 => self.evaluate_34(ctx),
            492 => self.evaluate_492(ctx),
            493 => self.evaluate_493(ctx),
            505 => self.evaluate_505(ctx),
            506 => self.evaluate_506(ctx),
            508 => self.evaluate_508(ctx),
            509 => self.evaluate_509(ctx),
            510 => self.evaluate_510(ctx),
            930 => self.evaluate_930(ctx),
            931 => self.evaluate_931(ctx),
            939 => self.evaluate_939(ctx),
            940 => self.evaluate_940(ctx),
            _ => ConditionResult::Unknown,
        }
    }

    fn is_external(&self, condition: u32) -> bool {
        self.external_conditions.contains(&condition)
    }
    fn is_known(&self, condition: u32) -> bool {
        matches!(
            condition,
            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
                | 492
                | 493
                | 505
                | 506
                | 508
                | 509
                | 510
                | 930
                | 931
                | 939
                | 940
        )
    }
}

impl ComdisConditionEvaluatorFV2604 {
    /// [1] wenn SG3 AJT+Z61+S_0109 oder SG3 AJT+Z62+S_0109 vorhanden
    fn evaluate_1(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["Z61", "Z62"], &["S_0109"])
    }

    /// [2] wenn SG3 AJT+Z58+S_0109 oder SG3 AJT+Z59+S_0109 oder SG3 AJT+Z60+S_0109 vorhanden
    fn evaluate_2(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["Z58", "Z59", "Z60"], &["S_0109"])
    }

    /// [31] wenn SG3 AJT+Z61+G_0089 oder SG3 AJT+Z62+G_0089 vorhanden
    fn evaluate_31(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["Z61", "Z62"], &["G_0089"])
    }

    /// [32] wenn SG3 AJT+Z58+G_0089 oder SG3 AJT+Z59+G_0089 oder SG3 AJT+Z60+G_0089 vorhanden
    fn evaluate_32(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["Z58", "Z59", "Z60"], &["G_0089"])
    }

    /// [33] wenn SG3 AJT+Z58/Z59/Z60/Z61/Z62+G_0089 vorhanden
    fn evaluate_33(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["Z58", "Z59", "Z60", "Z61", "Z62"], &["G_0089"])
    }

    /// [34] wenn SG3 AJT+A99+G_0089 vorhanden
    fn evaluate_34(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A99"], &["G_0089"])
    }

    /// [3] Nur MP-ID aus Sparte Strom
    /// EXTERNAL: Requires context from outside the message.
    fn evaluate_3(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("mp_id_from_electricity_sector")
    }

    /// [4] wenn SG3 AJT+Z58/Z59/Z60/Z61/Z62+S_0109 vorhanden
    fn evaluate_4(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["Z58", "Z59", "Z60", "Z61", "Z62"], &["S_0109"])
    }

    /// [5] wenn SG3 AJT+A01/A02/A03/A04/A06/A07/A09/A12/A15+E_0504 vorhanden
    fn evaluate_5(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(
            ctx,
            &[
                "A01", "A02", "A03", "A04", "A06", "A07", "A09", "A12", "A15",
            ],
            &["E_0504"],
        )
    }

    /// [6] wenn SG3 AJT+A07+E_0504 vorhanden
    fn evaluate_6(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A07"], &["E_0504"])
    }

    /// [7] wenn SG3 AJT+A02+E_0504 vorhanden
    fn evaluate_7(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A02"], &["E_0504"])
    }

    /// [8] wenn SG3 AJT+A01/A04/A06/A09/A12+E_0504 vorhanden
    fn evaluate_8(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A01", "A04", "A06", "A09", "A12"], &["E_0504"])
    }

    /// [9] wenn SG3 AJT+A05/A10/A11/A14+E_0504 vorhanden
    fn evaluate_9(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A05", "A10", "A11", "A14"], &["E_0504"])
    }

    /// [10] wenn SG3 AJT+A03+E_0504 vorhanden
    fn evaluate_10(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A03"], &["E_0504"])
    }

    /// [11] wenn SG3 AJT+A15+E_0504 vorhanden
    fn evaluate_11(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A15"], &["E_0504"])
    }

    /// [12] wenn SG3 AJT+A99+S_0109 vorhanden
    fn evaluate_12(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A99"], &["S_0109"])
    }

    /// [13] wenn SG3 AJT+A07+E_1008 vorhanden
    fn evaluate_13(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A07"], &["E_1008"])
    }

    /// [14] wenn SG3 AJT+A02+E_1008 vorhanden
    fn evaluate_14(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A02"], &["E_1008"])
    }

    /// [15] wenn SG3 AJT+A01/A04/A06/A09+E_1008 vorhanden
    fn evaluate_15(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A01", "A04", "A06", "A09"], &["E_1008"])
    }

    /// [16] wenn SG3 AJT+A03+E_1008 vorhanden
    fn evaluate_16(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A03"], &["E_1008"])
    }

    /// [17] wenn SG3 AJT+A15+E_1008 vorhanden
    fn evaluate_17(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A15"], &["E_1008"])
    }

    /// [18] wenn SG3 AJT+A05/A10/A11+E_1008 vorhanden
    fn evaluate_18(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(ctx, &["A05", "A10", "A11"], &["E_1008"])
    }

    /// [19] wenn SG3 AJT+A99+E_0265/E_0271/E_0274/E_0516/E_0520/E_0567 vorhanden
    fn evaluate_19(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(
            ctx,
            &["A99"],
            &["E_0265", "E_0271", "E_0274", "E_0516", "E_0520", "E_0567"],
        )
    }

    /// [20] wenn SG3 AJT+A01/A02/A03/A04/A06/A07/A09/A15+E_1008 vorhanden
    fn evaluate_20(&self, ctx: &EvaluationContext) -> ConditionResult {
        sg3_ajt(
            ctx,
            &["A01", "A02", "A03", "A04", "A06", "A07", "A09", "A15"],
            &["E_1008"],
        )
    }

    /// [21] wenn im DE3155 in demselben COM der Code EM vorhanden ist
    fn evaluate_21(&self, ctx: &EvaluationContext) -> ConditionResult {
        let com_segments = ctx.find_segments("COM");
        ConditionResult::from(com_segments.iter().any(|seg| {
            seg.elements
                .first()
                .and_then(|e| e.get(1))
                .map(|s| s.as_str())
                == Some("EM")
        }))
    }

    /// [22] wenn im DE3155 in demselben COM der Code TE / FX / AJ / AL vorhanden ist
    fn evaluate_22(&self, ctx: &EvaluationContext) -> ConditionResult {
        let com_segments = ctx.find_segments("COM");
        ConditionResult::from(com_segments.iter().any(|seg| {
            matches!(
                seg.elements
                    .first()
                    .and_then(|e| e.get(1))
                    .map(|s| s.as_str()),
                Some("TE") | Some("FX") | Some("AJ") | Some("AL")
            )
        }))
    }

    /// [23] Wenn MP-ID in NAD+MS (Nachrichtensender) mit Rolle MSB vorhanden
    /// EXTERNAL: Requires context from outside the message.
    fn evaluate_23(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("sender_is_msb")
    }

    /// [24] Wenn MP-ID in NAD+MR (Nachrichtenempfänger) mit Rolle ESA vorhanden
    /// EXTERNAL: Requires context from outside the message.
    fn evaluate_24(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("recipient_is_esa")
    }

    /// [25] Wenn MP-ID in NAD+MR (Nachrichtenempfänger) mit Rolle LF vorhanden
    /// EXTERNAL: Requires context from outside the message.
    fn evaluate_25(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("recipient_is_lf")
    }

    /// [26] Wenn MP-ID in NAD+MR (Nachrichtenempfänger) mit Rolle NB vorhanden
    /// EXTERNAL: Requires context from outside the message.
    fn evaluate_26(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("recipient_is_nb")
    }

    /// [27] Wenn MP-ID in NAD+MS (Nachrichtensender) mit Rolle NB vorhanden
    /// EXTERNAL: Requires context from outside the message.
    fn evaluate_27(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("sender_is_nb")
    }

    /// [28] Angabe der Datenaustauschreferenz aus der CONTRL
    /// EXTERNAL: Requires context from outside the message.
    // REVIEW: Condition states the field contains the data exchange reference from a prior CONTRL message. Whether the COMDIS is responding to a CONTRL cannot be determined from the EDIFACT message content alone — it depends on external business context about which prior message triggered this dispute. (medium confidence)
    fn evaluate_28(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("responding_to_contrl")
    }

    /// [29] Angabe der Datenaustauschreferenz aus der APERAK
    /// EXTERNAL: Requires context from outside the message.
    // REVIEW: Condition states the field contains the data exchange reference from a prior APERAK message. Whether the COMDIS is responding to an APERAK cannot be determined from the EDIFACT message content alone — it depends on external business context about which prior message triggered this dispute. (medium confidence)
    fn evaluate_29(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("responding_to_aperak")
    }

    /// [30] Angabe der Nachrichtennummer aus der APERAK
    /// EXTERNAL: Requires context from outside the message.
    // REVIEW: Condition states the field contains the message number from a prior APERAK. Like condition 29, whether the COMDIS is responding to an APERAK is external business context, not determinable from message content. Shares the same external predicate as condition 29. (medium confidence)
    fn evaluate_30(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("responding_to_aperak")
    }

    /// [492] Wenn MP-ID in NAD+MR (Nachrichtenempfänger) aus Sparte Strom
    /// EXTERNAL: Requires context from outside the message.
    // REVIEW: Whether NAD+MR MP-ID belongs to Sparte Strom cannot be determined from the EDIFACT message content alone — it requires external business context (market participant registry or configuration). (medium confidence)
    fn evaluate_492(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("recipient_is_electricity_sector")
    }

    /// [493] Wenn MP-ID in NAD+MR (Nachrichtenempfänger) aus Sparte Gas
    /// EXTERNAL: Requires context from outside the message.
    // REVIEW: Whether NAD+MR MP-ID belongs to Sparte Gas cannot be determined from the EDIFACT message content alone — it requires external business context (market participant registry or configuration). (medium confidence)
    fn evaluate_493(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.external.evaluate("recipient_is_gas_sector")
    }

    /// [505] Hinweis: BGM DE1004 aus der vorher per REMADV abgelehnten INVOIC-Nachricht
    fn evaluate_505(&self, _ctx: &EvaluationContext) -> ConditionResult {
        ConditionResult::True
    }

    /// [506] Hinweis: BGM DE1004 aus der vorher per IFTSTA abgelehnten MSCONS-Nachricht
    fn evaluate_506(&self, _ctx: &EvaluationContext) -> ConditionResult {
        ConditionResult::True
    }

    /// [508] Hinweis: Es darf nur eine Information im DE3148 übermittelt werden
    fn evaluate_508(&self, _ctx: &EvaluationContext) -> ConditionResult {
        // Hinweis: Es darf nur eine Information im DE3148 übermittelt werden — informational note, always applies
        ConditionResult::True
    }

    /// [509] Hinweis: Wenn Eingang der zugrundeliegenden Nachricht vor dem 06.06.2025, 00:00 Uhr bestätigt wurde
    fn evaluate_509(&self, _ctx: &EvaluationContext) -> ConditionResult {
        // Hinweis: Wenn Eingang der zugrundeliegenden Nachricht vor dem 06.06.2025, 00:00 Uhr bestätigt wurde — informational note, always applies
        ConditionResult::True
    }

    /// [510] Hinweis: Wenn Eingang der zugrundeliegenden Nachricht nach dem 06.06.2025, 00:00 Uhr bestätigt wurde
    fn evaluate_510(&self, _ctx: &EvaluationContext) -> ConditionResult {
        // Hinweis: Wenn Eingang der zugrundeliegenden Nachricht nach dem 06.06.2025, 00:00 Uhr bestätigt wurde — informational note, always applies
        ConditionResult::True
    }

    /// [930] Format: max. 2 Nachkommastellen
    // REVIEW: Format condition for max 2 decimal places. COMDIS carries monetary amounts (MOA) as the primary numeric value segment. Using MOA elements[0][1] for the amount value. (medium confidence)
    fn evaluate_930(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.format_check("MOA", 0, 1, |val| validate_max_decimal_places(val, 2))
    }

    /// [931] Format: ZZZ = +00
    fn evaluate_931(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.format_check("DTM", 0, 1, validate_timezone_utc)
    }

    /// [939] Format: Die Zeichenkette muss die Zeichen @ und . enthalten
    fn evaluate_939(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.format_check("COM", 0, 0, validate_email)
    }

    /// [940] Format: Die Zeichenkette muss mit dem Zeichen + beginnen und danach dürfen nur noch Ziffern folgen
    fn evaluate_940(&self, ctx: &EvaluationContext) -> ConditionResult {
        ctx.format_check("COM", 0, 0, validate_phone)
    }
}

/// HAND-EDITED (#334): "wenn SG3 AJT+<answer>+<list> vorhanden" reads the AJT of
/// the SG3 the evaluated field sits in, message-wide only when the scope does
/// not reach an SG3. SG3 repeats once per reason, and FTX+ACD/ACB combine these
/// conditions with `⊻`, which holds only per SG3. Absence is `False`, not
/// `Unknown`: the SG3 is fully known.
fn sg3_ajt(ctx: &EvaluationContext, answers: &[&str], lists: &[&str]) -> ConditionResult {
    fn component(seg: &OwnedSegment, elem: usize) -> Option<&str> {
        seg.elements
            .get(elem)
            .and_then(|e| e.first())
            .map(String::as_str)
    }
    ConditionResult::from(ctx.scoped_find_segments_in("SG3", "AJT").iter().any(|seg| {
        component(seg, 0).is_some_and(|a| answers.contains(&a))
            && component(seg, 1).is_some_and(|l| lists.contains(&l))
    }))
}