Skip to content

Commit

Permalink
Merge pull request #703 from epage/iresult
Browse files Browse the repository at this point in the history
fix: Deprecate IResult
  • Loading branch information
epage authored Jan 22, 2025
2 parents 8912c26 + 30f464b commit 22862c5
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 45 deletions.
36 changes: 22 additions & 14 deletions src/binary/bits/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::error::IResult;
use crate::error::InputError;
use crate::Partial;

Expand All @@ -10,7 +9,8 @@ fn test_complete_byte_consumption_bits() {
let input = &[0x12, 0x34, 0x56, 0x78][..];

// Take 3 bit slices with sizes [4, 8, 4].
let result: IResult<&[u8], (u8, u8, u8)> =
#[allow(clippy::type_complexity)]
let result: PResult<(&[u8], (u8, u8, u8)), InputError<_>> =
bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize)))
.parse_peek(input);

Expand All @@ -34,7 +34,7 @@ fn test_partial_byte_consumption_bits() {
let input = &[0x12, 0x34, 0x56, 0x78][..];

// Take bit slices with sizes [4, 8].
let result: IResult<&[u8], (u8, u8)> =
let result: PResult<(&[u8], (u8, u8)), InputError<_>> =
bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize)))
.parse_peek(input);

Expand All @@ -55,7 +55,7 @@ fn test_incomplete_bits() {
let input = Partial::new(&[0x12][..]);

// Take bit slices with sizes [4, 8].
let result: IResult<_, (u8, u8)> =
let result: PResult<(_, (u8, u8)), InputError<_>> =
bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input);

