use super::PatternMatch;
use crate::types::redaction::PiiCategory;
use once_cell::sync::Lazy;
use regex::Regex;
static RE_PHONE: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
\b
(?:\+\d{1,3}[\s.\-]?)? # optional country code
(?:\(?\d{2,4}\)?[\s.\-]?)? # optional area code (may be parenthesised)
\d{3,4}[\s.\-]?\d{3,4} # subscriber digits
\b
",
)
.expect("phone regex compiles")
});
pub fn find_all(text: &str) -> Vec<PatternMatch> {
RE_PHONE
.find_iter(text)
.filter_map(|m| {
let raw = m.as_str();
let digit_count = raw.chars().filter(|c| c.is_ascii_digit()).count();
if !(7..=15).contains(&digit_count) {
return None;
}
Some(PatternMatch {
start: m.start(),
end: m.end(),
category: PiiCategory::Phone,
text: raw.to_string(),
})
})
.collect()
}