Skip to content

Commit

Permalink
fail on too short numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Laux committed Nov 2, 2023
1 parent 92bcc56 commit 5364d2d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser/parse_from_text/base_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum CustomError<I> {
UnexpectedContent,
PrecedingWhitespaceMissing,
OptionIsUnexpectedNone,
PhoneNumberNotEnoughDigits,
UnxepectedError(String),
}

Expand Down
12 changes: 12 additions & 0 deletions src/parser/parse_from_text/phone_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use nom::{combinator::recognize, IResult};
const MAX_COUNTRY_LEN: usize = 3;

Check failure on line 12 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest, 1.64.0)

constant `MAX_COUNTRY_LEN` is never used

Check warning on line 12 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / clippy

constant `MAX_COUNTRY_LEN` is never used

warning: constant `MAX_COUNTRY_LEN` is never used --> src/parser/parse_from_text/phone_numbers.rs:12:7 | 12 | const MAX_COUNTRY_LEN: usize = 3; | ^^^^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
const MAX_AREA_LEN: usize = 10; // TODO find real number?

Check failure on line 13 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest, 1.64.0)

constant `MAX_AREA_LEN` is never used

Check warning on line 13 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / clippy

constant `MAX_AREA_LEN` is never used

warning: constant `MAX_AREA_LEN` is never used --> src/parser/parse_from_text/phone_numbers.rs:13:7 | 13 | const MAX_AREA_LEN: usize = 10; // TODO find real number? | ^^^^^^^^^^^^
const MAX_LOCAL_LEN: usize = 15; // TODO find real number?

Check failure on line 14 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest, 1.64.0)

constant `MAX_LOCAL_LEN` is never used

Check warning on line 14 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / clippy

constant `MAX_LOCAL_LEN` is never used

warning: constant `MAX_LOCAL_LEN` is never used --> src/parser/parse_from_text/phone_numbers.rs:14:7 | 14 | const MAX_LOCAL_LEN: usize = 15; // TODO find real number? | ^^^^^^^^^^^^^
const PHONE_NUMBER_MINIMUM_DIGITS: usize = 5;

Check failure on line 15 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest, 1.64.0)

constant `PHONE_NUMBER_MINIMUM_DIGITS` is never used

Check warning on line 15 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / clippy

constant `PHONE_NUMBER_MINIMUM_DIGITS` is never used

warning: constant `PHONE_NUMBER_MINIMUM_DIGITS` is never used --> src/parser/parse_from_text/phone_numbers.rs:15:7 | 15 | const PHONE_NUMBER_MINIMUM_DIGITS: usize = 5; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^

/// spaces, dots, or dashes
fn is_sdd(input: char) -> bool {

Check failure on line 18 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest, 1.64.0)

function `is_sdd` is never used

Check warning on line 18 in src/parser/parse_from_text/phone_numbers.rs

View workflow job for this annotation

GitHub Actions / clippy

function `is_sdd` is never used

warning: function `is_sdd` is never used --> src/parser/parse_from_text/phone_numbers.rs:18:4 | 18 | fn is_sdd(input: char) -> bool { | ^^^^^^
Expand All @@ -31,6 +32,10 @@ fn eat_while_digit_or_sdd_but_spare_last_digit(
) -> IResult<&str, &str, CustomError<&str>> {
let (_, result) = take_while_m_n(1, MAX_LOCAL_LEN, is_digit_or_ssd)(input)?;

if result.chars().filter(|c| is_digit(*c)).count() < PHONE_NUMBER_MINIMUM_DIGITS {
return Err(nom::Err::Error(CustomError::PhoneNumberNotEnoughDigits));
}

for (offset, char) in result.chars().rev().enumerate() {
// find index of last digit
if is_digit(char.as_char()) {
Expand Down Expand Up @@ -132,4 +137,11 @@ mod test {
)
}
}

#[test]
fn test_not_enough_digits(){
telephone_number("(0)152 28").expect_err("fails because number is to short");
telephone_number("152 28").expect_err("fails because too short");
telephone_number("(152) 28").expect_err("fails because too short");
}
}

0 comments on commit 5364d2d

Please sign in to comment.