swift_mt_message/messages/
mt199.rs

1use crate::errors::SwiftValidationError;
2use crate::fields::*;
3use crate::parser::utils::*;
4use serde::{Deserialize, Serialize};
5
6/// **MT199: Free Format Message**
7///
8/// Free format communication between financial institutions regarding customer payments.
9///
10/// **Usage:** General payment inquiries, informal communication, status messages
11/// **Category:** Category 1 (Customer Payments & Cheques)
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13pub struct MT199 {
14    /// Sender's Reference (Field 20)
15    #[serde(rename = "20")]
16    pub field_20: Field20,
17
18    /// Related Reference (Field 21)
19    #[serde(rename = "21", skip_serializing_if = "Option::is_none")]
20    pub field_21: Option<Field21NoOption>,
21
22    /// Narrative (Field 79)
23    #[serde(rename = "79")]
24    pub field_79: Field79,
25}
26
27impl MT199 {
28    /// Parse message from Block 4 content
29    pub fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
30        let mut parser = crate::parser::MessageParser::new(block4, "199");
31
32        // Parse mandatory field 20
33        let field_20 = parser.parse_field::<Field20>("20")?;
34
35        // Parse optional field 21
36        let field_21 = parser.parse_optional_field::<Field21NoOption>("21")?;
37
38        // Parse mandatory field 79
39        let field_79 = parser.parse_field::<Field79>("79")?;
40
41        Ok(MT199 {
42            field_20,
43            field_21,
44            field_79,
45        })
46    }
47
48    /// Parse from generic SWIFT input (tries to detect blocks)
49    pub fn parse(input: &str) -> Result<Self, crate::errors::ParseError> {
50        let block4 = extract_block4(input)?;
51        Self::parse_from_block4(&block4)
52    }
53
54    // ========================================================================
55    // NETWORK VALIDATION RULES (SR 2025 MTn99)
56    // ========================================================================
57
58    /// Main validation method - validates all network rules
59    ///
60    /// According to SR 2025 MTn99 specification:
61    /// "There are no network validated rules for this message type beyond the standard
62    /// field-specific rules."
63    ///
64    /// Returns array of validation errors, respects stop_on_first_error flag.
65    ///
66    /// ## Parameters
67    /// - `stop_on_first_error`: If true, returns immediately upon finding the first error
68    ///
69    /// ## Returns
70    /// - Empty vector (MT199 has no network validation rules)
71    pub fn validate_network_rules(&self, _stop_on_first_error: bool) -> Vec<SwiftValidationError> {
72        // MT199 has no network validated rules according to SR 2025 specification
73        // All validation is performed at the field level during parsing
74        Vec::new()
75    }
76
77    /// Check if this is a reject message
78    pub fn is_reject_message(&self) -> bool {
79        self.field_79
80            .information
81            .first()
82            .map(|line| line.starts_with("/REJT/"))
83            .unwrap_or(false)
84    }
85
86    /// Check if this is a return message
87    pub fn is_return_message(&self) -> bool {
88        self.field_79
89            .information
90            .first()
91            .map(|line| line.starts_with("/RETN/"))
92            .unwrap_or(false)
93    }
94}
95
96impl crate::traits::SwiftMessageBody for MT199 {
97    fn message_type() -> &'static str {
98        "199"
99    }
100
101    fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
102        // Call the existing public method implementation
103        MT199::parse_from_block4(block4)
104    }
105
106    fn to_mt_string(&self) -> String {
107        // Call the existing public method implementation
108        let mut result = String::new();
109
110        append_field(&mut result, &self.field_20);
111        append_optional_field(&mut result, &self.field_21);
112        append_field(&mut result, &self.field_79);
113
114        finalize_mt_string(result, false)
115    }
116
117    fn validate_network_rules(&self, stop_on_first_error: bool) -> Vec<SwiftValidationError> {
118        // Call the existing public method implementation
119        MT199::validate_network_rules(self, stop_on_first_error)
120    }
121}