diff --git a/src/parser/parse_from_text/base_parsers.rs b/src/parser/parse_from_text/base_parsers.rs index 9881d36..de249e6 100644 --- a/src/parser/parse_from_text/base_parsers.rs +++ b/src/parser/parse_from_text/base_parsers.rs @@ -19,6 +19,7 @@ pub enum CustomError { UnexpectedContent, PrecedingWhitespaceMissing, OptionIsUnexpectedNone, + PhoneNumberNotEnoughDigits, UnxepectedError(String), } diff --git a/src/parser/parse_from_text/phone_numbers.rs b/src/parser/parse_from_text/phone_numbers.rs index 07480b1..ed72acf 100644 --- a/src/parser/parse_from_text/phone_numbers.rs +++ b/src/parser/parse_from_text/phone_numbers.rs @@ -12,6 +12,7 @@ use nom::{combinator::recognize, IResult}; const MAX_COUNTRY_LEN: usize = 3; const MAX_AREA_LEN: usize = 10; // TODO find real number? const MAX_LOCAL_LEN: usize = 15; // TODO find real number? +const PHONE_NUMBER_MINIMUM_DIGITS: usize = 5; /// spaces, dots, or dashes fn is_sdd(input: char) -> bool { @@ -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()) { @@ -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"); + } }