added more functions from original code, added macro

This commit is contained in:
Vlasislav Kashin
2025-07-02 18:02:47 +03:00
parent 929fdbae8a
commit d0fb47705a
14 changed files with 1177 additions and 41 deletions

View File

@@ -0,0 +1,9 @@
use thiserror::Error;
use crate::regexp_cache::ErrorInvalidRegex;
#[derive(Debug, PartialEq, Error)]
pub enum PhoneNumberUtilError {
#[error("{0}")]
InvalidRegexError(#[from] ErrorInvalidRegex)
}

View File

@@ -253,11 +253,13 @@ pub(super) fn create_extn_pattern(for_parsing: bool) -> String {
/// * `remove_non_matches` - indicates whether characters that are not able to be
/// replaced should be stripped from the number. If this is false, they will be
/// left unchanged in the number.
///
/// Returns: normalized_string
pub(super) fn normalize_helper(
normalization_replacements: &HashMap<char, char>,
remove_non_matches: bool,
phone_number: &mut String,
) {
phone_number: &str
) -> String {
let mut normalized_number = String::with_capacity(phone_number.len());
// Skip UTF checking because strings in rust are valid UTF-8 already
for phone_char in phone_number.chars() {
@@ -269,7 +271,7 @@ pub(super) fn normalize_helper(
// If neither of the above are true, we remove this character.
}
*phone_number = normalized_number;
normalized_number
}
/// Returns `true` if there is any possible number data set for a particular
@@ -307,7 +309,7 @@ pub(super) fn desc_has_data(desc: &PhoneNumberDesc) -> bool {
/// Returns the types we have metadata for based on the PhoneMetadata object
/// passed in.
pub(super) fn get_supported_types_for_metadata(
pub(super) fn populate_supported_types_for_metadata(
metadata: &PhoneMetadata,
types: &mut HashSet<PhoneNumberType>,
) {
@@ -327,6 +329,13 @@ pub(super) fn get_supported_types_for_metadata(
});
}
pub(super) fn get_supported_types_for_metadata(metadata: &PhoneMetadata) -> HashSet<PhoneNumberType> {
const EFFECTIVE_NUMBER_TYPES: usize = 11 /* count */ - 2 /* filter type or unknown */;
let mut types = HashSet::with_capacity(EFFECTIVE_NUMBER_TYPES);
populate_supported_types_for_metadata(metadata, &mut types);
types
}
/// Helper method to check a number against possible lengths for this number
/// type, and determine whether it matches, or is too short or too long.
pub(super) fn test_number_length(
@@ -444,7 +453,7 @@ pub(crate) fn copy_core_fields_only(from_number: &PhoneNumber, to_number: &mut P
/// Determines whether the given number is a national number match for the given
/// PhoneNumberDesc. Does not check against possible lengths!
pub(super) fn is_match(
matcher_api: Box<dyn MatcherApi>,
matcher_api: &Box<dyn MatcherApi>,
number: &str,
number_desc: &PhoneNumberDesc,
) -> bool {

View File

@@ -1,13 +1,17 @@
mod helper_constants;
pub mod helper_functions;
mod errors;
mod enums;
mod phonenumberutil;
mod regex_and_mappings;
mod phone_number_regexps_and_mappings;
use std::sync::LazyLock;
pub use enums::{MatchType, PhoneNumberFormat, PhoneNumberType, ValidationResultErr, ValidNumberLenType};
use thiserror::Error;
use crate::phonenumberutil::phonenumberutil::PhoneNumberUtil;
#[derive(Debug, Error)]
pub enum ErrorType {
#[error("No parsing")]
@@ -23,3 +27,7 @@ pub enum ErrorType {
#[error("Too long nsn")]
TooLongNsn, // TOO_LONG in the java version.
}
static PHONE_NUMBER_UTIL: LazyLock<PhoneNumberUtil> = LazyLock::new(|| {
PhoneNumberUtil::new()
});

View File

@@ -2,7 +2,11 @@ use std::collections::{HashMap, HashSet};
use regex::Regex;
use crate::{phonenumberutil::{helper_constants::{self, CAPTURE_UP_TO_SECOND_NUMBER_START, DIGITS, MIN_LENGTH_FOR_NSN, PLUS_CHARS, PLUS_SIGN, RFC3966_VISUAL_SEPARATOR, STAR_SIGN, VALID_ALPHA, VALID_ALPHA_INCL_UPPERCASE, VALID_PUNCTUATION}, helper_functions::create_extn_pattern}, regexp_cache::RegexCache};
use crate::{phonenumberutil::{helper_constants::{
CAPTURE_UP_TO_SECOND_NUMBER_START, DIGITS, MIN_LENGTH_FOR_NSN, PLUS_CHARS,
PLUS_SIGN, RFC3966_VISUAL_SEPARATOR, STAR_SIGN, VALID_ALPHA, VALID_ALPHA_INCL_UPPERCASE,
VALID_PUNCTUATION
}, helper_functions::create_extn_pattern}, regexp_cache::RegexCache};
pub(super) struct PhoneNumberRegExpsAndMappings {
/// Regular expression of viable phone numbers. This is location independent.
@@ -150,6 +154,17 @@ pub(super) struct PhoneNumberRegExpsAndMappings {
/// Regular expression of valid domainname for the phone-context parameter,
/// following the syntax defined in RFC3966.
pub rfc3966_domainname_pattern: Regex,
/// *Rust note*: It's for some reason calculated inside function in C++,
/// so, we move it here
///
/// A pattern that is used to determine if a numberFormat under
/// availableFormats is eligible to be used by the AYTF. It is eligible when
/// the format element under numberFormat contains groups of the dollar sign
/// followed by a single digit, separated by valid phone number punctuation.
/// This prevents invalid punctuation (such as the star sign in Israeli star
/// numbers) getting into the output of the AYTF.
pub is_format_eligible_as_you_type_formatting_regex: Regex
}
impl PhoneNumberRegExpsAndMappings {
@@ -200,6 +215,8 @@ impl PhoneNumberRegExpsAndMappings {
alpha_map.insert('X', '9');
alpha_map.insert('Y', '9');
alpha_map.insert('Z', '9');
// IMPORTANT: only uppercase letters like in Java version
self.alpha_mappings = alpha_map;
let mut combined_map = HashMap::with_capacity(100);
@@ -241,7 +258,7 @@ impl PhoneNumberRegExpsAndMappings {
self.all_plus_number_grouping_symbols = all_plus_number_groupings;
}
fn new() -> Self {
pub fn new() -> Self {
let alphanum = fast_cat::concat_str!(VALID_ALPHA_INCL_UPPERCASE, DIGITS);
let extn_patterns_for_parsing = create_extn_pattern(true);
let valid_phone_number = format!(
@@ -303,6 +320,9 @@ impl PhoneNumberRegExpsAndMappings {
rfc3966_domainname_pattern: Regex::new(
&format!("^({}\\.)*{}\\.?$", rfc3966_domainlabel, rfc3966_toplabel)
).unwrap(),
is_format_eligible_as_you_type_formatting_regex: Regex::new(
&format!("[{}]*\\$1[{}]*(\\$\\d[{}]*)*",VALID_PUNCTUATION, VALID_PUNCTUATION, VALID_PUNCTUATION)
).unwrap(),
};
instance.initialize_regexp_mappings();
instance

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +0,0 @@
pub struct PhoneNumberRegExpsAndMappings{
}