Phonelib
Phonelib is a comprehensive Rust library for handling phone numbers. It provides functions for validation, formatting, type detection, text extraction, batch processing, and much more.
Features
- โ Phone Number Validation - Check if phone numbers are valid
- ๐ Country Detection - Extract country information from phone numbers
- ๐ง Normalization - Clean and standardize phone number formats
- ๐จ Multiple Format Support - E.164, International, National, RFC3966
- ๐ฑ Type Detection - Identify mobile, landline, toll-free, premium numbers
- ๐ Text Extraction - Parse phone numbers from free-form text
- โ๏ธ Comparison & Equality - Compare numbers regardless of format with
PhoneNumberstruct - ๐ฒ Random Number Generation - Generate valid random phone numbers
- ๐ Batch Processing - Process multiple numbers efficiently
- ๐ Smart Suggestions - Get correction suggestions for invalid numbers
- ๐ Privacy Tools - Redact phone numbers in text
Installation
Add this to your Cargo.toml:
[]
= "0.2.1"
Quick Start
use *;
// Basic validation
let is_valid = is_valid_phone_number;
// Format a number
let formatted = format_phone_number;
// Detect number type
let number_type = detect_phone_number_type;
// Extract phone numbers from text
let text = "Call me at +12025550173 or +442079460958";
let numbers = extract_phone_numbers_from_text;
// Compare phone numbers (PhoneNumber struct with Eq trait)
let num1 = parse.unwrap;
let num2 = parse.unwrap;
assert_eq!; // Same number, different formats
API Reference
Core Types
Basic Functions
Phone Number Validation
use is_valid_phone_number;
let phone_number = "+1234567890".to_string;
if is_valid_phone_number
Country Extraction
use extract_country;
let phone_number = "+1234567890".to_string;
match extract_country
Phone Number Normalization
use ;
// Returns normalized number without modifying input
let normalized = normalize_phone_number;
println!; // Some("+1234567890")
// Modifies the input string in place
let mut phone = "+1 (234) 567-890".to_string;
normalize_phone_number_in_place;
println!;
Phone Number Formatting
use ;
let number = "1234567890".to_string;
// E.164 format
let e164 = format_phone_number;
// Result: Some("+1234567890")
// International format
let intl = format_phone_number;
// Result: Some("+1 234 567-890")
// National format
let national = format_phone_number;
// Result: Some("(234) 567-890")
// RFC3966 format
let rfc = format_phone_number;
// Result: Some("tel:+1-234-567-890")
Phone Number Type Detection
use ;
let mobile = "447123456789".to_string;
let landline = "442079460958".to_string;
let toll_free = "18001234567".to_string;
// Detect specific type
match detect_phone_number_type
// Quick type checks
if is_mobile_number
if is_landline_number
if is_toll_free_number
Random Phone Number Generation
use ;
// Generate a single random number
let random_us = generate_random_phone_number;
println!;
// Generate multiple random numbers
let random_numbers = generate_random_phone_numbers;
println!;
Phone Number Comparison
use ;
// Compare two numbers
let num1 = "+1234567890".to_string;
let num2 = "(234) 567-890".to_string;
if are_phone_numbers_equal
// Group equivalent numbers
let numbers = vec!;
let groups = group_equivalent_phone_numbers;
for in groups.iter.enumerate
Batch Processing
use ;
let numbers = vec!;
// Batch validation
let valid_results = validate_phone_numbers_batch;
println!;
// Batch normalization
let normalized_results = normalize_phone_numbers_batch;
println!;
// Batch type detection
let type_results = detect_phone_number_types_batch;
println!;
// Comprehensive batch analysis
let analyses = analyze_phone_numbers_batch;
for analysis in analyses
Smart Suggestions & Intelligence
use ;
// Get suggestions for invalid numbers
let invalid_number = "123456789".to_string;
let suggestions = suggest_phone_number_corrections;
println!;
// Check if a number might be valid with different formatting
let maybe_valid = "123-456-7890".to_string;
if is_potentially_valid_phone_number
// Guess country from number patterns
let mystery_number = "442079460958".to_string;
match guess_country_from_number
Text Extraction
Extract phone numbers from free-form text:
use ;
let text = "Contact us at +12025550173 or call our UK office at +442079460958";
// Extract all phone numbers
let numbers = extract_phone_numbers_from_text;
for num in &numbers
// Extract only valid numbers
let valid_numbers = extract_valid_phone_numbers_from_text;
// Extract with country hint for national numbers
let us_text = "Call (202) 555-0173 for assistance";
let numbers = extract_phone_numbers_with_country_hint;
// Count phone numbers
let count = count_phone_numbers_in_text;
println!;
// Replace phone numbers
let replaced = replace_phone_numbers_in_text;
// Redact for privacy (show last 4 digits)
let redacted = redact_phone_numbers;
println!; // "Contact us at ********0173 or..."
PhoneNumber Struct with Equality
The PhoneNumber struct provides type-safe phone number handling with proper equality comparison:
use ;
use HashSet;
// Parse phone numbers
let num1 = parse.unwrap;
let num2 = parse.unwrap;
let num3 = parse.unwrap;
// Equality comparison (same number, different formats)
assert_eq!;
assert_ne!;
// Use in HashSet for deduplication
let mut set = new;
set.insert;
set.insert; // Won't be added - duplicate
assert_eq!;
// PhoneNumber methods
println!;
println!;
println!;
println!;
println!;
// Parse with country hint for national numbers
let national = parse_with_country;
// PhoneNumberSet for efficient deduplication
let mut phone_set = new;
phone_set.add;
phone_set.add; // Duplicate - not added
phone_set.add;
assert_eq!;
// Check membership
assert!;
// Create from iterator
let numbers = vec!;
let set: PhoneNumberSet = numbers.into_iter.collect;
assert_eq!;
Country Support
The library supports 246 countries with accurate:
- Country codes and prefixes
- Valid phone number lengths
- Mobile vs. landline detection patterns
- Toll-free and premium number identification
Supported Countries Include:
- ๐บ๐ธ United States & Canada (NANP)
- ๐ฌ๐ง United Kingdom
- ๐ฉ๐ช Germany
- ๐ซ๐ท France
- ๐ฎ๐ณ India
- ๐ฆ๐บ Australia
- And 240+ more countries worldwide
Performance
The library is optimized for performance:
- โ Zero external dependencies
- โ Efficient string processing
- โ Batch processing capabilities
- โ In-place operations available
Benchmark results show excellent performance for validation and normalization operations.
Examples
Complete Example
use *;
Contributing
Contributions to the Phonelib library are welcome! Here's how you can help:
- ๐ Report bugs - Open an issue if you find any problems
- ๐ก Suggest features - Share ideas for new functionality
- ๐ง Submit pull requests - Help improve the code
- ๐ Improve documentation - Help make the docs better
- ๐งช Add tests - Increase test coverage
Development Setup
# Clone the repository
# Run tests
# Run benchmarks
# Check code formatting
# Run clippy for linting
Changelog
v0.2.1 (Latest)
๐ Text Extraction & Equality Release
- ๐ Text Extraction - Extract phone numbers from free-form text
extract_phone_numbers_from_text- Find all phone numbers in textextract_valid_phone_numbers_from_text- Find only valid numbersextract_phone_numbers_with_country_hint- Parse with default countryreplace_phone_numbers_in_text- Custom replacement functionredact_phone_numbers- Privacy-focused maskingcount_phone_numbers_in_text- Quick count
- โ๏ธ PhoneNumber Struct - Type-safe phone numbers with equality
- Implements
Eq,PartialEq,Hashfor use in collections - Implements
Display,FromStrfor easy conversion - Methods:
e164(),national_number(),format(),is_mobile(), etc.
- Implements
- ๐๏ธ PhoneNumberSet - Efficient deduplication collection
- ๐ Improved Documentation - Complete rustdoc coverage
- ๐ง Code Quality - All clippy warnings resolved
v0.2.0
๐ Major Feature Release
- โจ Phone Number Formatting - Multiple format support (E.164, International, National, RFC3966)
- ๐ฑ Type Detection - Identify mobile, landline, toll-free, premium numbers
- ๐ฒ Random Generation - Generate valid random phone numbers by country
- โ๏ธ Number Comparison - Compare numbers regardless of format
- ๐ Batch Processing - Process multiple numbers efficiently
- ๐ Smart Suggestions - Get correction suggestions for invalid numbers
- ๐ Comprehensive Analysis - Detailed phone number analysis
- ๐ Enhanced Country Support - Better patterns for major countries
v0.1.6
- ๐ Bug fixes and performance improvements
- ๐ Documentation updates
License
This project is licensed under the MIT License - see the LICENSE file for details.
Rusty Rails Project
Phonelib is part of the larger Rusty Rails project, which aims to bridge the gap between Rust and Ruby/Ruby on Rails ecosystems. We're actively working on recreating Ruby libraries in Rust to make working with Rust more easy and fun for new developers.
Related Projects
- ๐ More Rust libraries coming soon!
- ๐ Performance-focused Ruby alternatives
- ๐ฆ Easy-to-use APIs familiar to Ruby developers
Made with โค๏ธ by the Rusty Rails team