swift_mt_message/messages/
mt900.rs

1use crate::errors::SwiftValidationError;
2use crate::fields::*;
3use crate::parser::utils::*;
4use serde::{Deserialize, Serialize};
5
6/// **MT900: Confirmation of Debit**
7///
8/// Confirms debit to account servicing institution's account.
9///
10/// **Usage:** Debit confirmations, account reconciliation
11/// **Category:** Category 9 (Cash Management & Customer Status)
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13pub struct MT900 {
14    /// Transaction Reference Number (Field 20)
15    #[serde(rename = "20")]
16    pub field_20: Field20,
17
18    /// Related Reference (Field 21)
19    #[serde(rename = "21")]
20    pub field_21: Field21NoOption,
21
22    /// Account Identification (Field 25)
23    #[serde(rename = "25")]
24    pub field_25: Field25AccountIdentification,
25
26    /// Date/Time Indication (Field 13D)
27    #[serde(rename = "13D")]
28    pub field_13d: Option<Field13D>,
29
30    /// Value Date, Currency Code, Amount (Field 32A)
31    #[serde(rename = "32A")]
32    pub field_32a: Field32A,
33
34    /// Ordering Institution (Field 52)
35    #[serde(flatten)]
36    pub field_52: Option<Field52OrderingInstitution>,
37
38    /// Sender to Receiver Information (Field 72)
39    #[serde(rename = "72")]
40    pub field_72: Option<Field72>,
41}
42
43impl MT900 {
44    /// Parse message from Block 4 content
45    pub fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
46        let mut parser = crate::parser::MessageParser::new(block4, "900");
47
48        // Parse mandatory fields in order matching to_mt_string
49        let field_20 = parser.parse_field::<Field20>("20")?;
50        let field_21 = parser.parse_field::<Field21NoOption>("21")?;
51        let field_25 = parser.parse_field::<Field25AccountIdentification>("25")?;
52
53        // Parse optional Field 13D before Field 32A
54        let field_13d = parser.parse_optional_field::<Field13D>("13D")?;
55
56        // Parse mandatory Field 32A
57        let field_32a = parser.parse_field::<Field32A>("32A")?;
58
59        // Parse optional fields
60        let field_52 = parser.parse_optional_variant_field::<Field52OrderingInstitution>("52")?;
61        let field_72 = parser.parse_optional_field::<Field72>("72")?;
62
63        // Verify all content is consumed
64        verify_parser_complete(&parser)?;
65
66        Ok(Self {
67            field_20,
68            field_21,
69            field_25,
70            field_13d,
71            field_32a,
72            field_52,
73            field_72,
74        })
75    }
76
77    /// Parse from SWIFT MT text format
78    pub fn parse(input: &str) -> Result<Self, crate::errors::ParseError> {
79        let block4 = extract_block4(input)?;
80        Self::parse_from_block4(&block4)
81    }
82
83    /// Convert to SWIFT MT text format
84    pub fn to_mt_string(&self) -> String {
85        let mut result = String::new();
86
87        append_field(&mut result, &self.field_20);
88        append_field(&mut result, &self.field_21);
89        append_field(&mut result, &self.field_25);
90        append_optional_field(&mut result, &self.field_13d);
91        append_field(&mut result, &self.field_32a);
92        append_optional_field(&mut result, &self.field_52);
93        append_optional_field(&mut result, &self.field_72);
94
95        result.push('-');
96        result
97    }
98
99    // ========================================================================
100    // NETWORK VALIDATION RULES (SR 2025 MT900)
101    // ========================================================================
102
103    /// Main validation method - validates all network rules
104    ///
105    /// **Note**: According to SR 2025 specifications, MT900 has no network validated rules.
106    /// This method always returns an empty vector.
107    ///
108    /// Returns array of validation errors, respects stop_on_first_error flag
109    pub fn validate_network_rules(&self, _stop_on_first_error: bool) -> Vec<SwiftValidationError> {
110        // MT900 has no network validated rules per SR 2025 specifications
111        Vec::new()
112    }
113}
114
115impl crate::traits::SwiftMessageBody for MT900 {
116    fn message_type() -> &'static str {
117        "900"
118    }
119
120    fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
121        // Call the existing public method implementation
122        MT900::parse_from_block4(block4)
123    }
124
125    fn to_mt_string(&self) -> String {
126        // Call the existing public method implementation
127        MT900::to_mt_string(self)
128    }
129
130    fn validate_network_rules(&self, stop_on_first_error: bool) -> Vec<SwiftValidationError> {
131        // Call the existing public method implementation
132        MT900::validate_network_rules(self, stop_on_first_error)
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139
140    #[test]
141    fn test_mt900_parse() {
142        let mt900_text = r#":20:20240719001
143:21:REF20240719001
144:25:12345678901234567890
145:32A:240719USD1000,00
146-"#;
147        let result = MT900::parse_from_block4(mt900_text);
148        assert!(result.is_ok());
149        let mt900 = result.unwrap();
150        assert_eq!(mt900.field_20.reference, "20240719001");
151        assert_eq!(mt900.field_21.reference, "REF20240719001");
152    }
153
154    #[test]
155    fn test_mt900_network_validation() {
156        let mt900_text = r#":20:20240719001
157:21:REF20240719001
158:25:12345678901234567890
159:32A:240719USD1000,00
160-"#;
161        let mt900 = MT900::parse_from_block4(mt900_text).unwrap();
162
163        // MT900 has no network validation rules, should always return empty vector
164        let errors = mt900.validate_network_rules(false);
165        assert!(errors.is_empty(), "MT900 should have no validation errors");
166
167        // Test with stop_on_first_error=true as well
168        let errors = mt900.validate_network_rules(true);
169        assert!(
170            errors.is_empty(),
171            "MT900 should have no validation errors with stop_on_first_error"
172        );
173    }
174
175    #[test]
176    fn test_mt900_trait_validate_network_rules() {
177        use crate::traits::SwiftMessageBody;
178
179        let mt900_text = r#":20:20240719001
180:21:REF20240719001
181:25:12345678901234567890
182:32A:240719USD1000,00
183-"#;
184        let mt900 = MT900::parse_from_block4(mt900_text).unwrap();
185
186        // Test trait method implementation
187        let errors = <MT900 as SwiftMessageBody>::validate_network_rules(&mt900, false);
188        assert!(
189            errors.is_empty(),
190            "Trait implementation should return empty vector"
191        );
192    }
193}