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::camt_029_001_09::ResolutionOfInvestigationV09;
21use crate::camt_056_001_08::FIToFIPaymentCancellationRequestV08;
22use crate::camt_057_001_06::NotificationToReceiveV06;
23use crate::common::ValidationError;
24use crate::pacs_008_001_08::FIToFICustomerCreditTransferV08;
25use crate::pacs_009_001_08::FinancialInstitutionCreditTransferV08;
26use serde::{Deserialize, Serialize};
27
28/// Document represents the root container for all supported CBPR+ ISO20022 message types
29#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
30pub enum Document {
31    /// pacs.008.001.08 - FI to FI Customer Credit Transfer
32    #[serde(rename = "FIToFICstmrCdtTrf")]
33    FIToFICustomerCreditTransferV08(Box<FIToFICustomerCreditTransferV08>),
34
35    /// pacs.009.001.08 - Financial Institution Credit Transfer  
36    #[serde(rename = "FinInstnCdtTrf")]
37    FinancialInstitutionCreditTransferV08(Box<FinancialInstitutionCreditTransferV08>),
38
39    /// camt.029.001.09 - Resolution of Investigation
40    #[serde(rename = "RsltnOfInvstgtn")]
41    ResolutionOfInvestigationV09(Box<ResolutionOfInvestigationV09>),
42
43    /// camt.056.001.08 - FI to FI Payment Cancellation Request
44    #[serde(rename = "FIToFIPmtCxlReq")]
45    FIToFIPaymentCancellationRequestV08(Box<FIToFIPaymentCancellationRequestV08>),
46
47    /// camt.057.001.06 - Notification to Receive
48    #[serde(rename = "NtfctnToRcv")]
49    NotificationToReceiveV06(Box<NotificationToReceiveV06>),
50
51    /// Unknown or unsupported document type
52    #[default]
53    UNKNOWN,
54}
55
56impl Document {
57    /// Validates the document according to ISO20022 and CBPR+ specifications
58    pub fn validate(&self) -> Result<(), ValidationError> {
59        match self {
60            Document::FIToFICustomerCreditTransferV08(value) => value.validate(),
61            Document::FinancialInstitutionCreditTransferV08(value) => value.validate(),
62            Document::ResolutionOfInvestigationV09(value) => value.validate(),
63            Document::FIToFIPaymentCancellationRequestV08(value) => value.validate(),
64            Document::NotificationToReceiveV06(value) => value.validate(),
65            Document::UNKNOWN => {
66                // Return an error for the UNKNOWN case
67                Err(ValidationError::new(
68                    9999,
69                    "Unknown document type".to_string(),
70                ))
71            }
72        }
73    }
74
75    /// Returns the message type identifier for the document
76    pub fn message_type(&self) -> &'static str {
77        match self {
78            Document::FIToFICustomerCreditTransferV08(_) => "pacs.008.001.08",
79            Document::FinancialInstitutionCreditTransferV08(_) => "pacs.009.001.08",
80            Document::ResolutionOfInvestigationV09(_) => "camt.029.001.09",
81            Document::FIToFIPaymentCancellationRequestV08(_) => "camt.056.001.08",
82            Document::NotificationToReceiveV06(_) => "camt.057.001.06",
83            Document::UNKNOWN => "unknown",
84        }
85    }
86
87    /// Returns whether the document is CBPR+ compliant
88    pub fn is_cbpr_plus_compliant(&self) -> bool {
89        match self {
90            Document::UNKNOWN => false,
91            _ => true, // All implemented message types are CBPR+ compliant
92        }
93    }
94}