1use crate::validator::{Validator, report_error};
4use crate::{EdifactError, Segment, ValidationIssue, ValidationReport, ValidationSeverity};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Status {
9 Mandatory,
11 Conditional,
13}
14
15#[derive(Debug, Clone, Copy)]
17pub struct ElementRef {
18 pub position: u8,
20 pub data_element: &'static str,
22 pub status: Status,
24 pub max_repeat: u8,
26}
27
28#[derive(Debug)]
30pub struct SegmentDefinition {
31 pub tag: &'static str,
33 pub name: &'static str,
35 pub elements: &'static [ElementRef],
37}
38
39type SegmentLookupFn = fn(&str) -> Option<&'static SegmentDefinition>;
40type IsCodeValidFn = fn(&str, &str) -> bool;
41type SuggestCodeFn = fn(&str, &str) -> Option<&'static str>;
42type ExpectedComponentsFn = fn(&str, usize) -> Option<u8>;
43type AdditionalStructureRuleFn = fn(&Segment<'_>) -> Result<(), EdifactError>;
44type CodeListRulesFn = fn(tag: &str) -> &'static [(usize, usize, &'static str)];
47
48pub(crate) fn base_code_list_rules(tag: &str) -> &'static [(usize, usize, &'static str)] {
56 match tag {
57 "BGM" => &[(0, 0, "1001")],
58 "DTM" => &[(0, 0, "2005")],
59 "NAD" => &[(0, 0, "3035")],
60 "QTY" => &[(0, 0, "6063")],
61 "RFF" => &[(0, 0, "1153")],
62 "MOA" => &[(0, 0, "5025")],
63 "PRI" => &[(0, 0, "5125")],
64 "LOC" => &[(0, 0, "3227")],
65 _ => &[],
66 }
67}
68
69#[derive(Debug, Clone)]
82pub struct DirectoryValidator {
83 directory_id: &'static str,
84 segment_lookup: SegmentLookupFn,
85 is_code_valid: IsCodeValidFn,
86 suggest_code: SuggestCodeFn,
87 expected_components: ExpectedComponentsFn,
88 code_list_rules: CodeListRulesFn,
89 additional_structure_rule: Option<AdditionalStructureRuleFn>,
90 message_type: Option<String>,
91 enforce_known_tags: bool,
92 structure_checks: bool,
93 code_list_checks: bool,
94}
95
96impl DirectoryValidator {
97 pub fn new(
99 directory_id: &'static str,
100 segment_lookup: SegmentLookupFn,
101 is_code_valid: IsCodeValidFn,
102 suggest_code: SuggestCodeFn,
103 expected_components: ExpectedComponentsFn,
104 additional_structure_rule: Option<AdditionalStructureRuleFn>,
105 ) -> Self {
106 Self {
107 directory_id,
108 segment_lookup,
109 is_code_valid,
110 suggest_code,
111 expected_components,
112 code_list_rules: base_code_list_rules,
113 additional_structure_rule,
114 message_type: None,
115 enforce_known_tags: true,
116 structure_checks: true,
117 code_list_checks: true,
118 }
119 }
120
121 pub fn with_code_list_rules(mut self, f: CodeListRulesFn) -> Self {
126 self.code_list_rules = f;
127 self
128 }
129
130 pub fn structure_only(mut self) -> Self {
132 self.structure_checks = true;
133 self.code_list_checks = false;
134 self
135 }
136
137 pub fn code_list_only(mut self) -> Self {
139 self.structure_checks = false;
140 self.code_list_checks = true;
141 self
142 }
143
144 pub fn enforce_known_tags(mut self, enforce: bool) -> Self {
146 self.enforce_known_tags = enforce;
147 self
148 }
149
150 fn detect_message_type(&self, segments: &[Segment<'_>]) -> Option<String> {
151 if let Some(explicit) = self.message_type.as_deref() {
152 return Some(explicit.to_owned());
153 }
154
155 segments
156 .iter()
157 .find(|s| s.tag == "UNH")
158 .and_then(|s| s.get_element(1))
159 .and_then(|e| e.get_component(0))
160 .map(str::to_owned)
161 }
162
163 fn required_segments_for(message_type: &str) -> &'static [&'static str] {
174 match message_type {
175 "UTILMD" | "ORDERS" | "INVOIC" => &["UNH", "BGM", "UNT"],
176 _ => &["UNH", "UNT"],
177 }
178 }
179
180 fn effective_component_count(seg: &Segment<'_>, element_idx: usize) -> Option<u8> {
192 let elem = seg.elements.get(element_idx)?;
193 let mut count = elem.components.len();
194 while count > 0 && elem.components[count - 1].as_ref().is_empty() {
195 count -= 1;
196 }
197 u8::try_from(count).ok()
198 }
199
200 fn validate_component_counts(&self, seg: &Segment<'_>) -> Result<(), EdifactError> {
201 for idx in 0..seg.elements.len() {
202 if let Some(expected) = (self.expected_components)(seg.tag, idx) {
203 let actual = Self::effective_component_count(seg, idx).unwrap_or(0);
204 if actual != expected {
205 return Err(EdifactError::InvalidComponentCount {
206 tag: seg.tag.to_owned(),
207 element_index: idx,
208 expected,
209 actual,
210 offset: seg.span.start,
211 });
212 }
213 }
214 }
215 Ok(())
216 }
217
218 fn validate_code_lists(&self, seg: &Segment<'_>) -> Result<(), EdifactError> {
219 let rules = (self.code_list_rules)(seg.tag);
220
221 for (elem_idx, comp_idx, de) in rules {
222 let value = seg
223 .get_element(*elem_idx)
224 .and_then(|e| e.get_component(*comp_idx))
225 .unwrap_or("");
226 if !value.is_empty() && !(self.is_code_valid)(de, value) {
227 let suggestion = (self.suggest_code)(de, value);
228 return Err(EdifactError::InvalidCodeValue {
229 tag: seg.tag.to_owned(),
230 element_index: *elem_idx,
231 value: value.to_owned(),
232 code_list: (*de).to_owned(),
233 offset: seg.span.start,
234 suggestion,
235 });
236 }
237 }
238
239 Ok(())
240 }
241}
242
243impl DirectoryValidator {
244 fn validate_segment(&self, seg: &Segment<'_>) -> Result<(), EdifactError> {
245 if !self.structure_checks && !self.code_list_checks {
246 return Ok(());
247 }
248
249 let Some(def) = (self.segment_lookup)(seg.tag) else {
250 if self.structure_checks && self.enforce_known_tags {
251 return Err(EdifactError::InvalidSegmentForMessage {
252 tag: seg.tag.to_owned(),
253 message_type: self
254 .message_type
255 .clone()
256 .unwrap_or_else(|| self.directory_id.to_owned()),
257 offset: seg.tag_span.start,
258 });
259 }
260 return Ok(());
261 };
262
263 let max_elements = def.elements.len();
264 let min_elements = def
265 .elements
266 .iter()
267 .rposition(|e| e.status == Status::Mandatory)
268 .map(|idx| idx + 1)
269 .unwrap_or(0);
270 let actual = seg.elements.len();
271
272 if self.structure_checks && (actual < min_elements || actual > max_elements) {
273 return Err(EdifactError::InvalidElementCount {
274 tag: seg.tag.to_owned(),
275 min: min_elements,
276 max: max_elements,
277 actual,
278 offset: seg.span.start,
279 });
280 }
281
282 if self.structure_checks {
283 for element in def
284 .elements
285 .iter()
286 .filter(|e| e.status == Status::Mandatory)
287 {
288 let idx = (element.position as usize).saturating_sub(1);
289 let is_present = seg
290 .elements
291 .get(idx)
292 .is_some_and(|elem| elem.components.iter().any(|c| !c.as_ref().is_empty()));
293 if !is_present {
294 return Err(EdifactError::MissingRequiredElement {
295 tag: seg.tag.to_owned(),
296 element_index: idx,
297 });
298 }
299 }
300 self.validate_component_counts(seg)?;
301
302 if let Some(rule) = self.additional_structure_rule {
303 rule(seg)?;
304 }
305 }
306
307 if self.code_list_checks {
308 self.validate_code_lists(seg)?;
309 }
310
311 Ok(())
312 }
313}
314
315impl Validator for DirectoryValidator {
316 fn set_message_type(&mut self, message_type: Option<&str>) {
317 self.message_type = message_type.map(str::to_owned);
318 }
319
320 fn validate_batch(&self, segments: &[Segment<'_>], report: &mut ValidationReport) {
321 for seg in segments {
322 if let Err(err) = self.validate_segment(seg) {
323 report_error(report, err);
324 }
325 }
326
327 if self.structure_checks {
328 if let Some(message_type) = self.detect_message_type(segments) {
329 for required_tag in Self::required_segments_for(&message_type) {
330 if segments.iter().all(|s| s.tag != *required_tag) {
331 report.add_error(
332 ValidationIssue::new(
333 ValidationSeverity::Error,
334 format!(
335 "required segment {} missing for message type {}",
336 required_tag, message_type
337 ),
338 )
339 .with_segment(*required_tag)
340 .with_suggestion("Add the mandatory segment at the correct position"),
341 );
342 }
343 }
344
345 let seq = Self::required_segments_for(&message_type);
346 let mut last_idx = None;
347 for tag in seq {
348 if let Some(idx) = segments.iter().position(|s| s.tag == *tag) {
349 if let Some(prev) = last_idx {
350 if idx < prev {
351 report.add_error(
352 ValidationIssue::new(
353 ValidationSeverity::Error,
354 format!(
355 "segment sequence violation for message type {}: '{}' appears out of order",
356 message_type, tag
357 ),
358 )
359 .with_segment(*tag)
360 .with_suggestion(
361 "Ensure required segments follow UN/EDIFACT canonical order",
362 ),
363 );
364 }
365 }
366 last_idx = Some(idx);
367 }
368 }
369 }
370 }
371 }
372}
373
374#[cfg(test)]
375mod tests {
376 use super::*;
377
378 static TEST_ELEMENTS: &[ElementRef] = &[ElementRef {
379 position: 1,
380 data_element: "C507",
381 status: Status::Mandatory,
382 max_repeat: 1,
383 }];
384
385 static TEST_SEGMENT: SegmentDefinition = SegmentDefinition {
386 tag: "TST",
387 name: "Test segment",
388 elements: TEST_ELEMENTS,
389 };
390
391 fn segment_lookup(tag: &str) -> Option<&'static SegmentDefinition> {
392 match tag {
393 "TST" => Some(&TEST_SEGMENT),
394 _ => None,
395 }
396 }
397
398 fn code_valid(_de: &str, _code: &str) -> bool {
399 true
400 }
401
402 fn suggest_code(_de: &str, _code: &str) -> Option<&'static str> {
403 None
404 }
405
406 fn expected_components(_tag: &str, _idx: usize) -> Option<u8> {
407 None
408 }
409
410 #[test]
411 fn mandatory_composite_present_when_any_component_non_empty() {
412 let input = b"TST+:ABC'";
413 let segments: Vec<_> = crate::from_bytes(input)
414 .collect::<Result<Vec<_>, _>>()
415 .expect("parse should succeed");
416
417 let validator = DirectoryValidator::new(
418 "TEST",
419 segment_lookup,
420 code_valid,
421 suggest_code,
422 expected_components,
423 None,
424 );
425
426 let mut report = ValidationReport::default();
427 validator.validate_batch(&segments, &mut report);
428 assert!(!report.has_errors());
429 }
430
431 fn parse_single(input: &[u8]) -> crate::model::Segment<'static> {
434 let leaked: &'static [u8] = Box::leak(input.to_vec().into_boxed_slice());
438 crate::from_bytes(leaked)
439 .collect::<Result<Vec<_>, _>>()
440 .expect("parse should succeed")
441 .into_iter()
442 .next()
443 .expect("at least one segment")
444 }
445
446 #[test]
447 fn trailing_empty_component_stripped_from_dtm() {
448 let seg = parse_single(b"DTM+137:20200101:'");
452 let count = DirectoryValidator::effective_component_count(&seg, 0);
453 assert_eq!(count, Some(2), "trailing empty component should be stripped");
454 }
455
456 #[test]
457 fn all_empty_components_result_in_zero() {
458 let seg = parse_single(b"NAD+MS++:'");
460 let count = DirectoryValidator::effective_component_count(&seg, 2);
461 assert_eq!(count, Some(0), "all-empty composite should have effective count 0");
462 }
463
464 #[test]
465 fn non_empty_component_not_stripped() {
466 let seg = parse_single(b"DTM+137:20200101:102'");
468 let count = DirectoryValidator::effective_component_count(&seg, 0);
469 assert_eq!(count, Some(3), "no components should be stripped when all non-empty");
470 }
471
472 #[test]
473 fn with_code_list_rules_overrides_base() {
474 fn custom_rules(tag: &str) -> &'static [(usize, usize, &'static str)] {
476 match tag {
477 "TST" => &[(0, 0, "CUSTOM_DE")],
478 _ => &[],
479 }
480 }
481 fn custom_code_valid(_de: &str, code: &str) -> bool {
482 code == "VALID"
483 }
484 fn no_suggestion(_de: &str, _code: &str) -> Option<&'static str> {
485 None
486 }
487
488 let input = b"TST+INVALID'";
489 let segments: Vec<_> = crate::from_bytes(input)
490 .collect::<Result<Vec<_>, _>>()
491 .expect("parse should succeed");
492
493 let validator = DirectoryValidator::new(
494 "TEST",
495 segment_lookup,
496 custom_code_valid,
497 no_suggestion,
498 expected_components,
499 None,
500 )
501 .with_code_list_rules(custom_rules);
502
503 let mut report = ValidationReport::default();
504 validator.validate_batch(&segments, &mut report);
505 assert!(
506 report.has_warnings(),
507 "INVALID is not in the custom code list so validation must warn"
508 );
509 }
510}