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    /// The answer is not a code of the EBD or Codeliste named beside it.
71    pub const ANSWER_NOT_IN_LIST: &'static str = "COD003";
72
73    // --- AHB validation (AHB001-AHB999) ---
74
75    /// A field required by the AHB for this PID is missing.
76    pub const MISSING_REQUIRED_FIELD: &'static str = "AHB001";
77
78    /// A field is present but not allowed by the AHB for this PID.
79    pub const FIELD_NOT_ALLOWED_FOR_PID: &'static str = "AHB002";
80
81    /// A conditional AHB rule is violated.
82    pub const CONDITIONAL_RULE_VIOLATION: &'static str = "AHB003";
83
84    /// The Pruefidentifikator is unknown / not supported.
85    pub const UNKNOWN_PRUEFIDENTIFIKATOR: &'static str = "AHB004";
86
87    /// A condition expression could not be fully evaluated (Unknown result).
88    pub const CONDITION_UNKNOWN: &'static str = "AHB005";
89
90    /// Package cardinality constraint violated: count of present codes outside [min..max].
91    pub const PACKAGE_CARDINALITY_VIOLATION: &'static str = "AHB006";
92
93    /// A group or segment allowed once per transaction is sent more often.
94    pub const REPEATED_IN_TRANSACTION: &'static str = "AHB007";
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_error_code_prefixes() {
103        assert!(ErrorCodes::MISSING_MANDATORY_SEGMENT.starts_with("STR"));
104        assert!(ErrorCodes::VALUE_TOO_LONG.starts_with("FMT"));
105        assert!(ErrorCodes::INVALID_CODE_VALUE.starts_with("COD"));
106        assert!(ErrorCodes::MISSING_REQUIRED_FIELD.starts_with("AHB"));
107    }
108
109    #[test]
110    fn test_all_codes_are_unique() {
111        let codes = [
112            ErrorCodes::MISSING_MANDATORY_SEGMENT,
113            ErrorCodes::MAX_REPETITIONS_EXCEEDED,
114            ErrorCodes::UNEXPECTED_SEGMENT,
115            ErrorCodes::WRONG_SEGMENT_ORDER,
116            ErrorCodes::MISSING_MANDATORY_GROUP,
117            ErrorCodes::GROUP_MAX_REP_EXCEEDED,
118            ErrorCodes::UNT_SEGMENT_COUNT_MISMATCH,
119            ErrorCodes::SKIPPED_UNKNOWN_SEGMENT,
120            ErrorCodes::VALUE_TOO_LONG,
121            ErrorCodes::INVALID_NUMERIC_FORMAT,
122            ErrorCodes::INVALID_ALPHANUMERIC_FORMAT,
123            ErrorCodes::INVALID_DATE_FORMAT,
124            ErrorCodes::VALUE_TOO_SHORT,
125            ErrorCodes::REQUIRED_ELEMENT_EMPTY,
126            ErrorCodes::INVALID_CODE_VALUE,
127            ErrorCodes::CODE_NOT_ALLOWED_FOR_PID,
128            ErrorCodes::MISSING_REQUIRED_FIELD,
129            ErrorCodes::FIELD_NOT_ALLOWED_FOR_PID,
130            ErrorCodes::CONDITIONAL_RULE_VIOLATION,
131            ErrorCodes::UNKNOWN_PRUEFIDENTIFIKATOR,
132            ErrorCodes::CONDITION_UNKNOWN,
133            ErrorCodes::PACKAGE_CARDINALITY_VIOLATION,
134            ErrorCodes::REPEATED_IN_TRANSACTION,
135        ];
136
137        let unique: std::collections::HashSet<&str> = codes.iter().copied().collect();
138        assert_eq!(codes.len(), unique.len(), "Duplicate error codes found");
139    }
140}