swift_mt_message/fields/
field50.rs1use serde::{Deserialize, Serialize};
2use swift_mt_message_macros::SwiftField;
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftField)]
8pub struct Field50F {
9 #[component("34x", validate = ["party_identifier_format"])]
11 pub party_identifier: String,
12 #[component("4*35x", validate = ["line_count", "line_length", "structured_address"])]
14 pub name_and_address: Vec<String>,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftField)]
21pub struct Field50A {
22 #[component("[1!a]", optional)]
24 pub account_line_indicator: Option<String>,
25 #[component("[34x]", optional)]
27 pub account_number: Option<String>,
28 #[component("4!a2!a2!c[3!c]", validate = ["bic"])]
30 pub bic: String,
31}
32
33#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37pub enum Field50 {
38 A(Field50A),
40 F(Field50F),
42 K(Field50K),
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftField)]
48pub struct Field50K {
49 #[component("[34x]", optional)]
51 pub account_number: Option<String>,
52
53 #[component("4*35x", validate = ["line_count", "line_length", "structured_address"])]
55 pub name_and_address: Vec<String>,
56}
57
58impl crate::SwiftField for Field50 {
59 fn parse(value: &str) -> crate::Result<Self> {
60 let content = value.trim();
61
62 if content.contains('\n') || content.lines().count() > 1 {
64 if content.starts_with('/') {
66 let field_50f = Field50F::parse(value)?;
68 Ok(Field50::F(field_50f))
69 } else {
70 let field_50k = Field50K::parse(value)?;
72 Ok(Field50::K(field_50k))
73 }
74 } else {
75 let field_50a = Field50A::parse(value)?;
77 Ok(Field50::A(field_50a))
78 }
79 }
80
81 fn to_swift_string(&self) -> String {
82 match self {
83 Field50::A(field_50a) => field_50a.to_swift_string(),
84 Field50::F(field_50f) => field_50f.to_swift_string(),
85 Field50::K(field_50k) => field_50k.to_swift_string(),
86 }
87 }
88
89 fn validate(&self) -> crate::ValidationResult {
90 match self {
91 Field50::A(field_50a) => field_50a.validate(),
92 Field50::F(field_50f) => field_50f.validate(),
93 Field50::K(field_50k) => field_50k.validate(),
94 }
95 }
96
97 fn format_spec() -> &'static str {
98 "multi_option"
99 }
100
101 fn sample() -> Self {
102 use rand::Rng;
103 let mut rng = rand::thread_rng();
104
105 match rng.gen_range(0..3) {
107 0 => Field50::A(Field50A::sample()),
108 1 => Field50::F(Field50F::sample()),
109 _ => Field50::K(Field50K::sample()),
110 }
111 }
112
113 fn sample_with_config(_config: &crate::sample::FieldConfig) -> Self {
114 Self::sample()
117 }
118}