#phonenumber #phone #number #parser #formatter

phonenumber

Library for parsing, formatting and validating international phone numbers

7 releases

0.3.1+8.12.9 Oct 12, 2020
0.3.0+8.12.9 Sep 9, 2020
0.2.4+8.11.3 Feb 7, 2020
0.2.3+8.10.6 Mar 6, 2019
0.1.0+8.7.0 Jul 31, 2017

#57 in Value formatting

Download history 7143/week @ 2022-12-02 8098/week @ 2022-12-09 6883/week @ 2022-12-16 4852/week @ 2022-12-23 3996/week @ 2022-12-30 5919/week @ 2023-01-06 7253/week @ 2023-01-13 6649/week @ 2023-01-20 7900/week @ 2023-01-27 8807/week @ 2023-02-03 8077/week @ 2023-02-10 8946/week @ 2023-02-17 9135/week @ 2023-02-24 8148/week @ 2023-03-03 7185/week @ 2023-03-10 7451/week @ 2023-03-17

33,725 downloads per month
Used in 14 crates (12 directly)

Apache-2.0

2.5MB
3.5K SLoC

phonenumber Crates.io phonenumber License Build Status

Rust version of libphonenumber by Google.

Usage

Add this to your Cargo.toml:

[dependencies]
phonenumber = "0.1"

Example

The following example parses, validates and formats the given phone number.

extern crate phonenumber;

use phonenumber::Mode;
use std::env;

fn main() {
	let mut args = env::args().skip(1).collect::<Vec<_>>();

	if args.len() < 1 {
		panic!("not enough arguments");
	}

	let number  = args.pop().unwrap();
	let country = args.pop().map(|c| c.parse().unwrap());

	let number = phonenumber::parse(country, number).unwrap();
	let valid  = phonenumber::is_valid(&number);

	if valid {
		println!("\x1b[32m{:#?}\x1b[0m", number);
		println!();
		println!("International: {}", number.format().mode(Mode::International));
		println!("     National: {}", number.format().mode(Mode::National));
		println!("      RFC3966: {}", number.format().mode(Mode::Rfc3966));
		println!("        E.164: {}", number.format().mode(Mode::E164));
	}
	else {
		println!("\x1b[31m{:#?}\x1b[0m", number);
	}
}

Dependencies

~5MB
~113K SLoC