Skip to content

Commit

Permalink
num2words: catch NAN case
Browse files Browse the repository at this point in the history
NAN string is parsed by the BigFloat package successfully (as it is when
parsed into any number type in Rust). This means the creation of the
Num2Words object succeeds and tries to parse that number. However, this
cause the call .to_u64().unwrap() to crash with the following error:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/asp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/num2words-1.0.0/src/lang/en.rs:78:53

Closes #12
  • Loading branch information
Ballasi committed Nov 20, 2023
1 parent d77bb85 commit a109dbc
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/num2words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ impl Num2Words {
/// ```
pub fn parse(num: &str) -> Option<Self> {
let num = BigFloat::parse(num)?;
if num.is_nan() {
return None;
}
Some(Self {
num,
lang: Lang::English,
Expand Down Expand Up @@ -320,5 +323,9 @@ mod tests {
Some(_) => assert!(false),
None => assert!(true),
}
match Num2Words::parse("NAN") {
Some(_) => assert!(false),

This comment has been minimized.

Copy link
@Ballasi

Ballasi Nov 21, 2023

Author Owner

I am honestly not fond of these assert(true/false);, now might be a good time to transfer that into assert!(num.is_none());

None => assert!(true),
}
}
}

0 comments on commit a109dbc

Please sign in to comment.