rustmax::nom::number

Function f32

Source
pub fn f32<I, E>(endian: Endianness) -> impl Parser<I, Output = f32, Error = E>
where E: ParseError<I>, I: Input<Item = u8>,
Expand description

Recognizes a 4 byte floating point number

If the parameter is nom::number::Endianness::Big, parse a big endian f32 float, otherwise if nom::number::Endianness::Little parse a little endian f32 float. Streaming version: Will return Err(nom::Err::Incomplete(_)) if there is not enough data.

use nom::number::f32;

let be_f32 = |s| {
  f32::<_, (_, ErrorKind)>(nom::number::Endianness::Big).parse(s)
};

assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert_eq!(be_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));

let le_f32 = |s| {
  f32::<_, (_, ErrorKind)>(nom::number::Endianness::Little).parse(s)
};

assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
assert_eq!(le_f32(&b"abc"[..]), Err(Err::Incomplete(Needed::new(1))));