Expand description
§Phonelib
A dependency-free Rust library for validating, parsing, formatting and manipulating international phone numbers.
§Features
- Validation - length- and (for the NANP) range-based validation
- Country detection - deterministic resolution of the calling code
- Normalization - canonical E.164 output
- Multiple formats - E.164, international, national, RFC 3966
- Type detection - mobile, landline, toll-free, premium, and more
- Text extraction - find phone numbers in free-form text
- Comparison - compare numbers regardless of formatting
- Batch processing - process many numbers in one call
§Quick start
use phonelib::*;
assert!(is_valid_phone_number("+12025550173"));
assert_eq!(
normalize_phone_number("+1 (202) 555-0173"),
Some("+12025550173".to_string())
);
let text = "Call me at +12025550173 or +442079460958";
assert_eq!(extract_phone_numbers_from_text(text).len(), 2);
let a = PhoneNumber::parse("+1 202-555-0173").unwrap();
let b = PhoneNumber::parse("12025550173").unwrap();
assert_eq!(a, b);§Dependencies
The crate has none, and no feature flags. Everything it needs is core and
alloc by way of std: the country and area-code tables are const data
built at compile time, so there is no runtime initialization and no global
state either.
§Accuracy and scope
Validation is driven by the country table in this crate: the country
calling code must be known and the national significant number must have a
length that country accepts. For numbers in the North American Numbering
Plan the area code and central office code are additionally checked against
the assigned NANP ranges, which is what makes US / CA / Caribbean
territory attribution exact.
A number must say which country it belongs to. Give it a leading +,
an 00 international access code, or a country via
PhoneNumber::parse_with_country. A bare national number is rejected with
ParseError::MissingCallingCode rather than guessed at, because its
leading digits usually match some country’s calling code —
(202) 555-0173 is a Washington DC number, and also a well-formed Egyptian
+20 2555 0173. The exception is calling code 1, where the area code is
checked against the numbering plan, so 12025550173 resolves without a +.
Outside the NANP the crate does not carry per-operator prefix ranges, so a number with a plausible length is accepted even if its prefix is not in service. If you need full carrier-grade validation for every country, use a library backed by the complete libphonenumber metadata.
detect_phone_number_type is likewise metadata-driven. Countries without
a published range table in this crate return PhoneNumberType::Unknown
rather than a guess. Range tables are carried for the North American
Numbering Plan and for GB, IE, DE, FR, IT, ES, NL, BE, CH, AT, SE, NO, PL,
PT, RU, KZ, TR, IN, CN, JP, KR, AU, NZ, BR, ZA and NG.
Structs§
- Country
- A country or territory in the international numbering plan.
- Extracted
Phone Number - Result of extracting a phone number from text
- Phone
Number - A parsed and validated phone number.
- Phone
Number Analysis - Detailed analysis result for a phone number.
- Phone
Number Set - A set of phone numbers, deduplicated by their E.164 form.
Enums§
- Parse
Error - Why a phone number could not be parsed.
- Phone
Format - Phone number format options.
- Phone
Number Type - The service class of a phone number.
Functions§
- analyze_
phone_ numbers_ batch - Comprehensive batch analysis of phone numbers.
- are_
phone_ numbers_ equal - Check if two phone numbers are equivalent, ignoring formatting.
- count_
phone_ numbers_ in_ text - Count how many phone numbers are in the text.
- countries
- Every country and territory the crate knows about.
- countries_
by_ calling_ code - Every country that uses a given E.164 calling code, in resolution order.
- countries_
by_ code - Every table entry for an ISO 3166-1 alpha-2 code.
- country_
by_ code - Look up a country by its ISO 3166-1 alpha-2 code.
- detect_
phone_ number_ type - Detect the service class of a phone number.
- detect_
phone_ number_ types_ batch - Detect phone number types for multiple numbers at once.
- extract_
countries - Every country whose numbering plan could account for this number.
- extract_
countries_ batch - Extract countries for multiple phone numbers at once.
- extract_
country - Extracts country information from a phone number.
- extract_
phone_ numbers_ from_ text - Extract all phone numbers from free-form text.
- extract_
phone_ numbers_ with_ country_ hint - Extract phone numbers from text, assuming a default country.
- extract_
valid_ phone_ numbers_ from_ text - Extract only the valid phone numbers from text.
- format_
phone_ number - Format a phone number according to the specified format.
- generate_
random_ phone_ number - Generate a random valid phone number for a specific country.
- generate_
random_ phone_ numbers - Generate several random valid phone numbers for a country.
- group_
equivalent_ phone_ numbers - Group phone numbers by equivalence, preserving input order.
- guess_
country_ from_ number - Get the most likely country for a phone number.
- is_
landline_ number - Check if a phone number is a landline number.
- is_
mobile_ number - Check if a phone number is a mobile number.
- is_
potentially_ valid_ phone_ number - Check if a phone number could plausibly become valid with reformatting.
- is_
toll_ free_ number - Check if a phone number is a toll-free number.
- is_
valid_ phone_ number - Validates whether a phone number is valid.
- normalize_
phone_ number - Normalizes a phone number to E.164 format.
- normalize_
phone_ number_ in_ place - Normalizes a phone number in place to E.164 format.
- normalize_
phone_ numbers_ batch - Normalize multiple phone numbers at once.
- redact_
phone_ numbers - Redact phone numbers in text for privacy.
- replace_
phone_ numbers_ in_ text - Replace phone numbers in text with a transformed version.
- suggest_
phone_ number_ corrections - Suggest corrections for an invalid phone number.
- try_
normalize_ phone_ number - Like
normalize_phone_numberbut reports why the number was rejected. - validate_
phone_ numbers_ batch - Validate multiple phone numbers at once.