mx_message/
mx_envelope.rs

1// MX Message Envelope Structure for ISO 20022 compliant XML generation
2
3use serde::{Deserialize, Serialize};
4
5/// Complete MX message envelope containing Business Application Header and Document
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7#[serde(rename = "Envelope")]
8pub struct MxEnvelope<H, D> {
9    /// XML namespace declarations
10    #[serde(rename = "@xmlns", skip_serializing_if = "Option::is_none")]
11    pub xmlns: Option<String>,
12
13    #[serde(rename = "@xmlns:xsi", skip_serializing_if = "Option::is_none")]
14    pub xmlns_xsi: Option<String>,
15
16    /// Business Application Header
17    #[serde(rename = "AppHdr")]
18    pub app_hdr: H,
19
20    /// Document containing the actual message
21    #[serde(rename = "Document")]
22    pub document: MxDocument<D>,
23}
24
25/// Document wrapper for MX messages
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub struct MxDocument<D> {
28    /// Document namespace
29    #[serde(rename = "@xmlns", skip_serializing_if = "Option::is_none")]
30    pub xmlns: Option<String>,
31
32    /// The actual message content
33    #[serde(flatten)]
34    pub message: D,
35}
36
37impl<H, D> MxEnvelope<H, D> {
38    /// Create a new MX envelope with default namespaces
39    pub fn new(app_hdr: H, document: D, document_namespace: String) -> Self {
40        Self {
41            xmlns: Some("urn:iso:std:iso:20022:tech:xsd:head.001.001.02".to_string()),
42            xmlns_xsi: Some("http://www.w3.org/2001/XMLSchema-instance".to_string()),
43            app_hdr,
44            document: MxDocument {
45                xmlns: Some(document_namespace),
46                message: document,
47            },
48        }
49    }
50
51    /// Create envelope for pacs.008 message
52    pub fn new_pacs008(app_hdr: H, document: D) -> Self {
53        Self::new(
54            app_hdr,
55            document,
56            "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08".to_string(),
57        )
58    }
59
60    /// Create envelope for pacs.009 message
61    pub fn new_pacs009(app_hdr: H, document: D) -> Self {
62        Self::new(
63            app_hdr,
64            document,
65            "urn:iso:std:iso:20022:tech:xsd:pacs.009.001.08".to_string(),
66        )
67    }
68
69    /// Create envelope for pain.001 message
70    pub fn new_pain001(app_hdr: H, document: D) -> Self {
71        Self::new(
72            app_hdr,
73            document,
74            "urn:iso:std:iso:20022:tech:xsd:pain.001.001.09".to_string(),
75        )
76    }
77
78    /// Create envelope for camt.052 message
79    pub fn new_camt052(app_hdr: H, document: D) -> Self {
80        Self::new(
81            app_hdr,
82            document,
83            "urn:iso:std:iso:20022:tech:xsd:camt.052.001.08".to_string(),
84        )
85    }
86
87    /// Create envelope for camt.053 message
88    pub fn new_camt053(app_hdr: H, document: D) -> Self {
89        Self::new(
90            app_hdr,
91            document,
92            "urn:iso:std:iso:20022:tech:xsd:camt.053.001.08".to_string(),
93        )
94    }
95}