swift_mt_message/fields/
field26.rs

1use super::swift_utils::{parse_alphanumeric, parse_exact_length};
2use crate::traits::SwiftField;
3use serde::{Deserialize, Serialize};
4
5/// **Field 26T: Transaction Type Code**
6///
7/// Specifies transaction type using standardized 3-character codes for categorization and processing.
8///
9/// **Format:** `3!c` (exactly 3 alphanumeric chars)
10/// **Common Codes:** PAY, SAL, FXD, DIV, TRD, INT
11///
12/// **Example:**
13/// ```text
14/// :26T:PAY
15/// ```
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17pub struct Field26T {
18    /// Transaction type code (3 chars, alphanumeric)
19    pub type_code: String,
20}
21
22impl SwiftField for Field26T {
23    fn parse(input: &str) -> crate::Result<Self>
24    where
25        Self: Sized,
26    {
27        // Must be exactly 3 characters
28        let type_code = parse_exact_length(input, 3, "Field 26T type code")?;
29
30        // Must be alphanumeric
31        parse_alphanumeric(&type_code, "Field 26T type code")?;
32
33        Ok(Field26T { type_code })
34    }
35
36    fn to_swift_string(&self) -> String {
37        format!(":26T:{}", self.type_code)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_field26t_valid() {
47        let field = Field26T::parse("PAY").unwrap();
48        assert_eq!(field.type_code, "PAY");
49        assert_eq!(field.to_swift_string(), ":26T:PAY");
50
51        let field = Field26T::parse("FXD").unwrap();
52        assert_eq!(field.type_code, "FXD");
53
54        let field = Field26T::parse("123").unwrap();
55        assert_eq!(field.type_code, "123");
56
57        let field = Field26T::parse("A1B").unwrap();
58        assert_eq!(field.type_code, "A1B");
59    }
60
61    #[test]
62    fn test_field26t_invalid() {
63        // Too short
64        assert!(Field26T::parse("PA").is_err());
65
66        // Too long
67        assert!(Field26T::parse("PAYM").is_err());
68
69        // Non-alphanumeric characters
70        assert!(Field26T::parse("PA-").is_err());
71        assert!(Field26T::parse("P@Y").is_err());
72    }
73}