use crate::checksum::{
aggressive_defiller, char_value, defiller, digitize, fix_doc_code, fix_name_separator,
is_mrz_charset, letterize, normalize_line, repair_positions, variants, verify,
};
use crate::dates::expand_date_with_pivot;
use crate::{Checks, Format, MrzData, MrzError, ParseOptions};
struct Overflow {
full: String,
check_ok: bool,
consumed: usize,
}
fn read_overflow(number_field: &str, check: char, optional: &str) -> Option<Overflow> {
if check != '<' || !number_field.ends_with('<') {
return None;
}
let head = number_field[0..8].trim_end_matches('<');
if head.is_empty() {
return None;
}
let end = optional.find('<')?;
if end < 2 {
return None; }
let (remainder, check_digit) = optional[..end].split_at(end - 1);
let check_digit = check_digit.chars().next()?;
if !check_digit.is_ascii_digit() {
return None;
}
let full = format!("{head}{remainder}");
Some(Overflow {
check_ok: verify(&full, check_digit),
full,
consumed: end + 1,
})
}
fn optional_tail<'a>(optional: &'a str, overflow: &Option<Overflow>) -> &'a str {
match overflow {
Some(o) => optional[o.consumed..].trim_end_matches('<'),
None => optional.trim_end_matches('<'),
}
}
fn opt_string(s: &str) -> Option<String> {
(!s.is_empty()).then(|| s.to_string())
}
fn clean_name(field: &str) -> (String, String) {
let trimmed = field.trim_end_matches('<');
let (surname, given) = match trimmed.split_once("<<") {
Some((s, g)) => (s, g),
None => (trimmed, ""),
};
(
surname.replace('<', " ").trim().to_string(),
given.replace('<', " ").trim().to_string(),
)
}
fn clean_sex(c: char) -> String {
match c {
'M' => "M".into(),
'F' => "F".into(),
_ => "X".into(),
}
}
fn ensure_charset(line: &str) -> Result<(), MrzError> {
for c in line.chars() {
char_value(c)?;
}
Ok(())
}
pub fn parse_td3(line1: &str, line2: &str) -> Result<MrzData, MrzError> {
parse_td3_with(line1, line2, &ParseOptions::default())
}
pub fn parse_td3_with(line1: &str, line2: &str, opts: &ParseOptions) -> Result<MrzData, MrzError> {
for line in [line1, line2] {
if line.len() != 44 {
return Err(MrzError::BadLength {
expected: 44,
got: line.len(),
});
}
ensure_charset(line)?;
}
if !line1.starts_with('P') {
return Err(MrzError::BadDocumentCode(line1[0..2].to_string()));
}
let (surname, given_names) = clean_name(&line1[5..44]);
let document_number = line2[0..9].trim_end_matches('<').to_string();
let personal_raw = &line2[28..42];
let overflow = read_overflow(&line2[0..9], line2.as_bytes()[9] as char, personal_raw);
let personal = optional_tail(personal_raw, &overflow);
let checks = Checks {
document_number: match &overflow {
Some(o) => o.check_ok,
None => verify(&line2[0..9], line2.as_bytes()[9] as char),
},
date_of_birth: verify(&line2[13..19], line2.as_bytes()[19] as char),
date_of_expiry: verify(&line2[21..27], line2.as_bytes()[27] as char),
personal_number: verify(personal_raw, line2.as_bytes()[42] as char),
composite: verify(
&format!("{}{}{}", &line2[0..10], &line2[13..20], &line2[21..43]),
line2.as_bytes()[43] as char,
),
};
Ok(MrzData {
format: Format::Td3,
document_type: line1[0..2].trim_end_matches('<').to_string(),
issuing_country: line1[2..5].trim_end_matches('<').to_string(),
document_number,
document_number_full: overflow.map(|o| o.full),
surname,
given_names,
nationality: line2[10..13].trim_end_matches('<').to_string(),
date_of_birth: expand_date_with_pivot(&line2[13..19], true, opts.pivot_yy),
sex: clean_sex(line2.as_bytes()[20] as char),
date_of_expiry: expand_date_with_pivot(&line2[21..27], false, opts.pivot_yy),
personal_number: opt_string(personal),
mrz_lines: format!("{line1}\n{line2}"),
checks,
})
}
pub fn parse_td2(line1: &str, line2: &str) -> Result<MrzData, MrzError> {
parse_td2_with(line1, line2, &ParseOptions::default())
}
pub fn parse_td2_with(line1: &str, line2: &str, opts: &ParseOptions) -> Result<MrzData, MrzError> {
for line in [line1, line2] {
if line.len() != 36 {
return Err(MrzError::BadLength {
expected: 36,
got: line.len(),
});
}
ensure_charset(line)?;
}
let code = line1[0..2].trim_end_matches('<');
if !matches!(code.as_bytes().first(), Some(b'I' | b'A' | b'C')) {
return Err(MrzError::BadDocumentCode(code.to_string()));
}
let (surname, given_names) = clean_name(&line1[5..36]);
let document_number = line2[0..9].trim_end_matches('<').to_string();
let optional_raw = &line2[28..35];
let overflow = read_overflow(&line2[0..9], line2.as_bytes()[9] as char, optional_raw);
let optional = optional_tail(optional_raw, &overflow);
let checks = Checks {
document_number: match &overflow {
Some(o) => o.check_ok,
None => verify(&line2[0..9], line2.as_bytes()[9] as char),
},
date_of_birth: verify(&line2[13..19], line2.as_bytes()[19] as char),
date_of_expiry: verify(&line2[21..27], line2.as_bytes()[27] as char),
personal_number: true, composite: verify(
&format!("{}{}{}", &line2[0..10], &line2[13..20], &line2[21..35]),
line2.as_bytes()[35] as char,
),
};
Ok(MrzData {
format: Format::Td2,
document_type: code.to_string(),
issuing_country: line1[2..5].trim_end_matches('<').to_string(),
document_number,
document_number_full: overflow.map(|o| o.full),
surname,
given_names,
nationality: line2[10..13].trim_end_matches('<').to_string(),
date_of_birth: expand_date_with_pivot(&line2[13..19], true, opts.pivot_yy),
sex: clean_sex(line2.as_bytes()[20] as char),
date_of_expiry: expand_date_with_pivot(&line2[21..27], false, opts.pivot_yy),
personal_number: opt_string(optional),
mrz_lines: format!("{line1}\n{line2}"),
checks,
})
}
pub fn parse_td1(line1: &str, line2: &str, line3: &str) -> Result<MrzData, MrzError> {
parse_td1_with(line1, line2, line3, &ParseOptions::default())
}
pub fn parse_td1_with(
line1: &str,
line2: &str,
line3: &str,
opts: &ParseOptions,
) -> Result<MrzData, MrzError> {
for line in [line1, line2, line3] {
if line.len() != 30 {
return Err(MrzError::BadLength {
expected: 30,
got: line.len(),
});
}
ensure_charset(line)?;
}
let code = line1[0..2].trim_end_matches('<');
if !matches!(code.as_bytes().first(), Some(b'I' | b'A' | b'C')) {
return Err(MrzError::BadDocumentCode(code.to_string()));
}
let (surname, given_names) = clean_name(line3);
let optional1_raw = &line1[15..30];
let overflow = read_overflow(&line1[5..14], line1.as_bytes()[14] as char, optional1_raw);
let optional1 = optional_tail(optional1_raw, &overflow);
let optional2 = line2[18..29].trim_end_matches('<');
let personal = [optional1, optional2]
.iter()
.filter(|s| !s.is_empty())
.cloned()
.collect::<Vec<_>>()
.join(" ");
let checks = Checks {
document_number: match &overflow {
Some(o) => o.check_ok,
None => verify(&line1[5..14], line1.as_bytes()[14] as char),
},
date_of_birth: verify(&line2[0..6], line2.as_bytes()[6] as char),
date_of_expiry: verify(&line2[8..14], line2.as_bytes()[14] as char),
personal_number: true, composite: verify(
&format!(
"{}{}{}{}",
&line1[5..30],
&line2[0..7],
&line2[8..15],
&line2[18..29]
),
line2.as_bytes()[29] as char,
),
};
Ok(MrzData {
format: Format::Td1,
document_type: code.to_string(),
issuing_country: line1[2..5].trim_end_matches('<').to_string(),
document_number: line1[5..14].trim_end_matches('<').to_string(),
document_number_full: overflow.map(|o| o.full),
surname,
given_names,
nationality: line2[15..18].trim_end_matches('<').to_string(),
date_of_birth: expand_date_with_pivot(&line2[0..6], true, opts.pivot_yy),
sex: clean_sex(line2.as_bytes()[7] as char),
date_of_expiry: expand_date_with_pivot(&line2[8..14], false, opts.pivot_yy),
personal_number: opt_string(&personal),
mrz_lines: format!("{line1}\n{line2}\n{line3}"),
checks,
})
}
pub fn parse_mrv_a(line1: &str, line2: &str) -> Result<MrzData, MrzError> {
parse_mrv_a_with(line1, line2, &ParseOptions::default())
}
pub fn parse_mrv_a_with(
line1: &str,
line2: &str,
opts: &ParseOptions,
) -> Result<MrzData, MrzError> {
for line in [line1, line2] {
if line.len() != 44 {
return Err(MrzError::BadLength {
expected: 44,
got: line.len(),
});
}
ensure_charset(line)?;
}
if !line1.starts_with('V') {
return Err(MrzError::BadDocumentCode(line1[0..2].to_string()));
}
let (surname, given_names) = clean_name(&line1[5..44]);
let document_number = line2[0..9].trim_end_matches('<').to_string();
let optional = line2[28..44].trim_end_matches('<');
let checks = Checks {
document_number: verify(&line2[0..9], line2.as_bytes()[9] as char),
date_of_birth: verify(&line2[13..19], line2.as_bytes()[19] as char),
date_of_expiry: verify(&line2[21..27], line2.as_bytes()[27] as char),
personal_number: true,
composite: true,
};
Ok(MrzData {
format: Format::MrvA,
document_type: line1[0..2].trim_end_matches('<').to_string(),
issuing_country: line1[2..5].trim_end_matches('<').to_string(),
document_number,
document_number_full: None,
surname,
given_names,
nationality: line2[10..13].trim_end_matches('<').to_string(),
date_of_birth: expand_date_with_pivot(&line2[13..19], true, opts.pivot_yy),
sex: clean_sex(line2.as_bytes()[20] as char),
date_of_expiry: expand_date_with_pivot(&line2[21..27], false, opts.pivot_yy),
personal_number: opt_string(optional),
mrz_lines: format!("{line1}\n{line2}"),
checks,
})
}
pub fn parse_mrv_b(line1: &str, line2: &str) -> Result<MrzData, MrzError> {
parse_mrv_b_with(line1, line2, &ParseOptions::default())
}
pub fn parse_mrv_b_with(
line1: &str,
line2: &str,
opts: &ParseOptions,
) -> Result<MrzData, MrzError> {
for line in [line1, line2] {
if line.len() != 36 {
return Err(MrzError::BadLength {
expected: 36,
got: line.len(),
});
}
ensure_charset(line)?;
}
if !line1.starts_with('V') {
return Err(MrzError::BadDocumentCode(line1[0..2].to_string()));
}
let (surname, given_names) = clean_name(&line1[5..36]);
let document_number = line2[0..9].trim_end_matches('<').to_string();
let optional = line2[28..36].trim_end_matches('<');
let checks = Checks {
document_number: verify(&line2[0..9], line2.as_bytes()[9] as char),
date_of_birth: verify(&line2[13..19], line2.as_bytes()[19] as char),
date_of_expiry: verify(&line2[21..27], line2.as_bytes()[27] as char),
personal_number: true,
composite: true,
};
Ok(MrzData {
format: Format::MrvB,
document_type: line1[0..2].trim_end_matches('<').to_string(),
issuing_country: line1[2..5].trim_end_matches('<').to_string(),
document_number,
document_number_full: None,
surname,
given_names,
nationality: line2[10..13].trim_end_matches('<').to_string(),
date_of_birth: expand_date_with_pivot(&line2[13..19], true, opts.pivot_yy),
sex: clean_sex(line2.as_bytes()[20] as char),
date_of_expiry: expand_date_with_pivot(&line2[21..27], false, opts.pivot_yy),
personal_number: opt_string(optional),
mrz_lines: format!("{line1}\n{line2}"),
checks,
})
}
fn repair_td3_line1(l: &str) -> String {
let l = fix_doc_code(&repair_positions(l, &[(2..5, letterize)]));
format!("{}{}", &l[0..5], fix_name_separator(&defiller(&l[5..])))
}
fn repair_td3_line2(l: &str) -> String {
let l = repair_positions(
l,
&[
(9..10, digitize), (10..13, letterize), (13..20, digitize), (21..28, digitize), (42..44, digitize), ],
);
format!(
"{}{}{}",
&l[0..28],
aggressive_defiller(&defiller(&l[28..42])),
&l[42..44]
)
}
fn repair_td2_line1(l: &str) -> String {
let l = fix_doc_code(&repair_positions(l, &[(2..5, letterize)]));
format!("{}{}", &l[0..5], fix_name_separator(&defiller(&l[5..])))
}
fn repair_td2_line2(l: &str) -> String {
let l = repair_positions(
l,
&[
(9..10, digitize), (10..13, letterize), (13..20, digitize), (21..28, digitize), (35..36, digitize), ],
);
format!(
"{}{}{}",
&l[0..28],
aggressive_defiller(&defiller(&l[28..35])),
&l[35..36]
)
}
fn repair_td1_line1(l: &str) -> String {
let l = fix_doc_code(&repair_positions(
l,
&[(2..5, letterize), (14..15, digitize)],
));
format!("{}{}", &l[0..15], aggressive_defiller(&defiller(&l[15..])))
}
fn repair_td1_line2(l: &str) -> String {
let l = repair_positions(
l,
&[
(0..7, digitize), (8..15, digitize), (15..18, letterize), (29..30, digitize), ],
);
format!(
"{}{}{}",
&l[0..18],
aggressive_defiller(&defiller(&l[18..29])),
&l[29..30]
)
}
fn repair_td1_line3(l: &str) -> String {
fix_name_separator(&defiller(l))
}
fn repair_mrv_line1(l: &str) -> String {
let l = fix_doc_code(&repair_positions(l, &[(2..5, letterize)]));
format!("{}{}", &l[0..5], fix_name_separator(&defiller(&l[5..])))
}
fn repair_mrv_a_line1(l: &str) -> String {
repair_mrv_line1(l)
}
fn repair_mrv_b_line1(l: &str) -> String {
repair_mrv_line1(l)
}
fn repair_mrv_a_line2(l: &str) -> String {
repair_positions(
l,
&[
(9..10, digitize), (10..13, letterize), (13..20, digitize), (21..28, digitize), ],
)
}
fn repair_mrv_b_line2(l: &str) -> String {
repair_positions(
l,
&[
(9..10, digitize), (10..13, letterize), (13..20, digitize), (21..28, digitize), ],
)
}
pub fn find_and_parse(text: &str) -> Result<MrzData, MrzError> {
find_and_parse_with(text, &ParseOptions::default())
}
pub fn find_and_parse_with(text: &str, opts: &ParseOptions) -> Result<MrzData, MrzError> {
let text = text.replace("<", "<");
let mut lines: Vec<&str> = Vec::new();
for line in text.lines() {
let tokens: Vec<&str> = line.split_whitespace().filter(|t| t.len() >= 20).collect();
if tokens.len() >= 2 {
lines.extend(tokens);
} else {
lines.push(line);
}
}
let mut fallback: Option<MrzData> = None;
let mut consider = |data: MrzData| -> Option<MrzData> {
if data.valid() {
return Some(data);
}
match &fallback {
Some(best) if best.checks.score() >= data.checks.score() => {}
_ => fallback = Some(data),
}
None
};
for i in 0..lines.len() {
let merged = normalize_line(lines[i]);
if merged.starts_with('P') && (84..=92).contains(&merged.len()) && is_mrz_charset(&merged) {
let head = &merged[0..44];
let tail = &merged[44..];
for l1 in [repair_td3_line1(head), head.to_string()] {
for l2 in variants(tail, 44, repair_td3_line2) {
if let Ok(data) = parse_td3_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
for l1 in variants(lines[i], 44, repair_td3_line1) {
if !l1.starts_with('P') {
continue;
}
for l2_raw in lines.iter().skip(i + 1).take(3) {
for l2 in variants(l2_raw, 44, repair_td3_line2) {
if let Ok(data) = parse_td3_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
}
for i in 0..lines.len() {
let merged = normalize_line(lines[i]);
if merged.starts_with('V') && (68..=76).contains(&merged.len()) && is_mrz_charset(&merged) {
let head = &merged[0..36];
let tail = &merged[36..];
for l1 in [repair_mrv_b_line1(head), head.to_string()] {
for l2 in variants(tail, 36, repair_mrv_b_line2) {
if let Ok(data) = parse_mrv_b_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
for l1 in variants(lines[i], 36, repair_mrv_b_line1) {
if !l1.starts_with('V') {
continue;
}
for l2_raw in lines.iter().skip(i + 1).take(3) {
for l2 in variants(l2_raw, 36, repair_mrv_b_line2) {
if let Ok(data) = parse_mrv_b_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
}
for i in 0..lines.len() {
let merged = normalize_line(lines[i]);
if merged.starts_with('V') && (84..=92).contains(&merged.len()) && is_mrz_charset(&merged) {
let head = &merged[0..44];
let tail = &merged[44..];
for l1 in [repair_mrv_a_line1(head), head.to_string()] {
for l2 in variants(tail, 44, repair_mrv_a_line2) {
if let Ok(data) = parse_mrv_a_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
for l1 in variants(lines[i], 44, repair_mrv_a_line1) {
if !l1.starts_with('V') {
continue;
}
for l2_raw in lines.iter().skip(i + 1).take(3) {
for l2 in variants(l2_raw, 44, repair_mrv_a_line2) {
if let Ok(data) = parse_mrv_a_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
}
for i in 0..lines.len().saturating_sub(2) {
for l1 in variants(lines[i], 30, repair_td1_line1) {
if !matches!(l1.as_bytes().first(), Some(b'I' | b'A' | b'C')) {
continue;
}
for l2 in variants(lines[i + 1], 30, repair_td1_line2) {
for l3 in variants(lines[i + 2], 30, repair_td1_line3) {
if let Ok(data) = parse_td1_with(&l1, &l2, &l3, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
}
for &line in &lines {
let merged = normalize_line(line);
if (68..=76).contains(&merged.len())
&& is_mrz_charset(&merged)
&& matches!(merged.as_bytes().first(), Some(b'I' | b'A' | b'C'))
{
let head = &merged[0..36];
let tail = &merged[36..];
for l1 in [repair_td2_line1(head), head.to_string()] {
for l2 in variants(tail, 36, repair_td2_line2) {
if let Ok(data) = parse_td2_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
}
for i in 0..lines.len().saturating_sub(1) {
for l1 in variants(lines[i], 36, repair_td2_line1) {
if !matches!(l1.as_bytes().first(), Some(b'I' | b'A' | b'C')) {
continue;
}
for l2 in variants(lines[i + 1], 36, repair_td2_line2) {
if let Ok(data) = parse_td2_with(&l1, &l2, opts) {
if let Some(valid) = consider(data) {
return Ok(valid);
}
}
}
}
}
fallback.ok_or(MrzError::NotFound)
}