Skip to main content

automapper_validation/validator/
codes.rs

1//! Standard error codes for validation issues.
2//!
3//! Error codes follow a prefix convention:
4//! - `STR0xx`: Structure validation
5//! - `FMT0xx`: Format validation
6//! - `COD0xx`: Code value validation
7//! - `AHB0xx`: AHB condition rule validation
8
9/// Standard error codes for validation issues.
10pub struct ErrorCodes;
11
12impl ErrorCodes {
13    // --- Structure validation (STR001-STR099) ---
14
15    /// A mandatory segment is missing.
16    pub const MISSING_MANDATORY_SEGMENT: &'static str = "STR001";
17
18    /// Segment repetitions exceed the MIG MaxRep count.
19    pub const MAX_REPETITIONS_EXCEEDED: &'static str = "STR002";
20
21    /// Unexpected segment found (not defined in MIG for this message type).
22    pub const UNEXPECTED_SEGMENT: &'static str = "STR003";
23
24    /// Segments are in wrong order according to the MIG.
25    pub const WRONG_SEGMENT_ORDER: &'static str = "STR004";
26
27    /// A mandatory segment group is missing.
28    pub const MISSING_MANDATORY_GROUP: &'static str = "STR005";
29
30    /// Segment group repetitions exceed MaxRep count.
31    pub const GROUP_MAX_REP_EXCEEDED: &'static str = "STR006";
32
33    /// UNT segment count does not match the actual number of segments.
34    pub const UNT_SEGMENT_COUNT_MISMATCH: &'static str = "STR007";
35
36    /// A segment was present in the EDIFACT input but the PID-filtered MIG
37    /// has no slot for it. The assembler advanced past it (skip mode on),
38    /// so subsequent legitimate content still assembled, but the message
39    /// contains data outside the AHB and callers should surface it.
40    pub const SKIPPED_UNKNOWN_SEGMENT: &'static str = "STR008";
41
42    // --- Format validation (FMT001-FMT099) ---
43
44    /// Value exceeds maximum allowed length.
45    pub const VALUE_TOO_LONG: &'static str = "FMT001";
46
47    /// Value does not match required numeric format.
48    pub const INVALID_NUMERIC_FORMAT: &'static str = "FMT002";
49
50    /// Value does not match required alphanumeric format.
51    pub const INVALID_ALPHANUMERIC_FORMAT: &'static str = "FMT003";
52
53    /// Invalid date/time format.
54    pub const INVALID_DATE_FORMAT: &'static str = "FMT004";
55
56    /// Value is shorter than minimum required length.
57    pub const VALUE_TOO_SHORT: &'static str = "FMT005";
58
59    /// A required element is empty.
60    pub const REQUIRED_ELEMENT_EMPTY: &'static str = "FMT006";
61
62    // --- Code validation (COD001-COD099) ---
63
64    /// Code value is not in the allowed code list for this element.
65    pub const INVALID_CODE_VALUE: &'static str = "COD001";
66
67    /// Code value is not allowed for this specific Pruefidentifikator.
68    pub const CODE_NOT_ALLOWED_FOR_PID: &'static str = "COD002";
69
70    // --- AHB validation (AHB001-AHB999) ---
71
72    /// A field required by the AHB for this PID is missing.
73    pub const MISSING_REQUIRED_FIELD: &'static str = "AHB001";
74
75    /// A field is present but not allowed by the AHB for this PID.
76    pub const FIELD_NOT_ALLOWED_FOR_PID: &'static str = "AHB002";
77
78    /// A conditional AHB rule is violated.
79    pub const CONDITIONAL_RULE_VIOLATION: &'static str = "AHB003";
80
81    /// The Pruefidentifikator is unknown / not supported.
82    pub const UNKNOWN_PRUEFIDENTIFIKATOR: &'static str = "AHB004";
83
84    /// A condition expression could not be fully evaluated (Unknown result).
85    pub const CONDITION_UNKNOWN: &'static str = "AHB005";
86
87    /// Package cardinality constraint violated: count of present codes outside [min..max].
88    pub const PACKAGE_CARDINALITY_VIOLATION: &'static str = "AHB006";
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn test_error_code_prefixes() {
97        assert!(ErrorCodes::MISSING_MANDATORY_SEGMENT.starts_with("STR"));
98        assert!(ErrorCodes::VALUE_TOO_LONG.starts_with("FMT"));
99        assert!(ErrorCodes::INVALID_CODE_VALUE.starts_with("COD"));
100        assert!(ErrorCodes::MISSING_REQUIRED_FIELD.starts_with("AHB"));
101    }
102
103    #[test]
104    fn test_all_codes_are_unique() {
105        let codes = [
106            ErrorCodes::MISSING_MANDATORY_SEGMENT,
107            ErrorCodes::MAX_REPETITIONS_EXCEEDED,
108            ErrorCodes::UNEXPECTED_SEGMENT,
109            ErrorCodes::WRONG_SEGMENT_ORDER,
110            ErrorCodes::MISSING_MANDATORY_GROUP,
111            ErrorCodes::GROUP_MAX_REP_EXCEEDED,
112            ErrorCodes::UNT_SEGMENT_COUNT_MISMATCH,
113            ErrorCodes::SKIPPED_UNKNOWN_SEGMENT,
114            ErrorCodes::VALUE_TOO_LONG,
115            ErrorCodes::INVALID_NUMERIC_FORMAT,
116            ErrorCodes::INVALID_ALPHANUMERIC_FORMAT,
117            ErrorCodes::INVALID_DATE_FORMAT,
118            ErrorCodes::VALUE_TOO_SHORT,
119            ErrorCodes::REQUIRED_ELEMENT_EMPTY,
120            ErrorCodes::INVALID_CODE_VALUE,
121            ErrorCodes::CODE_NOT_ALLOWED_FOR_PID,
122            ErrorCodes::MISSING_REQUIRED_FIELD,
123            ErrorCodes::FIELD_NOT_ALLOWED_FOR_PID,
124            ErrorCodes::CONDITIONAL_RULE_VIOLATION,
125            ErrorCodes::UNKNOWN_PRUEFIDENTIFIKATOR,
126            ErrorCodes::CONDITION_UNKNOWN,
127            ErrorCodes::PACKAGE_CARDINALITY_VIOLATION,
128        ];
129
130        let unique: std::collections::HashSet<&str> = codes.iter().copied().collect();
131        assert_eq!(codes.len(), unique.len(), "Duplicate error codes found");
132    }
133}