swift_mt_message/messages/
mt111.rs

1use crate::errors::SwiftValidationError;
2use crate::fields::*;
3use crate::parser::utils::*;
4use serde::{Deserialize, Serialize};
5
6/// **MT111: Request for Stop Payment of a Cheque**
7///
8/// Request from drawer bank to drawee bank to stop payment of a specific cheque.
9/// Used to prevent payment when cheque is lost, stolen, or disputed.
10///
11/// **Usage:** Cheque stop payment requests
12/// **Category:** Category 1 (Customer Payments)
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
14pub struct MT111 {
15    /// Sender's reference (Field 20)
16    #[serde(rename = "20")]
17    pub field_20: Field20,
18
19    /// Cheque number (Field 21)
20    #[serde(rename = "21")]
21    pub field_21: Field21NoOption,
22
23    /// Date of issue (Field 30)
24    #[serde(rename = "30")]
25    pub field_30: Field30,
26
27    /// Amount (Field 32)
28    #[serde(flatten)]
29    pub field_32: Field32AB,
30
31    /// Drawer bank (Field 52)
32    #[serde(flatten, skip_serializing_if = "Option::is_none")]
33    pub field_52: Option<Field52OrderingInstitution>,
34
35    /// Payee (Field 59)
36    #[serde(rename = "59", skip_serializing_if = "Option::is_none")]
37    pub field_59: Option<Field59NoOption>,
38
39    /// Queries (Field 75)
40    #[serde(rename = "75", skip_serializing_if = "Option::is_none")]
41    pub field_75: Option<Field75>,
42}
43
44impl MT111 {
45    /// Parse message from Block 4 content
46    pub fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
47        let mut parser = crate::parser::MessageParser::new(block4, "111");
48
49        // Parse mandatory fields
50        let field_20 = parser.parse_field::<Field20>("20")?;
51        let field_21 = parser.parse_field::<Field21NoOption>("21")?;
52        let field_30 = parser.parse_field::<Field30>("30")?;
53
54        // Parse amount - can be 32A or 32B per spec
55        let field_32 = parser.parse_variant_field::<Field32AB>("32")?;
56
57        // Parse optional fields
58        let field_52 = parser.parse_optional_variant_field::<Field52OrderingInstitution>("52")?;
59        let field_59 = parser.parse_optional_field::<Field59NoOption>("59")?;
60        let field_75 = parser.parse_optional_field::<Field75>("75")?;
61
62        Ok(MT111 {
63            field_20,
64            field_21,
65            field_30,
66            field_32,
67            field_52,
68            field_59,
69            field_75,
70        })
71    }
72
73    /// Parse from generic SWIFT input (tries to detect blocks)
74    pub fn parse(input: &str) -> Result<Self, crate::errors::ParseError> {
75        let block4 = extract_block4(input)?;
76        Self::parse_from_block4(&block4)
77    }
78
79    // ========================================================================
80    // NETWORK VALIDATION RULES (SR 2025 MT111)
81    // ========================================================================
82
83    // NOTE: MT111 has no message-level network validation rules (C-series, D-series, E-series, T-series)
84    // per SR 2025 specification. All validation is performed at the field level:
85    // - Field 20, 21: Reference format validation (implemented in field types)
86    // - Field 30: Date validation (implemented in field types)
87    // - Field 32a: Currency and amount validation (implemented in field types)
88    // - Field 52a: BIC validation (implemented in field types)
89    // - Field 59: Account must not be used (enforced by Field59NoOption type)
90    // - Field 75: Narrative format (implemented in field types)
91
92    /// Main validation method - validates all network rules
93    /// Returns array of validation errors, respects stop_on_first_error flag
94    pub fn validate_network_rules(&self, _stop_on_first_error: bool) -> Vec<SwiftValidationError> {
95        // No message-level network validation rules for MT111
96        Vec::new()
97    }
98}
99
100impl crate::traits::SwiftMessageBody for MT111 {
101    fn message_type() -> &'static str {
102        "111"
103    }
104
105    fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
106        // Call the existing public method implementation
107        MT111::parse_from_block4(block4)
108    }
109
110    fn to_mt_string(&self) -> String {
111        // Call the existing public method implementation
112        let mut result = String::new();
113
114        append_field(&mut result, &self.field_20);
115        append_field(&mut result, &self.field_21);
116        append_field(&mut result, &self.field_30);
117        append_field(&mut result, &self.field_32);
118        append_optional_field(&mut result, &self.field_52);
119        append_optional_field(&mut result, &self.field_59);
120        append_optional_field(&mut result, &self.field_75);
121
122        finalize_mt_string(result, false)
123    }
124
125    fn validate_network_rules(&self, stop_on_first_error: bool) -> Vec<SwiftValidationError> {
126        // Call the existing public method implementation
127        MT111::validate_network_rules(self, stop_on_first_error)
128    }
129}