mx_message/
message_registry.rs

1// Plasmatic MX Message Parsing Library
2// https://github.com/GoPlasmatic/MXMessage
3//
4// Copyright (c) 2025 Plasmatic
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// You may obtain a copy of this library at
18// https://github.com/GoPlasmatic/MXMessage
19
20//! Message Type Registry
21//!
22//! Single source of truth for ISO20022 message type mappings.
23//! This module provides mappings between:
24//! - Short form message types (e.g., "pacs.008")
25//! - Full form message types (e.g., "pacs.008.001.08")
26//! - Rust struct names (e.g., "FIToFICustomerCreditTransferV08")
27//! - XML element names (e.g., "FIToFICstmrCdtTrf")
28//! - XML namespaces
29
30/// Message type registry entry
31/// Format: (short_form, full_form, rust_type_name, xml_element_name)
32pub struct MessageTypeInfo {
33    pub short_form: &'static str,
34    pub full_form: &'static str,
35    pub rust_type_name: &'static str,
36    pub xml_element_name: &'static str,
37}
38
39/// Complete registry of all supported ISO20022 message types
40pub const MESSAGE_REGISTRY: &[MessageTypeInfo] = &[
41    // PACS - Payment Clearing and Settlement
42    MessageTypeInfo {
43        short_form: "pacs.008",
44        full_form: "pacs.008.001.08",
45        rust_type_name: "FIToFICustomerCreditTransferV08",
46        xml_element_name: "FIToFICstmrCdtTrf",
47    },
48    MessageTypeInfo {
49        short_form: "pacs.002",
50        full_form: "pacs.002.001.10",
51        rust_type_name: "FIToFIPaymentStatusReportV10",
52        xml_element_name: "FIToFIPmtStsRpt",
53    },
54    MessageTypeInfo {
55        short_form: "pacs.003",
56        full_form: "pacs.003.001.08",
57        rust_type_name: "FIToFICustomerDirectDebitV08",
58        xml_element_name: "FIToFICstmrDrctDbt",
59    },
60    MessageTypeInfo {
61        short_form: "pacs.004",
62        full_form: "pacs.004.001.09",
63        rust_type_name: "PaymentReturnV09",
64        xml_element_name: "PmtRtr",
65    },
66    MessageTypeInfo {
67        short_form: "pacs.009",
68        full_form: "pacs.009.001.08",
69        rust_type_name: "FinancialInstitutionCreditTransferV08",
70        xml_element_name: "FICdtTrf",
71    },
72    MessageTypeInfo {
73        short_form: "pacs.010",
74        full_form: "pacs.010.001.03",
75        rust_type_name: "FinancialInstitutionDirectDebitV03",
76        xml_element_name: "FIDrctDbt",
77    },
78    // PAIN - Payment Initiation
79    MessageTypeInfo {
80        short_form: "pain.001",
81        full_form: "pain.001.001.09",
82        rust_type_name: "CustomerCreditTransferInitiationV09",
83        xml_element_name: "CstmrCdtTrfInitn",
84    },
85    MessageTypeInfo {
86        short_form: "pain.002",
87        full_form: "pain.002.001.10",
88        rust_type_name: "CustomerPaymentStatusReportV10",
89        xml_element_name: "CstmrPmtStsRpt",
90    },
91    MessageTypeInfo {
92        short_form: "pain.008",
93        full_form: "pain.008.001.08",
94        rust_type_name: "CustomerDirectDebitInitiationV08",
95        xml_element_name: "CstmrDrctDbtInitn",
96    },
97    // CAMT - Cash Management
98    MessageTypeInfo {
99        short_form: "camt.025",
100        full_form: "camt.025.001.08",
101        rust_type_name: "ReceiptV08",
102        xml_element_name: "Rcpt",
103    },
104    MessageTypeInfo {
105        short_form: "camt.029",
106        full_form: "camt.029.001.09",
107        rust_type_name: "ResolutionOfInvestigationV09",
108        xml_element_name: "RsltnOfInvstgtn",
109    },
110    MessageTypeInfo {
111        short_form: "camt.052",
112        full_form: "camt.052.001.08",
113        rust_type_name: "BankToCustomerAccountReportV08",
114        xml_element_name: "BkToCstmrAcctRpt",
115    },
116    MessageTypeInfo {
117        short_form: "camt.053",
118        full_form: "camt.053.001.08",
119        rust_type_name: "BankToCustomerStatementV08",
120        xml_element_name: "BkToCstmrStmt",
121    },
122    MessageTypeInfo {
123        short_form: "camt.054",
124        full_form: "camt.054.001.08",
125        rust_type_name: "BankToCustomerDebitCreditNotificationV08",
126        xml_element_name: "BkToCstmrDbtCdtNtfctn",
127    },
128    MessageTypeInfo {
129        short_form: "camt.056",
130        full_form: "camt.056.001.08",
131        rust_type_name: "FIToFIPaymentCancellationRequestV08",
132        xml_element_name: "FIToFIPmtCxlReq",
133    },
134    MessageTypeInfo {
135        short_form: "camt.057",
136        full_form: "camt.057.001.06",
137        rust_type_name: "NotificationToReceiveV06",
138        xml_element_name: "NtfctnToRcv",
139    },
140    MessageTypeInfo {
141        short_form: "camt.060",
142        full_form: "camt.060.001.05",
143        rust_type_name: "AccountReportingRequestV05",
144        xml_element_name: "AcctRptgReq",
145    },
146    MessageTypeInfo {
147        short_form: "camt.107",
148        full_form: "camt.107.001.01",
149        rust_type_name: "ChequePresentmentNotificationV01",
150        xml_element_name: "ChqPresntmntNtfctn",
151    },
152    MessageTypeInfo {
153        short_form: "camt.108",
154        full_form: "camt.108.001.01",
155        rust_type_name: "ChequeCancellationOrStopRequestV01",
156        xml_element_name: "ChqCxlOrStopReq",
157    },
158    MessageTypeInfo {
159        short_form: "camt.109",
160        full_form: "camt.109.001.01",
161        rust_type_name: "ChequeCancellationOrStopReportV01",
162        xml_element_name: "ChqCxlOrStopRpt",
163    },
164    // ADMI - Administration
165    MessageTypeInfo {
166        short_form: "admi.024",
167        full_form: "admi.024.001.01",
168        rust_type_name: "NotificationOfCorrespondenceV01",
169        xml_element_name: "NtfctnOfCrrspndnc",
170    },
171];
172
173/// Get namespace URI for a message type
174pub fn get_namespace(message_type: &str) -> String {
175    // Look up in the registry
176    for info in MESSAGE_REGISTRY {
177        if message_type == info.short_form || message_type == info.full_form {
178            return format!("urn:iso:std:iso:20022:tech:xsd:{}", info.full_form);
179        }
180    }
181
182    // Default fallback: construct namespace from message type
183    format!("urn:iso:std:iso:20022:tech:xsd:{}", message_type)
184}
185
186/// Convert message type to short form (e.g., "pacs.008.001.08" -> "pacs.008")
187pub fn normalize_message_type(message_type: &str) -> String {
188    for info in MESSAGE_REGISTRY {
189        if message_type == info.short_form || message_type == info.full_form {
190            return info.short_form.to_string();
191        }
192    }
193    message_type.to_string()
194}
195
196/// Map XML element name to message type short form
197pub fn element_to_message_type(element_name: &str) -> Option<&'static str> {
198    MESSAGE_REGISTRY
199        .iter()
200        .find(|info| info.xml_element_name == element_name || info.rust_type_name == element_name)
201        .map(|info| info.short_form)
202}
203
204/// Map message type to XML element name
205pub fn message_type_to_element(message_type: &str) -> Option<&'static str> {
206    MESSAGE_REGISTRY
207        .iter()
208        .find(|info| info.short_form == message_type || info.full_form == message_type)
209        .map(|info| info.xml_element_name)
210}
211
212/// Map message type to Rust type name
213pub fn message_type_to_rust_type(message_type: &str) -> Option<&'static str> {
214    MESSAGE_REGISTRY
215        .iter()
216        .find(|info| info.short_form == message_type || info.full_form == message_type)
217        .map(|info| info.rust_type_name)
218}
219
220/// Get full form of message type (e.g., "pacs.008" -> "pacs.008.001.08")
221pub fn get_full_form(message_type: &str) -> Option<&'static str> {
222    MESSAGE_REGISTRY
223        .iter()
224        .find(|info| info.short_form == message_type || info.full_form == message_type)
225        .map(|info| info.full_form)
226}
227
228#[cfg(test)]
229mod tests {
230    use super::*;
231
232    #[test]
233    fn test_element_to_message_type() {
234        assert_eq!(
235            element_to_message_type("FIToFICstmrCdtTrf"),
236            Some("pacs.008")
237        );
238        assert_eq!(element_to_message_type("BkToCstmrStmt"), Some("camt.053"));
239        assert_eq!(
240            element_to_message_type("CstmrCdtTrfInitn"),
241            Some("pain.001")
242        );
243        assert_eq!(element_to_message_type("UnknownElement"), None);
244    }
245
246    #[test]
247    fn test_message_type_to_element() {
248        assert_eq!(
249            message_type_to_element("pacs.008"),
250            Some("FIToFICstmrCdtTrf")
251        );
252        assert_eq!(
253            message_type_to_element("pacs.008.001.08"),
254            Some("FIToFICstmrCdtTrf")
255        );
256        assert_eq!(message_type_to_element("unknown"), None);
257    }
258
259    #[test]
260    fn test_normalize_message_type() {
261        assert_eq!(normalize_message_type("pacs.008.001.08"), "pacs.008");
262        assert_eq!(normalize_message_type("pacs.008"), "pacs.008");
263        assert_eq!(normalize_message_type("unknown.type"), "unknown.type");
264    }
265
266    #[test]
267    fn test_get_namespace() {
268        assert_eq!(
269            get_namespace("pacs.008"),
270            "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08"
271        );
272        assert_eq!(
273            get_namespace("camt.053"),
274            "urn:iso:std:iso:20022:tech:xsd:camt.053.001.08"
275        );
276    }
277
278    #[test]
279    fn test_get_full_form() {
280        assert_eq!(get_full_form("pacs.008"), Some("pacs.008.001.08"));
281        assert_eq!(get_full_form("pacs.008.001.08"), Some("pacs.008.001.08"));
282        assert_eq!(get_full_form("unknown"), None);
283    }
284}