automapper-validation 0.20.0

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
//! Conditions naming one group instance — "in dieser SG15", "in derselben SG9
//! LIN", "in derselben SG29" — in the message types other than UTILMD. Each
//! used to ask whether *some* instance of the message matched (or looked under
//! a path the message type does not have), so every instance got the same
//! answer. Each case asks from sibling instances where the answer differs.

use std::collections::BTreeMap;

use automapper_validation::eval::{
    ConditionResult, EvaluationContext, GroupScope, MapExternalProvider,
};
use automapper_validation::evaluator_factory::evaluator_for;
use mig_assembly::assembler::{
    AssembledGroup, AssembledGroupInstance, AssembledSegment, AssembledTree,
};
use mig_assembly::navigator::AssembledTreeNavigator;
use mig_types::segment::OwnedSegment;

use ConditionResult::{False, True, Unknown};

/// A segment from its EDIFACT text: `STS+Z20+Z32+A99:E_0278`.
fn seg(text: &str) -> AssembledSegment {
    let mut parts = text.split('+');
    let tag = parts.next().unwrap().to_string();
    AssembledSegment {
        tag,
        elements: parts
            .map(|e| e.split(':').map(str::to_string).collect())
            .collect(),
        mig_number: None,
        segment_number: None,
    }
}

fn inst(segments: &[&str], child_groups: Vec<AssembledGroup>) -> AssembledGroupInstance {
    AssembledGroupInstance {
        segments: segments.iter().map(|s| seg(s)).collect(),
        child_groups,
        entry_mig_number: None,
        variant_mig_numbers: vec![],
        skipped_segments: vec![],
        skipped_positions: Vec::new(),
    }
}

fn grp(id: &str, repetitions: Vec<AssembledGroupInstance>) -> AssembledGroup {
    AssembledGroup {
        group_id: id.to_string(),
        repetitions,
    }
}

fn tree(groups: Vec<AssembledGroup>) -> AssembledTree {
    AssembledTree {
        segments: vec![],
        groups,
        post_group_start: 0,
        inter_group_segments: BTreeMap::new(),
    }
}

fn flat(tree: &AssembledTree) -> Vec<OwnedSegment> {
    fn walk(groups: &[AssembledGroup], out: &mut Vec<OwnedSegment>) {
        for group in groups {
            for instance in &group.repetitions {
                for s in &instance.segments {
                    out.push(OwnedSegment {
                        id: s.tag.clone(),
                        elements: s.elements.clone(),
                        segment_number: out.len() as u32,
                    });
                }
                walk(&instance.child_groups, out);
            }
        }
    }
    let mut out = Vec::new();
    walk(&tree.groups, &mut out);
    out
}

type Case = (u32, &'static [(&'static str, usize)], ConditionResult);

/// Runs `cases` for `variant` in every format version of `fvs` against `tree`.
fn check(
    variant: &str,
    fvs: &[&str],
    tree: &AssembledTree,
    cases: &[Case],
    wrong: &mut Vec<String>,
) {
    let navigator = AssembledTreeNavigator::new(tree);
    let external = MapExternalProvider::new(Default::default());
    let segments = flat(tree);
    let ctx = EvaluationContext::with_navigator("00000", &external, &segments, &navigator);
    for fv in fvs {
        let evaluator = evaluator_for(variant, fv).unwrap_or_else(|| panic!("{variant} {fv}"));
        for &(condition, path, expected) in cases {
            let got = evaluator.evaluate(condition, &ctx.with_scope(GroupScope { path }));
            if got != expected {
                wrong.push(format!(
                    "{variant} {fv} [{condition}] at {path:?}: {got:?}, expected {expected:?}"
                ));
            }
        }
    }
}

