automapper_validation/display/
narrator.rs1use crate::{IssueKind, UnresolvedConditions, ValidationIssue};
4
5use super::IssueNarrator;
6
7pub struct TechnicalNarrator;
13
14fn at(location: Option<&str>) -> String {
16 location.map_or_else(String::new, |l| format!(" at {l}"))
17}
18
19fn ids(list: &[u32]) -> String {
20 list.iter()
21 .map(|id| format!("[{id}]"))
22 .collect::<Vec<_>>()
23 .join(", ")
24}
25
26fn unresolved_detail(u: &UnresolvedConditions) -> String {
27 if u.is_empty() {
28 return String::new();
29 }
30 let mut parts = Vec::new();
31 if !u.external.is_empty() {
32 parts.push(format!(
33 "external conditions require provider: {}",
34 ids(&u.external)
35 ));
36 }
37 if !u.undetermined.is_empty() {
38 parts.push(format!(
39 "conditions could not be determined from message data: {}",
40 ids(&u.undetermined)
41 ));
42 }
43 if !u.missing.is_empty() {
44 parts.push(format!("missing conditions: {}", ids(&u.missing)));
45 }
46 format!(" ({})", parts.join("; "))
47}
48
49impl IssueNarrator for TechnicalNarrator {
50 fn describe(&self, issue: &ValidationIssue, location: Option<&str>) -> String {
51 match &issue.kind {
52 IssueKind::MissingRequiredField { field_name } => {
53 format!("Required field '{field_name}'{} is missing", at(location))
54 }
55 IssueKind::GroupNotAllowed { group } => format!(
56 "Group '{group}' is present although its condition does not hold: {}",
57 issue.rule.as_deref().unwrap_or_default()
58 ),
59 IssueKind::SegmentNotAllowed { segment } => format!(
60 "Segment '{segment}' is present although its condition does not hold: {}",
61 issue.rule.as_deref().unwrap_or_default()
62 ),
63 IssueKind::FieldConditionNotSatisfied { field_name } => format!(
64 "Field '{field_name}'{} is present but does not satisfy condition: {}",
65 at(location),
66 issue.rule.as_deref().unwrap_or_default()
67 ),
68 IssueKind::ConditionUnknown { field_name, unresolved } => format!(
69 "Condition for field '{field_name}' could not be fully evaluated{}",
70 unresolved_detail(unresolved)
71 ),
72 IssueKind::PackageCardinality { package_id, present, min, max, codes } => format!(
73 "Package [{package_id}P{min}..{max}]{}: {present} code(s) present (allowed {min}..{max}). \
74 Codes in package: [{}]",
75 at(location),
76 codes.join(", ")
77 ),
78 IssueKind::RepeatedInTransaction { path, count } => format!(
79 "'{path}' is sent {count} times in one transaction; its condition allows it once: {}",
80 issue.rule.as_deref().unwrap_or_default()
81 ),
82 IssueKind::RepetitionCount { path, count } => format!(
83 "'{path}' is sent {count} times in one group; its condition allows another number: {}",
84 issue.rule.as_deref().unwrap_or_default()
85 ),
86 IssueKind::CodeNotAllowedForPid { value, allowed } => format!(
87 "Code '{value}' is not allowed for this PID. Allowed: [{}]",
88 allowed.join(", ")
89 ),
90 IssueKind::AnswerNotInList { answer, list, allowed } => format!(
91 "Answer '{answer}'{} is not a code of {list}. Its codes: [{}]",
92 at(location),
93 allowed.join(", ")
94 ),
95 IssueKind::ValueNotNumeric { field_name } => format!(
96 "Field '{field_name}'{} is not a number; its format is {}",
97 at(location),
98 issue.expected_value.as_deref().unwrap_or_default()
99 ),
100 IssueKind::ValueTooLong { field_name, length, max } => format!(
101 "Field '{field_name}'{} has {length} characters; its format allows {max}",
102 at(location)
103 ),
104 IssueKind::UntSegmentCountMismatch { declared, actual } => {
105 format!("UNT segment count mismatch: declared {declared}, actual {actual}")
106 }
107 IssueKind::UntCountNotVerifiable { unh_count } => format!(
108 "UNT validation requires per-message segments, found {unh_count} UNH segments"
109 ),
110 IssueKind::StructureDiagnostic { detail, .. } => detail.clone(),
111 }
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use crate::display::{Bo4eView, EdifactView, IssueNarrator, IssueView, TechnicalNarrator};
118 use crate::{IssueKind, Severity, UnresolvedConditions, ValidationIssue};
119 use mig_assembly::StructureDiagnosticKind;
120
121 fn missing_field_issue() -> ValidationIssue {
122 ValidationIssue::new(
123 Severity::Error,
124 IssueKind::MissingRequiredField {
125 field_name: "Merkmal, Code".into(),
126 },
127 )
128 .with_field_path("SG4/SG8/SG10/CCI/C240/7037")
129 .with_bo4e_path("stammdaten.Marktlokation.haushaltskunde")
130 }
131
132 #[test]
133 fn edifact_view_narrates_with_the_edifact_path() {
134 let issue = missing_field_issue();
135 let loc = EdifactView.location(&issue);
136 assert_eq!(loc.as_deref(), Some("SG4/SG8/SG10/CCI/C240/7037"));
137 assert_eq!(
138 TechnicalNarrator.describe(&issue, loc.as_deref()),
139 "Required field 'Merkmal, Code' at SG4/SG8/SG10/CCI/C240/7037 is missing"
140 );
141 }
142
143 #[test]
144 fn bo4e_view_narrates_with_the_bo4e_path() {
145 let issue = missing_field_issue();
146 let loc = Bo4eView.location(&issue);
147 assert_eq!(
148 loc.as_deref(),
149 Some("stammdaten.Marktlokation.haushaltskunde")
150 );
151 assert_eq!(
152 TechnicalNarrator.describe(&issue, loc.as_deref()),
153 "Required field 'Merkmal, Code' at stammdaten.Marktlokation.haushaltskunde is missing"
154 );
155 }
156
157 #[test]
158 fn bo4e_view_has_no_location_for_envelope_issues() {
159 let issue = ValidationIssue::new(
160 Severity::Error,
161 IssueKind::UntSegmentCountMismatch {
162 declared: 30,
163 actual: 31,
164 },
165 )
166 .with_field_path("UNT/0074");
167
168 assert_eq!(Bo4eView.location(&issue), None);
169 assert_eq!(
170 TechnicalNarrator.describe(&issue, None),
171 "UNT segment count mismatch: declared 30, actual 31"
172 );
173 }
174
175 #[test]
180 fn field_condition_not_satisfied_matches_the_legacy_message() {
181 let issue = ValidationIssue::new(
182 Severity::Error,
183 IssueKind::FieldConditionNotSatisfied {
184 field_name: "Merkmal, Code".into(),
185 },
186 )
187 .with_field_path("SG4/SG8/SG10/CCI/C240/7037")
188 .with_rule("Muss [182]");
189 let loc = EdifactView.location(&issue);
190
191 assert_eq!(
194 TechnicalNarrator.describe(&issue, loc.as_deref()),
195 "Field 'Merkmal, Code' at SG4/SG8/SG10/CCI/C240/7037 is present but does not satisfy condition: Muss [182]"
196 );
197 }
198
199 #[test]
200 fn condition_unknown_joins_multiple_id_groups_with_semicolons() {
201 let issue = ValidationIssue::new(
202 Severity::Info,
203 IssueKind::ConditionUnknown {
204 field_name: "Merkmal, Code".into(),
205 unresolved: UnresolvedConditions {
206 external: vec![182],
207 undetermined: vec![6],
208 missing: vec![570],
209 },
210 },
211 )
212 .with_field_path("SG4/SG8/SG10/CCI/C240/7037");
213 let loc = EdifactView.location(&issue);
214
215 assert_eq!(
221 TechnicalNarrator.describe(&issue, loc.as_deref()),
222 "Condition for field 'Merkmal, Code' could not be fully evaluated (external conditions require provider: [182]; conditions could not be determined from message data: [6]; missing conditions: [570])"
223 );
224 }
225
226 #[test]
227 fn package_cardinality_matches_the_legacy_message() {
228 let issue = ValidationIssue::new(
229 Severity::Error,
230 IssueKind::PackageCardinality {
231 package_id: "1".into(),
232 present: 2,
233 min: 0,
234 max: 1,
235 codes: vec!["A".into(), "B".into()],
236 },
237 )
238 .with_field_path("SG4/SG8/SG10");
239 let loc = EdifactView.location(&issue);
240
241 assert_eq!(
246 TechnicalNarrator.describe(&issue, loc.as_deref()),
247 "Package [1P0..1] at SG4/SG8/SG10: 2 code(s) present (allowed 0..1). Codes in package: [A, B]"
248 );
249 }
250
251 #[test]
252 fn code_not_allowed_for_pid_matches_the_legacy_message() {
253 let issue = ValidationIssue::new(
254 Severity::Warning,
255 IssueKind::CodeNotAllowedForPid {
256 value: "Z99".into(),
257 allowed: vec!["Z01".into(), "Z02".into(), "Z98".into()],
258 },
259 );
260
261 assert_eq!(
264 TechnicalNarrator.describe(&issue, None),
265 "Code 'Z99' is not allowed for this PID. Allowed: [Z01, Z02, Z98]"
266 );
267 }
268
269 #[test]
270 fn unt_count_not_verifiable_matches_the_legacy_message() {
271 let issue = ValidationIssue::new(
272 Severity::Warning,
273 IssueKind::UntCountNotVerifiable { unh_count: 3 },
274 );
275
276 assert_eq!(
279 TechnicalNarrator.describe(&issue, None),
280 "UNT validation requires per-message segments, found 3 UNH segments"
281 );
282 }
283
284 #[test]
285 fn structure_diagnostic_passes_its_detail_through_verbatim() {
286 let detail = "Segment 'FTX' at position 15 is not defined in the PID-filtered MIG; \
292 the assembler advanced past it";
293 let issue = ValidationIssue::new(
294 Severity::Warning,
295 IssueKind::StructureDiagnostic {
296 kind: StructureDiagnosticKind::UnexpectedSegment,
297 segment_id: "FTX".into(),
298 position: 15,
299 detail: detail.into(),
300 },
301 );
302
303 assert_eq!(TechnicalNarrator.describe(&issue, None), detail);
304 }
305}