pub fn bytes<I, O, E1, E2, P>(parser: P) -> impl Parser<(I, usize), O, E2>where
E1: ParserError<I> + ErrorConvert<E2>,
E2: ParserError<(I, usize)>,
I: Stream<Token = u8> + Clone,
P: Parser<I, O, E1>,
Expand description
Convert a bits
stream back into a byte stream
Warning: A partial byte remaining in the input will be ignored and the given parser will start parsing at the next full byte.
use winnow::prelude::*;
use winnow::Bytes;
use winnow::binary::bits::{bits, bytes, take};
use winnow::combinator::rest;
use winnow::error::InputError;
type Stream<'i> = &'i Bytes;
fn stream(b: &[u8]) -> Stream<'_> {
Bytes::new(b)
}
fn parse(input: Stream<'_>) -> IResult<Stream<'_>, (u8, u8, &[u8])> {
bits::<_, _, InputError<(_, usize)>, _, _>((
take(4usize),
take(8usize),
bytes::<_, _, InputError<_>, _, _>(rest)
)).parse_peek(input)
}
let input = stream(&[0x12, 0x34, 0xff, 0xff]);
assert_eq!(parse(input), Ok(( stream(&[]), (0x01, 0x23, &[0xff, 0xff][..]) )));