mx_message/
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::common::ValidationError;
21
22use crate::camt_027_001_07::ClaimNonReceiptV07;
23use crate::camt_028_001_09::AdditionalPaymentInformationV09;
24use crate::camt_029_001_09::ResolutionOfInvestigationV09;
25use crate::camt_052_001_08::BankToCustomerAccountReportV08;
26use crate::camt_053_001_08::BankToCustomerStatementV08;
27use crate::camt_056_001_08::FIToFIPaymentCancellationRequestV08;
28use crate::camt_057_001_06::NotificationToReceiveV06;
29use crate::camt_998_001_03::CashManagementProprietaryMessageV03;
30use crate::pacs_008_001_08::FIToFICustomerCreditTransferV08;
31use crate::pacs_009_001_08::FinancialInstitutionCreditTransferV08;
32use serde::{Deserialize, Serialize};
33
34/// Document represents the root container for all supported CBPR+ ISO20022 message types
35#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
36pub enum Document {
37    /// pacs.008.001.08 - FI to FI Customer Credit Transfer
38    #[serde(rename = "FIToFICstmrCdtTrf")]
39    FIToFICustomerCreditTransferV08(Box<FIToFICustomerCreditTransferV08>),
40
41    /// pacs.009.001.08 - Financial Institution Credit Transfer  
42    #[serde(rename = "FinInstnCdtTrf")]
43    FinancialInstitutionCreditTransferV08(Box<FinancialInstitutionCreditTransferV08>),
44
45    /// camt.027.001.07 - Claim Non Receipt
46    #[serde(rename = "ClmNonRct")]
47    ClaimNonReceiptV07(Box<ClaimNonReceiptV07>),
48
49    /// camt.028.001.09 - Additional Payment Information
50    #[serde(rename = "AddtlPmtInf")]
51    AdditionalPaymentInformationV09(Box<AdditionalPaymentInformationV09>),
52
53    /// camt.029.001.09 - Resolution of Investigation
54    #[serde(rename = "RsltnOfInvstgtn")]
55    ResolutionOfInvestigationV09(Box<ResolutionOfInvestigationV09>),
56
57    /// camt.052.001.08 - Bank to Customer Account Report
58    #[serde(rename = "BkToCstmrAcctRpt")]
59    BankToCustomerAccountReportV08(Box<BankToCustomerAccountReportV08>),
60
61    /// camt.053.001.08 - Bank to Customer Statement
62    #[serde(rename = "BkToCstmrStmt")]
63    BankToCustomerStatementV08(Box<BankToCustomerStatementV08>),
64
65    /// camt.056.001.08 - FI to FI Payment Cancellation Request
66    #[serde(rename = "FIToFIPmtCxlReq")]
67    FIToFIPaymentCancellationRequestV08(Box<FIToFIPaymentCancellationRequestV08>),
68
69    /// camt.057.001.06 - Notification to Receive
70    #[serde(rename = "NtfctnToRcv")]
71    NotificationToReceiveV06(Box<NotificationToReceiveV06>),
72
73    /// camt.998.001.03 - Cash Management Proprietary Message
74    #[serde(rename = "CshMgmtPrtryMsg")]
75    CashManagementProprietaryMessageV03(Box<CashManagementProprietaryMessageV03>),
76
77    /// Unknown or unsupported document type
78    #[default]
79    UNKNOWN,
80}
81
82impl Document {
83    /// Validates the document according to ISO20022 and CBPR+ specifications
84    pub fn validate(&self) -> Result<(), ValidationError> {
85        match self {
86            Document::FIToFICustomerCreditTransferV08(value) => value.validate(),
87            Document::FinancialInstitutionCreditTransferV08(value) => value.validate(),
88            Document::ClaimNonReceiptV07(value) => value.validate(),
89            Document::AdditionalPaymentInformationV09(value) => value.validate(),
90            Document::ResolutionOfInvestigationV09(value) => value.validate(),
91            Document::BankToCustomerAccountReportV08(value) => value.validate(),
92            Document::BankToCustomerStatementV08(value) => value.validate(),
93            Document::FIToFIPaymentCancellationRequestV08(value) => value.validate(),
94            Document::NotificationToReceiveV06(value) => value.validate(),
95            Document::CashManagementProprietaryMessageV03(value) => value.validate(),
96            Document::UNKNOWN => {
97                // Return an error for the UNKNOWN case
98                Err(ValidationError::new(
99                    9999,
100                    "Unknown document type".to_string(),
101                ))
102            }
103        }
104    }
105
106    /// Returns the message type identifier for the document
107    pub fn message_type(&self) -> &'static str {
108        match self {
109            Document::FIToFICustomerCreditTransferV08(_) => "pacs.008.001.08",
110            Document::FinancialInstitutionCreditTransferV08(_) => "pacs.009.001.08",
111            Document::ClaimNonReceiptV07(_) => "camt.027.001.07",
112            Document::AdditionalPaymentInformationV09(_) => "camt.028.001.09",
113            Document::ResolutionOfInvestigationV09(_) => "camt.029.001.09",
114            Document::BankToCustomerAccountReportV08(_) => "camt.052.001.08",
115            Document::BankToCustomerStatementV08(_) => "camt.053.001.08",
116            Document::FIToFIPaymentCancellationRequestV08(_) => "camt.056.001.08",
117            Document::NotificationToReceiveV06(_) => "camt.057.001.06",
118            Document::CashManagementProprietaryMessageV03(_) => "camt.998.001.03",
119            Document::UNKNOWN => "unknown",
120        }
121    }
122
123    /// Returns whether the document is CBPR+ compliant
124    pub fn is_cbpr_plus_compliant(&self) -> bool {
125        match self {
126            Document::UNKNOWN => false,
127            _ => true, // All implemented message types are CBPR+ compliant
128        }
129    }
130}