Function length_and_then

fn length_and_then<Input, Output, Count, Error, CountParser, ParseNext>(count: CountParser, parser: ParseNext) -> impl Parser<Input, Output, Error>
where
    Input: StreamIsPartial + Stream + UpdateSlice + Clone,
    Count: ToUsize,
    CountParser: Parser<Input, Count, Error>,
    ParseNext: Parser<Input, Output, Error>,
    Error: ParserError<Input>

Parse a length-prefixed slice (TLV)

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

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

Example

# use winnow::{error::ErrMode, error::InputError, error::Needed, stream::{Partial, StreamIsPartial}};
# use winnow::prelude::*;
use winnow::Bytes;
use winnow::binary::be_u16;
use winnow::binary::length_and_then;

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<'i>(s: &mut Stream<'i>) -> ModalResult<&'i [u8]> {
  length_and_then(be_u16, "abc").parse_next(s)
}

assert_eq!(parser.parse_peek(stream(b"\x00\x03abcefg")), Ok((stream(&b"efg"[..]), &b"abc"[..])));
assert!(parser.parse_peek(stream(b"\x00\x03123123")).is_err());
assert_eq!(parser.parse_peek(stream(b"\x00\x03a")), Err(ErrMode::Incomplete(Needed::new(2))));