use crate::error::FieldIssue;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DialPlan {
pub locale: &'static str,
pub country_code: &'static str,
pub national_min: usize,
pub national_max: usize,
}
pub const DIAL_PLANS: &[DialPlan] = &[
DialPlan {
locale: "US",
country_code: "1",
national_min: 10,
national_max: 10,
},
DialPlan {
locale: "CA",
country_code: "1",
national_min: 10,
national_max: 10,
},
DialPlan {
locale: "MX",
country_code: "52",
national_min: 10,
national_max: 10,
},
DialPlan {
locale: "GB",
country_code: "44",
national_min: 9,
national_max: 10,
},
DialPlan {
locale: "ES",
country_code: "34",
national_min: 9,
national_max: 9,
},
DialPlan {
locale: "BR",
country_code: "55",
national_min: 10,
national_max: 11,
},
DialPlan {
locale: "FR",
country_code: "33",
national_min: 9,
national_max: 9,
},
DialPlan {
locale: "DE",
country_code: "49",
national_min: 3,
national_max: 12,
},
DialPlan {
locale: "AU",
country_code: "61",
national_min: 9,
national_max: 9,
},
DialPlan {
locale: "IN",
country_code: "91",
national_min: 10,
national_max: 10,
},
DialPlan {
locale: "JP",
country_code: "81",
national_min: 9,
national_max: 10,
},
];
#[must_use]
pub fn dial_plan(locale: &str) -> Option<&'static DialPlan> {
let locale = locale.trim();
DIAL_PLANS
.iter()
.find(|plan| plan.locale.eq_ignore_ascii_case(locale))
}
pub fn check_phone_e164(field: &str, value: &str) -> Result<(), FieldIssue> {
let Some(digits) = value.strip_prefix('+') else {
return Err(FieldIssue::new(
field,
"INVALID_PHONE",
"phone must be E.164 with a leading +",
));
};
if !digits.bytes().all(|b| b.is_ascii_digit()) {
return Err(FieldIssue::new(
field,
"INVALID_PHONE",
"phone may only contain + and digits",
));
}
if !(7..=15).contains(&digits.len()) {
return Err(FieldIssue::new(
field,
"INVALID_PHONE",
"phone digit count must be between 7 and 15",
));
}
if digits.starts_with('0') {
return Err(FieldIssue::new(
field,
"INVALID_PHONE",
"phone country code cannot start with 0",
));
}
Ok(())
}
pub fn check_phone_locale(field: &str, value: &str, locale: &str) -> Result<(), FieldIssue> {
check_phone_e164(field, value)?;
let Some(plan) = dial_plan(locale) else {
return Err(FieldIssue::new(
field,
"INVALID_PHONE_LOCALE",
format!("unknown phone locale {locale}"),
));
};
let digits = &value[1..];
if !digits.starts_with(plan.country_code) {
return Err(FieldIssue::new(
field,
"INVALID_PHONE",
format!("phone must start with +{}", plan.country_code),
));
}
let national = digits.len() - plan.country_code.len();
if national < plan.national_min || national > plan.national_max {
return Err(FieldIssue::new(
field,
"INVALID_PHONE",
format!(
"national number length must be {}..={}",
plan.national_min, plan.national_max
),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_us_and_german_numbers() {
assert!(check_phone_locale("phone", "+14155552671", "US").is_ok());
assert!(check_phone_locale("phone", "+49301234567", "DE").is_ok());
assert!(check_phone_locale("phone", "+4915123456789", "de").is_ok());
}
#[test]
fn rejects_short_or_zero_cc_e164() {
assert!(check_phone_e164("phone", "+123").is_err());
assert!(check_phone_e164("phone", "+0123456789").is_err());
}
#[test]
fn rejects_untrimmed_e164() {
assert!(check_phone_e164("phone", "+14155552671 ").is_err());
assert!(check_phone_e164("phone", " +14155552671").is_err());
}
}