Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vdods opened this issue Mar 31, 2022 · 0 comments

Comments

@vdods
Copy link

vdods commented Mar 31, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant