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::{date_completeness, expand_date_with_pivot};
use crate::{Checks, Format, MrzData, MrzError, ParseOptions};
struct Overflow {
full: String,
check_ok: bool,
legacy: bool,
consumed: usize,
}
fn read_overflow(number_field: &str, check: char, optional: &str) -> Option<Overflow> {
if check != '<' {
return None;
}
if number_field.trim_end_matches('<').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 consumed = end + 1;
let spec_full = format!("{number_field}{remainder}");
if verify(&spec_full, check_digit) {
return Some(Overflow {
full: spec_full,
check_ok: true,
legacy: false,
consumed,
});
}
if number_field.ends_with('<') {
let head = number_field[0..8].trim_end_matches('<');
let legacy_full = format!("{head}{remainder}");
if verify(&legacy_full, check_digit) {
return Some(Overflow {
full: legacy_full,
check_ok: true,
legacy: true,
consumed,
});
}
}
Some(Overflow {
full: spec_full,
check_ok: false,
legacy: false,
consumed,
})
}
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,
),
};
let document_number_legacy_encoding = overflow.as_ref().is_some_and(|o| o.legacy);
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),
document_number_legacy_encoding,
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),
date_of_birth_completeness: date_completeness(&line2[13..19]),
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,
),
};
let document_number_legacy_encoding = overflow.as_ref().is_some_and(|o| o.legacy);
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),
document_number_legacy_encoding,
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),
date_of_birth_completeness: date_completeness(&line2[13..19]),
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,
),
};
let document_number_legacy_encoding = overflow.as_ref().is_some_and(|o| o.legacy);
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),
document_number_legacy_encoding,
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),
date_of_birth_completeness: date_completeness(&line2[0..6]),
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,
document_number_legacy_encoding: false,
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),
date_of_birth_completeness: date_completeness(&line2[13..19]),
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,
document_number_legacy_encoding: false,
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),
date_of_birth_completeness: date_completeness(&line2[13..19]),
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);
}
}
}
}
}
if fallback.is_some() {
if let Some(data) = damaged_pass(&lines, opts) {
return Ok(data);
}
}
fallback.ok_or(MrzError::NotFound)
}
type TwoLineParse = fn(&str, &str, &ParseOptions) -> Result<MrzData, MrzError>;
type TwoLineFormat = (
usize,
&'static [u8],
fn(&str) -> String,
fn(&str) -> String,
TwoLineParse,
);
const MAX_DAMAGED_ATTEMPTS: usize = 200_000;
fn restored(raw: &str, target: usize, repair: fn(&str) -> String) -> Vec<String> {
let n = normalize_line(raw);
if n.len() + 1 != target || !is_mrz_charset(&n) {
return Vec::new();
}
let mut seen = std::collections::HashSet::new();
let mut out: Vec<String> = Vec::new();
for shaped in crate::repair::width_candidates(&n, target) {
if !shaped.contains(crate::repair::UNKNOWN) {
continue;
}
for concrete in crate::repair::concrete_fillings(&shaped) {
for form in [repair(&concrete), concrete] {
if seen.insert(form.clone()) {
out.push(form);
}
}
}
}
out
}
fn accept_damaged(data: &MrzData) -> bool {
data.valid()
&& data
.validity(crate::Date::new(2000, 1, 1))
.dates_well_formed
}
fn damaged_pass(lines: &[&str], opts: &ParseOptions) -> Option<MrzData> {
let mut budget = MAX_DAMAGED_ATTEMPTS;
let mut hits: Vec<MrzData> = Vec::new();
let record = |data: MrzData, hits: &mut Vec<MrzData>| {
if accept_damaged(&data) && !hits.iter().any(|h| h.mrz_lines == data.mrz_lines) {
hits.push(data);
}
};
for i in 0..lines.len().saturating_sub(2) {
let (a, b, c) = (lines[i], lines[i + 1], lines[i + 2]);
let shapes: [(Vec<String>, Vec<String>, Vec<String>); 3] = [
(
restored(a, 30, repair_td1_line1),
variants(b, 30, repair_td1_line2),
variants(c, 30, repair_td1_line3),
),
(
variants(a, 30, repair_td1_line1),
restored(b, 30, repair_td1_line2),
variants(c, 30, repair_td1_line3),
),
(
variants(a, 30, repair_td1_line1),
variants(b, 30, repair_td1_line2),
restored(c, 30, repair_td1_line3),
),
];
for (v1, v2, v3) in shapes {
for l1 in &v1 {
if !matches!(l1.as_bytes().first(), Some(b'I' | b'A' | b'C')) {
continue;
}
for l2 in &v2 {
for l3 in &v3 {
if budget == 0 {
return single(hits);
}
budget -= 1;
if let Ok(data) = parse_td1_with(l1, l2, l3, opts) {
record(data, &mut hits);
}
}
}
}
}
}
let two_line: [TwoLineFormat; 4] = [
(44, b"P", repair_td3_line1, repair_td3_line2, parse_td3_with),
(
44,
b"V",
repair_mrv_a_line1,
repair_mrv_a_line2,
parse_mrv_a_with,
),
(
36,
b"IAC",
repair_td2_line1,
repair_td2_line2,
parse_td2_with,
),
(
36,
b"V",
repair_mrv_b_line1,
repair_mrv_b_line2,
parse_mrv_b_with,
),
];
for i in 0..lines.len().saturating_sub(1) {
let (a, b) = (lines[i], lines[i + 1]);
for (width, prefixes, rep1, rep2, parse) in two_line {
for (v1, v2) in [
(restored(a, width, rep1), variants(b, width, rep2)),
(variants(a, width, rep1), restored(b, width, rep2)),
] {
for l1 in &v1 {
if !l1.bytes().next().is_some_and(|c| prefixes.contains(&c)) {
continue;
}
for l2 in &v2 {
if budget == 0 {
return single(hits);
}
budget -= 1;
if let Ok(data) = parse(l1, l2, opts) {
record(data, &mut hits);
}
}
}
}
}
}
single(hits)
}
fn single(mut hits: Vec<MrzData>) -> Option<MrzData> {
let first = hits.first()?.clone();
let same = |a: &MrzData, b: &MrzData| {
a.document_number == b.document_number
&& a.date_of_birth == b.date_of_birth
&& a.date_of_expiry == b.date_of_expiry
&& a.surname == b.surname
&& a.given_names == b.given_names
&& a.nationality == b.nationality
};
if hits.iter().all(|h| same(h, &first)) {
return Some(hits.remove(0));
}
None
}