use core::{fmt, hash::Hash, str::FromStr};
use crate::{Assessment, Format, Formatted, Region};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CallingCode(u16);
impl CallingCode {
pub const fn value(self) -> u16 {
self.0
}
}
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Extension(Box<str>);
impl Extension {
pub(crate) fn new(value: String) -> Self {
Self(value.into_boxed_str())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for Extension {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Debug for Extension {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Extension(**redacted**)")
}
}
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct PhoneNumber {
pub(crate) calling_code: u16,
pub(crate) national: Box<str>,
pub(crate) extension: Option<Extension>,
}
impl PhoneNumber {
pub fn calling_code(&self) -> CallingCode {
CallingCode(self.calling_code)
}
pub fn national_number(&self) -> &str {
&self.national
}
pub fn extension(&self) -> Option<&Extension> {
self.extension.as_ref()
}
pub fn region(&self) -> Option<Region> {
let plan = crate::assessment::resolve(self)?;
let region = Region::try_from(plan.region.as_str()).ok()?;
(crate::assessment::classify(plan, &self.national) != crate::NumberKind::Unknown)
.then_some(region)
}
pub fn assess(&self) -> Assessment {
Assessment::new(self)
}
pub fn is_valid_for(&self, region: Region) -> bool {
crate::assessment::is_valid_for(self, region)
}
pub fn format(&self, format: Format) -> Formatted<'_> {
Formatted::new(self, format)
}
pub fn match_with(&self, other: &Self) -> crate::Match {
crate::matching::compare(self, other, crate::MatchOptions::default())
}
pub fn match_with_options(&self, other: &Self, options: crate::MatchOptions) -> crate::Match {
crate::matching::compare(self, other, options)
}
}
impl FromStr for PhoneNumber {
type Err = crate::Error;
fn from_str(value: &str) -> crate::Result<Self> {
crate::parse(value)
}
}
impl fmt::Debug for PhoneNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PhoneNumber")
.field("calling_code", &self.calling_code())
.field("value", &"**redacted**")
.field("has_extension", &self.extension().is_some())
.finish()
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct ParsedNumber {
number: PhoneNumber,
source: Box<str>,
default_region: Option<Region>,
}
impl fmt::Debug for ParsedNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ParsedNumber")
.field("number", &self.number)
.field("source", &"**redacted**")
.field("default_region", &self.default_region)
.finish()
}
}
impl ParsedNumber {
pub fn parse(source: impl Into<String>) -> crate::Result<Self> {
Self::parse_with(source.into(), None)
}
pub fn parse_in(region: Region, source: impl Into<String>) -> crate::Result<Self> {
Self::parse_with(source.into(), Some(region))
}
fn parse_with(source: String, default_region: Option<Region>) -> crate::Result<Self> {
let number = match default_region {
Some(region) => crate::parse_in(region, &source)?,
None => crate::parse(&source)?,
};
Ok(Self {
number,
source: source.into_boxed_str(),
default_region,
})
}
pub const fn number(&self) -> &PhoneNumber {
&self.number
}
pub fn source(&self) -> &str {
&self.source
}
pub const fn default_region(&self) -> Option<Region> {
self.default_region
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for PhoneNumber {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.format(Format::Rfc3966).to_string())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for PhoneNumber {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = <std::borrow::Cow<'de, str>>::deserialize(deserializer)?;
crate::parse_strict(&value).map_err(serde::de::Error::custom)
}
}