mx_message/
app_header.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::error::ValidationError;
21use crate::header::*;
22use serde::{Deserialize, Serialize};
23
24/// AppHeader represents the application header container for all supported CBPR+ ISO20022 message types
25#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
26pub enum AppHeader {
27    // Administrative Messages Headers
28    /// admi.024.001.01 - Notification of Correspondence Header
29    #[serde(rename = "BizAppHdr_ADMI_024_001_01")]
30    BusinessApplicationHeaderV02Admi024_001_01(
31        Box<bah_admi_024_001_01::BusinessApplicationHeaderV02>,
32    ),
33
34    // Cash Management Messages Headers (camt)
35    /// camt.025.001.08 - Receipt Header
36    #[serde(rename = "BizAppHdr_CAMT_025_001_08")]
37    BusinessApplicationHeaderV02Camt025_001_08(
38        Box<bah_camt_025_001_08::BusinessApplicationHeaderV02>,
39    ),
40
41    /// camt.029.001 - Resolution of Investigation Header
42    #[serde(rename = "BizAppHdr_CAMT_029_001")]
43    BusinessApplicationHeaderV02Camt029_001(Box<bah_camt_029_001::BusinessApplicationHeaderV02>),
44
45    /// camt.052.001.08 - Bank to Customer Account Report Header
46    #[serde(rename = "BizAppHdr_CAMT_052_001_08")]
47    BusinessApplicationHeaderV02Camt052_001_08(
48        Box<bah_camt_052_001_08::BusinessApplicationHeaderV02>,
49    ),
50
51    /// camt.053.001.08 - Bank to Customer Statement Header
52    #[serde(rename = "BizAppHdr_CAMT_053_001_08")]
53    BusinessApplicationHeaderV02Camt053_001_08(
54        Box<bah_camt_053_001_08::BusinessApplicationHeaderV02>,
55    ),
56
57    /// camt.054.001 - Bank to Customer Debit Credit Notification Header
58    #[serde(rename = "BizAppHdr_CAMT_054_001")]
59    BusinessApplicationHeaderV02Camt054_001(Box<bah_camt_054_001::BusinessApplicationHeaderV02>),
60
61    /// camt.055.001.08 - Customer Payment Cancellation Request Header
62    #[serde(rename = "BizAppHdr_CAMT_055_001_08")]
63    BusinessApplicationHeaderV02Camt055_001_08(
64        Box<bah_camt_055_001_08::BusinessApplicationHeaderV02>,
65    ),
66
67    /// camt.056.001.08 - FI to FI Payment Cancellation Request Header
68    #[serde(rename = "BizAppHdr_CAMT_056_001_08")]
69    BusinessApplicationHeaderV02Camt056_001_08(
70        Box<bah_camt_056_001_08::BusinessApplicationHeaderV02>,
71    ),
72
73    /// camt.057.001.06 - Notification to Receive Header
74    #[serde(rename = "BizAppHdr_CAMT_057_001_06")]
75    BusinessApplicationHeaderV02Camt057_001_06(
76        Box<bah_camt_057_001_06::BusinessApplicationHeaderV02>,
77    ),
78
79    /// camt.058.001.08 - Notification to Receive Cancellation Advice Header
80    #[serde(rename = "BizAppHdr_CAMT_058_001_08")]
81    BusinessApplicationHeaderV02Camt058_001_08(
82        Box<bah_camt_058_001_08::BusinessApplicationHeaderV02>,
83    ),
84
85    /// camt.060.001.05 - Account Reporting Request Header
86    #[serde(rename = "BizAppHdr_CAMT_060_001_05")]
87    BusinessApplicationHeaderV02Camt060_001_05(
88        Box<bah_camt_060_001_05::BusinessApplicationHeaderV02>,
89    ),
90
91    /// camt.105.001.02 - Charges Payment Notification Header
92    #[serde(rename = "BizAppHdr_CAMT_105_001_02")]
93    BusinessApplicationHeaderV02Camt105_001_02(
94        Box<bah_camt_105_001_02::BusinessApplicationHeaderV02>,
95    ),
96
97    /// camt.105.001.02.mc - Charges Payment Notification Header (Multi-Currency)
98    #[serde(rename = "BizAppHdr_CAMT_105_001_02_MC")]
99    BusinessApplicationHeaderV02Camt105_001_02mc(
100        Box<bah_camt_105_001_02_mc::BusinessApplicationHeaderV02>,
101    ),
102
103    /// camt.106.001.02 - Charges Payment Request Header
104    #[serde(rename = "BizAppHdr_CAMT_106_001_02")]
105    BusinessApplicationHeaderV02Camt106_001_02(
106        Box<bah_camt_106_001_02::BusinessApplicationHeaderV02>,
107    ),
108
109    /// camt.106.001.02.mc - Charges Payment Request Header (Multi-Currency)
110    #[serde(rename = "BizAppHdr_CAMT_106_001_02_MC")]
111    BusinessApplicationHeaderV02Camt106_001_02mc(
112        Box<bah_camt_106_001_02_mc::BusinessApplicationHeaderV02>,
113    ),
114
115    /// camt.107.001.01 - Cheque Presentment Notification Header
116    #[serde(rename = "BizAppHdr_CAMT_107_001_01")]
117    BusinessApplicationHeaderV02Camt107_001_01(
118        Box<bah_camt_107_001_01::BusinessApplicationHeaderV02>,
119    ),
120
121    /// camt.108.001.01 - Cheque Cancellation or Stop Request Header
122    #[serde(rename = "BizAppHdr_CAMT_108_001_01")]
123    BusinessApplicationHeaderV02Camt108_001_01(
124        Box<bah_camt_108_001_01::BusinessApplicationHeaderV02>,
125    ),
126
127    /// camt.109.001.01 - Cheque Cancellation or Stop Report Header
128    #[serde(rename = "BizAppHdr_CAMT_109_001_01")]
129    BusinessApplicationHeaderV02Camt109_001_01(
130        Box<bah_camt_109_001_01::BusinessApplicationHeaderV02>,
131    ),
132
133    // Payments Clearing and Settlement Messages Headers (pacs)
134    /// pacs.002.001.10 - FI to FI Payment Status Report Header
135    #[serde(rename = "BizAppHdr_PACS_002_001_10")]
136    BusinessApplicationHeaderV02Pacs002_001_10(
137        Box<bah_pacs_002_001_10::BusinessApplicationHeaderV02>,
138    ),
139
140    /// pacs.003.001.08 - FI to FI Customer Direct Debit Header
141    #[serde(rename = "BizAppHdr_PACS_003_001_08")]
142    BusinessApplicationHeaderV02Pacs003_001_08(
143        Box<bah_pacs_003_001_08::BusinessApplicationHeaderV02>,
144    ),
145
146    /// pacs.004.001.09 - Payment Return Header
147    #[serde(rename = "BizAppHdr_PACS_004_001_09")]
148    BusinessApplicationHeaderV02Pacs004_001_09(
149        Box<bah_pacs_004_001_09::BusinessApplicationHeaderV02>,
150    ),
151
152    /// pacs.008.001.08 - FI to FI Customer Credit Transfer Header
153    #[serde(rename = "BizAppHdr_PACS_008_001_08")]
154    BusinessApplicationHeaderV02Pacs008_001_08(
155        Box<bah_pacs_008_001_08::BusinessApplicationHeaderV02>,
156    ),
157
158    /// pacs.008.001.08.stp - FI to FI Customer Credit Transfer Header (STP)
159    #[serde(rename = "BizAppHdr_PACS_008_001_08_STP")]
160    BusinessApplicationHeaderV02Pacs008_001_08stp(
161        Box<bah_pacs_008_001_08_stp::BusinessApplicationHeaderV02>,
162    ),
163
164    /// pacs.009.001.08 - Financial Institution Credit Transfer Header
165    #[serde(rename = "BizAppHdr_PACS_009_001_08")]
166    BusinessApplicationHeaderV02Pacs009_001_08(
167        Box<bah_pacs_009_001_08::BusinessApplicationHeaderV02>,
168    ),
169
170    /// pacs.009.001.08.adv - Financial Institution Credit Transfer Header (Advice)
171    #[serde(rename = "BizAppHdr_PACS_009_001_08_ADV")]
172    BusinessApplicationHeaderV02Pacs009_001_08adv(
173        Box<bah_pacs_009_001_08_adv::BusinessApplicationHeaderV02>,
174    ),
175
176    /// pacs.009.001.08.cov - Financial Institution Credit Transfer Header (Cover)
177    #[serde(rename = "BizAppHdr_PACS_009_001_08_COV")]
178    BusinessApplicationHeaderV02Pacs009_001_08cov(
179        Box<bah_pacs_009_001_08_cov::BusinessApplicationHeaderV02>,
180    ),
181
182    // Payment Initiation Messages Headers (pain)
183    /// pain.001.001.09 - Customer Credit Transfer Initiation Header
184    #[serde(rename = "BizAppHdr_PAIN_001_001_09")]
185    BusinessApplicationHeaderV02Pain001_001_09(
186        Box<bah_pain_001_001_09::BusinessApplicationHeaderV02>,
187    ),
188
189    /// pain.002.001.10 - Customer Payment Status Report Header
190    #[serde(rename = "BizAppHdr_PAIN_002_001_10")]
191    BusinessApplicationHeaderV02Pain002_001_10(
192        Box<bah_pain_002_001_10::BusinessApplicationHeaderV02>,
193    ),
194
195    /// pain.008.001.08 - Customer Direct Debit Initiation Header
196    #[serde(rename = "BizAppHdr_PAIN_008_001_08")]
197    BusinessApplicationHeaderV02Pain008_001_08(
198        Box<bah_pain_008_001_08::BusinessApplicationHeaderV02>,
199    ),
200
201    /// Unknown or unsupported application header type
202    #[default]
203    UNKNOWN,
204}
205
206impl AppHeader {
207    /// Validates the application header according to ISO20022 and CBPR+ specifications
208    pub fn validate(&self) -> Result<(), ValidationError> {
209        match self {
210            // Administrative Messages Headers
211            AppHeader::BusinessApplicationHeaderV02Admi024_001_01(value) => value.validate(),
212
213            // Cash Management Messages Headers
214            AppHeader::BusinessApplicationHeaderV02Camt025_001_08(value) => value.validate(),
215            AppHeader::BusinessApplicationHeaderV02Camt029_001(value) => value.validate(),
216            AppHeader::BusinessApplicationHeaderV02Camt052_001_08(value) => value.validate(),
217            AppHeader::BusinessApplicationHeaderV02Camt053_001_08(value) => value.validate(),
218            AppHeader::BusinessApplicationHeaderV02Camt054_001(value) => value.validate(),
219            AppHeader::BusinessApplicationHeaderV02Camt055_001_08(value) => value.validate(),
220            AppHeader::BusinessApplicationHeaderV02Camt056_001_08(value) => value.validate(),
221            AppHeader::BusinessApplicationHeaderV02Camt057_001_06(value) => value.validate(),
222            AppHeader::BusinessApplicationHeaderV02Camt058_001_08(value) => value.validate(),
223            AppHeader::BusinessApplicationHeaderV02Camt060_001_05(value) => value.validate(),
224            AppHeader::BusinessApplicationHeaderV02Camt105_001_02(value) => value.validate(),
225            AppHeader::BusinessApplicationHeaderV02Camt105_001_02mc(value) => value.validate(),
226            AppHeader::BusinessApplicationHeaderV02Camt106_001_02(value) => value.validate(),
227            AppHeader::BusinessApplicationHeaderV02Camt106_001_02mc(value) => value.validate(),
228            AppHeader::BusinessApplicationHeaderV02Camt107_001_01(value) => value.validate(),
229            AppHeader::BusinessApplicationHeaderV02Camt108_001_01(value) => value.validate(),
230            AppHeader::BusinessApplicationHeaderV02Camt109_001_01(value) => value.validate(),
231
232            // Payments Clearing and Settlement Messages Headers
233            AppHeader::BusinessApplicationHeaderV02Pacs002_001_10(value) => value.validate(),
234            AppHeader::BusinessApplicationHeaderV02Pacs003_001_08(value) => value.validate(),
235            AppHeader::BusinessApplicationHeaderV02Pacs004_001_09(value) => value.validate(),
236            AppHeader::BusinessApplicationHeaderV02Pacs008_001_08(value) => value.validate(),
237            AppHeader::BusinessApplicationHeaderV02Pacs008_001_08stp(value) => value.validate(),
238            AppHeader::BusinessApplicationHeaderV02Pacs009_001_08(value) => value.validate(),
239            AppHeader::BusinessApplicationHeaderV02Pacs009_001_08adv(value) => value.validate(),
240            AppHeader::BusinessApplicationHeaderV02Pacs009_001_08cov(value) => value.validate(),
241
242            // Payment Initiation Messages Headers
243            AppHeader::BusinessApplicationHeaderV02Pain001_001_09(value) => value.validate(),
244            AppHeader::BusinessApplicationHeaderV02Pain002_001_10(value) => value.validate(),
245            AppHeader::BusinessApplicationHeaderV02Pain008_001_08(value) => value.validate(),
246
247            AppHeader::UNKNOWN => Err(ValidationError::new(
248                9999,
249                "Unknown application header type".to_string(),
250            )),
251        }
252    }
253
254    /// Returns the message type identifier for the application header
255    pub fn message_type(&self) -> &'static str {
256        match self {
257            // Administrative Messages Headers
258            AppHeader::BusinessApplicationHeaderV02Admi024_001_01(_) => "admi.024.001.01",
259
260            // Cash Management Messages Headers
261            AppHeader::BusinessApplicationHeaderV02Camt025_001_08(_) => "camt.025.001.08",
262            AppHeader::BusinessApplicationHeaderV02Camt029_001(_) => "camt.029.001",
263            AppHeader::BusinessApplicationHeaderV02Camt052_001_08(_) => "camt.052.001.08",
264            AppHeader::BusinessApplicationHeaderV02Camt053_001_08(_) => "camt.053.001.08",
265            AppHeader::BusinessApplicationHeaderV02Camt054_001(_) => "camt.054.001",
266            AppHeader::BusinessApplicationHeaderV02Camt055_001_08(_) => "camt.055.001.08",
267            AppHeader::BusinessApplicationHeaderV02Camt056_001_08(_) => "camt.056.001.08",
268            AppHeader::BusinessApplicationHeaderV02Camt057_001_06(_) => "camt.057.001.06",
269            AppHeader::BusinessApplicationHeaderV02Camt058_001_08(_) => "camt.058.001.08",
270            AppHeader::BusinessApplicationHeaderV02Camt060_001_05(_) => "camt.060.001.05",
271            AppHeader::BusinessApplicationHeaderV02Camt105_001_02(_) => "camt.105.001.02",
272            AppHeader::BusinessApplicationHeaderV02Camt105_001_02mc(_) => "camt.105.001.02.mc",
273            AppHeader::BusinessApplicationHeaderV02Camt106_001_02(_) => "camt.106.001.02",
274            AppHeader::BusinessApplicationHeaderV02Camt106_001_02mc(_) => "camt.106.001.02.mc",
275            AppHeader::BusinessApplicationHeaderV02Camt107_001_01(_) => "camt.107.001.01",
276            AppHeader::BusinessApplicationHeaderV02Camt108_001_01(_) => "camt.108.001.01",
277            AppHeader::BusinessApplicationHeaderV02Camt109_001_01(_) => "camt.109.001.01",
278
279            // Payments Clearing and Settlement Messages Headers
280            AppHeader::BusinessApplicationHeaderV02Pacs002_001_10(_) => "pacs.002.001.10",
281            AppHeader::BusinessApplicationHeaderV02Pacs003_001_08(_) => "pacs.003.001.08",
282            AppHeader::BusinessApplicationHeaderV02Pacs004_001_09(_) => "pacs.004.001.09",
283            AppHeader::BusinessApplicationHeaderV02Pacs008_001_08(_) => "pacs.008.001.08",
284            AppHeader::BusinessApplicationHeaderV02Pacs008_001_08stp(_) => "pacs.008.001.08.stp",
285            AppHeader::BusinessApplicationHeaderV02Pacs009_001_08(_) => "pacs.009.001.08",
286            AppHeader::BusinessApplicationHeaderV02Pacs009_001_08adv(_) => "pacs.009.001.08.adv",
287            AppHeader::BusinessApplicationHeaderV02Pacs009_001_08cov(_) => "pacs.009.001.08.cov",
288
289            // Payment Initiation Messages Headers
290            AppHeader::BusinessApplicationHeaderV02Pain001_001_09(_) => "pain.001.001.09",
291            AppHeader::BusinessApplicationHeaderV02Pain002_001_10(_) => "pain.002.001.10",
292            AppHeader::BusinessApplicationHeaderV02Pain008_001_08(_) => "pain.008.001.08",
293
294            AppHeader::UNKNOWN => "unknown",
295        }
296    }
297
298    /// Returns whether the application header is CBPR+ compliant
299    pub fn is_cbpr_plus_compliant(&self) -> bool {
300        match self {
301            AppHeader::UNKNOWN => false,
302            _ => true, // All implemented application header types are CBPR+ compliant
303        }
304    }
305}