Function winnow::multi::length_value

source ·
pub fn length_value<I, O, N, E, F, G>(f: F, g: G) -> impl Parser<I, O, E>where
    I: StreamIsPartial + Stream + UpdateSlice,
    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.

Partial version: Will return Err(winnow::error::ErrMode::Incomplete(_)) if there is not enough data.

Arguments

  • f The parser to apply.
  • g The parser to apply on the subslice.

Example

use winnow::Bytes;
use winnow::number::be_u16;
use winnow::multi::length_value;
use winnow::bytes::tag;

type Stream<'i> = Partial<&'i Bytes>;

fn stream(b: &[u8]) -> Stream<'_> {
    Partial::new(Bytes::new(b))
}

fn complete_stream(b: &[u8]) -> Stream<'_> {
    let mut p = Partial::new(Bytes::new(b));
    let _ = p.complete();
    p
}

fn parser(s: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
  length_value(be_u16, "abc").parse_next(s)
}

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