Phonelib
Phonelib is a comprehensive Rust library for handling phone numbers. It provides functions for validation, formatting, type detection, 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
- ๐ฒ Random Number Generation - Generate valid random phone numbers
- โ๏ธ Number Comparison - Compare numbers regardless of format
- ๐ Batch Processing - Process multiple numbers efficiently
- ๐ Smart Suggestions - Get correction suggestions for invalid numbers
Installation
Add this to your Cargo.toml
:
[]
= "0.2.0"
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;
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 = "447123456789".to_string;
match guess_country_from_number
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.0 (Latest)
๐ 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