extern crate alloc;
use alloc::string::{String, ToString};
use core::fmt;
use core::ops::Deref;
use core::str::FromStr;
use crate::error::ValidError;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct PhoneE164(String);
impl PhoneE164 {
pub fn parse(s: &str) -> Result<Self, ValidError> {
validate_phone(s)
}
pub fn new(s: String) -> Result<Self, ValidError> {
validate_phone(&s)
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_inner(self) -> String {
self.0
}
}
fn validate_phone(input: &str) -> Result<PhoneE164, ValidError> {
if input.is_empty() {
return Err(ValidError::InvalidPhone("phone is empty".to_string()));
}
if input.contains('\r') || input.contains('\n') || input.contains(' ') {
return Err(ValidError::InvalidPhone(
"phone must not contain whitespace or control".to_string(),
));
}
#[cfg(feature = "regex")]
{
#[cfg(feature = "std")]
{
use std::sync::OnceLock;
static RE: OnceLock<regex::Regex> = OnceLock::new();
let re = match RE.get() {
Some(r) => r,
None => {
let init = match regex::Regex::new(r"^\+[1-9]\d{1,14}$") {
Ok(r) => r,
Err(_) => {
return Err(ValidError::InvalidPhone(
"internal regex error".to_string(),
))
}
};
let _ = RE.set(init);
match RE.get() {
Some(r) => r,
None => {
return Err(ValidError::InvalidPhone(
"internal regex error".to_string(),
))
}
}
}
};
if !re.is_match(input) {
return Err(ValidError::InvalidPhone(
"phone must match E.164 format +[1-9] followed by 1-14 digits".to_string(),
));
}
}
#[cfg(not(feature = "std"))]
{
let re = match regex::Regex::new(r"^\+[1-9]\d{1,14}$") {
Ok(r) => r,
Err(_) => return Err(ValidError::InvalidPhone("internal regex error".to_string())),
};
if !re.is_match(input) {
return Err(ValidError::InvalidPhone(
"phone must match E.164 format +[1-9] followed by 1-14 digits".to_string(),
));
}
}
Ok(PhoneE164(input.to_string()))
}
#[cfg(not(feature = "regex"))]
{
let bytes = input.as_bytes();
if bytes[0] != b'+' {
return Err(ValidError::InvalidPhone(
"phone must start with '+'".to_string(),
));
}
let digits = &bytes[1..];
if digits.len() < 2 || digits.len() > 15 {
return Err(ValidError::InvalidPhone(
"phone must have 2-15 digits after '+'".to_string(),
));
}
if digits[0] == b'0' {
return Err(ValidError::InvalidPhone(
"phone must not start with 0 after '+'".to_string(),
));
}
for &b in digits {
if !b.is_ascii_digit() {
return Err(ValidError::InvalidPhone(
"phone must contain only digits after '+'".to_string(),
));
}
}
return Ok(PhoneE164(input.to_string()));
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for PhoneE164 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = <String as serde::Deserialize>::deserialize(deserializer)?;
Self::new(s).map_err(serde::de::Error::custom)
}
}
impl Deref for PhoneE164 {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for PhoneE164 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<String> for PhoneE164 {
type Error = ValidError;
fn try_from(value: String) -> Result<Self, Self::Error> {
PhoneE164::new(value)
}
}
impl TryFrom<&str> for PhoneE164 {
type Error = ValidError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
PhoneE164::parse(value)
}
}
impl FromStr for PhoneE164 {
type Err = ValidError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
PhoneE164::parse(s)
}
}
impl AsRef<str> for PhoneE164 {
fn as_ref(&self) -> &str {
&self.0
}
}
#[allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_e164() {
assert!(PhoneE164::parse("+14155552671").is_ok());
assert!(PhoneE164::parse("+442071838750").is_ok());
assert!(PhoneE164::parse("+12").is_ok());
}
#[test]
fn invalid_no_plus() {
assert!(PhoneE164::parse("14155552671").is_err());
}
#[test]
fn invalid_zero_start() {
assert!(PhoneE164::parse("+0123").is_err());
}
#[test]
fn invalid_too_long() {
assert!(PhoneE164::parse("+1234567890123456").is_err());
}
}