Skip to content

Conversation

@rubdos
Copy link
Member

@rubdos rubdos commented Oct 13, 2025

Follow-up from #92, since now @thvdveld has push access. Makes things a bit easier to collaborate on :-)


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

@codecov
Copy link

codecov bot commented Oct 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.69%. Comparing base (1fd3d17) to head (e026c5e).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #93      +/-   ##
==========================================
- Coverage   67.81%   67.69%   -0.13%     
==========================================
  Files          18       18              
  Lines        2172     2139      -33     
==========================================
- Hits         1473     1448      -25     
+ Misses        699      691       -8     

☔ 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.

@thvdveld
Copy link
Collaborator

Italian leading zero is still broken, but to fix this we need to rewrite other parts of the implementation. I think it should be fine to merge this already fixing #81.

@rubdos rubdos merged commit 1d11bec into main Oct 15, 2025
9 checks passed
@rubdos rubdos deleted the fix/parse-national-prefix-with-zero branch October 15, 2025 12:21
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

3 participants