Rlibphonenumber
A Rust port of Google's comprehensive library for parsing, formatting, and validating international phone numbers.
Built on base libphonenumber 9.0.8 Used metadata version: 9.0.23
Overview
This library is a new adaptation of Google's libphonenumber for Rust. Its primary goal is to provide a powerful and efficient tool for handling phone numbers, with a structure that is intuitively close to the original C++ version.
You might be aware of an existing Rust implementation of libphonenumber. However, its maintenance has slowed, and I believe that a fresh start is the best path forward. This project aims to deliver a more direct and familiar port for developers acquainted with the C++ or Java versions of the original library.
This library gives you access to a wide range of functionalities, including:
- Parsing and formatting phone numbers.
- Validating phone numbers for all regions of the world.
- Determining the number type (e.g., Mobile, Fixed-line, Toll-free).
- Providing example numbers for every country.
Performance
The following benchmarks were run against the rust-phonenumber crate. All tests were performed on the same machine and dataset. Lower is better.
Formatting
| Format | rlibphonenumber (this crate) | rust-phonenumber | Performance Gain |
|---|---|---|---|
| E164 | ~668 ns | ~12.82 µs | ~19x faster |
| International | ~11.76 µs | ~17.20 µs | ~1.5x faster |
| National | ~15.19 µs | ~22.66 µs | ~1.5x faster |
| RFC3966 | ~13.41 µs | ~18.59 µs | ~1.4x faster |
Parsing
| Task | rlibphonenumber (this crate) | rust-phonenumber | Performance Gain |
|---|---|---|---|
| Parse | ~11.60 µs | ~13.45 µs | ~16% faster |
Installation
Add rlibphonenumber to your Cargo.toml:
[]
= "0.2.1" # Please use the latest version from crates.io
Getting Started: A Detailed Example
Using the library is straightforward. The PhoneNumberUtil struct is the main entry point for all operations. For convenience, a thread-safe static instance, PHONE_NUMBER_UTIL, is provided.
Here is a detailed example that demonstrates how to parse a number, validate it, and format it in several standard ways.
use ;
Expected Output:
✅ Successfully parsed number.
- Original input: '+1-587-530-2271' (in 'US')
- Country Code: 1
- National Number: 5875302271
Is the number valid? Yes
Formatted Outputs:
- International: +1 587-530-2271
- National: (587) 530-2271
- E.164: +15875302271
- RFC3966: tel:+1-587-530-2271
Additional Information:
- Number Type: FixedLineOrMobile
- Number Region: CA
Project Status
The library is under active development. The core PhoneNumberUtil is fully implemented and passes the original library's test suite.
The project roadmap includes porting these additional components:
AsYouTypeFormatter: For formatting phone numbers as a user types.PhoneNumberOfflineGeocoder: To provide geographical location information for a number.PhoneNumberToCarrierMapper: To identify the carrier associated with a number.
Contributing
Contributions are highly welcome! Whether you are fixing a bug, improving documentation, or helping to port a new module, your help is appreciated.
Code Generation
To maintain consistency with the original library, this project uses pre-compiled metadata. If you need to regenerate the metadata, for instance, after updating the PhoneNumberMetadata.xml file, you can use the provided tools.
The tools directory contains a rewritten Rust-based code generator for the C++ pre-compiled metadata.
To run the code generation process, execute the following script:
This script will:
- Build the Java-based tool that converts the XML metadata to a Rust-compatible format.
- Run the generator for the main metadata and the test metadata.
- Place the generated
.rsfiles into thesrc/generated/metadatadirectory.
You can skip the Java build step by passing the --skip-install flag, which is useful if no changes were made to the generator itself.
NEW
You can add --tag flag, this will clone original project's metadata and replace resources dir
License
This project is licensed under the Apache License, Version 2.0. Please see the LICENSE file for details.