Function nom8::multi::length_value

source ·
pub fn length_value<I, O, N, E, F, G, const STREAMING: bool>(
    f: F,
    g: G
) -> impl FnMut(I) -> IResult<I, O, E>where
    I: InputLength + InputTake + InputIter + IntoOutput + InputIsStreaming<STREAMING> + Clone,
    N: ToUsize,
    F: Parser<I, N, E>,
    G: Parser<I, O, E>,
    E: ParseError<I>,
Expand description

Gets a number from the first parser, takes a subslice of the input of that size, then applies the second parser on that subslice. If the second parser returns Incomplete, length_value will return an error.

Complete version: Returns an error if there is not enough input data.

Streaming version: Will return Err(nom8::Err::Incomplete(_)) if there is not enough data.

Arguments

  • f The parser to apply.
  • g The parser to apply on the subslice.
use nom8::number::be_u16;
use nom8::multi::length_value;
use nom8::bytes::tag;

fn parser(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
  length_value(be_u16, tag("abc"))(s)
}

assert_eq!(parser(Streaming(b"\x00\x03abcefg")), Ok((Streaming(&b"efg"[..]), &b"abc"[..])));
assert_eq!(parser(Streaming(b"\x00\x03123123")), Err(Err::Error(Error::new(Streaming(&b"123"[..]), ErrorKind::Tag))));
assert_eq!(parser(Streaming(b"\x00\x03a")), Err(Err::Incomplete(Needed::new(2))));