mx_message/
app_document.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
20use crate::document::*;
21use crate::error::ValidationError;
22use serde::{Deserialize, Serialize};
23
24/// Document represents the root container for all supported CBPR+ ISO20022 message types
25#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
26pub enum Document {
27    // Administrative Messages
28    /// admi.024.001.01 - Notification of Correspondence
29    #[serde(rename = "NtfctnOfCrspdc")]
30    NotificationOfCorrespondenceV01(Box<admi_024_001_01::NotificationOfCorrespondenceV01>),
31
32    // Cash Management Messages (camt)
33    /// camt.025.001.08 - Receipt
34    #[serde(rename = "Rct")]
35    ReceiptV08(Box<camt_025_001_08::ReceiptV08>),
36
37    /// camt.029.001.09 - Resolution of Investigation
38    #[serde(rename = "RsltnOfInvstgtn")]
39    ResolutionOfInvestigationV09(Box<camt_029_001_09::ResolutionOfInvestigationV09>),
40
41    /// camt.052.001.08 - Bank to Customer Account Report
42    #[serde(rename = "BkToCstmrAcctRpt")]
43    BankToCustomerAccountReportV08(Box<camt_052_001_08::BankToCustomerAccountReportV08>),
44
45    /// camt.053.001.08 - Bank to Customer Statement
46    #[serde(rename = "BkToCstmrStmt")]
47    BankToCustomerStatementV08(Box<camt_053_001_08::BankToCustomerStatementV08>),
48
49    /// camt.054.001.08 - Bank to Customer Debit Credit Notification
50    #[serde(rename = "BkToCstmrDbtCdtNtfctn")]
51    BankToCustomerDebitCreditNotificationV08(
52        Box<camt_054_001_08::BankToCustomerDebitCreditNotificationV08>,
53    ),
54
55    /// camt.055.001.08 - Customer Payment Cancellation Request
56    #[serde(rename = "CstmrPmtCxlReq")]
57    CustomerPaymentCancellationRequestV08(
58        Box<camt_055_001_08::CustomerPaymentCancellationRequestV08>,
59    ),
60
61    /// camt.056.001.08 - FI to FI Payment Cancellation Request
62    #[serde(rename = "FIToFIPmtCxlReq")]
63    FIToFIPaymentCancellationRequestV08(Box<camt_056_001_08::FIToFIPaymentCancellationRequestV08>),
64
65    /// camt.057.001.06 - Notification to Receive
66    #[serde(rename = "NtfctnToRcv")]
67    NotificationToReceiveV06(Box<camt_057_001_06::NotificationToReceiveV06>),
68
69    /// camt.058.001.08 - Notification to Receive Cancellation Advice
70    #[serde(rename = "NtfctnToRcvCxlAdvc")]
71    NotificationToReceiveCancellationAdviceV08(
72        Box<camt_058_001_08::NotificationToReceiveCancellationAdviceV08>,
73    ),
74
75    /// camt.060.001.05 - Account Reporting Request
76    #[serde(rename = "AcctRptgReq")]
77    AccountReportingRequestV05(Box<camt_060_001_05::AccountReportingRequestV05>),
78
79    /// camt.105.001.02 - Charges Payment Notification
80    #[serde(rename = "ChrgsInf")]
81    ChargesPaymentNotificationV02(Box<camt_105_001_02::ChargesPaymentNotificationV02>),
82
83    /// camt.105.001.02.mc - Charges Payment Notification (Multi-Currency)
84    #[serde(rename = "ChrgsInfMC")]
85    ChargesPaymentNotificationV02MC(Box<camt_105_001_02_mc::ChargesPaymentNotificationV02>),
86
87    /// camt.106.001.02 - Charges Payment Request
88    #[serde(rename = "ChrgsReq")]
89    ChargesPaymentRequestV02(Box<camt_106_001_02::ChargesPaymentRequestV02>),
90
91    /// camt.106.001.02.mc - Charges Payment Request (Multi-Currency)
92    #[serde(rename = "ChrgsReqMC")]
93    ChargesPaymentRequestV02MC(Box<camt_106_001_02_mc::ChargesPaymentRequestV02>),
94
95    /// camt.107.001.01 - Cheque Presentment Notification
96    #[serde(rename = "ChqPresntmntNtfctn")]
97    ChequePresentmentNotificationV01(Box<camt_107_001_01::ChequePresentmentNotificationV01>),
98
99    /// camt.108.001.01 - Cheque Cancellation or Stop Request
100    #[serde(rename = "ChqCxlOrStopReq")]
101    ChequeCancellationOrStopRequestV01(Box<camt_108_001_01::ChequeCancellationOrStopRequestV01>),
102
103    /// camt.109.001.01 - Cheque Cancellation or Stop Report
104    #[serde(rename = "ChqCxlOrStopRpt")]
105    ChequeCancellationOrStopReportV01(Box<camt_109_001_01::ChequeCancellationOrStopReportV01>),
106
107    // Payments Clearing and Settlement Messages (pacs)
108    /// pacs.002.001.10 - FI to FI Payment Status Report
109    #[serde(rename = "FIToFIPmtStsRpt")]
110    FIToFIPaymentStatusReportV10(Box<pacs_002_001_10::FIToFIPaymentStatusReportV10>),
111
112    /// pacs.003.001.08 - FI to FI Customer Direct Debit
113    #[serde(rename = "FIToFICstmrDrctDbt")]
114    FIToFICustomerDirectDebitV08(Box<pacs_003_001_08::FIToFICustomerDirectDebitV08>),
115
116    /// pacs.004.001.09 - Payment Return
117    #[serde(rename = "PmtRtr")]
118    PaymentReturnV09(Box<pacs_004_001_09::PaymentReturnV09>),
119
120    /// pacs.008.001.08 - FI to FI Customer Credit Transfer
121    #[serde(rename = "FIToFICstmrCdtTrf")]
122    FIToFICustomerCreditTransferV08(Box<pacs_008_001_08::FIToFICustomerCreditTransferV08>),
123
124    /// pacs.008.001.08.stp - FI to FI Customer Credit Transfer (STP)
125    #[serde(rename = "FIToFICstmrCdtTrfSTP")]
126    FIToFICustomerCreditTransferV08STP(Box<pacs_008_001_08_stp::FIToFICustomerCreditTransferV08>),
127
128    /// pacs.009.001.08 - Financial Institution Credit Transfer
129    #[serde(rename = "FinInstnCdtTrf")]
130    FinancialInstitutionCreditTransferV08(
131        Box<pacs_009_001_08::FinancialInstitutionCreditTransferV08>,
132    ),
133
134    /// pacs.009.001.08.adv - Financial Institution Credit Transfer (Advice)
135    #[serde(rename = "FinInstnCdtTrfAdv")]
136    FinancialInstitutionCreditTransferV08ADV(
137        Box<pacs_009_001_08_adv::FinancialInstitutionCreditTransferV08>,
138    ),
139
140    /// pacs.009.001.08.cov - Financial Institution Credit Transfer (Cover)
141    #[serde(rename = "FinInstnCdtTrfCov")]
142    FinancialInstitutionCreditTransferV08COV(
143        Box<pacs_009_001_08_cov::FinancialInstitutionCreditTransferV08>,
144    ),
145
146    // Payment Initiation Messages (pain)
147    /// pain.001.001.09 - Customer Credit Transfer Initiation
148    #[serde(rename = "CstmrCdtTrfInitn")]
149    CustomerCreditTransferInitiationV09(Box<pain_001_001_09::CustomerCreditTransferInitiationV09>),
150
151    /// pain.002.001.10 - Customer Payment Status Report
152    #[serde(rename = "CstmrPmtStsRpt")]
153    CustomerPaymentStatusReportV10(Box<pain_002_001_10::CustomerPaymentStatusReportV10>),
154
155    /// pain.008.001.08 - Customer Direct Debit Initiation
156    #[serde(rename = "CstmrDrctDbtInitn")]
157    CustomerDirectDebitInitiationV08(Box<pain_008_001_08::CustomerDirectDebitInitiationV08>),
158
159    /// Unknown or unsupported document type
160    #[default]
161    UNKNOWN,
162}
163
164impl Document {
165    /// Validates the document according to ISO20022 and CBPR+ specifications
166    pub fn validate(&self) -> Result<(), ValidationError> {
167        match self {
168            // Administrative Messages
169            Document::NotificationOfCorrespondenceV01(value) => value.validate(),
170
171            // Cash Management Messages
172            Document::ReceiptV08(value) => value.validate(),
173            Document::ResolutionOfInvestigationV09(value) => value.validate(),
174            Document::BankToCustomerAccountReportV08(value) => value.validate(),
175            Document::BankToCustomerStatementV08(value) => value.validate(),
176            Document::BankToCustomerDebitCreditNotificationV08(value) => value.validate(),
177            Document::CustomerPaymentCancellationRequestV08(value) => value.validate(),
178            Document::FIToFIPaymentCancellationRequestV08(value) => value.validate(),
179            Document::NotificationToReceiveV06(value) => value.validate(),
180            Document::NotificationToReceiveCancellationAdviceV08(value) => value.validate(),
181            Document::AccountReportingRequestV05(value) => value.validate(),
182            Document::ChargesPaymentNotificationV02(value) => value.validate(),
183            Document::ChargesPaymentNotificationV02MC(value) => value.validate(),
184            Document::ChargesPaymentRequestV02(value) => value.validate(),
185            Document::ChargesPaymentRequestV02MC(value) => value.validate(),
186            Document::ChequePresentmentNotificationV01(value) => value.validate(),
187            Document::ChequeCancellationOrStopRequestV01(value) => value.validate(),
188            Document::ChequeCancellationOrStopReportV01(value) => value.validate(),
189
190            // Payments Clearing and Settlement Messages
191            Document::FIToFIPaymentStatusReportV10(value) => value.validate(),
192            Document::FIToFICustomerDirectDebitV08(value) => value.validate(),
193            Document::PaymentReturnV09(value) => value.validate(),
194            Document::FIToFICustomerCreditTransferV08(value) => value.validate(),
195            Document::FIToFICustomerCreditTransferV08STP(value) => value.validate(),
196            Document::FinancialInstitutionCreditTransferV08(value) => value.validate(),
197            Document::FinancialInstitutionCreditTransferV08ADV(value) => value.validate(),
198            Document::FinancialInstitutionCreditTransferV08COV(value) => value.validate(),
199
200            // Payment Initiation Messages
201            Document::CustomerCreditTransferInitiationV09(value) => value.validate(),
202            Document::CustomerPaymentStatusReportV10(value) => value.validate(),
203            Document::CustomerDirectDebitInitiationV08(value) => value.validate(),
204
205            Document::UNKNOWN => Err(ValidationError::new(
206                9999,
207                "Unknown document type".to_string(),
208            )),
209        }
210    }
211
212    /// Returns the message type identifier for the document
213    pub fn message_type(&self) -> &'static str {
214        match self {
215            // Administrative Messages
216            Document::NotificationOfCorrespondenceV01(_) => "admi.024.001.01",
217
218            // Cash Management Messages
219            Document::ReceiptV08(_) => "camt.025.001.08",
220            Document::ResolutionOfInvestigationV09(_) => "camt.029.001.09",
221            Document::BankToCustomerAccountReportV08(_) => "camt.052.001.08",
222            Document::BankToCustomerStatementV08(_) => "camt.053.001.08",
223            Document::BankToCustomerDebitCreditNotificationV08(_) => "camt.054.001.08",
224            Document::CustomerPaymentCancellationRequestV08(_) => "camt.055.001.08",
225            Document::FIToFIPaymentCancellationRequestV08(_) => "camt.056.001.08",
226            Document::NotificationToReceiveV06(_) => "camt.057.001.06",
227            Document::NotificationToReceiveCancellationAdviceV08(_) => "camt.058.001.08",
228            Document::AccountReportingRequestV05(_) => "camt.060.001.05",
229            Document::ChargesPaymentNotificationV02(_) => "camt.105.001.02",
230            Document::ChargesPaymentNotificationV02MC(_) => "camt.105.001.02.mc",
231            Document::ChargesPaymentRequestV02(_) => "camt.106.001.02",
232            Document::ChargesPaymentRequestV02MC(_) => "camt.106.001.02.mc",
233            Document::ChequePresentmentNotificationV01(_) => "camt.107.001.01",
234            Document::ChequeCancellationOrStopRequestV01(_) => "camt.108.001.01",
235            Document::ChequeCancellationOrStopReportV01(_) => "camt.109.001.01",
236
237            // Payments Clearing and Settlement Messages
238            Document::FIToFIPaymentStatusReportV10(_) => "pacs.002.001.10",
239            Document::FIToFICustomerDirectDebitV08(_) => "pacs.003.001.08",
240            Document::PaymentReturnV09(_) => "pacs.004.001.09",
241            Document::FIToFICustomerCreditTransferV08(_) => "pacs.008.001.08",
242            Document::FIToFICustomerCreditTransferV08STP(_) => "pacs.008.001.08.stp",
243            Document::FinancialInstitutionCreditTransferV08(_) => "pacs.009.001.08",
244            Document::FinancialInstitutionCreditTransferV08ADV(_) => "pacs.009.001.08.adv",
245            Document::FinancialInstitutionCreditTransferV08COV(_) => "pacs.009.001.08.cov",
246
247            // Payment Initiation Messages
248            Document::CustomerCreditTransferInitiationV09(_) => "pain.001.001.09",
249            Document::CustomerPaymentStatusReportV10(_) => "pain.002.001.10",
250            Document::CustomerDirectDebitInitiationV08(_) => "pain.008.001.08",
251
252            Document::UNKNOWN => "unknown",
253        }
254    }
255
256    /// Returns whether the document is CBPR+ compliant
257    pub fn is_cbpr_plus_compliant(&self) -> bool {
258        match self {
259            Document::UNKNOWN => false,
260            _ => true, // All implemented message types are CBPR+ compliant
261        }
262    }
263}