Add more tests, better naming and bug fixes
This commit is contained in:
@@ -65,7 +65,7 @@ pub enum MatchType {
|
||||
|
||||
/// Possible outcomes when testing if a PhoneNumber is possible.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ValidNumberLenType {
|
||||
pub enum NumberLengthType {
|
||||
/// The number length matches that of valid numbers for this region.
|
||||
IsPossible,
|
||||
/// The number length matches that of local numbers for this region only
|
||||
|
||||
@@ -82,7 +82,7 @@ pub struct InvalidMetadataForValidRegionError;
|
||||
|
||||
/// Possible outcomes when testing if a PhoneNumber is possible.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error)]
|
||||
pub enum ValidationResultErr {
|
||||
pub enum ValidationError {
|
||||
/// The number has an invalid country calling code.
|
||||
#[error("The number has an invalid country calling code")]
|
||||
InvalidCountryCode,
|
||||
|
||||
@@ -11,8 +11,8 @@ use crate::{
|
||||
};
|
||||
|
||||
use super::{
|
||||
PhoneNumberFormat, PhoneNumberType, ValidNumberLenType,
|
||||
errors::ValidationResultErr,
|
||||
PhoneNumberFormat, PhoneNumberType, NumberLengthType,
|
||||
errors::ValidationError,
|
||||
helper_constants::{
|
||||
OPTIONAL_EXT_SUFFIX, PLUS_SIGN, POSSIBLE_CHARS_AFTER_EXT_LABEL,
|
||||
POSSIBLE_SEPARATORS_BETWEEN_NUMBER_AND_EXT_LABEL, RFC3966_EXTN_PREFIX, RFC3966_PREFIX,
|
||||
@@ -342,7 +342,7 @@ pub fn test_number_length(
|
||||
phone_number: &str,
|
||||
phone_metadata: &PhoneMetadata,
|
||||
phone_number_type: PhoneNumberType,
|
||||
) -> Result<ValidNumberLenType, ValidationResultErr> {
|
||||
) -> Result<NumberLengthType, ValidationError> {
|
||||
let desc_for_type = get_number_desc_by_type(phone_metadata, phone_number_type);
|
||||
// There should always be "possibleLengths" set for every element. This is
|
||||
// declared in the XML schema which is verified by
|
||||
@@ -394,31 +394,31 @@ pub fn test_number_length(
|
||||
// If the type is not suported at all (indicated by the possible lengths
|
||||
// containing -1 at this point) we return invalid length.
|
||||
if *possible_lengths.first().unwrap_or(&-1) == -1 {
|
||||
return Err(ValidationResultErr::InvalidLength);
|
||||
return Err(ValidationError::InvalidLength);
|
||||
}
|
||||
|
||||
let actual_length = phone_number.len() as i32;
|
||||
// This is safe because there is never an overlap beween the possible lengths
|
||||
// and the local-only lengths; this is checked at build time.
|
||||
if local_lengths.contains(&actual_length) {
|
||||
return Ok(ValidNumberLenType::IsPossibleLocalOnly);
|
||||
return Ok(NumberLengthType::IsPossibleLocalOnly);
|
||||
}
|
||||
|
||||
// here we can unwrap safe
|
||||
let minimum_length = possible_lengths[0];
|
||||
|
||||
if minimum_length == actual_length {
|
||||
return Ok(ValidNumberLenType::IsPossible);
|
||||
return Ok(NumberLengthType::IsPossible);
|
||||
} else if minimum_length > actual_length {
|
||||
return Err(ValidationResultErr::TooShort);
|
||||
return Err(ValidationError::TooShort);
|
||||
} else if possible_lengths[possible_lengths.len() - 1] < actual_length {
|
||||
return Err(ValidationResultErr::TooLong);
|
||||
return Err(ValidationError::TooLong);
|
||||
}
|
||||
// We skip the first element; we've already checked it.
|
||||
return if possible_lengths[1..].contains(&actual_length) {
|
||||
Ok(ValidNumberLenType::IsPossible)
|
||||
Ok(NumberLengthType::IsPossible)
|
||||
} else {
|
||||
Err(ValidationResultErr::InvalidLength)
|
||||
Err(ValidationError::InvalidLength)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -428,7 +428,7 @@ pub fn test_number_length(
|
||||
pub fn test_number_length_with_unknown_type(
|
||||
phone_number: &str,
|
||||
phone_metadata: &PhoneMetadata,
|
||||
) -> Result<ValidNumberLenType, ValidationResultErr> {
|
||||
) -> Result<NumberLengthType, ValidationError> {
|
||||
return test_number_length(phone_number, phone_metadata, PhoneNumberType::Unknown);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ pub(self) mod comparisons;
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
pub use enums::{MatchType, PhoneNumberFormat, PhoneNumberType, ValidNumberLenType};
|
||||
pub use enums::{MatchType, PhoneNumberFormat, PhoneNumberType, NumberLengthType};
|
||||
use crate::phonenumberutil::phonenumberutil::PhoneNumberUtil;
|
||||
|
||||
pub static PHONE_NUMBER_UTIL: LazyLock<PhoneNumberUtil> = LazyLock::new(|| {
|
||||
|
||||
@@ -25,7 +25,7 @@ use crate::{
|
||||
errors::{
|
||||
ExtractNumberError, GetExampleNumberError, InternalLogicError,
|
||||
InvalidMetadataForValidRegionError, InvalidNumberError, ParseError,
|
||||
ValidationResultErr,
|
||||
ValidationError,
|
||||
}, helper_constants::{
|
||||
DEFAULT_EXTN_PREFIX, MAX_LENGTH_COUNTRY_CODE, MAX_LENGTH_FOR_NSN, MIN_LENGTH_FOR_NSN,
|
||||
NANPA_COUNTRY_CODE, PLUS_SIGN, REGION_CODE_FOR_NON_GEO_ENTITY, RFC3966_EXTN_PREFIX,
|
||||
@@ -35,7 +35,7 @@ use crate::{
|
||||
is_national_number_suffix_of_the_other, load_compiled_metadata, normalize_helper,
|
||||
prefix_number_with_country_calling_code, test_number_length,
|
||||
test_number_length_with_unknown_type,
|
||||
}, helper_types::{PhoneNumberWithCountryCodeSource}, MatchType, PhoneNumberFormat, PhoneNumberType, ValidNumberLenType
|
||||
}, helper_types::{PhoneNumberWithCountryCodeSource}, MatchType, PhoneNumberFormat, PhoneNumberType, NumberLengthType
|
||||
},
|
||||
phonemetadata::{NumberFormat, PhoneMetadata, PhoneNumberDesc},
|
||||
phonenumber::{phone_number::CountryCodeSource, PhoneNumber},
|
||||
@@ -53,7 +53,7 @@ pub type RegexResult<T> = std::result::Result<T, ErrorInvalidRegex>;
|
||||
pub type ParseResult<T> = std::result::Result<T, ParseError>;
|
||||
|
||||
pub type ExampleNumberResult = std::result::Result<PhoneNumber, GetExampleNumberError>;
|
||||
pub type ValidationResult = std::result::Result<ValidNumberLenType, ValidationResultErr>;
|
||||
pub type ValidationResult = std::result::Result<NumberLengthType, ValidationError>;
|
||||
pub type MatchResult = std::result::Result<MatchType, InvalidNumberError>;
|
||||
pub type ExtractNumberResult<T> = std::result::Result<T, ExtractNumberError>;
|
||||
pub type InternalLogicResult<T> = std::result::Result<T, InternalLogicError>;
|
||||
@@ -780,8 +780,8 @@ impl PhoneNumberUtil {
|
||||
.ok_or(InvalidMetadataForValidRegionError {})?;
|
||||
let national_number = self.get_national_significant_number(&number_no_extension);
|
||||
let format = if self.can_be_internationally_dialled(&number_no_extension)?
|
||||
&& test_number_length_with_unknown_type(&national_number, region_metadata)
|
||||
.is_err_and(|e| matches!(e, ValidationResultErr::TooShort))
|
||||
&& !test_number_length_with_unknown_type(&national_number, region_metadata)
|
||||
.is_err_and(|e| matches!(e, ValidationError::TooShort))
|
||||
{
|
||||
PhoneNumberFormat::International
|
||||
} else {
|
||||
@@ -1686,14 +1686,14 @@ impl PhoneNumberUtil {
|
||||
// with this country calling code in the metadata for the default region in
|
||||
// this case.
|
||||
if !self.has_valid_country_calling_code(country_code) {
|
||||
return Err(ValidationResultErr::InvalidCountryCode);
|
||||
return Err(ValidationError::InvalidCountryCode);
|
||||
}
|
||||
let region_code = self.get_region_code_for_country_code(country_code);
|
||||
// Metadata cannot be NULL because the country calling code is valid.
|
||||
let Some(metadata) =
|
||||
self.get_metadata_for_region_or_calling_code(country_code, region_code)
|
||||
else {
|
||||
return Err(ValidationResultErr::InvalidCountryCode);
|
||||
return Err(ValidationError::InvalidCountryCode);
|
||||
};
|
||||
return test_number_length(&national_number, metadata, phone_number_type);
|
||||
}
|
||||
@@ -1712,7 +1712,7 @@ impl PhoneNumberUtil {
|
||||
number_copy.set_national_number(national_number);
|
||||
if self
|
||||
.is_possible_number_with_reason(&number_copy)
|
||||
.is_err_and(|err| matches!(err, ValidationResultErr::TooShort))
|
||||
.is_err_and(|err| matches!(err, ValidationError::TooShort))
|
||||
|| national_number == 0
|
||||
{
|
||||
return Ok(false);
|
||||
@@ -1831,11 +1831,11 @@ impl PhoneNumberUtil {
|
||||
let validation_result =
|
||||
test_number_length_with_unknown_type(&potential_national_number, country_metadata);
|
||||
if !validation_result
|
||||
.is_ok_and(|res| matches!(res, ValidNumberLenType::IsPossibleLocalOnly))
|
||||
.is_ok_and(|res| matches!(res, NumberLengthType::IsPossibleLocalOnly))
|
||||
&& !validation_result.is_err_and(|err| {
|
||||
matches!(
|
||||
err,
|
||||
ValidationResultErr::TooShort | ValidationResultErr::InvalidLength
|
||||
ValidationError::TooShort | ValidationError::InvalidLength
|
||||
)
|
||||
})
|
||||
{
|
||||
@@ -2040,7 +2040,7 @@ impl PhoneNumberUtil {
|
||||
&national_number,
|
||||
default_region_metadata,
|
||||
)
|
||||
.is_err_and(|e| matches!(e, ValidationResultErr::TooLong))
|
||||
.is_err_and(|e| matches!(e, ValidationError::TooLong))
|
||||
{
|
||||
if keep_raw_input {
|
||||
phone_number.set_country_code_source(
|
||||
@@ -2379,11 +2379,10 @@ impl PhoneNumberUtil {
|
||||
}
|
||||
|
||||
if (matches!(phone_number_type, PhoneNumberType::Mobile)
|
||||
&& !self
|
||||
&& self
|
||||
.reg_exps
|
||||
.geo_mobile_countries_without_mobile_area_codes
|
||||
.contains(&country_calling_code))
|
||||
{
|
||||
.contains(&country_calling_code)) {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user