whisperfish / rust-phonenumber

Library for parsing, formatting and validating international phone numbers.
Apache License 2.0
162 stars 55 forks source link

Output of PhoneNumber format using International doesn't parse in PhoneNumber::from_str (i.e. roundtrip fails) #46

Open vdods opened 2 years ago

vdods commented 2 years ago

The phone number "+1 800 723 3456" parses fine as expected. Then the formatted output is "+1 800-723-3456", but this fails to parse. Though "+1-800-723-3456" successfully parses.

I'd suggest being more forgiving in PhoneNumber::from_str and just entirely ignoring spaces and hyphens, and potentially other characters. At the very least, make it so PhoneNumber::from_str can successfully parse its own formatted output.

Example code:

#[test]
fn test() {
    use phonenumber::{Mode, PhoneNumber};
    let original_phone_number_string = String::from("+1 800 723 3456");
    println!("original_phone_number_string: {:?}", original_phone_number_string);
    let parsed_phone_number = PhoneNumber::from_str(&original_phone_number_string).unwrap();
    println!("parsed_phone_number: {:?}", parsed_phone_number);
    let formatted_phone_number_string = parsed_phone_number.format().mode(Mode::International).to_string();
    println!("formatted_phone_number_string: {:?}", formatted_phone_number_string);
    // This call fails, and we panic on the unwrap for dramatic purposes.
    let reparsed_phone_number = PhoneNumber::from_str(&formatted_phone_number_string).unwrap();
    println!("reparsed_phone_number: {:?}", reparsed_phone_number);
}

Is there some documentation somewhere indicating what formats are accepted by PhoneNumber::from_str? I poked around the google libphonenumber repo a bit but didn't see anything obvious either.