use crate::PhoneNumber;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Match {
Exact,
SameNumber,
ShortNational,
None,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct MatchOptions {
compare_extensions: bool,
}
impl MatchOptions {
pub const fn new() -> Self {
Self {
compare_extensions: true,
}
}
pub const fn with_extensions(mut self, compare: bool) -> Self {
self.compare_extensions = compare;
self
}
}
impl Default for MatchOptions {
fn default() -> Self {
Self::new()
}
}
pub(crate) fn compare(left: &PhoneNumber, right: &PhoneNumber, options: MatchOptions) -> Match {
if left.calling_code() == right.calling_code()
&& left.national_number() == right.national_number()
{
if !options.compare_extensions || left.extension() == right.extension() {
Match::Exact
} else {
Match::SameNumber
}
} else if left.calling_code() == right.calling_code()
&& suffix_match(left.national_number(), right.national_number())
{
Match::ShortNational
} else {
Match::None
}
}
fn suffix_match(left: &str, right: &str) -> bool {
let shortest = left.len().min(right.len());
shortest >= 7 && (left.ends_with(right) || right.ends_with(left))
}