Skip to content

Commit

Permalink
fix(binary): Clarify length_value as length_and_then
Browse files Browse the repository at this point in the history
Since this is like `Parser::and_then`, I went with that name, even if I
don't like it.
  • Loading branch information
epage committed Dec 11, 2023
1 parent c174d4a commit 2538d04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
24 changes: 19 additions & 5 deletions src/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2452,7 +2452,7 @@ where
/// 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.
/// `length_and_then` will return an error.
///
/// *Complete version*: Returns an error if there is not enough input data.
///
Expand All @@ -2469,7 +2469,7 @@ where
/// # use winnow::prelude::*;
/// use winnow::Bytes;
/// use winnow::binary::be_u16;
/// use winnow::binary::length_value;
/// use winnow::binary::length_and_then;
/// use winnow::token::tag;
///
/// type Stream<'i> = Partial<&'i Bytes>;
Expand All @@ -2485,14 +2485,14 @@ where
/// }
///
/// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
/// length_value(be_u16, "abc").parse_peek(s)
/// length_and_then(be_u16, "abc").parse_peek(s)
/// }
///
/// assert_eq!(parser(stream(b"\x00\x03abcefg")), Ok((stream(&b"efg"[..]), &b"abc"[..])));
/// assert_eq!(parser(stream(b"\x00\x03123123")), Err(ErrMode::Backtrack(InputError::new(complete_stream(&b"123"[..]), ErrorKind::Tag))));
/// assert_eq!(parser(stream(b"\x00\x03a")), Err(ErrMode::Incomplete(Needed::new(2))));
/// ```
pub fn length_value<I, O, N, E, F, G>(mut f: F, mut g: G) -> impl Parser<I, O, E>
pub fn length_and_then<I, O, N, E, F, G>(mut f: F, mut g: G) -> impl Parser<I, O, E>
where
I: StreamIsPartial,
I: Stream + UpdateSlice + Clone,
Expand All @@ -2501,7 +2501,7 @@ where
G: Parser<I, O, E>,
E: ParserError<I>,
{
trace("length_value", move |i: &mut I| {
trace("length_and_then", move |i: &mut I| {
let data = length_take(f.by_ref()).parse_next(i)?;
let mut data = I::update_slice(i.clone(), data);
let _ = data.complete();
Expand All @@ -2510,6 +2510,20 @@ where
})
}

/// Deprecated since 0.5.27, replaced with [`length_and_then`]
#[deprecated(since = "0.5.27", note = "Replaced with `length_and_then`")]
pub fn length_value<I, O, N, E, F, G>(f: F, g: G) -> impl Parser<I, O, E>
where
I: StreamIsPartial,
I: Stream + UpdateSlice + Clone,
N: ToUsize,
F: Parser<I, N, E>,
G: Parser<I, O, E>,
E: ParserError<I>,
{
length_and_then(f, g)
}

/// Gets a number from the first parser,
/// then applies the second parser that many times.
///
Expand Down
26 changes: 13 additions & 13 deletions src/binary/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,29 +1244,29 @@ mod partial {
}

#[test]
fn length_value_test() {
fn length_and_then_test() {
use crate::stream::StreamIsPartial;

fn length_value_1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
length_value(be_u8, be_u16).parse_peek(i)
fn length_and_then_1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
length_and_then(be_u8, be_u16).parse_peek(i)
}
fn length_value_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (u8, u8)> {
length_value(be_u8, (be_u8, be_u8)).parse_peek(i)
fn length_and_then_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (u8, u8)> {
length_and_then(be_u8, (be_u8, be_u8)).parse_peek(i)
}

let mut empty_complete = Partial::new(&b""[..]);
let _ = empty_complete.complete();

let i1 = [0, 5, 6];
assert_eq!(
length_value_1(Partial::new(&i1)),
length_and_then_1(Partial::new(&i1)),
Err(ErrMode::Backtrack(error_position!(
&empty_complete,
ErrorKind::Slice
)))
);
assert_eq!(
length_value_2(Partial::new(&i1)),
length_and_then_2(Partial::new(&i1)),
Err(ErrMode::Backtrack(error_position!(
&empty_complete,
ErrorKind::Token
Expand All @@ -1278,14 +1278,14 @@ mod partial {
let mut middle_complete = Partial::new(&i2[1..2]);
let _ = middle_complete.complete();
assert_eq!(
length_value_1(Partial::new(&i2)),
length_and_then_1(Partial::new(&i2)),
Err(ErrMode::Backtrack(error_position!(
&middle_complete,
ErrorKind::Slice
)))
);
assert_eq!(
length_value_2(Partial::new(&i2)),
length_and_then_2(Partial::new(&i2)),
Err(ErrMode::Backtrack(error_position!(
&empty_complete,
ErrorKind::Token
Expand All @@ -1295,21 +1295,21 @@ mod partial {

let i3 = [2, 5, 6, 3, 4, 5, 7];
assert_eq!(
length_value_1(Partial::new(&i3)),
length_and_then_1(Partial::new(&i3)),
Ok((Partial::new(&i3[3..]), 1286))
);
assert_eq!(
length_value_2(Partial::new(&i3)),
length_and_then_2(Partial::new(&i3)),
Ok((Partial::new(&i3[3..]), (5, 6)))
);

let i4 = [3, 5, 6, 3, 4, 5];
assert_eq!(
length_value_1(Partial::new(&i4)),
length_and_then_1(Partial::new(&i4)),
Ok((Partial::new(&i4[4..]), 1286))
);
assert_eq!(
length_value_2(Partial::new(&i4)),
length_and_then_2(Partial::new(&i4)),
Ok((Partial::new(&i4[4..]), (5, 6)))
);
}
Expand Down

0 comments on commit 2538d04

Please sign in to comment.