4

There is a function float_s that parses floats in stream mod (can return Incomplete). I want to use CompleteStr as input type instead. How I can achieve that?

Simple approach fails with complains about &str and CompleteStr mismatches:

named!(parse_float_complete(CompleteStr) -> f32,
    ws!(::num::float_s)
);

I'm using nom 4.0.0.

magras
  • 1,709
  • 21
  • 32

3 Answers3

1

nom v4.1.0 fixed this problem:

  • float and double now work on all of nom's input types (&[u8], &str, CompleteByteSlice, CompleteStr and any type that implements the required traits). float_s and double_s got the same modification, but are now deprecated
magras
  • 1,709
  • 21
  • 32
0

float_s expects a string, so you have to extract the string from the CompleteStr:

named!(parse_float_complete(CompleteStr) -> f32,
    ws!(call!(|input| ::num::float_s(input.0).map(|output, result| CompleteStr(output, result))))
);
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
  • It should be `call!(|input: CompleteStr| ::nom::float_s(input.0).map(|(output, result)| (CompleteStr(output), result)))`, but you need to map `Err` part too, so it's very unpractical solution. – magras Jul 18 '18 at 10:58
  • And I understand that I can unwrap-wrap input and output. I'm just hoping that there is some less cumbersome solution. – magras Jul 18 '18 at 11:00
0

My current workaround is to just copy-paste float_s implementation:

fn float_cs(input: CompleteStr) -> ::nom::IResult<CompleteStr, f32> {
  flat_map!(input, call!(::nom::recognize_float), parse_to!(f32))
}
magras
  • 1,709
  • 21
  • 32