Skip to content

Commit

Permalink
Add support for floating point numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdejonge committed Jan 29, 2025
1 parent 97e34be commit ac048e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "nom-parse-trait"
description = "Adding traits to make parsing types easier"
version = "0.3.0"
version = "0.3.1"
license = "MIT/Apache-2.0"
keywords = ["nom", "parser", "parsable"]
categories = ["parsing"]
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ normal text parsing (e.g. the string "123" will be parsed to the number 123).
- `u32`
- `u64`
- `u128`
- `f32`
- `f64`

Also, the `bool` type has a default implementation, where it will parse the string
"true" to `true` and "false" to `false`.

### `u8` and `char`

The `u8` and `char` types have a default implementation, where it will take a single
byte or character from the input.

### `Vec<T>`
A default implementation for Vec<T> has been provided, as long as T implements
`ParseFrom`, where it uses the `nom::character::complete::line_ending` parser
Expand Down
40 changes: 38 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ macro_rules! unsigned_parsable {
I: Input,
<I as Input>::Item: AsChar,
{
fn parse(input: I) -> nom::IResult<I, $ty, E> {
fn parse(input: I) -> nom::IResult<I, Self, E> {
nom::character::complete::$ty(input)
}
}
Expand All @@ -97,7 +97,7 @@ macro_rules! signed_parsable {
<I as Input>::Item: AsChar,
I: for <'a> Compare<&'a[u8]>,
{
fn parse(input: I) -> nom::IResult<I, $ty, E> {
fn parse(input: I) -> nom::IResult<I, Self, E> {
nom::character::complete::$ty(input)
}
}
Expand All @@ -107,6 +107,26 @@ macro_rules! signed_parsable {

signed_parsable!(i8 i16 i32 i64 i128);

macro_rules! floating_parsable {
($($ty:ty => $fn:tt),+) => {
$(
impl<I, E: error::ParseError<I>> ParseFrom<I, E> for $ty
where
I: Input + Offset + AsBytes + ParseTo<$ty> + Compare<&'static str>,
<I as Input>::Item: AsChar,
<I as Input>::Iter: Clone,
I: for<'a> Compare<&'a [u8]>,
{
fn parse(input: I) -> nom::IResult<I, Self, E> {
nom::number::complete::$fn(input)
}
}
)*
}
}

floating_parsable!(f32 => float, f64 => double);

/// Support reading the words "true" or "false" from the input and interpreting them as boolean values.
impl<I, E: error::ParseError<I>> ParseFrom<I, E> for bool
where
Expand Down Expand Up @@ -247,6 +267,22 @@ mod tests {
test_unsigned!(u16 u32 u64 u128);
test_unsigned!(i16 i32 i64 i128);

mod floats {
use crate::*;

#[test]
fn parse_f32() {
assert_eq!(Ok::<_, ()>(6e8), f32::parse_complete("6e8"));
assert_eq!(Ok::<_, ()>(3.14e-2), f32::parse_complete(b"3.14e-2".as_ref()));
}

#[test]
fn parse_f64() {
assert_eq!(Ok::<_, ()>(6e8), f64::parse_complete("6e8"));
assert_eq!(Ok::<_, ()>(3.14e-2), f64::parse_complete(b"3.14e-2".as_ref()));
}
}

mod char {
use crate::*;
use nom::error::*;
Expand Down

0 comments on commit ac048e6

Please sign in to comment.