Skip to content

Commit

Permalink
Improve the floating point implementation to not depend on the ParseT…
Browse files Browse the repository at this point in the history
…o trait
  • Loading branch information
marcdejonge committed Jan 31, 2025
1 parent 35be971 commit c5bae99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 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.1"
version = "0.3.2"
license = "MIT/Apache-2.0"
keywords = ["nom", "parser", "parsable"]
categories = ["parsing"]
Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,32 @@ macro_rules! signed_parsable {
signed_parsable!(i8 i16 i32 i64 i128);

macro_rules! floating_parsable {
($($ty:ty => $fn:tt),+) => {
($($ty:tt)+) => {
$(
impl<I, E: error::ParseError<I>> ParseFrom<I, E> for $ty
where
I: Input + Offset + AsBytes + ParseTo<$ty> + Compare<&'static str>,
I: Input + Offset + AsBytes + 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)
use std::str::FromStr;
use nom::number::complete::recognize_float_or_exceptions;
use std::str::from_utf8;

let (i, s) = recognize_float_or_exceptions(input)?;
match from_utf8(s.as_bytes()).ok().and_then(|s| $ty::from_str(s).ok()) {
Some(f) => Ok((i, f)),
None => Err(nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::Float))),
}
}
}
)*
}
}

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

/// 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
Expand Down

0 comments on commit c5bae99

Please sign in to comment.