use super::swift_utils::{parse_alphanumeric, parse_exact_length};
use crate::traits::SwiftField;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct Field26T {
pub type_code: String,
}
impl SwiftField for Field26T {
fn parse(input: &str) -> crate::Result<Self>
where
Self: Sized,
{
let type_code = parse_exact_length(input, 3, "Field 26T type code")?;
parse_alphanumeric(&type_code, "Field 26T type code")?;
Ok(Field26T { type_code })
}
fn to_swift_string(&self) -> String {
format!(":26T:{}", self.type_code)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_field26t_valid() {
let field = Field26T::parse("PAY").unwrap();
assert_eq!(field.type_code, "PAY");
assert_eq!(field.to_swift_string(), ":26T:PAY");
let field = Field26T::parse("FXD").unwrap();
assert_eq!(field.type_code, "FXD");
let field = Field26T::parse("123").unwrap();
assert_eq!(field.type_code, "123");
let field = Field26T::parse("A1B").unwrap();
assert_eq!(field.type_code, "A1B");
}
#[test]
fn test_field26t_invalid() {
assert!(Field26T::parse("PA").is_err());
assert!(Field26T::parse("PAYM").is_err());
assert!(Field26T::parse("PA-").is_err());
assert!(Field26T::parse("P@Y").is_err());
}
}