use std::collections::HashMap;
use crate::Exceptions;
use crate::common::Result;
use once_cell::sync::Lazy;
static TWO_DIGIT_DATA_LENGTH: Lazy<HashMap<String, DataLength>> = Lazy::new(|| {
let mut hm = HashMap::new();
hm.insert("00".into(), DataLength::fixed(18));
hm.insert("01".into(), DataLength::fixed(14));
hm.insert("02".into(), DataLength::fixed(14));
hm.insert("10".into(), DataLength::variable(20));
hm.insert("11".into(), DataLength::fixed(6));
hm.insert("12".into(), DataLength::fixed(6));
hm.insert("13".into(), DataLength::fixed(6));
hm.insert("15".into(), DataLength::fixed(6));
hm.insert("16".into(), DataLength::fixed(6));
hm.insert("17".into(), DataLength::fixed(6));
hm.insert("20".into(), DataLength::fixed(2));
hm.insert("21".into(), DataLength::variable(20));
hm.insert("22".into(), DataLength::variable(29)); hm.insert("30".into(), DataLength::variable(8));
hm.insert("37".into(), DataLength::variable(8));
for i in 90..=99 {
hm.insert(i.to_string(), DataLength::variable(30));
}
hm
});
static THREE_DIGIT_DATA_LENGTH: Lazy<HashMap<String, DataLength>> = Lazy::new(|| {
let mut hm = HashMap::new();
hm.insert("235".into(), DataLength::variable(28));
hm.insert("240".into(), DataLength::variable(30));
hm.insert("241".into(), DataLength::variable(30));
hm.insert("242".into(), DataLength::variable(6));
hm.insert("243".into(), DataLength::variable(20));
hm.insert("250".into(), DataLength::variable(30));
hm.insert("251".into(), DataLength::variable(30));
hm.insert("253".into(), DataLength::variable(30));
hm.insert("254".into(), DataLength::variable(20));
hm.insert("255".into(), DataLength::variable(25));
hm.insert("400".into(), DataLength::variable(30));
hm.insert("401".into(), DataLength::variable(30));
hm.insert("402".into(), DataLength::fixed(17));
hm.insert("403".into(), DataLength::variable(30));
hm.insert("410".into(), DataLength::fixed(13));
hm.insert("411".into(), DataLength::fixed(13));
hm.insert("412".into(), DataLength::fixed(13));
hm.insert("413".into(), DataLength::fixed(13));
hm.insert("414".into(), DataLength::fixed(13));
hm.insert("415".into(), DataLength::fixed(13));
hm.insert("416".into(), DataLength::fixed(13));
hm.insert("417".into(), DataLength::fixed(13));
hm.insert("420".into(), DataLength::variable(20));
hm.insert("421".into(), DataLength::variable(15)); hm.insert("422".into(), DataLength::fixed(3));
hm.insert("423".into(), DataLength::variable(15));
hm.insert("424".into(), DataLength::fixed(3));
hm.insert("425".into(), DataLength::variable(15));
hm.insert("426".into(), DataLength::fixed(3));
hm.insert("427".into(), DataLength::variable(3));
hm.insert("710".into(), DataLength::variable(20));
hm.insert("711".into(), DataLength::variable(20));
hm.insert("712".into(), DataLength::variable(20));
hm.insert("713".into(), DataLength::variable(20));
hm.insert("714".into(), DataLength::variable(20));
hm.insert("715".into(), DataLength::variable(20));
hm
});
static THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH: Lazy<HashMap<String, DataLength>> = Lazy::new(|| {
let mut hm = HashMap::new();
for i in 310..=316 {
hm.insert(i.to_string(), DataLength::fixed(6));
}
for i in 320..=337 {
hm.insert(i.to_string(), DataLength::fixed(6));
}
for i in 340..=357 {
hm.insert(i.to_string(), DataLength::fixed(6));
}
for i in 360..=369 {
hm.insert(i.to_string(), DataLength::fixed(6));
}
hm.insert("390".into(), DataLength::variable(15));
hm.insert("391".into(), DataLength::variable(18));
hm.insert("392".into(), DataLength::variable(15));
hm.insert("393".into(), DataLength::variable(18));
hm.insert("394".into(), DataLength::fixed(4));
hm.insert("395".into(), DataLength::fixed(6));
hm.insert("703".into(), DataLength::variable(30));
hm.insert("723".into(), DataLength::variable(30));
hm
});
static FOUR_DIGIT_DATA_LENGTH: Lazy<HashMap<String, DataLength>> = Lazy::new(|| {
let mut hm = HashMap::new();
hm.insert("4300".into(), DataLength::variable(35));
hm.insert("4301".into(), DataLength::variable(35));
hm.insert("4302".into(), DataLength::variable(70));
hm.insert("4303".into(), DataLength::variable(70));
hm.insert("4304".into(), DataLength::variable(70));
hm.insert("4305".into(), DataLength::variable(70));
hm.insert("4306".into(), DataLength::variable(70));
hm.insert("4307".into(), DataLength::fixed(2));
hm.insert("4308".into(), DataLength::variable(30));
hm.insert("4309".into(), DataLength::fixed(20));
hm.insert("4310".into(), DataLength::variable(35));
hm.insert("4311".into(), DataLength::variable(35));
hm.insert("4312".into(), DataLength::variable(70));
hm.insert("4313".into(), DataLength::variable(70));
hm.insert("4314".into(), DataLength::variable(70));
hm.insert("4315".into(), DataLength::variable(70));
hm.insert("4316".into(), DataLength::variable(70));
hm.insert("4317".into(), DataLength::fixed(2));
hm.insert("4318".into(), DataLength::variable(20));
hm.insert("4319".into(), DataLength::variable(30));
hm.insert("4320".into(), DataLength::variable(35));
hm.insert("4321".into(), DataLength::fixed(1));
hm.insert("4322".into(), DataLength::fixed(1));
hm.insert("4323".into(), DataLength::fixed(1));
hm.insert("4324".into(), DataLength::fixed(10));
hm.insert("4325".into(), DataLength::fixed(10));
hm.insert("4326".into(), DataLength::fixed(6));
hm.insert("7001".into(), DataLength::fixed(13));
hm.insert("7002".into(), DataLength::variable(30));
hm.insert("7003".into(), DataLength::fixed(10));
hm.insert("7004".into(), DataLength::variable(4));
hm.insert("7005".into(), DataLength::variable(12));
hm.insert("7006".into(), DataLength::fixed(6));
hm.insert("7007".into(), DataLength::variable(12));
hm.insert("7008".into(), DataLength::variable(3));
hm.insert("7009".into(), DataLength::variable(10));
hm.insert("7010".into(), DataLength::variable(2));
hm.insert("7011".into(), DataLength::variable(10));
hm.insert("7020".into(), DataLength::variable(20));
hm.insert("7021".into(), DataLength::variable(20));
hm.insert("7022".into(), DataLength::variable(20));
hm.insert("7023".into(), DataLength::variable(30));
hm.insert("7040".into(), DataLength::fixed(4));
hm.insert("7240".into(), DataLength::variable(20));
hm.insert("8001".into(), DataLength::fixed(14));
hm.insert("8002".into(), DataLength::variable(20));
hm.insert("8003".into(), DataLength::variable(30));
hm.insert("8004".into(), DataLength::variable(30));
hm.insert("8005".into(), DataLength::fixed(6));
hm.insert("8006".into(), DataLength::fixed(18));
hm.insert("8007".into(), DataLength::variable(34));
hm.insert("8008".into(), DataLength::variable(12));
hm.insert("8009".into(), DataLength::variable(50));
hm.insert("8010".into(), DataLength::variable(30));
hm.insert("8011".into(), DataLength::variable(12));
hm.insert("8012".into(), DataLength::variable(20));
hm.insert("8013".into(), DataLength::variable(25));
hm.insert("8017".into(), DataLength::fixed(18));
hm.insert("8018".into(), DataLength::fixed(18));
hm.insert("8019".into(), DataLength::variable(10));
hm.insert("8020".into(), DataLength::variable(25));
hm.insert("8026".into(), DataLength::fixed(18));
hm.insert("8100".into(), DataLength::fixed(6)); hm.insert("8101".into(), DataLength::fixed(10)); hm.insert("8102".into(), DataLength::fixed(2)); hm.insert("8110".into(), DataLength::variable(70));
hm.insert("8111".into(), DataLength::fixed(4));
hm.insert("8112".into(), DataLength::variable(70));
hm.insert("8200".into(), DataLength::variable(70));
hm
});
pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String> {
if rawInformation.is_empty() {
return Ok(String::default());
}
if rawInformation.len() < 2 {
return Err(Exceptions::NOT_FOUND);
}
let twoDigitDataLength = TWO_DIGIT_DATA_LENGTH.get(&rawInformation[..2]);
if let Some(tddl) = twoDigitDataLength {
if tddl.variable {
return processVariableAI(2, tddl.length, rawInformation);
}
return processFixedAI(2, tddl.length, rawInformation);
}
if rawInformation.len() < 3 {
return Err(Exceptions::NOT_FOUND);
}
let firstThreeDigits = &rawInformation[..3];
let threeDigitDataLength = THREE_DIGIT_DATA_LENGTH.get(firstThreeDigits);
if let Some(tddl) = threeDigitDataLength {
if tddl.variable {
return processVariableAI(3, tddl.length, rawInformation);
}
return processFixedAI(3, tddl.length, rawInformation);
}
if rawInformation.len() < 4 {
return Err(Exceptions::NOT_FOUND);
}
let threeDigitPlusDigitDataLength = THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH.get(firstThreeDigits);
if let Some(tdpddl) = threeDigitPlusDigitDataLength {
if tdpddl.variable {
return processVariableAI(4, tdpddl.length, rawInformation);
}
return processFixedAI(4, tdpddl.length, rawInformation);
}
let firstFourDigitLength = FOUR_DIGIT_DATA_LENGTH.get(&rawInformation[..4]);
if let Some(ffdl) = firstFourDigitLength {
if ffdl.variable {
return processVariableAI(4, ffdl.length, rawInformation);
}
return processFixedAI(4, ffdl.length, rawInformation);
}
Err(Exceptions::NOT_FOUND)
}
fn processFixedAI(aiSize: usize, fieldSize: usize, rawInformation: &str) -> Result<String> {
if rawInformation.len() < aiSize {
return Err(Exceptions::NOT_FOUND);
}
let ai = &rawInformation[..aiSize];
if rawInformation.len() < aiSize + fieldSize {
return Err(Exceptions::NOT_FOUND);
}
let field = &rawInformation[aiSize..aiSize + fieldSize];
let remaining = &rawInformation[aiSize + fieldSize..];
let result = format!("({ai}){field}");
let parsedAI = parseFieldsInGeneralPurpose(remaining)?;
Ok(if parsedAI.is_empty() {
result
} else {
format!("{result}{parsedAI}")
})
}
fn processVariableAI(
aiSize: usize,
variableFieldSize: usize,
rawInformation: &str,
) -> Result<String> {
let ai = &rawInformation[..aiSize];
let maxSize = rawInformation.len().min(aiSize + variableFieldSize);
let field = &rawInformation[aiSize..maxSize];
let remaining = &rawInformation[maxSize..];
let result = format!("({ai}){field}"); let parsedAI = parseFieldsInGeneralPurpose(remaining)?;
Ok(if parsedAI.is_empty() {
result
} else {
format!("{result}{parsedAI}")
})
}
struct DataLength {
pub variable: bool,
pub length: usize,
}
impl DataLength {
pub const fn fixed(length: usize) -> Self {
Self {
variable: false,
length,
}
}
pub const fn variable(length: usize) -> Self {
Self {
variable: true,
length,
}
}
}
#[cfg(test)]
mod FieldParserTest {
fn checkFields(expected: &str) {
let field = expected.replace(['(', ')'], "");
let actual = super::parseFieldsInGeneralPurpose(&field).expect("parse");
assert_eq!(expected, actual);
}
#[test]
fn testParseField() {
checkFields("(15)991231(3103)001750(10)12A");
}
#[test]
fn testParseField2() {
checkFields("(15)991231(15)991231(3103)001750(10)12A");
}
#[test]
fn testParseField2023Ais() {
checkFields("(16)261231(255)53123456789012345");
checkFields("(253)1234567890128ABCDEFGHIJKLMNOPQ");
checkFields("(3370)000123(4307)DE(7004)1234(8112)ABC");
}
}