swift_mt_message/fields/
field50.rs

1use serde::{Deserialize, Serialize};
2use swift_mt_message_macros::SwiftField;
3
4/// Field 50F: Ordering Customer (Option F)
5///
6/// Ordering customer with party identifier and name/address.
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftField)]
8pub struct Field50F {
9    /// Party identifier
10    #[component("34x", validate = ["party_identifier_format"])]
11    pub party_identifier: String,
12    /// Name and address lines
13    #[component("4*35x", validate = ["line_count", "line_length", "structured_address"])]
14    pub name_and_address: Vec<String>,
15}
16
17/// Field 50A: Ordering Customer (Option A)
18///
19/// Ordering customer with BIC-based identification.
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftField)]
21pub struct Field50A {
22    /// Account line indicator (optional)
23    #[component("[1!a]", optional)]
24    pub account_line_indicator: Option<String>,
25    /// Account number (optional)
26    #[component("[34x]", optional)]
27    pub account_number: Option<String>,
28    /// BIC code
29    #[component("4!a2!a2!c[3!c]", validate = ["bic"])]
30    pub bic: String,
31}
32
33/// Field 50: Ordering Customer
34///
35/// Multi-option field with different format specifications per option.
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37pub enum Field50 {
38    /// Option A: BIC-based identification
39    A(Field50A),
40    /// Option F: Party identifier with name/address
41    F(Field50F),
42    /// Option K: Name and address only
43    K(Field50K),
44}
45
46/// Type alias for Field 50K: Ordering Customer (Option K)
47pub type Field50K = crate::fields::common::GenericMultiLine4x35;
48
49impl crate::SwiftField for Field50 {
50    fn parse(value: &str) -> crate::Result<Self> {
51        let content = value.trim();
52
53        // Determine option based on format pattern
54        if content.contains('\n') || content.lines().count() > 1 {
55            // Option K or F - multiple lines
56            if content.starts_with('/') {
57                // Option F - has party identifier
58                let field_50f = Field50F::parse(value)?;
59                Ok(Field50::F(field_50f))
60            } else {
61                // Option K - name and address only
62                let field_50k = Field50K::parse(value)?;
63                Ok(Field50::K(field_50k))
64            }
65        } else {
66            // Option A - BIC format
67            let field_50a = Field50A::parse(value)?;
68            Ok(Field50::A(field_50a))
69        }
70    }
71
72    fn to_swift_string(&self) -> String {
73        match self {
74            Field50::A(field_50a) => field_50a.to_swift_string(),
75            Field50::F(field_50f) => field_50f.to_swift_string(),
76            Field50::K(field_50k) => field_50k.to_swift_string(),
77        }
78    }
79
80    fn validate(&self) -> crate::ValidationResult {
81        match self {
82            Field50::A(field_50a) => field_50a.validate(),
83            Field50::F(field_50f) => field_50f.validate(),
84            Field50::K(field_50k) => field_50k.validate(),
85        }
86    }
87
88    fn format_spec() -> &'static str {
89        "multi_option"
90    }
91}