Add some phonenumberutil functions

This commit is contained in:
Vlasislav Kashin
2025-07-09 13:22:32 +03:00
parent 29f5e5664c
commit 7692433296
11 changed files with 1409 additions and 91 deletions

View File

@@ -1,3 +1,6 @@
use core::error;
use std::num::ParseIntError;
use thiserror::Error;
use crate::regexp_cache::ErrorInvalidRegex;
@@ -5,5 +8,38 @@ use crate::regexp_cache::ErrorInvalidRegex;
#[derive(Debug, PartialEq, Error)]
pub enum PhoneNumberUtilError {
#[error("{0}")]
InvalidRegexError(#[from] ErrorInvalidRegex)
}
InvalidRegexError(#[from] ErrorInvalidRegex),
#[error("Parse error: {0}")]
ParseError(#[from] ParseError),
#[error("Extract number error: {0}")]
ExtractNumberError(#[from] ExtractNumberError)
}
#[derive(Debug, PartialEq, Error)]
pub enum ParseError {
// Removed as OK variant
// NoParsingError,
#[error("Invalid country code")]
InvalidCountryCodeError, // INVALID_COUNTRY_CODE in the java version.
#[error("Not a number")]
NotANumber,
#[error("Too short after idd")]
TooShortAfterIdd,
#[error("Too short Nsn")]
TooShortNsn,
#[error("Too long nsn")]
TooLongNsn, // TOO_LONG in the java version.
#[error("{0}")]
InvalidRegexError(#[from] ErrorInvalidRegex),
#[error("{0}")]
ParseNumberAsIntError(#[from] ParseIntError)
}
#[derive(Debug, PartialEq, Error)]
pub enum ExtractNumberError {
#[error("No valid start character found")]
NoValidStartCharacter,
#[error("Invalid number")]
NotANumber,
}

View File

@@ -0,0 +1,31 @@
use std::borrow::Cow;
use crate::proto_gen::phonenumber::phone_number::CountryCodeSource;
#[derive(Debug)]
pub struct PhoneNumberWithCountryCodeSource<'a> {
pub phone_number: Cow<'a, str>,
pub country_code_source: CountryCodeSource
}
impl<'a> PhoneNumberWithCountryCodeSource<'a> {
pub fn new(phone_number: Cow<'a, str>, country_code_source: CountryCodeSource) -> Self {
Self { phone_number, country_code_source }
}
}
#[derive(Debug)]
pub struct PhoneNumberAndCarrierCode<'a> {
pub carrier_code: Option<&'a str>,
pub phone_number: Cow<'a, str>
}
impl<'a> PhoneNumberAndCarrierCode<'a> {
pub fn new<B: Into<Cow<'a, str>>>(carrier_code: Option<&'a str>, phone_number: B) -> Self {
Self { carrier_code, phone_number: phone_number.into() }
}
pub fn new_phone<B: Into<Cow<'a, str>>>(phone_number: B) -> Self {
Self { carrier_code: None, phone_number: phone_number.into() }
}
}

View File

@@ -4,30 +4,15 @@ mod errors;
mod enums;
mod phonenumberutil;
mod phone_number_regexps_and_mappings;
pub(self) mod helper_types;
use std::sync::LazyLock;
pub use enums::{MatchType, PhoneNumberFormat, PhoneNumberType, ValidationResultErr, ValidNumberLenType};
use thiserror::Error;
use crate::phonenumberutil::phonenumberutil::PhoneNumberUtil;
// use crate::phonenumberutil::phonenumberutil::PhoneNumberUtil;
#[derive(Debug, Error)]
pub enum ErrorType {
#[error("No parsing")]
NoParsingError,
#[error("Invalid country code")]
InvalidCountryCodeError, // INVALID_COUNTRY_CODE in the java version.
#[error("Not a number")]
NotANumber,
#[error("Too short after idd")]
TooShortAfterIdd,
#[error("Too short Nsn")]
TooShortNsn,
#[error("Too long nsn")]
TooLongNsn, // TOO_LONG in the java version.
}
static PHONE_NUMBER_UTIL: LazyLock<PhoneNumberUtil> = LazyLock::new(|| {
PhoneNumberUtil::new()
});
// static PHONE_NUMBER_UTIL: LazyLock<PhoneNumberUtil> = LazyLock::new(|| {
// PhoneNumberUtil::new()
// });

File diff suppressed because it is too large Load Diff