pub struct PhoneNumberUtil { /* private fields */ }Expand description
The main struct for all phone number-related operations.
It encapsulates the library’s core logic and provides a public API for parsing, formatting, and validating phone numbers. An instance of this struct is the primary entry point for using the library’s features.
Implementations§
Source§impl PhoneNumberUtil
impl PhoneNumberUtil
pub fn new_for_metadata(metadata_collection: PhoneMetadataCollection) -> Self
Sourcepub fn can_be_internationally_dialled(&self, phone_number: &PhoneNumber) -> bool
pub fn can_be_internationally_dialled(&self, phone_number: &PhoneNumber) -> bool
Checks if a PhoneNumber can be dialed internationally.
§Parameters
phone_number: A reference to thePhoneNumberobject to be checked.
§Returns
true if the number can be dialed from another country, false otherwise.
§Panics
This method panics if the underlying metadata contains an invalid regular expression, which indicates a critical library bug.
Sourcepub fn convert_alpha_characters_in_number(
&self,
number: impl AsRef<str>,
) -> String
pub fn convert_alpha_characters_in_number( &self, number: impl AsRef<str>, ) -> String
Converts all alpha characters in a phone number string to their corresponding digits.
For example, an input of “1-800-FLOWERS” will be converted to “1-800-3569377”.
§Parameters
number: A string slice orStringrepresenting the phone number.
§Returns
A String containing the phone number with all alphabetic characters converted to digits.
Sourcepub fn format<'a>(
&self,
phone_number: &'a PhoneNumber,
number_format: PhoneNumberFormat,
) -> Cow<'a, str>
pub fn format<'a>( &self, phone_number: &'a PhoneNumber, number_format: PhoneNumberFormat, ) -> Cow<'a, str>
Formats a PhoneNumber into a standardized format.
§Parameters
phone_number: ThePhoneNumberto be formatted.number_format: ThePhoneNumberFormatto be applied (e.g., E164, INTERNATIONAL, NATIONAL).
§Returns
A Cow<'a, str> which is either a borrowed reference to a pre-formatted string or a
newly allocated String with the formatted number.
§Panics
This method panics if the underlying metadata contains an invalid regular expression, indicating a library bug.
Sourcepub fn format_in_original_format<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
) -> Cow<'a, str>
pub fn format_in_original_format<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, ) -> Cow<'a, str>
Formats a PhoneNumber, attempting to preserve original formatting and punctuation.
The number is formatted in the national format of the region it is from.
§Parameters
phone_number: ThePhoneNumberto be formatted.region_calling_from: The two-letter region code (ISO 3166-1) from where the call is being made.
§Returns
A Cow<'a, str> containing the formatted number.
§Panics
This method panics if metadata is invalid, which indicates a library bug.
Sourcepub fn format_national_number_with_carrier_code(
&self,
phone_number: &PhoneNumber,
carrier_code: impl AsRef<str>,
) -> String
pub fn format_national_number_with_carrier_code( &self, phone_number: &PhoneNumber, carrier_code: impl AsRef<str>, ) -> String
Sourcepub fn format_number_for_mobile_dialing<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
with_formatting: bool,
) -> Option<Cow<'a, str>>
pub fn format_number_for_mobile_dialing<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, with_formatting: bool, ) -> Option<Cow<'a, str>>
Formats a PhoneNumber for dialing from a mobile device.
§Parameters
phone_number: ThePhoneNumberto format.region_calling_from: The two-letter region code (ISO 3166-1) where the user is.with_formatting: Iftrue, the number is formatted with punctuation; otherwise, only digits are returned.
§Returns
A Cow<'a, str> with the dialable number.
§Panics
Panics if formatting fails due to a library bug.
Sourcepub fn format_out_of_country_calling_number<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
) -> Cow<'a, str>
pub fn format_out_of_country_calling_number<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, ) -> Cow<'a, str>
Formats a PhoneNumber for out-of-country calling.
§Parameters
phone_number: ThePhoneNumberto format.region_calling_from: The two-letter region code (ISO 3166-1) of the calling location.
§Returns
A Cow<'a, str> representing the number formatted for international dialing.
§Panics
Panics on invalid metadata, indicating a library bug.
Sourcepub fn format_out_of_country_keeping_alpha_chars<'a>(
&self,
phone_number: &'a PhoneNumber,
region_calling_from: impl AsRef<str>,
) -> Cow<'a, str>
pub fn format_out_of_country_keeping_alpha_chars<'a>( &self, phone_number: &'a PhoneNumber, region_calling_from: impl AsRef<str>, ) -> Cow<'a, str>
Formats a PhoneNumber for out-of-country calling while preserving any alphabetic characters.
§Parameters
phone_number: ThePhoneNumberto format.region_calling_from: The two-letter region code (ISO 3166-1) of the calling location.
§Returns
A Cow<'a, str> with the formatted number.
§Panics
Panics on invalid metadata, indicating a library bug.
Sourcepub fn get_example_number(
&self,
region_code: impl AsRef<str>,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_example_number( &self, region_code: impl AsRef<str>, ) -> Result<PhoneNumber, GetExampleNumberError>
Sourcepub fn get_example_number_for_type(
&self,
number_type: PhoneNumberType,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_example_number_for_type( &self, number_type: PhoneNumberType, ) -> Result<PhoneNumber, GetExampleNumberError>
Sourcepub fn get_invalid_example_number(
&self,
region_code: impl AsRef<str>,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_invalid_example_number( &self, region_code: impl AsRef<str>, ) -> Result<PhoneNumber, GetExampleNumberError>
Sourcepub fn get_example_number_for_non_geo_entity(
&self,
country_calling_code: i32,
) -> Result<PhoneNumber, GetExampleNumberError>
pub fn get_example_number_for_non_geo_entity( &self, country_calling_code: i32, ) -> Result<PhoneNumber, GetExampleNumberError>
Gets a valid example number for the specified non-geographical entity.
§Arguments
country_calling_code- The country calling code for the non-geographical entity (e.g., +800 for Universal International Freephone Service).
§Returns
A PhoneNumber object for the given calling code, or an error if the code is invalid or unsupported.
Sourcepub fn is_possible_number_for_string(
&self,
phone_number: &str,
region_dialing_from: &str,
) -> bool
pub fn is_possible_number_for_string( &self, phone_number: &str, region_dialing_from: &str, ) -> bool
Checks whether a phone number string is a “possible” number, given the region where the number is being dialed from.
This is a fast, “weak” check that only looks at the length and structure of the number, without verifying if it is an actual valid number in that region.
§Arguments
phone_number- The phone number string to check.region_dialing_from- The CLDR region code (e.g., “US”, “GB”) where the number is being dialed from.
Sourcepub fn is_possible_number_for_type(
&self,
phone_number: &PhoneNumber,
phone_number_type: PhoneNumberType,
) -> bool
pub fn is_possible_number_for_type( &self, phone_number: &PhoneNumber, phone_number_type: PhoneNumberType, ) -> bool
Checks whether a phone number is “possible” for a specific type (e.g., Mobile, Fixed Line).
This performs a length-based check for the specified region and type.
§Arguments
phone_number- The phone number object to check.phone_number_type- The specific type (e.g.,PhoneNumberType::Mobile).
Sourcepub fn get_supported_global_network_calling_codes(
&self,
) -> impl Iterator<Item = i32>
pub fn get_supported_global_network_calling_codes( &self, ) -> impl Iterator<Item = i32>
Gets an iterator over all supported global network calling codes. These are country codes for non-geographical entities, such as satellite services.
Sourcepub fn get_supported_calling_codes(&self) -> impl Iterator<Item = i32>
pub fn get_supported_calling_codes(&self) -> impl Iterator<Item = i32>
Gets an iterator over all supported country calling codes.
Sourcepub fn get_supported_types_for_region(
&self,
region_code: &str,
) -> Option<HashSet<PhoneNumberType>>
pub fn get_supported_types_for_region( &self, region_code: &str, ) -> Option<HashSet<PhoneNumberType>>
Sourcepub fn get_supported_types_for_non_geo_entity(
&self,
country_calling_code: i32,
) -> Option<HashSet<PhoneNumberType>>
pub fn get_supported_types_for_non_geo_entity( &self, country_calling_code: i32, ) -> Option<HashSet<PhoneNumberType>>
Sourcepub fn trim_unwanted_end_chars<'a>(&self, phone_number: &'a str) -> &'a str
pub fn trim_unwanted_end_chars<'a>(&self, phone_number: &'a str) -> &'a str
Trims unwanted characters from the end of a phone number string.
This removes characters that are unlikely to be part of a phone number, such as trailing punctuation or whitespace.
§Arguments
phone_number- The phone number string to trim.
Sourcepub fn normalize_digits_only(&self, phone_number: &str) -> String
pub fn normalize_digits_only(&self, phone_number: &str) -> String
Normalizes a string of characters representing a phone number.
§Arguments
phone_number- The phone number string to normalize.
Sourcepub fn normalize_diallable_chars_only(&self, phone_number: &str) -> String
pub fn normalize_diallable_chars_only(&self, phone_number: &str) -> String
Normalizes a string of characters to only those that can be dialed from a keypad.
§Arguments
phone_number- The phone number string to normalize.
Sourcepub fn get_length_of_geographical_area_code(
&self,
phone_number: &PhoneNumber,
) -> usize
pub fn get_length_of_geographical_area_code( &self, phone_number: &PhoneNumber, ) -> usize
Sourcepub fn get_length_of_national_destination_code(
&self,
phone_number: &PhoneNumber,
) -> usize
pub fn get_length_of_national_destination_code( &self, phone_number: &PhoneNumber, ) -> usize
Sourcepub fn get_national_significant_number(
&self,
phone_number: &PhoneNumber,
) -> String
pub fn get_national_significant_number( &self, phone_number: &PhoneNumber, ) -> String
Sourcepub fn get_number_type(&self, phone_number: &PhoneNumber) -> PhoneNumberType
pub fn get_number_type(&self, phone_number: &PhoneNumber) -> PhoneNumberType
Sourcepub fn get_region_code_for_country_code(
&self,
country_code: i32,
) -> Option<&str>
pub fn get_region_code_for_country_code( &self, country_code: i32, ) -> Option<&str>
Gets the primary region code for a given country calling code.
Note: Some country codes are shared by multiple regions (e.g., +1 for USA, Canada). This returns the main region for that code (e.g., “US” for +1).
§Parameters
country_code: The country calling code.
§Returns
A string slice with the corresponding two-letter region code. Returns “ZZ” for invalid codes.
Sourcepub fn get_region_code_for_number(
&self,
phone_number: &PhoneNumber,
) -> Option<&str>
pub fn get_region_code_for_number( &self, phone_number: &PhoneNumber, ) -> Option<&str>
Sourcepub fn get_region_codes_for_country_code(
&self,
country_code: i32,
) -> Option<impl ExactSizeIterator<Item = &str>>
pub fn get_region_codes_for_country_code( &self, country_code: i32, ) -> Option<impl ExactSizeIterator<Item = &str>>
Sourcepub fn get_supported_regions(&self) -> impl ExactSizeIterator<Item = &str>
pub fn get_supported_regions(&self) -> impl ExactSizeIterator<Item = &str>
Gets an iterator over all supported two-letter region codes.
§Returns
An ExactSizeIterator that yields string slices of all supported region codes.
Sourcepub fn is_alpha_number(&self, number: impl AsRef<str>) -> bool
pub fn is_alpha_number(&self, number: impl AsRef<str>) -> bool
Sourcepub fn is_nanpa_country(&self, region_code: impl AsRef<str>) -> bool
pub fn is_nanpa_country(&self, region_code: impl AsRef<str>) -> bool
Sourcepub fn is_number_geographical(&self, phone_number: &PhoneNumber) -> bool
pub fn is_number_geographical(&self, phone_number: &PhoneNumber) -> bool
Sourcepub fn is_number_match(
&self,
first_number: &PhoneNumber,
second_number: &PhoneNumber,
) -> MatchType
pub fn is_number_match( &self, first_number: &PhoneNumber, second_number: &PhoneNumber, ) -> MatchType
Sourcepub fn is_possible_number(&self, phone_number: &PhoneNumber) -> bool
pub fn is_possible_number(&self, phone_number: &PhoneNumber) -> bool
Sourcepub fn is_possible_number_with_reason(
&self,
phone_number: &PhoneNumber,
) -> Result<NumberLengthType, ValidationError>
pub fn is_possible_number_with_reason( &self, phone_number: &PhoneNumber, ) -> Result<NumberLengthType, ValidationError>
Sourcepub fn is_valid_number(&self, phone_number: &PhoneNumber) -> bool
pub fn is_valid_number(&self, phone_number: &PhoneNumber) -> bool
Sourcepub fn is_valid_number_for_region(
&self,
phone_number: &PhoneNumber,
region: impl AsRef<str>,
) -> bool
pub fn is_valid_number_for_region( &self, phone_number: &PhoneNumber, region: impl AsRef<str>, ) -> bool
Sourcepub fn parse_and_keep_raw_input_with_default_region(
&self,
number_to_parse: impl AsRef<str>,
default_region: impl AsRef<str>,
) -> Result<PhoneNumber, ParseError>
pub fn parse_and_keep_raw_input_with_default_region( &self, number_to_parse: impl AsRef<str>, default_region: impl AsRef<str>, ) -> Result<PhoneNumber, ParseError>
Parses a string into a PhoneNumber, keeping the raw input string.
§Parameters
number_to_parse: The phone number string.default_region: The two-letter region code (ISO 3166-1) to use if the number is not in international format.
§Returns
A Result containing the parsed PhoneNumber on success, or a ParseError on failure.
Sourcepub fn parse_and_keep_raw_input(
&self,
number_to_parse: impl AsRef<str>,
) -> Result<PhoneNumber, ParseError>
pub fn parse_and_keep_raw_input( &self, number_to_parse: impl AsRef<str>, ) -> Result<PhoneNumber, ParseError>
Sourcepub fn parse_with_default_region(
&self,
number_to_parse: impl AsRef<str>,
default_region: impl AsRef<str>,
) -> Result<PhoneNumber, ParseError>
pub fn parse_with_default_region( &self, number_to_parse: impl AsRef<str>, default_region: impl AsRef<str>, ) -> Result<PhoneNumber, ParseError>
Parses a string into a PhoneNumber.
This is the primary method for converting a string representation of a number
into a structured PhoneNumber object.
§Parameters
number_to_parse: The phone number string.default_region: The two-letter region code (ISO 3166-1) to use if the number is not in international format.
§Returns
A Result containing the parsed PhoneNumber on success, or a ParseError on failure.