swift_iso15022/model/field/
field_72.rs1use indexmap::IndexSet;
5use pest::Parser;
6
7use crate::errors::{ParseError, RequiredTagNotFoundError, UnexpectedFieldError};
8use crate::model::field::{FieldTrait, RawField};
9use crate::rules::{Rule, RulesParser};
10
11#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
13pub struct Field72(pub(crate) IndexSet<String>);
14
15impl FieldTrait for Field72 {
16 const TAG: &'static str = "72";
17
18 fn to_swift_string_without_tag(&self) -> String {
19 let mut s = String::with_capacity(6);
20
21 for v in self.0.iter() {
22 s.push_str(&format!("{v}\r\n"))
23 }
24
25 s.trim_end().into()
26 }
27
28 fn parse_raw_field_tag(field: &RawField) -> Result<Self, ParseError> {
29 if field.tag != Self::TAG {
30 return Err(RequiredTagNotFoundError::new(Self::TAG).into());
31 }
32
33 let mut narrative: IndexSet<String> = IndexSet::default();
34
35 let mut parsed_field = RulesParser::parse(Rule::field_72, &field.value)?;
36
37 let pairs = parsed_field.next().unwrap().into_inner();
38
39 for pair in pairs {
40 match pair.as_rule() {
41 Rule::narrative => {
42 narrative.insert(pair.as_str().into());
43 }
44 Rule::EOI => (),
45 _ => {
46 return Err(UnexpectedFieldError::new(
47 field.tag.as_str(),
48 &format!("{:?}", pair.as_rule()),
49 )
50 .into());
51 }
52 }
53 }
54
55 Ok(Field72(narrative))
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use crate::field::FieldTrait;
62
63 use super::*;
64
65 const SWIFT_STR: &'static str = ":72:/INS/ABNANL2A";
66
67 #[test]
68 fn test_parse_str_swift() {
69 let tag_field = Field72::parse_str_swift(SWIFT_STR).unwrap();
70
71 assert_eq!(
72 tag_field.0,
73 IndexSet::from([
74 "/INS/ABNANL2A".to_string()
75 ])
76 );
77 }
78
79 #[test]
80 fn test_to_str_swift() {
81 let field_tag = Field72(IndexSet::from([
82 "/INS/ABNANL2A".to_string()
83 ]));
84
85 assert_eq!(field_tag.to_swift_string(), SWIFT_STR);
86 }
87
88 #[test]
89 fn test_serialization() {
90 let field_tag = Field72(IndexSet::from([
91 "/INS/ABNANL2A".to_string()
92 ]));
93
94 let serialized = serde_json::to_string(&field_tag).unwrap();
95
96 let expected_json = r#"["/INS/ABNANL2A"]"#;
97
98 assert_eq!(serialized, expected_json);
99 }
100
101 #[test]
102 fn test_deserialization() {
103 let json = r#"["/INS/ABNANL2A"]"#;
104
105 let deserialized: Field72 = serde_json::from_str(json).unwrap();
106 assert_eq!(
107 deserialized.0,
108 IndexSet::from([
109 "/INS/ABNANL2A".to_string()
110 ])
111 )
112 }
113}