Skip to content

Conversation

@thvdveld
Copy link
Collaborator

@thvdveld thvdveld commented Oct 9, 2025

Fixes the parsing of phone numbers with a leading 0 after the country code, such as "+330631966543".

The parsing does not fail when doing:
parse(Some(country::FR), "+330631966543")

However, it does fail when the country code is not passed to it:
parse(None, "+330631966543")

When the country code is not passed to it, then the following block is not executed (which removes the national prefix when present):

    // Extract carrier and strip national prefix if present.
    if let Some(meta) = country.and_then(|c| database.by_id(c.as_ref())) {
        let mut potential = helper::national_number(meta, number.clone());


        // Strip national prefix if present.
        if let Some(prefix) = meta.national_prefix.as_ref() {
            if potential.national.starts_with(prefix) {
                potential.national = helper::trim(potential.national, prefix.len());
            }
        }


        if validator::length(meta, &potential, Type::Unknown) != Validation::TooShort {
            number = potential;
        }
    }

When parsing the number, we should find out what the country code is and then strip the national prefix, which is what this PR does. However, I'm not satisfied with how it turns out since detecting the country code is now duplicated. Normally, getting the country code is done with the Country helper struct that takes a &PhoneNumber.

This PR also adds a test case for the formatting of numbers with a national prefix.

Fixes #81

@thvdveld thvdveld changed the title Add more test cases Fix number with leading 0 after country code Oct 9, 2025
@codecov
Copy link

codecov bot commented Oct 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.63%. Comparing base (1fd3d17) to head (65499ea).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #89      +/-   ##
==========================================
+ Coverage   67.81%   68.63%   +0.81%     
==========================================
  Files          18       18              
  Lines        2172     2216      +44     
==========================================
+ Hits         1473     1521      +48     
+ Misses        699      695       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@rubdos rubdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about: you make the country binding mutable, and you try to eagerly assign it (with the block you just wrote), and you do the same with the national binding, resetting the country if your guess was wrong?

// Normalize the number and extract country code.
number = helper::country_code(database, country, number)?;

let country = if country.is_none() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let country = if country.is_none() {
let country = country.or_else(|| {

@thvdveld thvdveld closed this Oct 9, 2025
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

Successfully merging this pull request may close these issues.

Phone numbers with one zero after the country code are considered invalid

2 participants