#[test]
fn each_instance_answers_for_itself() {
    let mut wrong = Vec::new();

    // APERAK: SG4 ERC with its SG5 RFF.
    let aperak = tree(vec![grp(
        "SG4",
        vec![
            inst(
                &["ERC+Z10"],
                vec![grp("SG5", vec![inst(&["RFF+TN:1"], vec![])])],
            ),
            inst(
                &["ERC+Z10"],
                vec![grp("SG5", vec![inst(&["RFF+ACW:2"], vec![])])],
            ),
        ],
    )]);
    check(
        "APERAK",
        &["FV2504"],
        &aperak,
        &[(4, &[("SG4", 0)], False), (4, &[("SG4", 1)], True)],
        &mut wrong,
    );

    // IFTSTA: SG14 CNI with its SG15 STS/RFF.
    let iftsta = tree(vec![grp(
        "SG14",
        vec![
            inst(
                &["CNI+1"],
                vec![grp(
                    "SG15",
                    vec![inst(
                        &[
                            "STS+Z37",
                            "STS+Z21++A99",
                            "RFF+Z45:1",
                            "RFF+Z17:1",
                            "STS+Z20+Z32+A99:E_0278",
                        ],
                        vec![],
                    )],
                )],
            ),
            inst(
                &["CNI+2"],
                vec![grp(
                    "SG15",
                    vec![inst(
                        &[
                            "STS+Z38",
                            "STS+Z21++B01",
                            "RFF+Z13:1",
                            "STS+Z20+Z32+A99:E_0281",
                        ],
                        vec![],
                    )],
                )],
            ),
            inst(
                &["CNI+3"],
                vec![grp("SG15", vec![inst(&["STS+Z15+Z13+A99:E_0286"], vec![])])],
            ),
        ],
    )]);
    const SG15_0: &[(&str, usize)] = &[("SG14", 0), ("SG15", 0)];
    const SG15_1: &[(&str, usize)] = &[("SG14", 1), ("SG15", 0)];
    const SG15_2: &[(&str, usize)] = &[("SG14", 2), ("SG15", 0)];
    let iftsta_cases: &[Case] = &[
        (135, SG15_0, True),
        (135, SG15_1, False),
        (144, SG15_0, False),
        (144, SG15_1, True),
        (145, SG15_0, False),
        (145, SG15_1, True),
        (78, SG15_0, True),
        (78, SG15_1, False),
        (79, SG15_0, False),
        (79, SG15_1, True),
    ];
    check(
        "IFTSTA",
        &["FV2504", "FV2510"],
        &iftsta,
        iftsta_cases,
        &mut wrong,
    );
    check(
        "IFTSTA",
        &["FV2510"],
        &iftsta,
        &[
            (149, SG15_0, False),
            (149, SG15_1, True),
            (150, SG15_0, True),
            (150, SG15_1, False),
            (151, SG15_0, True),
            (151, SG15_1, False),
            (152, SG15_0, False),
            (152, SG15_1, True),
            (154, SG15_2, True),
            (154, SG15_0, False),
        ],
        &mut wrong,
    );

    // INSRPT: SG3 with its SG7 LIN/STS.
    let insrpt = tree(vec![grp(
        "SG3",
        vec![inst(
            &["DOC+Z01"],
            vec![grp(
                "SG7",
                vec![
                    inst(&["LIN+1", "STS+Z06+Z10"], vec![]),
                    inst(&["LIN+2", "STS+Z06+Z11"], vec![]),
                ],
            )],
        )],
    )]);
    check(
        "INSRPT",
        &["FV2510"],
        &insrpt,
        &[
            (8, &[("SG3", 0), ("SG7", 0)], True),
            (8, &[("SG3", 0), ("SG7", 1)], False),
        ],
        &mut wrong,
    );

    // MSCONS: SG5 / SG6 LOC / SG9 LIN / SG10 QTY+STS.
    let sg9 = |sts: &str| {
        inst(
            &["LIN+1"],
            vec![grp("SG10", vec![inst(&["QTY+220:1", sts], vec![])])],
        )
    };
    let mscons = tree(vec![grp(
        "SG5",
        vec![inst(
            &["NAD+DP"],
            vec![grp(
                "SG6",
                vec![inst(
                    &["LOC+172+DE0001"],
                    vec![grp(
                        "SG9",
                        vec![
                            sg9("STS+10+Z38"),
                            sg9("STS+10+Z39"),
                            sg9("STS+10+Z36"),
                            sg9("STS+10+Z37"),
                        ],
                    )],
                )],
            )],
        )],
    )]);
    const M0: &[(&str, usize)] = &[("SG5", 0), ("SG6", 0), ("SG9", 0), ("SG10", 0)];
    const M1: &[(&str, usize)] = &[("SG5", 0), ("SG6", 0), ("SG9", 1), ("SG10", 0)];
    const M2: &[(&str, usize)] = &[("SG5", 0), ("SG6", 0), ("SG9", 2), ("SG10", 0)];
    const M3: &[(&str, usize)] = &[("SG5", 0), ("SG6", 0), ("SG9", 3), ("SG10", 0)];
    check(
        "MSCONS",
        &["FV2504", "FV2510", "FV2604"],
        &mscons,
        &[
            (83, M0, False),
            (83, M1, True),
            (84, M0, True),
            (84, M1, False),
            (85, M0, True),
            (85, M2, False),
            (86, M0, True),
            (86, M3, False),
        ],
        &mut wrong,
    );

    // ORDERS: SG29 LIN/PIA with its SG30 CCI/CAV.
    let orders = tree(vec![grp(
        "SG29",
        vec![
            inst(
                &["LIN++Z54", "PIA+5+9991000000078:Z11"],
                vec![grp("SG30", vec![inst(&["CCI+Z01", "CAV+ZH9"], vec![])])],
            ),
            inst(&["LIN++Z54", "PIA+5+9991000000086:Z11"], vec![]),
        ],
    )]);
    const O0: &[(&str, usize)] = &[("SG29", 0)];
    const O1: &[(&str, usize)] = &[("SG29", 1)];
    check(
        "ORDERS",
        &["FV2504", "FV2510", "FV2604"],
        &orders,
        &[
            (81, O0, True),
            (81, O1, False),
            (112, O0, True),
            (112, O1, False),
            (85, O0, False),
            (85, O1, True),
            (161, O0, False),
            (161, O1, True),
        ],
        &mut wrong,
    );

    // QUOTES: SG27 with its SG31 PRI/RNG.
    let quotes = tree(vec![grp(
        "SG27",
        vec![inst(
            &["LIN+1"],
            vec![grp(
                "SG31",
                vec![
                    inst(&["PRI+CAL:1", "RNG+Z03"], vec![]),
                    inst(&["PRI+CAL:2", "RNG+Z04"], vec![]),
                ],
            )],
        )],
    )]);
    const Q0: &[(&str, usize)] = &[("SG27", 0), ("SG31", 0)];
    const Q1: &[(&str, usize)] = &[("SG27", 0), ("SG31", 1)];
    check(
        "QUOTES",
        &["FV2510"],
        &quotes,
        &[
            (93, Q0, False),
            (93, Q1, True),
            (94, Q0, True),
            (94, Q1, False),
        ],
        &mut wrong,
    );

    // UTILTS: SG5 with its SG8 SEQ/RFF.
    let utilts = tree(vec![grp(
        "SG5",
        vec![inst(
            &["IDE+24+1"],
            vec![grp(
                "SG8",
                vec![
                    inst(&["SEQ+Z37", "RFF+Z19:1"], vec![]),
                    inst(&["SEQ+Z37", "RFF+Z23:1"], vec![]),
                ],
            )],
        )],
    )]);
    const U0: &[(&str, usize)] = &[("SG5", 0), ("SG8", 0)];
    const U1: &[(&str, usize)] = &[("SG5", 0), ("SG8", 1)];
    check(
        "UTILTS",
        &["FV2504"],
        &utilts,
        &[
            (5, U0, False),
            (5, U1, True),
            (6, U0, True),
            (6, U1, False),
            (7, U0, True),
            (7, U1, False),
        ],
        &mut wrong,
    );

    // PRICAT: SG17 PGI with its SG36 LIN.
    let pricat = tree(vec![grp(
        "SG17",
        vec![inst(
            &["PGI+Z01"],
            vec![grp(
                "SG36",
                vec![
                    inst(&["LIN+1++1-01:Z12"], vec![]),
                    inst(&["LIN+2++1-02:Z12"], vec![]),
                ],
            )],
        )],
    )]);
    const P0: &[(&str, usize)] = &[("SG17", 0), ("SG36", 0)];
    const P1: &[(&str, usize)] = &[("SG17", 0), ("SG36", 1)];
    check(
        "PRICAT",
        &["FV2510"],
        &pricat,
        &[
            (61, P0, False),
            (61, P1, True),
            (65, P0, False),
            (65, P1, True),
        ],
        &mut wrong,
    );

    let _ = Unknown;
    assert!(wrong.is_empty(), "{wrong:#?}");
}