assert!(result.is_err());
Expand All @@ -70,7 +70,8 @@ fn test_take_complete_0() {
assert_eq!(count, 0usize);
let offset = 0usize;

let result: IResult<(&[u8], usize), usize> = take(count).parse_peek((input, offset));
let result: PResult<((&[u8], usize), usize), InputError<_>> =
take(count).parse_peek((input, offset));

assert_eq!(result, Ok(((input, offset), 0)));
}
Expand All @@ -79,7 +80,8 @@ fn test_take_complete_0() {
fn test_take_complete_eof() {
let input = &[0b00010010][..];

let result: IResult<(&[u8], usize), usize> = take(1usize).parse_peek((input, 8));
let result: PResult<((&[u8], usize), usize), InputError<_>> =
take(1usize).parse_peek((input, 8));

assert_eq!(
result,
Expand All @@ -94,7 +96,8 @@ fn test_take_complete_eof() {
fn test_take_complete_span_over_multiple_bytes() {
let input = &[0b00010010, 0b00110100, 0b11111111, 0b11111111][..];

let result: IResult<(&[u8], usize), usize> = take(24usize).parse_peek((input, 4));
let result: PResult<((&[u8], usize), usize), InputError<_>> =
take(24usize).parse_peek((input, 4));

assert_eq!(
result,
Expand All @@ -109,7 +112,8 @@ fn test_take_partial_0() {
assert_eq!(count, 0usize);
let offset = 0usize;

let result: IResult<(_, usize), usize> = take(count).parse_peek((input, offset));
let result: PResult<((_, usize), usize), InputError<_>> =
take(count).parse_peek((input, offset));

assert_eq!(result, Ok(((input, offset), 0)));
}
Expand All @@ -121,7 +125,7 @@ fn test_pattern_partial_ok() {
let bits_to_take = 4usize;
let value_to_pattern = 0b0001;

let result: IResult<(_, usize), usize> =
let result: PResult<((_, usize), usize), InputError<_>> =
pattern(value_to_pattern, bits_to_take).parse_peek((input, offset));

assert_eq!(result, Ok(((input, bits_to_take), value_to_pattern)));
Expand All @@ -134,7 +138,7 @@ fn test_pattern_partial_err() {
let bits_to_take = 4usize;
let value_to_pattern = 0b1111;

let result: IResult<(_, usize), usize> =
let result: PResult<((_, usize), usize), InputError<_>> =
pattern(value_to_pattern, bits_to_take).parse_peek((input, offset));

assert_eq!(
Expand All @@ -150,7 +154,7 @@ fn test_pattern_partial_err() {
fn test_bool_0_complete() {
let input = [0b10000000].as_ref();

let result: IResult<(&[u8], usize), bool> = bool.parse_peek((input, 0));
let result: PResult<((&[u8], usize), bool), InputError<_>> = bool.parse_peek((input, 0));

assert_eq!(result, Ok(((input, 1), true)));
}
Expand All @@ -159,7 +163,7 @@ fn test_bool_0_complete() {
fn test_bool_eof_complete() {
let input = [0b10000000].as_ref();

let result: IResult<(&[u8], usize), bool> = bool.parse_peek((input, 8));
let result: PResult<((&[u8], usize), bool), InputError<_>> = bool.parse_peek((input, 8));

assert_eq!(
result,
Expand All @@ -174,7 +178,9 @@ fn test_bool_eof_complete() {
fn test_bool_0_partial() {
let input = Partial::new([0b10000000].as_ref());

let result: IResult<(Partial<&[u8]>, usize), bool> = bool.parse_peek((input, 0));
#[allow(clippy::type_complexity)]
let result: PResult<((Partial<&[u8]>, usize), bool), InputError<_>> =
bool.parse_peek((input, 0));

assert_eq!(result, Ok(((input, 1), true)));
}
Expand All @@ -183,7 +189,9 @@ fn test_bool_0_partial() {
fn test_bool_eof_partial() {
let input = Partial::new([0b10000000].as_ref());

let result: IResult<(Partial<&[u8]>, usize), bool> = bool.parse_peek((input, 8));
#[allow(clippy::type_complexity)]
let result: PResult<((Partial<&[u8]>, usize), bool), InputError<_>> =
bool.parse_peek((input, 8));

assert_eq!(
result,
Expand Down
8 changes: 3 additions & 5 deletions src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,12 @@ Ok(
#[test]
#[cfg(feature = "std")]
fn test_parser_into() {
use crate::error::InputError;
use crate::token::take;

let mut parser = take::<_, _, InputError<_>>(3u8).output_into();
let result: crate::error::IResult<&[u8], Vec<u8>> = parser.parse_peek(&b"abcdefg"[..]);

assert_parse!(
result,
take(3u8)
.output_into::<Vec<u8>>()
.parse_peek(&b"abcdefg"[..]),
str![[r#"
Ok(
(
Expand Down
13 changes: 2 additions & 11 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,8 @@ use crate::Parser;
/// - [`ErrMode::into_inner`]
pub type PResult<O, E = ContextError> = Result<O, ErrMode<E>>;

/// For use with [`Parser::parse_peek`] which allows the input stream to be threaded through a
/// parser.
///
/// - `Ok((I, O))` is the remaining [input][crate::stream] and the parsed value
/// - [`Err(ErrMode<E>)`][ErrMode] is the error along with how to respond to it
///
/// By default, the error type (`E`) is [`InputError`]
///
/// When integrating into the result of the application, see
/// - [`Parser::parse`]
/// - [`ErrMode::into_inner`]
/// Deprecated, replaced with [`PResult`]
#[deprecated(since = "0.6.25", note = "Replaced with `PResult`")]
pub type IResult<I, O, E = InputError<I>> = PResult<(I, O), E>;

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod seq;
#[cfg(test)]
macro_rules! assert_parse(
($left: expr, $right: expr) => {
let res: $crate::error::IResult<_, _, $crate::error::InputError<_>> = $left;
let res: $crate::error::PResult<_, $crate::error::InputError<_>> = $left;
snapbox::assert_data_eq!(snapbox::data::ToDebug::to_debug(&res), $right);
};
);
Expand Down
11 changes: 6 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::combinator::impls;
#[cfg(feature = "unstable-recover")]
#[cfg(feature = "std")]
use crate::error::FromRecoverableError;
use crate::error::{AddContext, FromExternalError, IResult, PResult, ParseError, ParserError};
use crate::error::{AddContext, FromExternalError, PResult, ParseError, ParserError};
use crate::stream::{Compare, Location, ParseSlice, Stream, StreamIsPartial};
#[cfg(feature = "unstable-recover")]
#[cfg(feature = "std")]
Expand Down Expand Up @@ -95,7 +95,7 @@ pub trait Parser<I, O, E> {
///
/// </div>
#[inline(always)]
fn parse_peek(&mut self, mut input: I) -> IResult<I, O, E> {
fn parse_peek(&mut self, mut input: I) -> PResult<(I, O), E> {
match self.parse_next(&mut input) {
Ok(o) => Ok((input, o)),
Err(err) => Err(err),
Expand Down Expand Up @@ -1284,8 +1284,9 @@ where
/// Deprecated
#[inline(always)]
#[deprecated(since = "0.6.23")]
#[allow(deprecated)]
pub fn unpeek<'a, I, O, E>(
mut peek: impl FnMut(I) -> IResult<I, O, E> + 'a,
mut peek: impl FnMut(I) -> crate::error::IResult<I, O, E> + 'a,
) -> impl FnMut(&mut I) -> PResult<O, E>
where
I: Clone,
Expand Down Expand Up @@ -1325,8 +1326,8 @@ mod tests {
#[test]
#[cfg(target_pointer_width = "64")]
fn size_test() {
assert_size!(IResult<&[u8], &[u8], (&[u8], u32)>, 40);
assert_size!(IResult<&str, &str, u32>, 40);
assert_size!(PResult<&[u8], (&[u8], u32)>, 40);
assert_size!(PResult<&str, u32>, 40);
assert_size!(Needed, 8);
assert_size!(ErrMode<u32>, 16);
assert_size!(ErrorKind, 1);
Expand Down
8 changes: 2 additions & 6 deletions src/token/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ use snapbox::str;

use crate::ascii::Caseless;
use crate::combinator::delimited;
use crate::error::IResult;
use crate::prelude::*;
use crate::stream::AsChar;
use crate::token::literal;
use crate::Partial;

#[test]
fn complete_take_while_m_n_utf8_all_matching() {
let result: IResult<&str, &str> =
take_while(1..=4, |c: char| c.is_alphabetic()).parse_peek("øn");
assert_parse!(
result,
take_while(1..=4, |c: char| c.is_alphabetic()).parse_peek("øn"),
str![[r#"
Ok(
(
Expand All @@ -34,9 +31,8 @@ Ok(

#[test]
fn complete_take_while_m_n_utf8_all_matching_substring() {
let result: IResult<&str, &str> = take_while(1, |c: char| c.is_alphabetic()).parse_peek("øn");
assert_parse!(
result,
take_while(1, |c: char| c.is_alphabetic()).parse_peek("øn"),
str![[r#"
Ok(
(
Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use snapbox::str;

use winnow::prelude::*;
use winnow::Partial;
use winnow::{error::ErrMode, error::IResult, error::InputError, error::Needed};
use winnow::{error::ErrMode, error::InputError, error::Needed};

use crate::TestResult;

Expand Down Expand Up @@ -119,7 +119,8 @@ fn usize_length_bytes_issue() {
use winnow::binary::be_u16;
use winnow::binary::length_take;
#[allow(clippy::type_complexity)]
let _: IResult<Partial<&[u8]>, &[u8]> = length_take(be_u16).parse_peek(Partial::new(b"012346"));
let _: PResult<(Partial<&[u8]>, &[u8])> =
length_take(be_u16).parse_peek(Partial::new(b"012346"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
macro_rules! assert_parse(
($left: expr, $right: expr) => {
let res: winnow::error::IResult<_, _, winnow::error::InputError<_>> = $left;
let res: winnow::error::PResult<_, winnow::error::InputError<_>> = $left;
snapbox::assert_data_eq!(snapbox::data::ToDebug::to_debug(&res), $right);
};
);
Expand Down

0 comments on commit 22862c5

Please sign in to comment.