diff options
Diffstat (limited to 'vendor/winnow/src')
40 files changed, 4331 insertions, 4715 deletions
diff --git a/vendor/winnow/src/_topic/error.rs b/vendor/winnow/src/_topic/error.rs index abf2f8dbb..c5374b4a8 100644 --- a/vendor/winnow/src/_topic/error.rs +++ b/vendor/winnow/src/_topic/error.rs @@ -1,9 +1,9 @@ //! # Custom Errors //! -//! The most basic error type is [`ParseError`][crate::error::ParseError] +//! The most basic error type is [`ParserError`][crate::error::ParserError] //! //! Optional traits include: -//! - [`ContextError`][crate::error::ContextError] +//! - [`AddContext`][crate::error::AddContext] //! - [`FromExternalError`][crate::error::FromExternalError] //! //! # Example diff --git a/vendor/winnow/src/_topic/language.rs b/vendor/winnow/src/_topic/language.rs index 245bab4c7..0cebc99b7 100644 --- a/vendor/winnow/src/_topic/language.rs +++ b/vendor/winnow/src/_topic/language.rs @@ -27,14 +27,14 @@ //! ```rust //! use winnow::prelude::*; //! use winnow::{ -//! error::ParseError, +//! error::ParserError, //! combinator::delimited, //! ascii::multispace0, //! }; //! //! /// A combinator that takes a parser `inner` and produces a parser that also consumes both leading and //! /// trailing whitespace, returning the output of `inner`. -//! fn ws<'a, F, O, E: ParseError<&'a str>>(inner: F) -> impl Parser<&'a str, O, E> +//! fn ws<'a, F, O, E: ParserError<&'a str>>(inner: F) -> impl Parser<&'a str, O, E> //! where //! F: Parser<&'a str, O, E>, //! { @@ -61,13 +61,13 @@ //! ```rust //! use winnow::prelude::*; //! use winnow::{ -//! error::ParseError, +//! error::ParserError, //! token::take_till1, //! }; //! -//! pub fn peol_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, (), E> +//! pub fn peol_comment<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<(), E> //! { -//! ('%', take_till1("\n\r")) +//! ('%', take_till1(['\n', '\r'])) //! .void() // Output is thrown away. //! .parse_next(i) //! } @@ -81,11 +81,11 @@ //! ```rust //! use winnow::prelude::*; //! use winnow::{ -//! error::ParseError, +//! error::ParserError, //! token::{tag, take_until0}, //! }; //! -//! pub fn pinline_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, (), E> { +//! pub fn pinline_comment<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<(), E> { //! ( //! "(*", //! take_until0("*)"), @@ -111,7 +111,7 @@ //! token::one_of, //! }; //! -//! pub fn identifier(input: &str) -> IResult<&str, &str> { +//! pub fn identifier<'s>(input: &mut &'s str) -> PResult<&'s str> { //! ( //! one_of(|c: char| c.is_alpha() || c == '_'), //! take_while(0.., |c: char| c.is_alphanum() || c == '_') @@ -136,6 +136,8 @@ #![doc = include_str!("../../examples/string/parser.rs")] //! ``` //! +//! See also [`escaped`] and [`escaped_transform`]. +//! //! ### Integers //! //! The following recipes all return string slices rather than integer values. How to obtain an @@ -160,11 +162,11 @@ //! token::tag, //! }; //! -//! fn hexadecimal(input: &str) -> IResult<&str, &str> { // <'a, E: ParseError<&'a str>> +//! fn hexadecimal<'s>(input: &mut &'s str) -> PResult<&'s str> { // <'a, E: ParserError<&'a str>> //! preceded( //! alt(("0x", "0X")), //! repeat(1.., -//! terminated(one_of("0123456789abcdefABCDEF"), repeat(0.., '_').map(|()| ())) +//! terminated(one_of(('0'..='9', 'a'..='f', 'A'..='F')), repeat(0.., '_').map(|()| ())) //! ).map(|()| ()).recognize() //! ).parse_next(input) //! } @@ -182,11 +184,11 @@ //! token::tag, //! }; //! -//! fn hexadecimal_value(input: &str) -> IResult<&str, i64> { +//! fn hexadecimal_value(input: &mut &str) -> PResult<i64> { //! preceded( //! alt(("0x", "0X")), //! repeat(1.., -//! terminated(one_of("0123456789abcdefABCDEF"), repeat(0.., '_').map(|()| ())) +//! terminated(one_of(('0'..='9', 'a'..='f', 'A'..='F')), repeat(0.., '_').map(|()| ())) //! ).map(|()| ()).recognize() //! ).try_map( //! |out: &str| i64::from_str_radix(&str::replace(&out, "_", ""), 16) @@ -194,6 +196,8 @@ //! } //! ``` //! +//! See also [`hex_uint`] +//! //! #### Octal //! //! ```rust @@ -206,11 +210,11 @@ //! token::tag, //! }; //! -//! fn octal(input: &str) -> IResult<&str, &str> { +//! fn octal<'s>(input: &mut &'s str) -> PResult<&'s str> { //! preceded( //! alt(("0o", "0O")), //! repeat(1.., -//! terminated(one_of("01234567"), repeat(0.., '_').map(|()| ())) +//! terminated(one_of('0'..='7'), repeat(0.., '_').map(|()| ())) //! ).map(|()| ()).recognize() //! ).parse_next(input) //! } @@ -228,11 +232,11 @@ //! token::tag, //! }; //! -//! fn binary(input: &str) -> IResult<&str, &str> { +//! fn binary<'s>(input: &mut &'s str) -> PResult<&'s str> { //! preceded( //! alt(("0b", "0B")), //! repeat(1.., -//! terminated(one_of("01"), repeat(0.., '_').map(|()| ())) +//! terminated(one_of('0'..='1'), repeat(0.., '_').map(|()| ())) //! ).map(|()| ()).recognize() //! ).parse_next(input) //! } @@ -243,21 +247,22 @@ //! ```rust //! use winnow::prelude::*; //! use winnow::{ -//! IResult, //! combinator::{repeat}, //! combinator::terminated, //! token::one_of, //! }; //! -//! fn decimal(input: &str) -> IResult<&str, &str> { +//! fn decimal<'s>(input: &mut &'s str) -> PResult<&'s str> { //! repeat(1.., -//! terminated(one_of("0123456789"), repeat(0.., '_').map(|()| ())) +//! terminated(one_of('0'..='9'), repeat(0.., '_').map(|()| ())) //! ).map(|()| ()) //! .recognize() //! .parse_next(input) //! } //! ``` //! +//! See also [`dec_uint`] and [`dec_int`] +//! //! ### Floating Point Numbers //! //! The following is adapted from [the Python parser by Valentin Lorentz](https://github.com/ProgVal/rust-python-parser/blob/master/src/numbers.rs). @@ -272,15 +277,15 @@ //! token::one_of, //! }; //! -//! fn float(input: &str) -> IResult<&str, &str> { +//! fn float<'s>(input: &mut &'s str) -> PResult<&'s str> { //! alt(( //! // Case one: .42 //! ( //! '.', //! decimal, //! opt(( -//! one_of("eE"), -//! opt(one_of("+-")), +//! one_of(['e', 'E']), +//! opt(one_of(['+', '-'])), //! decimal //! )) //! ).recognize() @@ -291,8 +296,8 @@ //! '.', //! decimal, //! )), -//! one_of("eE"), -//! opt(one_of("+-")), +//! one_of(['e', 'E']), +//! opt(one_of(['+', '-'])), //! decimal //! ).recognize() //! , // Case three: 42. and 42.42 @@ -304,12 +309,22 @@ //! )).parse_next(input) //! } //! -//! fn decimal(input: &str) -> IResult<&str, &str> { +//! fn decimal<'s>(input: &mut &'s str) -> PResult<&'s str> { //! repeat(1.., -//! terminated(one_of("0123456789"), repeat(0.., '_').map(|()| ())) +//! terminated(one_of('0'..='9'), repeat(0.., '_').map(|()| ())) //! ). //! map(|()| ()) //! .recognize() //! .parse_next(input) //! } //! ``` +//! +//! See also [`float`] + +#![allow(unused_imports)] +use crate::ascii::dec_int; +use crate::ascii::dec_uint; +use crate::ascii::escaped; +use crate::ascii::escaped_transform; +use crate::ascii::float; +use crate::ascii::hex_uint; diff --git a/vendor/winnow/src/_topic/performance.rs b/vendor/winnow/src/_topic/performance.rs index fac12da4c..5bda958ee 100644 --- a/vendor/winnow/src/_topic/performance.rs +++ b/vendor/winnow/src/_topic/performance.rs @@ -6,7 +6,7 @@ //! //! Tips //! - When enough cases of an [`alt`] have unique prefixes, prefer [`dispatch`] -//! - When parsing text, try to parse is as bytes (`u8`) rather than `char`s ([`BStr`] can make +//! - When parsing text, try to parse as bytes (`u8`) rather than `char`s ([`BStr`] can make //! debugging easier) //! - Find simplified subsets of the grammar to parse, falling back to the full grammar when it //! doesn't work. For example, when parsing json strings, parse them without support for escapes, @@ -14,7 +14,7 @@ //! - Watch for large return types. A surprising place these can show up is when chaining parsers //! with a tuple. //! -//! ## Built-time Performance +//! ## Build-time Performance //! //! Returning complex types as `impl Trait` can negatively impact build times. This can hit in //! surprising cases like: @@ -24,7 +24,7 @@ //! # where //! # I: winnow::stream::Stream<Token=O>, //! # I: winnow::stream::StreamIsPartial, -//! # E: winnow::error::ParseError<I>, +//! # E: winnow::error::ParserError<I>, //! { //! // ...some chained combinators... //! # winnow::token::any @@ -38,9 +38,9 @@ //! # where //! # I: winnow::stream::Stream<Token=O>, //! # I: winnow::stream::StreamIsPartial, -//! # E: winnow::error::ParseError<I>, +//! # E: winnow::error::ParserError<I>, //! { -//! move |input: I| { +//! move |input: &mut I| { //! // ...some chained combinators... //! # winnow::token::any //! .parse_next(input) diff --git a/vendor/winnow/src/_topic/stream.rs b/vendor/winnow/src/_topic/stream.rs index 7455e185b..4f94a94b9 100644 --- a/vendor/winnow/src/_topic/stream.rs +++ b/vendor/winnow/src/_topic/stream.rs @@ -13,14 +13,14 @@ //! ## Implementing a custom stream //! //! Let's assume we have an input type we'll call `MyStream`. `MyStream` is a sequence of `MyItem` type. -//! The goal is to define parsers with this signature: `MyStream -> IResult<MyStream, Output>`. +//! The goal is to define parsers with this signature: `&mut MyStream -> PResult<Output>`. //! //! ```rust //! # use winnow::prelude::*; //! # use winnow::token::tag; //! # type MyStream<'i> = &'i str; //! # type Output<'i> = &'i str; -//! fn parser(i: MyStream<'_>) -> IResult<MyStream<'_>, Output<'_>> { +//! fn parser<'s>(i: &mut MyStream<'s>) -> PResult<Output<'s>> { //! "test".parse_next(i) //! } //! ``` diff --git a/vendor/winnow/src/_tutorial/chapter_1.rs b/vendor/winnow/src/_tutorial/chapter_1.rs index d6a45c8b7..1bf146bec 100644 --- a/vendor/winnow/src/_tutorial/chapter_1.rs +++ b/vendor/winnow/src/_tutorial/chapter_1.rs @@ -9,51 +9,47 @@ //! - `Ok` indicates the parser successfully found what it was looking for; or //! - `Err` indicates the parser could not find what it was looking for. //! -//! Parsers do more than just return a binary "success"/"failure" code. If -//! the parser was successful, then it will return a tuple where the first field -//! will contain everything the parser did not process. The second will contain -//! everything the parser processed. The idea is that a parser can happily parse the first -//! *part* of an input, without being able to parse the whole thing. +//! Parsers do more than just return a binary "success"/"failure" code. +//! On success, the parser will return the processed data. The input will be left pointing to +//! data that still needs processing //! //! If the parser failed, then there are multiple errors that could be returned. //! For simplicity, however, in the next chapters we will leave these unexplored. //! //! ```text -//! ┌─► Ok( -//! │ what the parser didn't touch, -//! │ what matched the parser -//! │ ) +//! ┌─► Ok(what matched the parser) //! ┌─────────┐ │ //! my input───►│my parser├──►either──┤ //! └─────────┘ └─► Err(...) //! ``` //! //! -//! To represent this model of the world, winnow uses the [`IResult<I, O>`] type. -//! The `Ok` variant has a tuple of `(remainder: I, output: O)`; +//! To represent this model of the world, winnow uses the [`PResult<O>`] type. +//! The `Ok` variant has `output: O`; //! whereas the `Err` variant stores an error. //! //! You can import that from: //! //! ```rust -//! use winnow::IResult; +//! use winnow::PResult; //! ``` //! +//! To combine parsers, we need a common way to refer to them which is where the [`Parser<I, O, E>`] +//! trait comes in with [`Parser::parse_next`] being the primary way to drive +//! parsing forward. +//! //! You'll note that `I` and `O` are parameterized -- while most of the examples in this book //! will be with `&str` (i.e. parsing a string); they do not have to be strings; nor do they //! have to be the same type (consider the simple example where `I = &str`, and `O = u64` -- this //! parses a string into an unsigned integer.) //! -//! To combine parsers, we need a common way to refer to them which is where the [`Parser`] -//! trait comes in with [`Parser::parse_next`] being the primary way to drive -//! parsing forward. //! //! # Let's write our first parser! //! //! The simplest parser we can write is one which successfully does nothing. //! //! To make it easier to implement a [`Parser`], the trait is implemented for -//! functions of the form `Fn(I) -> IResult<I, O>`. +//! functions of the form `Fn(&mut I) -> PResult<O>`. //! //! This parser function should take in a `&str`: //! @@ -62,27 +58,27 @@ //! - Since it doesn't parse anything, it also should just return an empty string. //! //! ```rust -//! use winnow::IResult; +//! use winnow::PResult; //! use winnow::Parser; //! -//! pub fn do_nothing_parser(input: &str) -> IResult<&str, &str> { -//! Ok((input, "")) +//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! Ok("") //! } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, output) = do_nothing_parser.parse_next(input).unwrap(); +//! let output = do_nothing_parser.parse_next(&mut input).unwrap(); //! // Same as: -//! // let (remainder, output) = do_nothing_parser(input).unwrap(); +//! // let output = do_nothing_parser(&mut input).unwrap(); //! -//! assert_eq!(remainder, "0x1a2b Hello"); +//! assert_eq!(input, "0x1a2b Hello"); //! assert_eq!(output, ""); //! } //! ``` #![allow(unused_imports)] -use crate::IResult; +use crate::PResult; use crate::Parser; pub use super::chapter_0 as previous; diff --git a/vendor/winnow/src/_tutorial/chapter_2.rs b/vendor/winnow/src/_tutorial/chapter_2.rs index 49b61f3f4..b0daedc74 100644 --- a/vendor/winnow/src/_tutorial/chapter_2.rs +++ b/vendor/winnow/src/_tutorial/chapter_2.rs @@ -4,54 +4,139 @@ //! //! ## Tokens //! -//! Matching a single token literal is common enough that `Parser` is implemented for -//! `char`. -//! +//! [`Stream`] provides some core operations to help with parsing. For example, to process a +//! single token, you can do: //! ```rust //! # use winnow::Parser; -//! # use winnow::IResult; -//! # -//! fn parse_prefix(input: &str) -> IResult<&str, char> { -//! '0'.parse_next(input) +//! # use winnow::PResult; +//! use winnow::stream::Stream; +//! use winnow::error::ParserError; +//! use winnow::error::ErrorKind; +//! use winnow::error::ErrMode; +//! +//! fn parse_prefix(input: &mut &str) -> PResult<char> { +//! let c = input.next_token().ok_or_else(|| { +//! ErrMode::from_error_kind(input, ErrorKind::Token) +//! })?; +//! if c != '0' { +//! return Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); +//! } +//! Ok(c) //! } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, output) = parse_prefix.parse_next(input).unwrap(); +//! let output = parse_prefix.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, "x1a2b Hello"); +//! assert_eq!(input, "x1a2b Hello"); //! assert_eq!(output, '0'); //! -//! assert!(parse_prefix("d").is_err()); +//! assert!(parse_prefix.parse_next(&mut "d").is_err()); //! } //! ``` //! -//! ## Tags +//! [`any`] and [`Parser::verify`] are [`Parser`] building blocks on top of [`Stream`]: +//! ```rust +//! # use winnow::PResult; +//! use winnow::Parser; +//! use winnow::token::any; //! -//! One of the most frequent way of matching a token is when they are combined into a string. -//! Again, this is common enough that `Parser` is implemented for `&str`: +//! fn parse_prefix(input: &mut &str) -> PResult<char> { +//! any.verify(|c| *c == '0').parse_next(input) +//! } +//! # +//! # fn main() { +//! # let mut input = "0x1a2b Hello"; +//! # +//! # let output = parse_prefix.parse_next(&mut input).unwrap(); +//! # +//! # assert_eq!(input, "x1a2b Hello"); +//! # assert_eq!(output, '0'); +//! # +//! # assert!(parse_prefix.parse_next(&mut "d").is_err()); +//! # } +//! ``` +//! +//! Matching a single token literal is common enough that [`Parser`] is implemented for +//! `char`. //! //! ```rust -//! # use winnow::Parser; -//! # use winnow::IResult; +//! # use winnow::PResult; +//! use winnow::Parser; +//! +//! fn parse_prefix(input: &mut &str) -> PResult<char> { +//! '0'.parse_next(input) +//! } //! # -//! fn parse_prefix(input: &str) -> IResult<&str, &str> { -//! "0x".parse_next(input) +//! # fn main() { +//! # let mut input = "0x1a2b Hello"; +//! # +//! # let output = parse_prefix.parse_next(&mut input).unwrap(); +//! # +//! # assert_eq!(input, "x1a2b Hello"); +//! # assert_eq!(output, '0'); +//! # +//! # assert!(parse_prefix.parse_next(&mut "d").is_err()); +//! # } +//! ``` +//! +//! ## Tags +//! +//! [`Stream`] also supports processing slices of tokens: +//! ```rust +//! # use winnow::Parser; +//! # use winnow::PResult; +//! use winnow::stream::Stream; +//! use winnow::error::ParserError; +//! use winnow::error::ErrorKind; +//! use winnow::error::ErrMode; +//! +//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! let expected = "0x"; +//! if input.len() < expected.len() { +//! return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); +//! } +//! let actual = input.next_slice(expected.len()); +//! if actual != expected { +//! return Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); +//! } +//! Ok(actual) //! } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, output) = parse_prefix.parse_next(input).unwrap(); -//! assert_eq!(remainder, "1a2b Hello"); +//! let output = parse_prefix.parse_next(&mut input).unwrap(); +//! assert_eq!(input, "1a2b Hello"); //! assert_eq!(output, "0x"); //! -//! assert!(parse_prefix("0o123").is_err()); +//! assert!(parse_prefix.parse_next(&mut "0o123").is_err()); //! } //! ``` //! -//! In `winnow`, we call this type of parser a [`tag`]. +//! Again, matching a literal is common enough that [`Parser`] is implemented for `&str`: +//! ```rust +//! # use winnow::PResult; +//! use winnow::Parser; +//! +//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! "0x".parse_next(input) +//! } +//! # +//! # fn main() { +//! # let mut input = "0x1a2b Hello"; +//! # +//! # let output = parse_prefix.parse_next(&mut input).unwrap(); +//! # assert_eq!(input, "1a2b Hello"); +//! # assert_eq!(output, "0x"); +//! # +//! # assert!(parse_prefix.parse_next(&mut "0o123").is_err()); +//! # } +//! ``` +//! +//! In `winnow`, we call this type of parser a [`tag`]. See [`token`] for additional individual +//! and token-slice parsers. //! //! ## Character Classes //! @@ -60,21 +145,21 @@ //! //! ```rust //! # use winnow::Parser; -//! # use winnow::IResult; +//! # use winnow::PResult; //! use winnow::token::one_of; //! -//! fn parse_digits(input: &str) -> IResult<&str, char> { -//! one_of("0123456789abcdefgABCDEFG").parse_next(input) +//! fn parse_digits(input: &mut &str) -> PResult<char> { +//! one_of(('0'..='9', 'a'..='f', 'A'..='F')).parse_next(input) //! } //! //! fn main() { -//! let input = "1a2b Hello"; +//! let mut input = "1a2b Hello"; //! -//! let (remainder, output) = parse_digits.parse_next(input).unwrap(); -//! assert_eq!(remainder, "a2b Hello"); +//! let output = parse_digits.parse_next(&mut input).unwrap(); +//! assert_eq!(input, "a2b Hello"); //! assert_eq!(output, '1'); //! -//! assert!(parse_digits("Z").is_err()); +//! assert!(parse_digits.parse_next(&mut "Z").is_err()); //! } //! ``` //! @@ -82,10 +167,10 @@ //! > Let's look at it more closely as its used above (resolving all generic parameters): //! > ```rust //! > # use winnow::prelude::*; -//! > # use winnow::error::Error; +//! > # use winnow::error::InputError; //! > pub fn one_of<'i>( -//! > list: &'static str -//! > ) -> impl Parser<&'i str, char, Error<&'i str>> { +//! > list: &'static [char] +//! > ) -> impl Parser<&'i str, char, InputError<&'i str>> { //! > // ... //! > # winnow::token::one_of(list) //! > } @@ -93,7 +178,7 @@ //! > If you have not programmed in a language where functions are values, the type signature of the //! > [`one_of`] function might be a surprise. //! > The function [`one_of`] *returns a function*. The function it returns is a -//! > `Parser`, taking a `&str` and returning an `IResult`. This is a common pattern in winnow for +//! > `Parser`, taking a `&str` and returning an `PResult`. This is a common pattern in winnow for //! > configurable or stateful parsers. //! //! Some of character classes are common enough that a named parser is provided, like with: @@ -104,48 +189,54 @@ //! You can then capture sequences of these characters with parsers like [`take_while`]. //! ```rust //! # use winnow::Parser; -//! # use winnow::IResult; +//! # use winnow::PResult; //! use winnow::token::take_while; //! -//! fn parse_digits(input: &str) -> IResult<&str, &str> { -//! take_while(1.., "0123456789abcdefgABCDEFG").parse_next(input) +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! take_while(1.., ('0'..='9', 'a'..='f', 'A'..='F')).parse_next(input) //! } //! //! fn main() { -//! let input = "1a2b Hello"; +//! let mut input = "1a2b Hello"; //! -//! let (remainder, output) = parse_digits.parse_next(input).unwrap(); -//! assert_eq!(remainder, " Hello"); +//! let output = parse_digits.parse_next(&mut input).unwrap(); +//! assert_eq!(input, " Hello"); //! assert_eq!(output, "1a2b"); //! -//! assert!(parse_digits("Z").is_err()); +//! assert!(parse_digits.parse_next(&mut "Z").is_err()); //! } //! ``` //! //! We could simplify this further with by using one of the built-in character classes, [`hex_digit1`]: //! ```rust //! # use winnow::Parser; -//! # use winnow::IResult; +//! # use winnow::PResult; //! use winnow::ascii::hex_digit1; //! -//! fn parse_digits(input: &str) -> IResult<&str, &str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! hex_digit1.parse_next(input) //! } //! //! fn main() { -//! let input = "1a2b Hello"; +//! let mut input = "1a2b Hello"; //! -//! let (remainder, output) = parse_digits.parse_next(input).unwrap(); -//! assert_eq!(remainder, " Hello"); +//! let output = parse_digits.parse_next(&mut input).unwrap(); +//! assert_eq!(input, " Hello"); //! assert_eq!(output, "1a2b"); //! -//! assert!(parse_digits("Z").is_err()); +//! assert!(parse_digits.parse_next(&mut "Z").is_err()); //! } //! ``` +//! +//! See [`ascii`] for more text-based parsers. #![allow(unused_imports)] +use crate::ascii; use crate::ascii::hex_digit1; use crate::stream::ContainsToken; +use crate::stream::Stream; +use crate::token; +use crate::token::any; use crate::token::one_of; use crate::token::tag; use crate::token::take_while; diff --git a/vendor/winnow/src/_tutorial/chapter_3.rs b/vendor/winnow/src/_tutorial/chapter_3.rs index 8d307e324..83714f4d7 100644 --- a/vendor/winnow/src/_tutorial/chapter_3.rs +++ b/vendor/winnow/src/_tutorial/chapter_3.rs @@ -10,15 +10,14 @@ //! Now that we can create more interesting parsers, we can sequence them together, like: //! //! ```rust +//! # use winnow::prelude::*; //! # use winnow::token::take_while; -//! # use winnow::Parser; -//! # use winnow::IResult; //! # -//! fn parse_prefix(input: &str) -> IResult<&str, &str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { //! "0x".parse_next(input) //! } //! -//! fn parse_digits(input: &str) -> IResult<&str, &str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! take_while(1.., ( //! ('0'..='9'), //! ('A'..='F'), @@ -27,28 +26,27 @@ //! } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, prefix) = parse_prefix.parse_next(input).unwrap(); -//! let (remainder, digits) = parse_digits.parse_next(remainder).unwrap(); +//! let prefix = parse_prefix.parse_next(&mut input).unwrap(); +//! let digits = parse_digits.parse_next(&mut input).unwrap(); //! //! assert_eq!(prefix, "0x"); //! assert_eq!(digits, "1a2b"); -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! } //! ``` //! //! To sequence these together, you can just put them in a tuple: //! ```rust +//! # use winnow::prelude::*; //! # use winnow::token::take_while; -//! # use winnow::Parser; -//! # use winnow::IResult; //! # -//! # fn parse_prefix(input: &str) -> IResult<&str, &str> { +//! # fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # "0x".parse_next(input) //! # } //! # -//! # fn parse_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -59,32 +57,31 @@ //! //... //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, (prefix, digits)) = ( +//! let (prefix, digits) = ( //! parse_prefix, //! parse_digits -//! ).parse_next(input).unwrap(); +//! ).parse_next(&mut input).unwrap(); //! //! assert_eq!(prefix, "0x"); //! assert_eq!(digits, "1a2b"); -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! } //! ``` //! //! Frequently, you won't care about the tag and you can instead use one of the provided combinators, //! like [`preceded`]: //! ```rust +//! # use winnow::prelude::*; //! # use winnow::token::take_while; -//! # use winnow::Parser; -//! # use winnow::IResult; //! use winnow::combinator::preceded; //! -//! # fn parse_prefix(input: &str) -> IResult<&str, &str> { +//! # fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # "0x".parse_next(input) //! # } //! # -//! # fn parse_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -95,63 +92,72 @@ //! //... //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, digits) = preceded( +//! let digits = preceded( //! parse_prefix, //! parse_digits -//! ).parse_next(input).unwrap(); +//! ).parse_next(&mut input).unwrap(); //! //! assert_eq!(digits, "1a2b"); -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! } //! ``` //! +//! See [`combinator`] for more sequencing parsers. +//! //! ## Alternatives //! //! Sometimes, we might want to choose between two parsers; and we're happy with //! either being used. //! -//! The de facto way to do this in winnow is with the [`alt()`] combinator which will execute each -//! parser in a tuple until it finds one that does not error. If all error, then by default you are -//! given the error from the last parser. -//! -//! We can see a basic example of `alt()` below. +//! [`Stream::checkpoint`] helps us to retry parsing: //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; -//! use winnow::combinator::alt; +//! use winnow::stream::Stream; //! -//! fn parse_digits(input: &str) -> IResult<&str, (&str, &str)> { -//! alt(( -//! ("0b", parse_bin_digits), -//! ("0o", parse_oct_digits), -//! ("0d", parse_dec_digits), -//! ("0x", parse_hex_digits), -//! )).parse_next(input) +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! let start = input.checkpoint(); +//! +//! if let Ok(output) = ("0b", parse_bin_digits).parse_next(input) { +//! return Ok(output); +//! } +//! +//! input.reset(start); +//! if let Ok(output) = ("0o", parse_oct_digits).parse_next(input) { +//! return Ok(output); +//! } +//! +//! input.reset(start); +//! if let Ok(output) = ("0d", parse_dec_digits).parse_next(input) { +//! return Ok(output); +//! } +//! +//! input.reset(start); +//! ("0x", parse_hex_digits).parse_next(input) //! } //! //! // ... -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -160,31 +166,147 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, (prefix, digits)) = parse_digits.parse_next(input).unwrap(); +//! let (prefix, digits) = parse_digits.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(prefix, "0x"); //! assert_eq!(digits, "1a2b"); //! -//! assert!(parse_digits("ghiWorld").is_err()); +//! assert!(parse_digits(&mut "ghiWorld").is_err()); //! } //! ``` //! +//! > **Warning:** the above example is for illustrative purposes and relying on `Result::Ok` or +//! > `Result::Err` can lead to incorrect behavior. This will be clarified in later when covering +//! > [error handling][`chapter_6`#errmode] +//! +//! [`opt`] is a basic building block for correctly handling retrying parsing: +//! ```rust +//! # use winnow::prelude::*; +//! # use winnow::token::take_while; +//! use winnow::combinator::opt; +//! +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! if let Some(output) = opt(("0b", parse_bin_digits)).parse_next(input)? { +//! Ok(output) +//! } else if let Some(output) = opt(("0o", parse_oct_digits)).parse_next(input)? { +//! Ok(output) +//! } else if let Some(output) = opt(("0d", parse_dec_digits)).parse_next(input)? { +//! Ok(output) +//! } else { +//! ("0x", parse_hex_digits).parse_next(input) +//! } +//! } +//! # +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='7'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='7'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='9'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='9'), +//! # ('A'..='F'), +//! # ('a'..='f'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn main() { +//! # let mut input = "0x1a2b Hello"; +//! # +//! # let (prefix, digits) = parse_digits.parse_next(&mut input).unwrap(); +//! # +//! # assert_eq!(input, " Hello"); +//! # assert_eq!(prefix, "0x"); +//! # assert_eq!(digits, "1a2b"); +//! # +//! # assert!(parse_digits(&mut "ghiWorld").is_err()); +//! # } +//! ``` +//! +//! [`alt`] encapsulates this if/else-if ladder pattern, with the last case being the `else`: +//! ```rust +//! # use winnow::prelude::*; +//! # use winnow::token::take_while; +//! use winnow::combinator::alt; +//! +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! alt(( +//! ("0b", parse_bin_digits), +//! ("0o", parse_oct_digits), +//! ("0d", parse_dec_digits), +//! ("0x", parse_hex_digits), +//! )).parse_next(input) +//! } +//! # +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='7'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='7'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='9'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='9'), +//! # ('A'..='F'), +//! # ('a'..='f'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn main() { +//! # let mut input = "0x1a2b Hello"; +//! # +//! # let (prefix, digits) = parse_digits.parse_next(&mut input).unwrap(); +//! # +//! # assert_eq!(input, " Hello"); +//! # assert_eq!(prefix, "0x"); +//! # assert_eq!(digits, "1a2b"); +//! # +//! # assert!(parse_digits(&mut "ghiWorld").is_err()); +//! # } +//! ``` +//! +//! > **Note:** [`success`] and [`fail`] are parsers that might be useful in the `else` case. +//! //! Sometimes a giant if/else-if ladder can be slow and you'd rather have a `match` statement for //! branches of your parser that have unique prefixes. In this case, you can use the //! [`dispatch`][crate::combinator::dispatch] macro: //! //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! use winnow::combinator::dispatch; //! use winnow::token::take; //! use winnow::combinator::fail; //! -//! fn parse_digits(input: &str) -> IResult<&str, &str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! dispatch!(take(2usize); //! "0b" => parse_bin_digits, //! "0o" => parse_oct_digits, @@ -195,25 +317,25 @@ //! } //! //! // ... -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -222,21 +344,32 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, digits) = parse_digits.parse_next(input).unwrap(); +//! let digits = parse_digits.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(digits, "1a2b"); //! -//! assert!(parse_digits("ghiWorld").is_err()); +//! assert!(parse_digits(&mut "ghiWorld").is_err()); //! } //! ``` +//! +//! > **Note:** [`peek`] may be useful when [`dispatch`]ing from hints from each case's parser. +//! +//! See [`combinator`] for more alternative parsers. #![allow(unused_imports)] +use super::chapter_6; +use crate::combinator; use crate::combinator::alt; use crate::combinator::dispatch; +use crate::combinator::fail; +use crate::combinator::opt; +use crate::combinator::peek; use crate::combinator::preceded; +use crate::combinator::success; +use crate::stream::Stream; pub use super::chapter_2 as previous; pub use super::chapter_4 as next; diff --git a/vendor/winnow/src/_tutorial/chapter_4.rs b/vendor/winnow/src/_tutorial/chapter_4.rs index 315d185ba..fb14613f3 100644 --- a/vendor/winnow/src/_tutorial/chapter_4.rs +++ b/vendor/winnow/src/_tutorial/chapter_4.rs @@ -1,18 +1,18 @@ //! # Chapter 4: Parsers With Custom Return Types //! //! So far, we have seen mostly functions that take an `&str`, and return a -//! `IResult<&str, &str>`. Splitting strings into smaller strings and characters is certainly +//! `PResult<&str>`. Splitting strings into smaller strings and characters is certainly //! useful, but it's not the only thing winnow is capable of! //! //! A useful operation when parsing is to convert between types; for example //! parsing from `&str` to another primitive, like [`usize`]. //! //! All we need to do for our parser to return a different type is to change -//! the second type parameter of [`IResult`] to the desired return type. -//! For example, to return a `usize`, return a `IResult<&str, usize>`. -//! Recall that the first type parameter of the `IResult` is the input +//! the type parameter of [`PResult`] to the desired return type. +//! For example, to return a `usize`, return a `PResult<usize>`. +//! Recall that the type parameter of the `PResult` is the input //! type, so even if you're returning something different, if your input -//! is a `&str`, the first type argument of `IResult` should be also. +//! is a `&str`, the type argument of `PResult` should be also. //! //! One winnow-native way of doing a type conversion is to use the //! [`Parser::parse_to`] combinator @@ -20,38 +20,36 @@ //! //! The following code converts from a string containing a number to `usize`: //! ```rust -//! # use winnow::Parser; -//! # use winnow::IResult; +//! # use winnow::prelude::*; //! # use winnow::ascii::digit1; //! # -//! fn parse_digits(input: &str) -> IResult<&str, usize> { +//! fn parse_digits(input: &mut &str) -> PResult<usize> { //! digit1 //! .parse_to() //! .parse_next(input) //! } //! //! fn main() { -//! let input = "1024 Hello"; +//! let mut input = "1024 Hello"; //! -//! let (remainder, output) = parse_digits.parse_next(input).unwrap(); -//! assert_eq!(remainder, " Hello"); +//! let output = parse_digits.parse_next(&mut input).unwrap(); +//! assert_eq!(input, " Hello"); //! assert_eq!(output, 1024); //! -//! assert!(parse_digits("Z").is_err()); +//! assert!(parse_digits(&mut "Z").is_err()); //! } //! ``` //! //! `Parser::parse_to` is just a convenient form of [`Parser::try_map`] which we can use to handle //! all radices of numbers: //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! use winnow::combinator::dispatch; //! use winnow::token::take; //! use winnow::combinator::fail; //! -//! fn parse_digits(input: &str) -> IResult<&str, usize> { +//! fn parse_digits(input: &mut &str) -> PResult<usize> { //! dispatch!(take(2usize); //! "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -62,25 +60,25 @@ //! } //! //! // ... -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -89,19 +87,21 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, digits) = parse_digits.parse_next(input).unwrap(); +//! let digits = parse_digits.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(digits, 0x1a2b); //! -//! assert!(parse_digits("ghiWorld").is_err()); +//! assert!(parse_digits(&mut "ghiWorld").is_err()); //! } //! ``` +//! +//! See also [`Parser`] for more output-modifying parsers. #![allow(unused_imports)] -use crate::IResult; +use crate::PResult; use crate::Parser; use std::str::FromStr; diff --git a/vendor/winnow/src/_tutorial/chapter_5.rs b/vendor/winnow/src/_tutorial/chapter_5.rs index 3a4be4b32..95a89dc8c 100644 --- a/vendor/winnow/src/_tutorial/chapter_5.rs +++ b/vendor/winnow/src/_tutorial/chapter_5.rs @@ -1,12 +1,11 @@ //! # Chapter 5: Repetition //! //! In [`chapter_3`], we covered how to sequence different parsers into a tuple but sometimes you need to run a -//! single parser multiple times, collecting the result into a [`Vec`]. +//! single parser multiple times, collecting the result into a container, like [`Vec`]. //! -//! Let's take our `parse_digits` and collect a list of them with [`repeat`]: +//! Let's collect the result of `parse_digits`: //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; @@ -15,40 +14,44 @@ //! use winnow::combinator::repeat; //! use winnow::combinator::terminated; //! -//! fn parse_list(input: &str) -> IResult<&str, Vec<usize>> { -//! repeat(0.., terminated(parse_digits, opt(','))).parse_next(input) +//! fn parse_list(input: &mut &str) -> PResult<Vec<usize>> { +//! let mut list = Vec::new(); +//! while let Some(output) = opt(terminated(parse_digits, opt(','))).parse_next(input)? { +//! list.push(output); +//! } +//! Ok(list) //! } //! //! // ... -//! # fn parse_digits(input: &str) -> IResult<&str, usize> { +//! # fn parse_digits(input: &mut &str) -> PResult<usize> { //! # dispatch!(take(2usize); -//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), -//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), -//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), -//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), -//! # _ => fail, -//! # ).parse_next(input) +//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), +//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), +//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), +//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), +//! # _ => fail, +//! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -57,62 +60,126 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b,0x3c4d,0x5e6f Hello"; +//! let mut input = "0x1a2b,0x3c4d,0x5e6f Hello"; //! -//! let (remainder, digits) = parse_list.parse_next(input).unwrap(); +//! let digits = parse_list.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(digits, vec![0x1a2b, 0x3c4d, 0x5e6f]); //! -//! assert!(parse_digits("ghiWorld").is_err()); +//! assert!(parse_digits(&mut "ghiWorld").is_err()); //! } //! ``` //! +//! We can implement this declaratively with [`repeat`]: +//! ```rust +//! # use winnow::prelude::*; +//! # use winnow::token::take_while; +//! # use winnow::combinator::dispatch; +//! # use winnow::token::take; +//! # use winnow::combinator::fail; +//! use winnow::combinator::opt; +//! use winnow::combinator::repeat; +//! use winnow::combinator::terminated; +//! +//! fn parse_list(input: &mut &str) -> PResult<Vec<usize>> { +//! repeat(0.., +//! terminated(parse_digits, opt(',')) +//! ).parse_next(input) +//! } +//! # +//! # fn parse_digits(input: &mut &str) -> PResult<usize> { +//! # dispatch!(take(2usize); +//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), +//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), +//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), +//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), +//! # _ => fail, +//! # ).parse_next(input) +//! # } +//! # +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='7'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='7'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='9'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # take_while(1.., ( +//! # ('0'..='9'), +//! # ('A'..='F'), +//! # ('a'..='f'), +//! # )).parse_next(input) +//! # } +//! # +//! # fn main() { +//! # let mut input = "0x1a2b,0x3c4d,0x5e6f Hello"; +//! # +//! # let digits = parse_list.parse_next(&mut input).unwrap(); +//! # +//! # assert_eq!(input, " Hello"); +//! # assert_eq!(digits, vec![0x1a2b, 0x3c4d, 0x5e6f]); +//! # +//! # assert!(parse_digits(&mut "ghiWorld").is_err()); +//! # } +//! ``` +//! //! You'll notice that the above allows trailing `,` when we intended to not support that. We can //! easily fix this by using [`separated0`]: //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; //! # use winnow::combinator::fail; //! use winnow::combinator::separated0; //! -//! fn parse_list(input: &str) -> IResult<&str, Vec<usize>> { +//! fn parse_list(input: &mut &str) -> PResult<Vec<usize>> { //! separated0(parse_digits, ",").parse_next(input) //! } //! //! // ... -//! # fn parse_digits(input: &str) -> IResult<&str, usize> { +//! # fn parse_digits(input: &mut &str) -> PResult<usize> { //! # dispatch!(take(2usize); -//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), -//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), -//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), -//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), -//! # _ => fail, -//! # ).parse_next(input) +//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), +//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), +//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), +//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), +//! # _ => fail, +//! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -121,14 +188,14 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b,0x3c4d,0x5e6f Hello"; +//! let mut input = "0x1a2b,0x3c4d,0x5e6f Hello"; //! -//! let (remainder, digits) = parse_list.parse_next(input).unwrap(); +//! let digits = parse_list.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(digits, vec![0x1a2b, 0x3c4d, 0x5e6f]); //! -//! assert!(parse_digits("ghiWorld").is_err()); +//! assert!(parse_digits(&mut "ghiWorld").is_err()); //! } //! ``` //! @@ -136,52 +203,50 @@ //! [`Accumulate`] to gather the results. This let's us make more complex parsers than we did in //! [`chapter_2`] by accumulating the results into a `()` and [`recognize`][Parser::recognize]-ing the captured input: //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; //! # use winnow::combinator::fail; //! # use winnow::combinator::separated0; //! # -//! fn recognize_list(input: &str) -> IResult<&str, &str> { +//! fn recognize_list<'s>(input: &mut &'s str) -> PResult<&'s str> { //! parse_list.recognize().parse_next(input) //! } //! -//! fn parse_list(input: &str) -> IResult<&str, ()> { +//! fn parse_list(input: &mut &str) -> PResult<()> { //! separated0(parse_digits, ",").parse_next(input) //! } //! -//! // ... -//! # fn parse_digits(input: &str) -> IResult<&str, usize> { +//! # fn parse_digits(input: &mut &str) -> PResult<usize> { //! # dispatch!(take(2usize); -//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), -//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), -//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), -//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), -//! # _ => fail, -//! # ).parse_next(input) +//! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), +//! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), +//! # "0d" => parse_dec_digits.try_map(|s| usize::from_str_radix(s, 10)), +//! # "0x" => parse_hex_digits.try_map(|s| usize::from_str_radix(s, 16)), +//! # _ => fail, +//! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -190,20 +255,22 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b,0x3c4d,0x5e6f Hello"; +//! let mut input = "0x1a2b,0x3c4d,0x5e6f Hello"; //! -//! let (remainder, digits) = recognize_list.parse_next(input).unwrap(); +//! let digits = recognize_list.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(digits, "0x1a2b,0x3c4d,0x5e6f"); //! -//! assert!(parse_digits("ghiWorld").is_err()); +//! assert!(parse_digits(&mut "ghiWorld").is_err()); //! } //! ``` +//! See [`combinator`] for more repetition parsers. #![allow(unused_imports)] use super::chapter_2; use super::chapter_3; +use crate::combinator; use crate::combinator::repeat; use crate::combinator::separated0; use crate::stream::Accumulate; diff --git a/vendor/winnow/src/_tutorial/chapter_6.rs b/vendor/winnow/src/_tutorial/chapter_6.rs index 268e38a31..d7d2c9fc8 100644 --- a/vendor/winnow/src/_tutorial/chapter_6.rs +++ b/vendor/winnow/src/_tutorial/chapter_6.rs @@ -2,51 +2,48 @@ //! //! ## `Error` //! -//! Back in [`chapter_1`], we glossed over the `Err` side of [`IResult`]. `IResult<I, O>` is -//! actually short for `IResult<I, O, E=Error>` where [`Error`] is a cheap, universal error type -//! for getting started. When humans are producing the file, like with `toml`, you might want to -//! sacrifice some performance for providing more details on how to resolve the problem +//! Back in [`chapter_1`], we glossed over the `Err` side of [`PResult`]. `PResult<O>` is +//! actually short for `PResult<O, E=ContextError>` where [`ContextError`] is a relatively cheap +//! way of building up reasonable errors for humans. //! -//! winnow includes [`VerboseError`] for this but you can [customize the error as you -//! wish][_topic::error]. You can use [`Parser::context`] to annotate the error with custom types +//! You can use [`Parser::context`] to annotate the error with custom types //! while unwinding to further improve the error quality. //! //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; -//! use winnow::error::VerboseError; +//! use winnow::error::StrContext; //! -//! fn parse_digits(input: &str) -> IResult<&str, (&str, &str), VerboseError<&str>> { +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { //! alt(( -//! ("0b", parse_bin_digits).context("binary"), -//! ("0o", parse_oct_digits).context("octal"), -//! ("0d", parse_dec_digits).context("decimal"), -//! ("0x", parse_hex_digits).context("hexadecimal"), +//! ("0b", parse_bin_digits).context(StrContext::Label("binary")), +//! ("0o", parse_oct_digits).context(StrContext::Label("octal")), +//! ("0d", parse_dec_digits).context(StrContext::Label("decimal")), +//! ("0x", parse_hex_digits).context(StrContext::Label("hexadecimal")), //! )).parse_next(input) //! } //! //! // ... -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -55,11 +52,11 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, (prefix, digits)) = parse_digits.parse_next(input).unwrap(); +//! let (prefix, digits) = parse_digits.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(prefix, "0x"); //! assert_eq!(digits, "1a2b"); //! } @@ -71,58 +68,57 @@ //! //! ## `ErrMode` //! -//! Let's break down `IResult<I, O, E>` one step further: +//! Let's break down `PResult<O, E>` one step further: //! ```rust -//! # use winnow::error::Error; +//! # use winnow::error::ErrorKind; //! # use winnow::error::ErrMode; -//! pub type IResult<I, O, E = Error<I>> = Result<(I, O), ErrMode<E>>; +//! pub type OResult<O, E = ErrorKind> = Result<O, ErrMode<E>>; //! ``` -//! `IResult` is just a fancy wrapper around `Result` that wraps our error in an [`ErrMode`] +//! [`PResult`] is just a fancy wrapper around `Result` that wraps our error in an [`ErrMode`] //! type. //! -//! `ErrMode` is an enum with `Backtrack` and `Cut` variants (ignore `Incomplete` as its only -//! relevant for [streaming][_topic::stream]). By default, errors are `Backtrack`, meaning that -//! other parsing branches will be attempted on failure, like the next case of an `alt`. `Cut` +//! [`ErrMode`] is an enum with [`Backtrack`] and [`Cut`] variants (ignore [`Incomplete`] as its only +//! relevant for [streaming][_topic::stream]). By default, errors are [`Backtrack`], meaning that +//! other parsing branches will be attempted on failure, like the next case of an [`alt`]. [`Cut`] //! shortcircuits all other branches, immediately reporting the error. //! //! So we can get the correct `context` by modifying the above example with [`cut_err`]: //! ```rust -//! # use winnow::IResult; -//! # use winnow::Parser; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; -//! # use winnow::error::VerboseError; +//! # use winnow::error::StrContext; //! use winnow::combinator::cut_err; //! -//! fn parse_digits(input: &str) -> IResult<&str, (&str, &str), VerboseError<&str>> { +//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { //! alt(( -//! ("0b", cut_err(parse_bin_digits)).context("binary"), -//! ("0o", cut_err(parse_oct_digits)).context("octal"), -//! ("0d", cut_err(parse_dec_digits)).context("decimal"), -//! ("0x", cut_err(parse_hex_digits)).context("hexadecimal"), +//! ("0b", cut_err(parse_bin_digits)).context(StrContext::Label("binary")), +//! ("0o", cut_err(parse_oct_digits)).context(StrContext::Label("octal")), +//! ("0d", cut_err(parse_dec_digits)).context(StrContext::Label("decimal")), +//! ("0x", cut_err(parse_hex_digits)).context(StrContext::Label("hexadecimal")), //! )).parse_next(input) //! } //! //! // ... -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str, VerboseError<&str>> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -131,11 +127,11 @@ //! # } //! //! fn main() { -//! let input = "0x1a2b Hello"; +//! let mut input = "0x1a2b Hello"; //! -//! let (remainder, (prefix, digits)) = parse_digits.parse_next(input).unwrap(); +//! let (prefix, digits) = parse_digits.parse_next(&mut input).unwrap(); //! -//! assert_eq!(remainder, " Hello"); +//! assert_eq!(input, " Hello"); //! assert_eq!(prefix, "0x"); //! assert_eq!(digits, "1a2b"); //! } @@ -147,10 +143,11 @@ use super::chapter_1; use super::chapter_3; use crate::combinator::alt; use crate::combinator::cut_err; +use crate::error::ContextError; use crate::error::ErrMode; -use crate::error::Error; -use crate::error::VerboseError; -use crate::IResult; +use crate::error::ErrMode::*; +use crate::error::ErrorKind; +use crate::PResult; use crate::Parser; use crate::_topic; diff --git a/vendor/winnow/src/_tutorial/chapter_7.rs b/vendor/winnow/src/_tutorial/chapter_7.rs index c20607c36..45f2d2d2a 100644 --- a/vendor/winnow/src/_tutorial/chapter_7.rs +++ b/vendor/winnow/src/_tutorial/chapter_7.rs @@ -3,53 +3,62 @@ //! So far, we've highlighted how to incrementally parse, but how do we bring this all together //! into our application? //! -//! The type we've been working with looks like: +//! Parsers we've been working with look like: //! ```rust -//! # use winnow::error::VerboseError; +//! # use winnow::error::ContextError; //! # use winnow::error::ErrMode; -//! type IResult<'i, O> = Result< -//! (&'i str, O), -//! ErrMode< -//! VerboseError<&'i str> -//! > +//! # use winnow::Parser; +//! # +//! pub fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! // ... +//! # Ok("") +//! } +//! +//! type PResult<O> = Result< +//! O, +//! ErrMode<ContextError> //! >; //! ``` -//! 1. We have to decide what to do about the `remainder` of the input. -//! 2. The error type is not compatible with the rest of the Rust ecosystem +//! 1. We have to decide what to do about the "remainder" of the `input`. +//! 2. The [`ErrMode<ContextError>`] is not compatible with the rest of the Rust ecosystem. +//! Normally, Rust applications want errors that are `std::error::Error + Send + Sync + 'static` +//! meaning: +//! - They implement the [`std::error::Error`] trait +//! - They can be sent across threads +//! - They are safe to be referenced across threads +//! - They do not borrow //! -//! Normally, Rust applications want errors that are `std::error::Error + Send + Sync + 'static` -//! meaning: -//! - They implement the [`std::error::Error`] trait -//! - They can be sent across threads -//! - They are safe to be referenced across threads -//! - They do not borrow -//! -//! winnow provides some helpers for this: +//! winnow provides [`Parser::parse`] to help with this: +//! - Ensures we hit [`eof`] +//! - Removes the [`ErrMode`] wrapper +//! - Wraps the error in [`ParseError`] +//! - Provides access to the original [`input`][ParseError::input] with the +//! [`offset`][ParseError::offset] of where it failed +//! - Provides a default renderer (via [`std::fmt::Display`]) //! ```rust -//! # use winnow::IResult; +//! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; //! # use winnow::combinator::fail; //! use winnow::Parser; -//! use winnow::error::Error; //! //! #[derive(Debug, PartialEq, Eq)] //! pub struct Hex(usize); //! //! impl std::str::FromStr for Hex { -//! type Err = Error<String>; +//! type Err = String; //! //! fn from_str(input: &str) -> Result<Self, Self::Err> { //! parse_digits //! .map(Hex) //! .parse(input) -//! .map_err(|e| e.into_owned()) +//! .map_err(|e| e.to_string()) //! } //! } //! //! // ... -//! # fn parse_digits(input: &str) -> IResult<&str, usize> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<usize> { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -59,25 +68,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -95,17 +104,14 @@ //! assert!(input.parse::<Hex>().is_err()); //! } //! ``` -//! - Ensures we hit [`eof`] -//! - Removes the [`ErrMode`] wrapper -//! -//! [`Error::into_owned`]: -//! - Converts the `&str` in `Error` to `String` which enables support for [`std::error::Error`] #![allow(unused_imports)] use super::chapter_1; use crate::combinator::eof; use crate::error::ErrMode; -use crate::error::Error; -use crate::IResult; +use crate::error::InputError; +use crate::error::ParseError; +use crate::PResult; +use crate::Parser; pub use super::chapter_6 as previous; diff --git a/vendor/winnow/src/ascii/mod.rs b/vendor/winnow/src/ascii/mod.rs index 4ada7c00b..8b3119fbf 100644 --- a/vendor/winnow/src/ascii/mod.rs +++ b/vendor/winnow/src/ascii/mod.rs @@ -8,21 +8,20 @@ mod tests; use crate::lib::std::ops::{Add, Shl}; use crate::combinator::alt; -use crate::token::one_of; - use crate::combinator::cut_err; use crate::combinator::opt; -use crate::error::ParseError; +use crate::error::ParserError; use crate::error::{ErrMode, ErrorKind, Needed}; -use crate::stream::ContainsToken; -use crate::stream::{AsBStr, AsChar, Offset, ParseSlice, Stream, StreamIsPartial}; +use crate::stream::{AsBStr, AsChar, ParseSlice, Stream, StreamIsPartial}; use crate::stream::{Compare, CompareResult}; +use crate::token::one_of; +use crate::token::take_till0; use crate::token::take_while; use crate::trace::trace; -use crate::IResult; +use crate::PResult; use crate::Parser; -/// Recognizes the string "\r\n". +/// Recognizes the string `"\r\n"`. /// /// *Complete version*: Will return an error if there's not enough input data. /// @@ -31,36 +30,38 @@ use crate::Parser; /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}}; /// # use winnow::ascii::crlf; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// crlf(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// crlf.parse_next(input) /// } /// -/// assert_eq!(parser("\r\nc"), Ok(("c", "\r\n"))); -/// assert_eq!(parser("ab\r\nc"), Err(ErrMode::Backtrack(Error::new("ab\r\nc", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(parser.parse_peek("ab\r\nc"), Err(ErrMode::Backtrack(InputError::new("ab\r\nc", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::crlf; -/// assert_eq!(crlf::<_, Error<_>>(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); -/// assert_eq!(crlf::<_, Error<_>>(Partial::new("ab\r\nc")), Err(ErrMode::Backtrack(Error::new(Partial::new("ab\r\nc"), ErrorKind::Tag)))); -/// assert_eq!(crlf::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(2)))); +/// assert_eq!(crlf::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); +/// assert_eq!(crlf::<_, InputError<_>>.parse_peek(Partial::new("ab\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("ab\r\nc"), ErrorKind::Tag)))); +/// assert_eq!(crlf::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn crlf<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn crlf<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, I: Compare<&'static str>, { - trace("crlf", move |input: I| "\r\n".parse_next(input)).parse_next(input) + trace("crlf", "\r\n").parse_next(input) } -/// Recognizes a string of any char except '\r\n' or '\n'. +/// Recognizes a string of any char except `"\r\n"` or `"\n"`. /// /// *Complete version*: Will return an error if there's not enough input data. /// @@ -69,39 +70,41 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::not_line_ending; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// not_line_ending(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// not_line_ending.parse_next(input) /// } /// -/// assert_eq!(parser("ab\r\nc"), Ok(("\r\nc", "ab"))); -/// assert_eq!(parser("ab\nc"), Ok(("\nc", "ab"))); -/// assert_eq!(parser("abc"), Ok(("", "abc"))); -/// assert_eq!(parser(""), Ok(("", ""))); -/// assert_eq!(parser("a\rb\nc"), Err(ErrMode::Backtrack(Error { input: "a\rb\nc", kind: ErrorKind::Tag }))); -/// assert_eq!(parser("a\rbc"), Err(ErrMode::Backtrack(Error { input: "a\rbc", kind: ErrorKind::Tag }))); +/// assert_eq!(parser.parse_peek("ab\r\nc"), Ok(("\r\nc", "ab"))); +/// assert_eq!(parser.parse_peek("ab\nc"), Ok(("\nc", "ab"))); +/// assert_eq!(parser.parse_peek("abc"), Ok(("", "abc"))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("a\rb\nc"), Err(ErrMode::Backtrack(InputError::new("\rb\nc", ErrorKind::Tag )))); +/// assert_eq!(parser.parse_peek("a\rbc"), Err(ErrMode::Backtrack(InputError::new("\rbc", ErrorKind::Tag )))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::not_line_ending; -/// assert_eq!(not_line_ending::<_, Error<_>>(Partial::new("ab\r\nc")), Ok((Partial::new("\r\nc"), "ab"))); -/// assert_eq!(not_line_ending::<_, Error<_>>(Partial::new("abc")), Err(ErrMode::Incomplete(Needed::Unknown))); -/// assert_eq!(not_line_ending::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::Unknown))); -/// assert_eq!(not_line_ending::<_, Error<_>>(Partial::new("a\rb\nc")), Err(ErrMode::Backtrack(Error::new(Partial::new("a\rb\nc"), ErrorKind::Tag )))); -/// assert_eq!(not_line_ending::<_, Error<_>>(Partial::new("a\rbc")), Err(ErrMode::Backtrack(Error::new(Partial::new("a\rbc"), ErrorKind::Tag )))); +/// assert_eq!(not_line_ending::<_, InputError<_>>.parse_peek(Partial::new("ab\r\nc")), Ok((Partial::new("\r\nc"), "ab"))); +/// assert_eq!(not_line_ending::<_, InputError<_>>.parse_peek(Partial::new("abc")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(not_line_ending::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(not_line_ending::<_, InputError<_>>.parse_peek(Partial::new("a\rb\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\rb\nc"), ErrorKind::Tag )))); +/// assert_eq!(not_line_ending::<_, InputError<_>>.parse_peek(Partial::new("a\rbc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\rbc"), ErrorKind::Tag )))); /// ``` #[inline(always)] -pub fn not_line_ending<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn not_line_ending<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, - I: Stream + AsBStr, + I: Stream, I: Compare<&'static str>, - <I as Stream>::Token: AsChar, + <I as Stream>::Token: AsChar + Clone, { - trace("not_line_ending", move |input: I| { + trace("not_line_ending", move |input: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { not_line_ending_::<_, _, true>(input) } else { @@ -111,45 +114,34 @@ where .parse_next(input) } -fn not_line_ending_<I, E: ParseError<I>, const PARTIAL: bool>( - input: I, -) -> IResult<I, <I as Stream>::Slice, E> +fn not_line_ending_<I, E: ParserError<I>, const PARTIAL: bool>( + input: &mut I, +) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, - I: Stream + AsBStr, + I: Stream, I: Compare<&'static str>, - <I as Stream>::Token: AsChar, + <I as Stream>::Token: AsChar + Clone, { - match input.offset_for(|item| { - let c = item.as_char(); - c == '\r' || c == '\n' - }) { - None if PARTIAL && input.is_partial() => Err(ErrMode::Incomplete(Needed::Unknown)), - None => Ok(input.next_slice(input.eof_offset())), - Some(offset) => { - let (new_input, res) = input.next_slice(offset); - let bytes = new_input.as_bstr(); - let nth = bytes[0]; - if nth == b'\r' { - let comp = new_input.compare("\r\n"); - match comp { - //FIXME: calculate the right index - CompareResult::Ok => {} - CompareResult::Incomplete if PARTIAL && input.is_partial() => { - return Err(ErrMode::Incomplete(Needed::Unknown)); - } - CompareResult::Incomplete | CompareResult::Error => { - let e: ErrorKind = ErrorKind::Tag; - return Err(ErrMode::from_error_kind(input, e)); - } - } + let res = take_till0(('\r', '\n')).parse_next(input)?; + if input.compare("\r") == CompareResult::Ok { + let comp = input.compare("\r\n"); + match comp { + //FIXME: calculate the right index + CompareResult::Ok => {} + CompareResult::Incomplete if PARTIAL && input.is_partial() => { + return Err(ErrMode::Incomplete(Needed::Unknown)); + } + CompareResult::Incomplete | CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + return Err(ErrMode::from_error_kind(input, e)); } - Ok((new_input, res)) } } + Ok(res) } -/// Recognizes an end of line (both '\n' and '\r\n'). +/// Recognizes an end of line (both `"\n"` and `"\r\n"`). /// /// *Complete version*: Will return an error if there's not enough input data. /// @@ -158,39 +150,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::line_ending; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// line_ending(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// line_ending.parse_next(input) /// } /// -/// assert_eq!(parser("\r\nc"), Ok(("c", "\r\n"))); -/// assert_eq!(parser("ab\r\nc"), Err(ErrMode::Backtrack(Error::new("ab\r\nc", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(parser.parse_peek("ab\r\nc"), Err(ErrMode::Backtrack(InputError::new("ab\r\nc", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::line_ending; -/// assert_eq!(line_ending::<_, Error<_>>(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); -/// assert_eq!(line_ending::<_, Error<_>>(Partial::new("ab\r\nc")), Err(ErrMode::Backtrack(Error::new(Partial::new("ab\r\nc"), ErrorKind::Tag)))); -/// assert_eq!(line_ending::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(line_ending::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); +/// assert_eq!(line_ending::<_, InputError<_>>.parse_peek(Partial::new("ab\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("ab\r\nc"), ErrorKind::Tag)))); +/// assert_eq!(line_ending::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn line_ending<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn line_ending<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, I: Compare<&'static str>, { - trace("line_ending", move |input: I| { - alt(("\n", "\r\n")).parse_next(input) - }) - .parse_next(input) + trace("line_ending", alt(("\n", "\r\n"))).parse_next(input) } -/// Matches a newline character '\n'. +/// Matches a newline character `'\n'`. /// /// *Complete version*: Will return an error if there's not enough input data. /// @@ -199,40 +190,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::newline; -/// fn parser(input: &str) -> IResult<&str, char> { -/// newline(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<char, InputError<&'s str>> { +/// newline.parse_next(input) /// } /// -/// assert_eq!(parser("\nc"), Ok(("c", '\n'))); -/// assert_eq!(parser("\r\nc"), Err(ErrMode::Backtrack(Error::new("\r\nc", ErrorKind::Verify)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(parser.parse_peek("\nc"), Ok(("c", '\n'))); +/// assert_eq!(parser.parse_peek("\r\nc"), Err(ErrMode::Backtrack(InputError::new("\r\nc", ErrorKind::Verify)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::newline; -/// assert_eq!(newline::<_, Error<_>>(Partial::new("\nc")), Ok((Partial::new("c"), '\n'))); -/// assert_eq!(newline::<_, Error<_>>(Partial::new("\r\nc")), Err(ErrMode::Backtrack(Error::new(Partial::new("\r\nc"), ErrorKind::Verify)))); -/// assert_eq!(newline::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("\nc")), Ok((Partial::new("c"), '\n'))); +/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\r\nc"), ErrorKind::Verify)))); +/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn newline<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> +pub fn newline<I, Error: ParserError<I>>(input: &mut I) -> PResult<char, Error> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, { - trace("newline", move |input: I| { - '\n'.map(|c: <I as Stream>::Token| c.as_char()) - .parse_next(input) - }) - .parse_next(input) + trace("newline", '\n'.map(AsChar::as_char)).parse_next(input) } -/// Matches a tab character '\t'. +/// Matches a tab character `'\t'`. /// /// *Complete version*: Will return an error if there's not enough input data. /// @@ -241,40 +230,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::tab; -/// fn parser(input: &str) -> IResult<&str, char> { -/// tab(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<char, InputError<&'s str>> { +/// tab.parse_next(input) /// } /// -/// assert_eq!(parser("\tc"), Ok(("c", '\t'))); -/// assert_eq!(parser("\r\nc"), Err(ErrMode::Backtrack(Error::new("\r\nc", ErrorKind::Verify)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(parser.parse_peek("\tc"), Ok(("c", '\t'))); +/// assert_eq!(parser.parse_peek("\r\nc"), Err(ErrMode::Backtrack(InputError::new("\r\nc", ErrorKind::Verify)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::tab; -/// assert_eq!(tab::<_, Error<_>>(Partial::new("\tc")), Ok((Partial::new("c"), '\t'))); -/// assert_eq!(tab::<_, Error<_>>(Partial::new("\r\nc")), Err(ErrMode::Backtrack(Error::new(Partial::new("\r\nc"), ErrorKind::Verify)))); -/// assert_eq!(tab::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("\tc")), Ok((Partial::new("c"), '\t'))); +/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\r\nc"), ErrorKind::Verify)))); +/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn tab<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> +pub fn tab<I, Error: ParserError<I>>(input: &mut I) -> PResult<char, Error> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, { - trace("tab", move |input: I| { - '\t'.map(|c: <I as Stream>::Token| c.as_char()) - .parse_next(input) - }) - .parse_next(input) + trace("tab", '\t'.map(AsChar::as_char)).parse_next(input) } -/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: `'a'..='z'`, `'A'..='Z'` /// /// *Complete version*: Will return the whole input if no terminating token is found (a non /// alphabetic character). @@ -285,39 +272,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::alpha0; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// alpha0(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// alpha0.parse_next(input) /// } /// -/// assert_eq!(parser("ab1c"), Ok(("1c", "ab"))); -/// assert_eq!(parser("1c"), Ok(("1c", ""))); -/// assert_eq!(parser(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("ab1c"), Ok(("1c", "ab"))); +/// assert_eq!(parser.parse_peek("1c"), Ok(("1c", ""))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alpha0; -/// assert_eq!(alpha0::<_, Error<_>>(Partial::new("ab1c")), Ok((Partial::new("1c"), "ab"))); -/// assert_eq!(alpha0::<_, Error<_>>(Partial::new("1c")), Ok((Partial::new("1c"), ""))); -/// assert_eq!(alpha0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alpha0::<_, InputError<_>>.parse_peek(Partial::new("ab1c")), Ok((Partial::new("1c"), "ab"))); +/// assert_eq!(alpha0::<_, InputError<_>>.parse_peek(Partial::new("1c")), Ok((Partial::new("1c"), ""))); +/// assert_eq!(alpha0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alpha0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn alpha0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("alpha0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| c.is_alpha()).parse_next(input) - }) - .parse_next(input) + trace("alpha0", take_while(0.., AsChar::is_alpha)).parse_next(input) } -/// Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// Recognizes one or more lowercase and uppercase ASCII alphabetic characters: `'a'..='z'`, `'A'..='Z'` /// /// *Complete version*: Will return an error if there's not enough input data, /// or the whole input if no terminating token is found (a non alphabetic character). @@ -328,39 +314,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::alpha1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// alpha1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// alpha1.parse_next(input) /// } /// -/// assert_eq!(parser("aB1c"), Ok(("1c", "aB"))); -/// assert_eq!(parser("1c"), Err(ErrMode::Backtrack(Error::new("1c", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("aB1c"), Ok(("1c", "aB"))); +/// assert_eq!(parser.parse_peek("1c"), Err(ErrMode::Backtrack(InputError::new("1c", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alpha1; -/// assert_eq!(alpha1::<_, Error<_>>(Partial::new("aB1c")), Ok((Partial::new("1c"), "aB"))); -/// assert_eq!(alpha1::<_, Error<_>>(Partial::new("1c")), Err(ErrMode::Backtrack(Error::new(Partial::new("1c"), ErrorKind::Slice)))); -/// assert_eq!(alpha1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alpha1::<_, InputError<_>>.parse_peek(Partial::new("aB1c")), Ok((Partial::new("1c"), "aB"))); +/// assert_eq!(alpha1::<_, InputError<_>>.parse_peek(Partial::new("1c")), Err(ErrMode::Backtrack(InputError::new(Partial::new("1c"), ErrorKind::Slice)))); +/// assert_eq!(alpha1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alpha1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn alpha1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("alpha1", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| c.is_alpha()).parse_next(input) - }) - .parse_next(input) + trace("alpha1", take_while(1.., AsChar::is_alpha)).parse_next(input) } -/// Recognizes zero or more ASCII numerical characters: 0-9 +/// Recognizes zero or more ASCII numerical characters: `'0'..='9'` /// /// *Complete version*: Will return an error if there's not enough input data, /// or the whole input if no terminating token is found (a non digit character). @@ -371,40 +356,39 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::digit0; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// digit0(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// digit0.parse_next(input) /// } /// -/// assert_eq!(parser("21c"), Ok(("c", "21"))); -/// assert_eq!(parser("21"), Ok(("", "21"))); -/// assert_eq!(parser("a21c"), Ok(("a21c", ""))); -/// assert_eq!(parser(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("21c"), Ok(("c", "21"))); +/// assert_eq!(parser.parse_peek("21"), Ok(("", "21"))); +/// assert_eq!(parser.parse_peek("a21c"), Ok(("a21c", ""))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::digit0; -/// assert_eq!(digit0::<_, Error<_>>(Partial::new("21c")), Ok((Partial::new("c"), "21"))); -/// assert_eq!(digit0::<_, Error<_>>(Partial::new("a21c")), Ok((Partial::new("a21c"), ""))); -/// assert_eq!(digit0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(digit0::<_, InputError<_>>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21"))); +/// assert_eq!(digit0::<_, InputError<_>>.parse_peek(Partial::new("a21c")), Ok((Partial::new("a21c"), ""))); +/// assert_eq!(digit0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn digit0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn digit0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("digit0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| c.is_dec_digit()).parse_next(input) - }) - .parse_next(input) + trace("digit0", take_while(0.., AsChar::is_dec_digit)).parse_next(input) } -/// Recognizes one or more ASCII numerical characters: 0-9 +/// Recognizes one or more ASCII numerical characters: `'0'..='9'` /// /// *Complete version*: Will return an error if there's not enough input data, /// or the whole input if no terminating token is found (a non digit character). @@ -415,24 +399,26 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::digit1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// digit1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// digit1.parse_next(input) /// } /// -/// assert_eq!(parser("21c"), Ok(("c", "21"))); -/// assert_eq!(parser("c1"), Err(ErrMode::Backtrack(Error::new("c1", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("21c"), Ok(("c", "21"))); +/// assert_eq!(parser.parse_peek("c1"), Err(ErrMode::Backtrack(InputError::new("c1", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::digit1; -/// assert_eq!(digit1::<_, Error<_>>(Partial::new("21c")), Ok((Partial::new("c"), "21"))); -/// assert_eq!(digit1::<_, Error<_>>(Partial::new("c1")), Err(ErrMode::Backtrack(Error::new(Partial::new("c1"), ErrorKind::Slice)))); -/// assert_eq!(digit1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(digit1::<_, InputError<_>>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21"))); +/// assert_eq!(digit1::<_, InputError<_>>.parse_peek(Partial::new("c1")), Err(ErrMode::Backtrack(InputError::new(Partial::new("c1"), ErrorKind::Slice)))); +/// assert_eq!(digit1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` /// /// ## Parsing an integer @@ -440,30 +426,29 @@ where /// You can use `digit1` in combination with [`Parser::try_map`][crate::Parser::try_map] to parse an integer: /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed, Parser}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed, Parser}; /// # use winnow::ascii::digit1; -/// fn parser(input: &str) -> IResult<&str, u32> { +/// fn parser<'s>(input: &mut &'s str) -> PResult<u32, InputError<&'s str>> { /// digit1.try_map(str::parse).parse_next(input) /// } /// -/// assert_eq!(parser("416"), Ok(("", 416))); -/// assert_eq!(parser("12b"), Ok(("b", 12))); -/// assert!(parser("b").is_err()); +/// assert_eq!(parser.parse_peek("416"), Ok(("", 416))); +/// assert_eq!(parser.parse_peek("12b"), Ok(("b", 12))); +/// assert!(parser.parse_peek("b").is_err()); /// ``` #[inline(always)] -pub fn digit1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn digit1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("digit1", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| c.is_dec_digit()).parse_next(input) - }) - .parse_next(input) + trace("digit1", take_while(1.., AsChar::is_dec_digit)).parse_next(input) } -/// Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// Recognizes zero or more ASCII hexadecimal numerical characters: `'0'..='9'`, `'A'..='F'`, +/// `'a'..='f'` /// /// *Complete version*: Will return the whole input if no terminating token is found (a non hexadecimal digit character). /// @@ -473,39 +458,39 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::hex_digit0; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// hex_digit0(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// hex_digit0.parse_next(input) /// } /// -/// assert_eq!(parser("21cZ"), Ok(("Z", "21c"))); -/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); -/// assert_eq!(parser(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(parser.parse_peek("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::hex_digit0; -/// assert_eq!(hex_digit0::<_, Error<_>>(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); -/// assert_eq!(hex_digit0::<_, Error<_>>(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(hex_digit0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(hex_digit0::<_, InputError<_>>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); +/// assert_eq!(hex_digit0::<_, InputError<_>>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(hex_digit0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn hex_digit0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn hex_digit0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("hex_digit0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| c.is_hex_digit()).parse_next(input) - }) - .parse_next(input) + trace("hex_digit0", take_while(0.., AsChar::is_hex_digit)).parse_next(input) } -/// Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// Recognizes one or more ASCII hexadecimal numerical characters: `'0'..='9'`, `'A'..='F'`, +/// `'a'..='f'` /// /// *Complete version*: Will return an error if there's not enough input data, /// or the whole input if no terminating token is found (a non hexadecimal digit character). @@ -516,39 +501,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::hex_digit1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// hex_digit1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// hex_digit1.parse_next(input) /// } /// -/// assert_eq!(parser("21cZ"), Ok(("Z", "21c"))); -/// assert_eq!(parser("H2"), Err(ErrMode::Backtrack(Error::new("H2", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(parser.parse_peek("H2"), Err(ErrMode::Backtrack(InputError::new("H2", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::hex_digit1; -/// assert_eq!(hex_digit1::<_, Error<_>>(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); -/// assert_eq!(hex_digit1::<_, Error<_>>(Partial::new("H2")), Err(ErrMode::Backtrack(Error::new(Partial::new("H2"), ErrorKind::Slice)))); -/// assert_eq!(hex_digit1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); +/// assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(Partial::new("H2")), Err(ErrMode::Backtrack(InputError::new(Partial::new("H2"), ErrorKind::Slice)))); +/// assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn hex_digit1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn hex_digit1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("hex_digit1", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| c.is_hex_digit()).parse_next(input) - }) - .parse_next(input) + trace("hex_digit1", take_while(1.., AsChar::is_hex_digit)).parse_next(input) } -/// Recognizes zero or more octal characters: 0-7 +/// Recognizes zero or more octal characters: `'0'..='7'` /// /// *Complete version*: Will return the whole input if no terminating token is found (a non octal /// digit character). @@ -559,39 +543,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::oct_digit0; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// oct_digit0(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// oct_digit0.parse_next(input) /// } /// -/// assert_eq!(parser("21cZ"), Ok(("cZ", "21"))); -/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); -/// assert_eq!(parser(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(parser.parse_peek("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::oct_digit0; -/// assert_eq!(oct_digit0::<_, Error<_>>(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); -/// assert_eq!(oct_digit0::<_, Error<_>>(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(oct_digit0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(oct_digit0::<_, InputError<_>>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); +/// assert_eq!(oct_digit0::<_, InputError<_>>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(oct_digit0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn oct_digit0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn oct_digit0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("oct_digit0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| c.is_oct_digit()).parse_next(input) - }) - .parse_next(input) + trace("oct_digit0", take_while(0.., AsChar::is_oct_digit)).parse_next(input) } -/// Recognizes one or more octal characters: 0-7 +/// Recognizes one or more octal characters: `'0'..='7'` /// /// *Complete version*: Will return an error if there's not enough input data, /// or the whole input if no terminating token is found (a non octal digit character). @@ -602,39 +585,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::oct_digit1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// oct_digit1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// oct_digit1.parse_next(input) /// } /// -/// assert_eq!(parser("21cZ"), Ok(("cZ", "21"))); -/// assert_eq!(parser("H2"), Err(ErrMode::Backtrack(Error::new("H2", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(parser.parse_peek("H2"), Err(ErrMode::Backtrack(InputError::new("H2", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::oct_digit1; -/// assert_eq!(oct_digit1::<_, Error<_>>(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); -/// assert_eq!(oct_digit1::<_, Error<_>>(Partial::new("H2")), Err(ErrMode::Backtrack(Error::new(Partial::new("H2"), ErrorKind::Slice)))); -/// assert_eq!(oct_digit1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(oct_digit1::<_, InputError<_>>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); +/// assert_eq!(oct_digit1::<_, InputError<_>>.parse_peek(Partial::new("H2")), Err(ErrMode::Backtrack(InputError::new(Partial::new("H2"), ErrorKind::Slice)))); +/// assert_eq!(oct_digit1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn oct_digit1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn oct_digit1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("oct_digit0", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| c.is_oct_digit()).parse_next(input) - }) - .parse_next(input) + trace("oct_digit0", take_while(1.., AsChar::is_oct_digit)).parse_next(input) } -/// Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// Recognizes zero or more ASCII numerical and alphabetic characters: `'a'..='z'`, `'A'..='Z'`, `'0'..='9'` /// /// *Complete version*: Will return the whole input if no terminating token is found (a non /// alphanumerical character). @@ -645,39 +627,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::alphanumeric0; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// alphanumeric0(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// alphanumeric0.parse_next(input) /// } /// -/// assert_eq!(parser("21cZ%1"), Ok(("%1", "21cZ"))); -/// assert_eq!(parser("&Z21c"), Ok(("&Z21c", ""))); -/// assert_eq!(parser(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(parser.parse_peek("&Z21c"), Ok(("&Z21c", ""))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alphanumeric0; -/// assert_eq!(alphanumeric0::<_, Error<_>>(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); -/// assert_eq!(alphanumeric0::<_, Error<_>>(Partial::new("&Z21c")), Ok((Partial::new("&Z21c"), ""))); -/// assert_eq!(alphanumeric0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alphanumeric0::<_, InputError<_>>.parse_peek(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); +/// assert_eq!(alphanumeric0::<_, InputError<_>>.parse_peek(Partial::new("&Z21c")), Ok((Partial::new("&Z21c"), ""))); +/// assert_eq!(alphanumeric0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alphanumeric0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn alphanumeric0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("alphanumeric0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| c.is_alphanum()).parse_next(input) - }) - .parse_next(input) + trace("alphanumeric0", take_while(0.., AsChar::is_alphanum)).parse_next(input) } -/// Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// Recognizes one or more ASCII numerical and alphabetic characters: `'a'..='z'`, `'A'..='Z'`, `'0'..='9'` /// /// *Complete version*: Will return an error if there's not enough input data, /// or the whole input if no terminating token is found (a non alphanumerical character). @@ -688,36 +669,35 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::alphanumeric1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// alphanumeric1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// alphanumeric1.parse_next(input) /// } /// -/// assert_eq!(parser("21cZ%1"), Ok(("%1", "21cZ"))); -/// assert_eq!(parser("&H2"), Err(ErrMode::Backtrack(Error::new("&H2", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(parser.parse_peek("&H2"), Err(ErrMode::Backtrack(InputError::new("&H2", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alphanumeric1; -/// assert_eq!(alphanumeric1::<_, Error<_>>(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); -/// assert_eq!(alphanumeric1::<_, Error<_>>(Partial::new("&H2")), Err(ErrMode::Backtrack(Error::new(Partial::new("&H2"), ErrorKind::Slice)))); -/// assert_eq!(alphanumeric1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); +/// assert_eq!(alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new("&H2")), Err(ErrMode::Backtrack(InputError::new(Partial::new("&H2"), ErrorKind::Slice)))); +/// assert_eq!(alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alphanumeric1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn alphanumeric1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, <I as Stream>::Token: AsChar, { - trace("alphanumeric1", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| c.is_alphanum()).parse_next(input) - }) - .parse_next(input) + trace("alphanumeric1", take_while(1.., AsChar::is_alphanum)).parse_next(input) } /// Recognizes zero or more spaces and tabs. @@ -731,34 +711,28 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::space0; -/// assert_eq!(space0::<_, Error<_>>(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); -/// assert_eq!(space0::<_, Error<_>>(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(space0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(space0::<_, InputError<_>>.parse_peek(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); +/// assert_eq!(space0::<_, InputError<_>>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(space0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn space0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn space0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, { - trace("space0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| { - let ch = c.as_char(); - matches!(ch, ' ' | '\t') - }) - .parse_next(input) - }) - .parse_next(input) + trace("space0", take_while(0.., AsChar::is_space)).parse_next(input) } -/// Recognizes one or more spaces and tabs. +/// Recognizes zero or more spaces and tabs. /// -/// *Complete version*: Will return an error if there's not enough input data, -/// or the whole input if no terminating token is found (a non space character). +/// *Complete version*: Will return the whole input if no terminating token is found (a non space +/// character). /// /// *Partial version*: Will return `Err(winnow::error::ErrMode::Incomplete(_))` if there's not enough input data, /// or if no terminating token is found (a non space character). @@ -766,40 +740,35 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::space1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// space1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// space1.parse_next(input) /// } /// -/// assert_eq!(parser(" \t21c"), Ok(("21c", " \t"))); -/// assert_eq!(parser("H2"), Err(ErrMode::Backtrack(Error::new("H2", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(parser.parse_peek("H2"), Err(ErrMode::Backtrack(InputError::new("H2", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::space1; -/// assert_eq!(space1::<_, Error<_>>(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); -/// assert_eq!(space1::<_, Error<_>>(Partial::new("H2")), Err(ErrMode::Backtrack(Error::new(Partial::new("H2"), ErrorKind::Slice)))); -/// assert_eq!(space1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(space1::<_, InputError<_>>.parse_peek(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); +/// assert_eq!(space1::<_, InputError<_>>.parse_peek(Partial::new("H2")), Err(ErrMode::Backtrack(InputError::new(Partial::new("H2"), ErrorKind::Slice)))); +/// assert_eq!(space1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn space1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn space1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, { - trace("space1", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| { - let ch = c.as_char(); - matches!(ch, ' ' | '\t') - }) - .parse_next(input) - }) - .parse_next(input) + trace("space1", take_while(1.., AsChar::is_space)).parse_next(input) } /// Recognizes zero or more spaces, tabs, carriage returns and line feeds. @@ -813,40 +782,35 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::ascii::multispace0; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// multispace0(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// multispace0.parse_next(input) /// } /// -/// assert_eq!(parser(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); -/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); -/// assert_eq!(parser(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(parser.parse_peek("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::multispace0; -/// assert_eq!(multispace0::<_, Error<_>>(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); -/// assert_eq!(multispace0::<_, Error<_>>(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(multispace0::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(multispace0::<_, InputError<_>>.parse_peek(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); +/// assert_eq!(multispace0::<_, InputError<_>>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(multispace0::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn multispace0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn multispace0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, { - trace("multispace0", move |input: I| { - take_while(0.., |c: <I as Stream>::Token| { - let ch = c.as_char(); - matches!(ch, ' ' | '\t' | '\r' | '\n') - }) - .parse_next(input) - }) - .parse_next(input) + trace("multispace0", take_while(0.., (' ', '\t', '\r', '\n'))).parse_next(input) } /// Recognizes one or more spaces, tabs, carriage returns and line feeds. @@ -860,43 +824,38 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::ascii::multispace1; -/// fn parser(input: &str) -> IResult<&str, &str> { -/// multispace1(input) +/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// multispace1.parse_next(input) /// } /// -/// assert_eq!(parser(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); -/// assert_eq!(parser("H2"), Err(ErrMode::Backtrack(Error::new("H2", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(parser.parse_peek("H2"), Err(ErrMode::Backtrack(InputError::new("H2", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, error::Needed}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::multispace1; -/// assert_eq!(multispace1::<_, Error<_>>(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); -/// assert_eq!(multispace1::<_, Error<_>>(Partial::new("H2")), Err(ErrMode::Backtrack(Error::new(Partial::new("H2"), ErrorKind::Slice)))); -/// assert_eq!(multispace1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(multispace1::<_, InputError<_>>.parse_peek(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); +/// assert_eq!(multispace1::<_, InputError<_>>.parse_peek(Partial::new("H2")), Err(ErrMode::Backtrack(InputError::new(Partial::new("H2"), ErrorKind::Slice)))); +/// assert_eq!(multispace1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn multispace1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn multispace1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, { - trace("multispace1", move |input: I| { - take_while(1.., |c: <I as Stream>::Token| { - let ch = c.as_char(); - matches!(ch, ' ' | '\t' | '\r' | '\n') - }) - .parse_next(input) - }) - .parse_next(input) + trace("multispace1", take_while(1.., (' ', '\t', '\r', '\n'))).parse_next(input) } -/// Decode a decimal unsigned integer +/// Decode a decimal unsigned integer (e.g. [`u32`]) /// /// *Complete version*: can parse until the end of input. /// @@ -906,14 +865,14 @@ where #[doc(alias = "u32")] #[doc(alias = "u64")] #[doc(alias = "u128")] -pub fn dec_uint<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> +pub fn dec_uint<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, O: Uint, { - trace("dec_uint", move |input: I| { + trace("dec_uint", move |input: &mut I| { if input.eof_offset() == 0 { if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() { return Err(ErrMode::Incomplete(Needed::new(1))); @@ -936,7 +895,8 @@ where if offset == 0 { return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); } else { - return Ok((input.next_slice(offset).0, value)); + let _ = input.next_slice(offset); + return Ok(value); } } } @@ -945,7 +905,8 @@ where if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() { Err(ErrMode::Incomplete(Needed::new(1))) } else { - Ok((input.next_slice(input.eof_offset()).0, value)) + let _ = input.finish(); + Ok(value) } }) .parse_next(input) @@ -1049,7 +1010,7 @@ impl Uint for i128 { } } -/// Decode a decimal signed integer +/// Decode a decimal signed integer (e.g. [`i32`]) /// /// *Complete version*: can parse until the end of input. /// @@ -1059,19 +1020,19 @@ impl Uint for i128 { #[doc(alias = "i32")] #[doc(alias = "i64")] #[doc(alias = "i128")] -pub fn dec_int<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> +pub fn dec_int<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, O: Int, { - trace("dec_int", move |input: I| { + trace("dec_int", move |input: &mut I| { fn sign(token: impl AsChar) -> bool { let token = token.as_char(); token == '+' || token == '-' } - let (input, sign) = opt(crate::token::one_of(sign).map(AsChar::as_char)) + let sign = opt(crate::token::one_of(sign).map(AsChar::as_char)) .map(|c| c != Some('-')) .parse_next(input)?; @@ -1101,7 +1062,8 @@ where if offset == 0 { return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); } else { - return Ok((input.next_slice(offset).0, value)); + let _ = input.next_slice(offset); + return Ok(value); } } } @@ -1110,7 +1072,8 @@ where if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() { Err(ErrMode::Incomplete(Needed::new(1))) } else { - Ok((input.next_slice(input.eof_offset()).0, value)) + let _ = input.finish(); + Ok(value) } }) .parse_next(input) @@ -1152,7 +1115,7 @@ impl Int for i128 { } } -/// Decode a variable-width hexadecimal integer. +/// Decode a variable-width hexadecimal integer (e.g. [`u32`]) /// /// *Complete version*: Will parse until the end of input if it has fewer characters than the type /// supports. @@ -1164,34 +1127,34 @@ impl Int for i128 { /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// use winnow::ascii::hex_uint; /// -/// fn parser(s: &[u8]) -> IResult<&[u8], u32> { +/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<u32, InputError<&'s [u8]>> { /// hex_uint(s) /// } /// -/// assert_eq!(parser(&b"01AE"[..]), Ok((&b""[..], 0x01AE))); -/// assert_eq!(parser(&b"abc"[..]), Ok((&b""[..], 0x0ABC))); -/// assert_eq!(parser(&b"ggg"[..]), Err(ErrMode::Backtrack(Error::new(&b"ggg"[..], ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(&b"01AE"[..]), Ok((&b""[..], 0x01AE))); +/// assert_eq!(parser.parse_peek(&b"abc"[..]), Ok((&b""[..], 0x0ABC))); +/// assert_eq!(parser.parse_peek(&b"ggg"[..]), Err(ErrMode::Backtrack(InputError::new(&b"ggg"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// use winnow::ascii::hex_uint; /// -/// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { +/// fn parser<'s>(s: &mut Partial<&'s [u8]>) -> PResult<u32, InputError<Partial<&'s [u8]>>> { /// hex_uint(s) /// } /// -/// assert_eq!(parser(Partial::new(&b"01AE;"[..])), Ok((Partial::new(&b";"[..]), 0x01AE))); -/// assert_eq!(parser(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); -/// assert_eq!(parser(Partial::new(&b"ggg"[..])), Err(ErrMode::Backtrack(Error::new(Partial::new(&b"ggg"[..]), ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(Partial::new(&b"01AE;"[..])), Ok((Partial::new(&b";"[..]), 0x01AE))); +/// assert_eq!(parser.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(parser.parse_peek(Partial::new(&b"ggg"[..])), Err(ErrMode::Backtrack(InputError::new(Partial::new(&b"ggg"[..]), ErrorKind::Slice)))); /// ``` #[inline] -pub fn hex_uint<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> +pub fn hex_uint<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E> where I: StreamIsPartial, I: Stream, @@ -1199,7 +1162,7 @@ where <I as Stream>::Token: AsChar, <I as Stream>::Slice: AsBStr, { - trace("hex_uint", move |input: I| { + trace("hex_uint", move |input: &mut I| { let invalid_offset = input .offset_for(|c| { let c = c.as_char(); @@ -1233,7 +1196,7 @@ where // Must be at least one digit return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); } - let (remaining, parsed) = input.next_slice(offset); + let parsed = input.next_slice(offset); let mut res = O::default(); for c in parsed.as_bstr() { @@ -1243,7 +1206,7 @@ where res = (res << O::from(4)) + nibble; } - Ok((remaining, res)) + Ok(res) }) .parse_next(input) } @@ -1291,7 +1254,7 @@ impl HexUint for u128 { } } -/// Recognizes floating point number in text format and returns a f32 or f64. +/// Recognizes floating point number in text format and returns a [`f32`] or [`f64`]. /// /// *Complete version*: Can parse until the end of input. /// @@ -1301,99 +1264,97 @@ impl HexUint for u128 { /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::error::Needed::Size; /// use winnow::ascii::float; /// -/// fn parser(s: &str) -> IResult<&str, f64> { +/// fn parser<'s>(s: &mut &'s str) -> PResult<f64, InputError<&'s str>> { /// float(s) /// } /// -/// assert_eq!(parser("11e-1"), Ok(("", 1.1))); -/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); -/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); -/// assert_eq!(parser("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("11e-1"), Ok(("", 1.1))); +/// assert_eq!(parser.parse_peek("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser.parse_peek("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Tag)))); /// ``` /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::ascii::float; /// -/// fn parser(s: Partial<&str>) -> IResult<Partial<&str>, f64> { +/// fn parser<'s>(s: &mut Partial<&'s str>) -> PResult<f64, InputError<Partial<&'s str>>> { /// float(s) /// } /// -/// assert_eq!(parser(Partial::new("11e-1 ")), Ok((Partial::new(" "), 1.1))); -/// assert_eq!(parser(Partial::new("11e-1")), Err(ErrMode::Incomplete(Needed::new(1)))); -/// assert_eq!(parser(Partial::new("123E-02")), Err(ErrMode::Incomplete(Needed::new(1)))); -/// assert_eq!(parser(Partial::new("123K-01")), Ok((Partial::new("K-01"), 123.0))); -/// assert_eq!(parser(Partial::new("abc")), Err(ErrMode::Backtrack(Error::new(Partial::new("abc"), ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek(Partial::new("11e-1 ")), Ok((Partial::new(" "), 1.1))); +/// assert_eq!(parser.parse_peek(Partial::new("11e-1")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(parser.parse_peek(Partial::new("123E-02")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(parser.parse_peek(Partial::new("123K-01")), Ok((Partial::new("K-01"), 123.0))); +/// assert_eq!(parser.parse_peek(Partial::new("abc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("abc"), ErrorKind::Tag)))); /// ``` #[inline(always)] #[doc(alias = "f32")] #[doc(alias = "double")] -pub fn float<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> +#[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug +pub fn float<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E> where I: StreamIsPartial, I: Stream, - I: Offset + Compare<&'static str>, + I: Compare<&'static str>, <I as Stream>::Slice: ParseSlice<O>, - <I as Stream>::Token: AsChar + Copy, + <I as Stream>::Token: AsChar + Clone, <I as Stream>::IterOffsets: Clone, I: AsBStr, - &'static str: ContainsToken<<I as Stream>::Token>, { - trace("float", move |input: I| { - let (i, s) = recognize_float_or_exceptions(input)?; - match s.parse_slice() { - Some(f) => Ok((i, f)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), - } + trace("float", move |input: &mut I| { + let s = recognize_float_or_exceptions(input)?; + s.parse_slice() + .ok_or_else(|| ErrMode::from_error_kind(input, ErrorKind::Verify)) }) .parse_next(input) } -fn recognize_float_or_exceptions<I, E: ParseError<I>>( - input: I, -) -> IResult<I, <I as Stream>::Slice, E> +#[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug +fn recognize_float_or_exceptions<I, E: ParserError<I>>( + input: &mut I, +) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, - I: Offset + Compare<&'static str>, - <I as Stream>::Token: AsChar + Copy, + I: Compare<&'static str>, + <I as Stream>::Token: AsChar + Clone, <I as Stream>::IterOffsets: Clone, I: AsBStr, - &'static str: ContainsToken<<I as Stream>::Token>, { alt(( recognize_float, crate::token::tag_no_case("nan"), - crate::token::tag_no_case("inf"), crate::token::tag_no_case("infinity"), + crate::token::tag_no_case("inf"), )) .parse_next(input) } -fn recognize_float<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +#[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug +fn recognize_float<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: StreamIsPartial, I: Stream, - I: Offset + Compare<&'static str>, - <I as Stream>::Token: AsChar + Copy, + I: Compare<&'static str>, + <I as Stream>::Token: AsChar + Clone, <I as Stream>::IterOffsets: Clone, I: AsBStr, - &'static str: ContainsToken<<I as Stream>::Token>, { ( - opt(one_of("+-")), + opt(one_of(['+', '-'])), alt(( (digit1, opt(('.', opt(digit1)))).map(|_| ()), ('.', digit1).map(|_| ()), )), - opt((one_of("eE"), opt(one_of("+-")), cut_err(digit1))), + opt((one_of(['e', 'E']), opt(one_of(['+', '-'])), cut_err(digit1))), ) .recognize() .parse_next(input) @@ -1408,14 +1369,15 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed, IResult}; /// # use winnow::ascii::digit1; /// # use winnow::prelude::*; /// use winnow::ascii::escaped; /// use winnow::token::one_of; /// /// fn esc(s: &str) -> IResult<&str, &str> { -/// escaped(digit1, '\\', one_of(r#""n\"#)).parse_next(s) +/// escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s) /// } /// /// assert_eq!(esc("123;"), Ok((";", "123"))); @@ -1423,7 +1385,8 @@ where /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult}; +/// # use winnow::prelude::*; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed, IResult}; /// # use winnow::ascii::digit1; /// # use winnow::prelude::*; /// # use winnow::Partial; @@ -1431,7 +1394,7 @@ where /// use winnow::token::one_of; /// /// fn esc(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// escaped(digit1, '\\', one_of("\"n\\")).parse_next(s) +/// escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s) /// } /// /// assert_eq!(esc(Partial::new("123;")), Ok((Partial::new(";"), "123"))); @@ -1445,13 +1408,13 @@ pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>( ) -> impl Parser<I, <I as Stream>::Slice, Error> where I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, + I: Stream, + <I as Stream>::Token: AsChar + Clone, F: Parser<I, O1, Error>, G: Parser<I, O2, Error>, - Error: ParseError<I>, + Error: ParserError<I>, { - trace("escaped", move |input: I| { + trace("escaped", move |input: &mut I| { if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() { streaming_escaped_internal(input, &mut normal, control_char, &mut escapable) } else { @@ -1461,56 +1424,41 @@ where } fn streaming_escaped_internal<I, Error, F, G, O1, O2>( - input: I, + input: &mut I, normal: &mut F, control_char: char, escapable: &mut G, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, + I: Stream, + <I as Stream>::Token: AsChar + Clone, F: Parser<I, O1, Error>, G: Parser<I, O2, Error>, - Error: ParseError<I>, + Error: ParserError<I>, { - let mut i = input.clone(); + let start = input.checkpoint(); - while i.eof_offset() > 0 { - let current_len = i.eof_offset(); + while input.eof_offset() > 0 { + let current_len = input.eof_offset(); - match normal.parse_next(i.clone()) { - Ok((i2, _)) => { - if i2.eof_offset() == 0 { - return Err(ErrMode::Incomplete(Needed::Unknown)); - } else if i2.eof_offset() == current_len { - let offset = input.offset_to(&i2); + match opt(normal.by_ref()).parse_next(input)? { + Some(_) => { + if input.eof_offset() == current_len { + let offset = input.offset_from(&start); + input.reset(start); return Ok(input.next_slice(offset)); - } else { - i = i2; } } - Err(ErrMode::Backtrack(_)) => { - if i.next_token().expect("eof_offset > 0").1.as_char() == control_char { - let next = control_char.len_utf8(); - match escapable.parse_next(i.next_slice(next).0) { - Ok((i2, _)) => { - if i2.eof_offset() == 0 { - return Err(ErrMode::Incomplete(Needed::Unknown)); - } else { - i = i2; - } - } - Err(e) => return Err(e), - } + None => { + if opt(control_char).parse_next(input)?.is_some() { + let _ = escapable.parse_next(input)?; } else { - let offset = input.offset_to(&i); + let offset = input.offset_from(&start); + input.reset(start); return Ok(input.next_slice(offset)); } } - Err(e) => { - return Err(e); - } } } @@ -1518,62 +1466,46 @@ where } fn complete_escaped_internal<'a, I: 'a, Error, F, G, O1, O2>( - input: I, + input: &mut I, normal: &mut F, control_char: char, escapable: &mut G, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, + I: Stream, + <I as Stream>::Token: crate::stream::AsChar + Clone, F: Parser<I, O1, Error>, G: Parser<I, O2, Error>, - Error: ParseError<I>, + Error: ParserError<I>, { - let mut i = input.clone(); - - while i.eof_offset() > 0 { - let current_len = i.eof_offset(); - - match normal.parse_next(i.clone()) { - Ok((i2, _)) => { - // return if we consumed everything or if the normal parser - // does not consume anything - if i2.eof_offset() == 0 { - return Ok(input.next_slice(input.eof_offset())); - } else if i2.eof_offset() == current_len { - let offset = input.offset_to(&i2); + let start = input.checkpoint(); + + while input.eof_offset() > 0 { + let current_len = input.eof_offset(); + + match opt(normal.by_ref()).parse_next(input)? { + Some(_) => { + if input.eof_offset() == current_len { + let offset = input.offset_from(&start); + input.reset(start); return Ok(input.next_slice(offset)); - } else { - i = i2; } } - Err(ErrMode::Backtrack(_)) => { - if i.next_token().expect("eof_offset > 0").1.as_char() == control_char { - let next = control_char.len_utf8(); - match escapable.parse_next(i.next_slice(next).0) { - Ok((i2, _)) => { - if i2.eof_offset() == 0 { - return Ok(input.next_slice(input.eof_offset())); - } else { - i = i2; - } - } - Err(e) => return Err(e), - } + None => { + if opt(control_char).parse_next(input)?.is_some() { + let _ = escapable.parse_next(input)?; } else { - let offset = input.offset_to(&i); + let offset = input.offset_from(&start); + input.reset(start); return Ok(input.next_slice(offset)); } } - Err(e) => { - return Err(e); - } } } - Ok(input.next_slice(input.eof_offset())) + input.reset(start); + Ok(input.finish()) } /// Matches a byte string with escaped characters. @@ -1588,14 +1520,14 @@ where /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use std::str::from_utf8; /// use winnow::token::tag; /// use winnow::ascii::escaped_transform; /// use winnow::ascii::alpha1; /// use winnow::combinator::alt; /// -/// fn parser(input: &str) -> IResult<&str, String> { +/// fn parser<'s>(input: &mut &'s str) -> PResult<String, InputError<&'s str>> { /// escaped_transform( /// alpha1, /// '\\', @@ -1607,13 +1539,13 @@ where /// ).parse_next(input) /// } /// -/// assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd")))); -/// assert_eq!(parser("ab\\ncd"), Ok(("", String::from("ab\ncd")))); +/// assert_eq!(parser.parse_peek("ab\\\"cd"), Ok(("", String::from("ab\"cd")))); +/// assert_eq!(parser.parse_peek("ab\\ncd"), Ok(("", String::from("ab\ncd")))); /// ``` /// /// ``` /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use std::str::from_utf8; /// # use winnow::Partial; /// use winnow::token::tag; @@ -1621,7 +1553,7 @@ where /// use winnow::ascii::alpha1; /// use winnow::combinator::alt; /// -/// fn parser(input: Partial<&str>) -> IResult<Partial<&str>, String> { +/// fn parser<'s>(input: &mut Partial<&'s str>) -> PResult<String, InputError<Partial<&'s str>>> { /// escaped_transform( /// alpha1, /// '\\', @@ -1633,9 +1565,8 @@ where /// ).parse_next(input) /// } /// -/// assert_eq!(parser(Partial::new("ab\\\"cd\"")), Ok((Partial::new("\""), String::from("ab\"cd")))); +/// assert_eq!(parser.parse_peek(Partial::new("ab\\\"cd\"")), Ok((Partial::new("\""), String::from("ab\"cd")))); /// ``` -#[cfg(feature = "alloc")] #[inline(always)] pub fn escaped_transform<I, Error, F, G, Output>( mut normal: F, @@ -1644,14 +1575,14 @@ pub fn escaped_transform<I, Error, F, G, Output>( ) -> impl Parser<I, Output, Error> where I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, + I: Stream, + <I as Stream>::Token: crate::stream::AsChar + Clone, Output: crate::stream::Accumulate<<I as Stream>::Slice>, F: Parser<I, <I as Stream>::Slice, Error>, G: Parser<I, <I as Stream>::Slice, Error>, - Error: ParseError<I>, + Error: ParserError<I>, { - trace("escaped_transform", move |input: I| { + trace("escaped_transform", move |input: &mut I| { if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() { streaming_escaped_transform_internal(input, &mut normal, control_char, &mut transform) } else { @@ -1660,122 +1591,83 @@ where }) } -#[cfg(feature = "alloc")] fn streaming_escaped_transform_internal<I, Error, F, G, Output>( - input: I, + input: &mut I, normal: &mut F, control_char: char, transform: &mut G, -) -> IResult<I, Output, Error> +) -> PResult<Output, Error> where I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, + I: Stream, + <I as Stream>::Token: crate::stream::AsChar + Clone, Output: crate::stream::Accumulate<<I as Stream>::Slice>, F: Parser<I, <I as Stream>::Slice, Error>, G: Parser<I, <I as Stream>::Slice, Error>, - Error: ParseError<I>, + Error: ParserError<I>, { - let mut offset = 0; let mut res = Output::initial(Some(input.eof_offset())); - let i = input.clone(); - - while offset < i.eof_offset() { - let current_len = i.eof_offset(); - let remainder = i.next_slice(offset).0; - match normal.parse_next(remainder.clone()) { - Ok((i2, o)) => { + while input.eof_offset() > 0 { + let current_len = input.eof_offset(); + match opt(normal.by_ref()).parse_next(input)? { + Some(o) => { res.accumulate(o); - if i2.eof_offset() == 0 { - return Err(ErrMode::Incomplete(Needed::Unknown)); - } else if i2.eof_offset() == current_len { - return Ok((remainder, res)); - } else { - offset = input.offset_to(&i2); + if input.eof_offset() == current_len { + return Ok(res); } } - Err(ErrMode::Backtrack(_)) => { - if remainder.next_token().expect("eof_offset > 0").1.as_char() == control_char { - let next = offset + control_char.len_utf8(); - match transform.parse_next(i.next_slice(next).0) { - Ok((i2, o)) => { - res.accumulate(o); - if i2.eof_offset() == 0 { - return Err(ErrMode::Incomplete(Needed::Unknown)); - } else { - offset = input.offset_to(&i2); - } - } - Err(e) => return Err(e), - } + None => { + if opt(control_char).parse_next(input)?.is_some() { + let o = transform.parse_next(input)?; + res.accumulate(o); } else { - return Ok((remainder, res)); + return Ok(res); } } - Err(e) => return Err(e), } } Err(ErrMode::Incomplete(Needed::Unknown)) } -#[cfg(feature = "alloc")] fn complete_escaped_transform_internal<I, Error, F, G, Output>( - input: I, + input: &mut I, normal: &mut F, control_char: char, transform: &mut G, -) -> IResult<I, Output, Error> +) -> PResult<Output, Error> where I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, + I: Stream, + <I as Stream>::Token: crate::stream::AsChar + Clone, Output: crate::stream::Accumulate<<I as Stream>::Slice>, F: Parser<I, <I as Stream>::Slice, Error>, G: Parser<I, <I as Stream>::Slice, Error>, - Error: ParseError<I>, + Error: ParserError<I>, { - let mut offset = 0; let mut res = Output::initial(Some(input.eof_offset())); - let i = input.clone(); + while input.eof_offset() > 0 { + let current_len = input.eof_offset(); - while offset < i.eof_offset() { - let current_len = i.eof_offset(); - let (remainder, _) = i.next_slice(offset); - match normal.parse_next(remainder.clone()) { - Ok((i2, o)) => { + match opt(normal.by_ref()).parse_next(input)? { + Some(o) => { res.accumulate(o); - if i2.eof_offset() == 0 { - return Ok((i.next_slice(i.eof_offset()).0, res)); - } else if i2.eof_offset() == current_len { - return Ok((remainder, res)); - } else { - offset = input.offset_to(&i2); + if input.eof_offset() == current_len { + return Ok(res); } } - Err(ErrMode::Backtrack(_)) => { - if remainder.next_token().expect("eof_offset > 0").1.as_char() == control_char { - let next = offset + control_char.len_utf8(); - match transform.parse_next(i.next_slice(next).0) { - Ok((i2, o)) => { - res.accumulate(o); - if i2.eof_offset() == 0 { - return Ok((i.next_slice(i.eof_offset()).0, res)); - } else { - offset = input.offset_to(&i2); - } - } - Err(e) => return Err(e), - } + None => { + if opt(control_char).parse_next(input)?.is_some() { + let o = transform.parse_next(input)?; + res.accumulate(o); } else { - return Ok((remainder, res)); + return Ok(res); } } - Err(e) => return Err(e), } } - Ok((input.next_slice(offset).0, res)) + Ok(res) } mod sealed { diff --git a/vendor/winnow/src/ascii/tests.rs b/vendor/winnow/src/ascii/tests.rs index b715d0920..aacbd86f2 100644 --- a/vendor/winnow/src/ascii/tests.rs +++ b/vendor/winnow/src/ascii/tests.rs @@ -1,12 +1,13 @@ use super::*; +use crate::prelude::*; mod complete { use super::*; use crate::combinator::alt; use crate::combinator::opt; use crate::error::ErrMode; - use crate::error::Error; use crate::error::ErrorKind; + use crate::error::InputError; use crate::stream::ParseSlice; use crate::token::none_of; use crate::token::one_of; @@ -16,7 +17,7 @@ mod complete { macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::IResult<_, _, Error<_>> = $left; + let res: $crate::IResult<_, _, InputError<_>> = $left; assert_eq!(res, $right); }; ); @@ -30,60 +31,75 @@ mod complete { let d: &[u8] = "azé12".as_bytes(); let e: &[u8] = b" "; let f: &[u8] = b" ;"; - //assert_eq!(alpha1::<_, Error>(a), Err(ErrMode::Incomplete(Needed::Size(1)))); - assert_parse!(alpha1(a), Ok((empty, a))); + //assert_eq!(alpha1::<_, InputError>(a), Err(ErrMode::Incomplete(Needed::Size(1)))); + assert_parse!(alpha1.parse_peek(a), Ok((empty, a))); assert_eq!( - alpha1(b), - Err(ErrMode::Backtrack(Error::new(b, ErrorKind::Slice))) + alpha1.parse_peek(b), + Err(ErrMode::Backtrack(InputError::new(b, ErrorKind::Slice))) ); - assert_eq!(alpha1::<_, Error<_>>(c), Ok((&c[1..], &b"a"[..]))); - assert_eq!(alpha1::<_, Error<_>>(d), Ok(("é12".as_bytes(), &b"az"[..]))); assert_eq!( - digit1(a), - Err(ErrMode::Backtrack(Error::new(a, ErrorKind::Slice))) + alpha1::<_, InputError<_>>.parse_peek(c), + Ok((&c[1..], &b"a"[..])) ); - assert_eq!(digit1::<_, Error<_>>(b), Ok((empty, b))); assert_eq!( - digit1(c), - Err(ErrMode::Backtrack(Error::new(c, ErrorKind::Slice))) + alpha1::<_, InputError<_>>.parse_peek(d), + Ok(("é12".as_bytes(), &b"az"[..])) + ); + assert_eq!( + digit1.parse_peek(a), + Err(ErrMode::Backtrack(InputError::new(a, ErrorKind::Slice))) + ); + assert_eq!(digit1::<_, InputError<_>>.parse_peek(b), Ok((empty, b))); + assert_eq!( + digit1.parse_peek(c), + Err(ErrMode::Backtrack(InputError::new(c, ErrorKind::Slice))) ); assert_eq!( - digit1(d), - Err(ErrMode::Backtrack(Error::new(d, ErrorKind::Slice))) + digit1.parse_peek(d), + Err(ErrMode::Backtrack(InputError::new(d, ErrorKind::Slice))) ); - assert_eq!(hex_digit1::<_, Error<_>>(a), Ok((empty, a))); - assert_eq!(hex_digit1::<_, Error<_>>(b), Ok((empty, b))); - assert_eq!(hex_digit1::<_, Error<_>>(c), Ok((empty, c))); + assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(a), Ok((empty, a))); + assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(c), Ok((empty, c))); assert_eq!( - hex_digit1::<_, Error<_>>(d), + hex_digit1::<_, InputError<_>>.parse_peek(d), Ok(("zé12".as_bytes(), &b"a"[..])) ); assert_eq!( - hex_digit1(e), - Err(ErrMode::Backtrack(Error::new(e, ErrorKind::Slice))) + hex_digit1.parse_peek(e), + Err(ErrMode::Backtrack(InputError::new(e, ErrorKind::Slice))) ); assert_eq!( - oct_digit1(a), - Err(ErrMode::Backtrack(Error::new(a, ErrorKind::Slice))) + oct_digit1.parse_peek(a), + Err(ErrMode::Backtrack(InputError::new(a, ErrorKind::Slice))) ); - assert_eq!(oct_digit1::<_, Error<_>>(b), Ok((empty, b))); + assert_eq!(oct_digit1::<_, InputError<_>>.parse_peek(b), Ok((empty, b))); assert_eq!( - oct_digit1(c), - Err(ErrMode::Backtrack(Error::new(c, ErrorKind::Slice))) + oct_digit1.parse_peek(c), + Err(ErrMode::Backtrack(InputError::new(c, ErrorKind::Slice))) ); assert_eq!( - oct_digit1(d), - Err(ErrMode::Backtrack(Error::new(d, ErrorKind::Slice))) + oct_digit1.parse_peek(d), + Err(ErrMode::Backtrack(InputError::new(d, ErrorKind::Slice))) + ); + assert_eq!( + alphanumeric1::<_, InputError<_>>.parse_peek(a), + Ok((empty, a)) ); - assert_eq!(alphanumeric1::<_, Error<_>>(a), Ok((empty, a))); //assert_eq!(fix_error!(b,(), alphanumeric), Ok((empty, b))); - assert_eq!(alphanumeric1::<_, Error<_>>(c), Ok((empty, c))); assert_eq!( - alphanumeric1::<_, Error<_>>(d), + alphanumeric1::<_, InputError<_>>.parse_peek(c), + Ok((empty, c)) + ); + assert_eq!( + alphanumeric1::<_, InputError<_>>.parse_peek(d), Ok(("é12".as_bytes(), &b"az"[..])) ); - assert_eq!(space1::<_, Error<_>>(e), Ok((empty, e))); - assert_eq!(space1::<_, Error<_>>(f), Ok((&b";"[..], &b" "[..]))); + assert_eq!(space1::<_, InputError<_>>.parse_peek(e), Ok((empty, e))); + assert_eq!( + space1::<_, InputError<_>>.parse_peek(f), + Ok((&b";"[..], &b" "[..])) + ); } #[cfg(feature = "alloc")] @@ -95,52 +111,64 @@ mod complete { let c = "a123"; let d = "azé12"; let e = " "; - assert_eq!(alpha1::<_, Error<_>>(a), Ok((empty, a))); + assert_eq!(alpha1::<_, InputError<_>>.parse_peek(a), Ok((empty, a))); + assert_eq!( + alpha1.parse_peek(b), + Err(ErrMode::Backtrack(InputError::new(b, ErrorKind::Slice))) + ); + assert_eq!(alpha1::<_, InputError<_>>.parse_peek(c), Ok((&c[1..], "a"))); + assert_eq!(alpha1::<_, InputError<_>>.parse_peek(d), Ok(("é12", "az"))); + assert_eq!( + digit1.parse_peek(a), + Err(ErrMode::Backtrack(InputError::new(a, ErrorKind::Slice))) + ); + assert_eq!(digit1::<_, InputError<_>>.parse_peek(b), Ok((empty, b))); assert_eq!( - alpha1(b), - Err(ErrMode::Backtrack(Error::new(b, ErrorKind::Slice))) + digit1.parse_peek(c), + Err(ErrMode::Backtrack(InputError::new(c, ErrorKind::Slice))) ); - assert_eq!(alpha1::<_, Error<_>>(c), Ok((&c[1..], "a"))); - assert_eq!(alpha1::<_, Error<_>>(d), Ok(("é12", "az"))); assert_eq!( - digit1(a), - Err(ErrMode::Backtrack(Error::new(a, ErrorKind::Slice))) + digit1.parse_peek(d), + Err(ErrMode::Backtrack(InputError::new(d, ErrorKind::Slice))) ); - assert_eq!(digit1::<_, Error<_>>(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(a), Ok((empty, a))); + assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, InputError<_>>.parse_peek(c), Ok((empty, c))); assert_eq!( - digit1(c), - Err(ErrMode::Backtrack(Error::new(c, ErrorKind::Slice))) + hex_digit1::<_, InputError<_>>.parse_peek(d), + Ok(("zé12", "a")) ); assert_eq!( - digit1(d), - Err(ErrMode::Backtrack(Error::new(d, ErrorKind::Slice))) + hex_digit1.parse_peek(e), + Err(ErrMode::Backtrack(InputError::new(e, ErrorKind::Slice))) ); - assert_eq!(hex_digit1::<_, Error<_>>(a), Ok((empty, a))); - assert_eq!(hex_digit1::<_, Error<_>>(b), Ok((empty, b))); - assert_eq!(hex_digit1::<_, Error<_>>(c), Ok((empty, c))); - assert_eq!(hex_digit1::<_, Error<_>>(d), Ok(("zé12", "a"))); assert_eq!( - hex_digit1(e), - Err(ErrMode::Backtrack(Error::new(e, ErrorKind::Slice))) + oct_digit1.parse_peek(a), + Err(ErrMode::Backtrack(InputError::new(a, ErrorKind::Slice))) ); + assert_eq!(oct_digit1::<_, InputError<_>>.parse_peek(b), Ok((empty, b))); assert_eq!( - oct_digit1(a), - Err(ErrMode::Backtrack(Error::new(a, ErrorKind::Slice))) + oct_digit1.parse_peek(c), + Err(ErrMode::Backtrack(InputError::new(c, ErrorKind::Slice))) ); - assert_eq!(oct_digit1::<_, Error<_>>(b), Ok((empty, b))); assert_eq!( - oct_digit1(c), - Err(ErrMode::Backtrack(Error::new(c, ErrorKind::Slice))) + oct_digit1.parse_peek(d), + Err(ErrMode::Backtrack(InputError::new(d, ErrorKind::Slice))) ); assert_eq!( - oct_digit1(d), - Err(ErrMode::Backtrack(Error::new(d, ErrorKind::Slice))) + alphanumeric1::<_, InputError<_>>.parse_peek(a), + Ok((empty, a)) ); - assert_eq!(alphanumeric1::<_, Error<_>>(a), Ok((empty, a))); //assert_eq!(fix_error!(b,(), alphanumeric), Ok((empty, b))); - assert_eq!(alphanumeric1::<_, Error<_>>(c), Ok((empty, c))); - assert_eq!(alphanumeric1::<_, Error<_>>(d), Ok(("é12", "az"))); - assert_eq!(space1::<_, Error<_>>(e), Ok((empty, e))); + assert_eq!( + alphanumeric1::<_, InputError<_>>.parse_peek(c), + Ok((empty, c)) + ); + assert_eq!( + alphanumeric1::<_, InputError<_>>.parse_peek(d), + Ok(("é12", "az")) + ); + assert_eq!(space1::<_, InputError<_>>.parse_peek(e), Ok((empty, e))); } use crate::stream::Offset; @@ -153,45 +181,45 @@ mod complete { let e = &b" \t\r\n;"[..]; let f = &b"123abcDEF;"[..]; - match alpha1::<_, Error<_>>(a) { + match alpha1::<_, InputError<_>>.parse_peek(a) { Ok((i, _)) => { - assert_eq!(a.offset_to(i) + i.len(), a.len()); + assert_eq!(i.offset_from(&a) + i.len(), a.len()); } _ => panic!("wrong return type in offset test for alpha"), } - match digit1::<_, Error<_>>(b) { + match digit1::<_, InputError<_>>.parse_peek(b) { Ok((i, _)) => { - assert_eq!(b.offset_to(i) + i.len(), b.len()); + assert_eq!(i.offset_from(&b) + i.len(), b.len()); } _ => panic!("wrong return type in offset test for digit"), } - match alphanumeric1::<_, Error<_>>(c) { + match alphanumeric1::<_, InputError<_>>.parse_peek(c) { Ok((i, _)) => { - assert_eq!(c.offset_to(i) + i.len(), c.len()); + assert_eq!(i.offset_from(&c) + i.len(), c.len()); } _ => panic!("wrong return type in offset test for alphanumeric"), } - match space1::<_, Error<_>>(d) { + match space1::<_, InputError<_>>.parse_peek(d) { Ok((i, _)) => { - assert_eq!(d.offset_to(i) + i.len(), d.len()); + assert_eq!(i.offset_from(&d) + i.len(), d.len()); } _ => panic!("wrong return type in offset test for space"), } - match multispace1::<_, Error<_>>(e) { + match multispace1::<_, InputError<_>>.parse_peek(e) { Ok((i, _)) => { - assert_eq!(e.offset_to(i) + i.len(), e.len()); + assert_eq!(i.offset_from(&e) + i.len(), e.len()); } _ => panic!("wrong return type in offset test for multispace"), } - match hex_digit1::<_, Error<_>>(f) { + match hex_digit1::<_, InputError<_>>.parse_peek(f) { Ok((i, _)) => { - assert_eq!(f.offset_to(i) + i.len(), f.len()); + assert_eq!(i.offset_from(&f) + i.len(), f.len()); } _ => panic!("wrong return type in offset test for hex_digit"), } - match oct_digit1::<_, Error<_>>(f) { + match oct_digit1::<_, InputError<_>>.parse_peek(f) { Ok((i, _)) => { - assert_eq!(f.offset_to(i) + i.len(), f.len()); + assert_eq!(i.offset_from(&f) + i.len(), f.len()); } _ => panic!("wrong return type in offset test for oct_digit"), } @@ -201,53 +229,62 @@ mod complete { fn is_not_line_ending_bytes() { let a: &[u8] = b"ab12cd\nefgh"; assert_eq!( - not_line_ending::<_, Error<_>>(a), + not_line_ending::<_, InputError<_>>.parse_peek(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..])) ); let b: &[u8] = b"ab12cd\nefgh\nijkl"; assert_eq!( - not_line_ending::<_, Error<_>>(b), + not_line_ending::<_, InputError<_>>.parse_peek(b), Ok((&b"\nefgh\nijkl"[..], &b"ab12cd"[..])) ); let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; assert_eq!( - not_line_ending::<_, Error<_>>(c), + not_line_ending::<_, InputError<_>>.parse_peek(c), Ok((&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..])) ); let d: &[u8] = b"ab12cd"; - assert_eq!(not_line_ending::<_, Error<_>>(d), Ok((&[][..], d))); + assert_eq!( + not_line_ending::<_, InputError<_>>.parse_peek(d), + Ok((&[][..], d)) + ); } #[test] fn is_not_line_ending_str() { let f = "βèƒôřè\rÂßÇáƒƭèř"; assert_eq!( - not_line_ending(f), - Err(ErrMode::Backtrack(Error::new(f, ErrorKind::Tag))) + not_line_ending.parse_peek(f), + Err(ErrMode::Backtrack(InputError::new( + &f[12..], + ErrorKind::Tag + ))) ); let g2: &str = "ab12cd"; - assert_eq!(not_line_ending::<_, Error<_>>(g2), Ok(("", g2))); + assert_eq!( + not_line_ending::<_, InputError<_>>.parse_peek(g2), + Ok(("", g2)) + ); } #[test] fn hex_digit_test() { let i = &b"0123456789abcdefABCDEF;"[..]; - assert_parse!(hex_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + assert_parse!(hex_digit1.parse_peek(i), Ok((&b";"[..], &i[..i.len() - 1]))); let i = &b"g"[..]; assert_parse!( - hex_digit1(i), - Err(ErrMode::Backtrack(error_position!(i, ErrorKind::Slice))) + hex_digit1.parse_peek(i), + Err(ErrMode::Backtrack(error_position!(&i, ErrorKind::Slice))) ); let i = &b"G"[..]; assert_parse!( - hex_digit1(i), - Err(ErrMode::Backtrack(error_position!(i, ErrorKind::Slice))) + hex_digit1.parse_peek(i), + Err(ErrMode::Backtrack(error_position!(&i, ErrorKind::Slice))) ); assert!(AsChar::is_hex_digit(b'0')); @@ -267,12 +304,12 @@ mod complete { #[test] fn oct_digit_test() { let i = &b"01234567;"[..]; - assert_parse!(oct_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + assert_parse!(oct_digit1.parse_peek(i), Ok((&b";"[..], &i[..i.len() - 1]))); let i = &b"8"[..]; assert_parse!( - oct_digit1(i), - Err(ErrMode::Backtrack(error_position!(i, ErrorKind::Slice))) + oct_digit1.parse_peek(i), + Err(ErrMode::Backtrack(error_position!(&i, ErrorKind::Slice))) ); assert!(AsChar::is_oct_digit(b'0')); @@ -290,7 +327,7 @@ mod complete { #[test] fn full_line_windows() { fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> { - (not_line_ending, line_ending).parse_next(i) + (not_line_ending, line_ending).parse_peek(i) } let input = b"abc\r\n"; let output = take_full_line(input); @@ -300,7 +337,7 @@ mod complete { #[test] fn full_line_unix() { fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> { - (not_line_ending, line_ending).parse_next(i) + (not_line_ending, line_ending).parse_peek(i) } let input = b"abc\n"; let output = take_full_line(input); @@ -310,87 +347,96 @@ mod complete { #[test] fn check_windows_lineending() { let input = b"\r\n"; - let output = line_ending(&input[..]); + let output = line_ending.parse_peek(&input[..]); assert_parse!(output, Ok((&b""[..], &b"\r\n"[..]))); } #[test] fn check_unix_lineending() { let input = b"\n"; - let output = line_ending(&input[..]); + let output = line_ending.parse_peek(&input[..]); assert_parse!(output, Ok((&b""[..], &b"\n"[..]))); } #[test] fn cr_lf() { - assert_parse!(crlf(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); assert_parse!( - crlf(&b"\r"[..]), + crlf.parse_peek(&b"\r\na"[..]), + Ok((&b"a"[..], &b"\r\n"[..])) + ); + assert_parse!( + crlf.parse_peek(&b"\r"[..]), Err(ErrMode::Backtrack(error_position!( - &b"\r"[..], + &&b"\r"[..], ErrorKind::Tag ))) ); assert_parse!( - crlf(&b"\ra"[..]), + crlf.parse_peek(&b"\ra"[..]), Err(ErrMode::Backtrack(error_position!( - &b"\ra"[..], + &&b"\ra"[..], ErrorKind::Tag ))) ); - assert_parse!(crlf("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(crlf.parse_peek("\r\na"), Ok(("a", "\r\n"))); assert_parse!( - crlf("\r"), - Err(ErrMode::Backtrack(error_position!("\r", ErrorKind::Tag))) + crlf.parse_peek("\r"), + Err(ErrMode::Backtrack(error_position!(&"\r", ErrorKind::Tag))) ); assert_parse!( - crlf("\ra"), - Err(ErrMode::Backtrack(error_position!("\ra", ErrorKind::Tag))) + crlf.parse_peek("\ra"), + Err(ErrMode::Backtrack(error_position!(&"\ra", ErrorKind::Tag))) ); } #[test] fn end_of_line() { - assert_parse!(line_ending(&b"\na"[..]), Ok((&b"a"[..], &b"\n"[..]))); - assert_parse!(line_ending(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); assert_parse!( - line_ending(&b"\r"[..]), + line_ending.parse_peek(&b"\na"[..]), + Ok((&b"a"[..], &b"\n"[..])) + ); + assert_parse!( + line_ending.parse_peek(&b"\r\na"[..]), + Ok((&b"a"[..], &b"\r\n"[..])) + ); + assert_parse!( + line_ending.parse_peek(&b"\r"[..]), Err(ErrMode::Backtrack(error_position!( - &b"\r"[..], + &&b"\r"[..], ErrorKind::Tag ))) ); assert_parse!( - line_ending(&b"\ra"[..]), + line_ending.parse_peek(&b"\ra"[..]), Err(ErrMode::Backtrack(error_position!( - &b"\ra"[..], + &&b"\ra"[..], ErrorKind::Tag ))) ); - assert_parse!(line_ending("\na"), Ok(("a", "\n"))); - assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(line_ending.parse_peek("\na"), Ok(("a", "\n"))); + assert_parse!(line_ending.parse_peek("\r\na"), Ok(("a", "\r\n"))); assert_parse!( - line_ending("\r"), - Err(ErrMode::Backtrack(error_position!("\r", ErrorKind::Tag))) + line_ending.parse_peek("\r"), + Err(ErrMode::Backtrack(error_position!(&"\r", ErrorKind::Tag))) ); assert_parse!( - line_ending("\ra"), - Err(ErrMode::Backtrack(error_position!("\ra", ErrorKind::Tag))) + line_ending.parse_peek("\ra"), + Err(ErrMode::Backtrack(error_position!(&"\ra", ErrorKind::Tag))) ); } fn digit_to_i16(input: &str) -> IResult<&str, i16> { let i = input; - let (i, opt_sign) = opt(alt(('+', '-'))).parse_next(i)?; + let (i, opt_sign) = opt(alt(('+', '-'))).parse_peek(i)?; let sign = match opt_sign { Some('+') | None => true, Some('-') => false, _ => unreachable!(), }; - let (i, s) = digit1::<_, crate::error::Error<_>>(i)?; + let (i, s) = digit1::<_, InputError<_>>.parse_peek(i)?; match s.parse_slice() { Some(n) => { if sign { @@ -399,15 +445,15 @@ mod complete { Ok((i, -n)) } } - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), + None => Err(ErrMode::from_error_kind(&i, ErrorKind::Verify)), } } fn digit_to_u32(i: &str) -> IResult<&str, u32> { - let (i, s) = digit1(i)?; + let (i, s) = digit1.parse_peek(i)?; match s.parse_slice() { Some(n) => Ok((i, n)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), + None => Err(ErrMode::from_error_kind(&i, ErrorKind::Verify)), } } @@ -416,7 +462,7 @@ mod complete { #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 fn ints(s in "\\PC*") { let res1 = digit_to_i16(&s); - let res2 = dec_int(s.as_str()); + let res2 = dec_int.parse_peek(s.as_str()); assert_eq!(res1, res2); } @@ -424,7 +470,7 @@ mod complete { #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 fn uints(s in "\\PC*") { let res1 = digit_to_u32(&s); - let res2 = dec_uint(s.as_str()); + let res2 = dec_uint.parse_peek(s.as_str()); assert_eq!(res1, res2); } } @@ -432,13 +478,13 @@ mod complete { #[test] fn hex_uint_tests() { fn hex_u32(input: &[u8]) -> IResult<&[u8], u32> { - hex_uint(input) + hex_uint.parse_peek(input) } assert_parse!( hex_u32(&b";"[..]), Err(ErrMode::Backtrack(error_position!( - &b";"[..], + &&b";"[..], ErrorKind::Slice ))) ); @@ -449,14 +495,14 @@ mod complete { assert_parse!( hex_u32(&b"00c5a31be2;"[..]), // overflow Err(ErrMode::Backtrack(error_position!( - &b"00c5a31be2;"[..], + &&b"00c5a31be2;"[..], ErrorKind::Verify ))) ); assert_parse!( hex_u32(&b"c5a31be201;"[..]), // overflow Err(ErrMode::Backtrack(error_position!( - &b"c5a31be201;"[..], + &&b"c5a31be201;"[..], ErrorKind::Verify ))) ); @@ -464,14 +510,14 @@ mod complete { assert_parse!( hex_u32(&b"ffffffffffffffff;"[..]), // overflow Err(ErrMode::Backtrack(error_position!( - &b"ffffffffffffffff;"[..], + &&b"ffffffffffffffff;"[..], ErrorKind::Verify ))) ); assert_parse!( hex_u32(&b"ffffffffffffffff"[..]), // overflow Err(ErrMode::Backtrack(error_position!( - &b"ffffffffffffffff"[..], + &&b"ffffffffffffffff"[..], ErrorKind::Verify ))) ); @@ -510,35 +556,40 @@ mod complete { let larger = test.to_string(); - assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32))); - assert_parse!(float(&larger[..]), Ok(("", expected32))); - - assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected64))); - assert_parse!(float(&larger[..]), Ok(("", expected64))); + assert_parse!( + float.parse_peek(larger.as_bytes()), + Ok((&b""[..], expected32)) + ); + assert_parse!(float.parse_peek(&larger[..]), Ok(("", expected32))); + + assert_parse!( + float.parse_peek(larger.as_bytes()), + Ok((&b""[..], expected64)) + ); + assert_parse!(float.parse_peek(&larger[..]), Ok(("", expected64))); } let remaining_exponent = "-1.234E-"; assert_parse!( - float::<_, f64, _>(remaining_exponent), - Err(ErrMode::Cut(Error { - input: "", - kind: ErrorKind::Slice - })) + float::<_, f64, _>.parse_peek(remaining_exponent), + Err(ErrMode::Cut(InputError::new("", ErrorKind::Slice))) ); - let (_i, nan) = float::<_, f32, ()>("NaN").unwrap(); + let (i, nan) = float::<_, f32, ()>.parse_peek("NaN").unwrap(); assert!(nan.is_nan()); + assert_eq!(i, ""); - let (_i, inf) = float::<_, f32, ()>("inf").unwrap(); + let (i, inf) = float::<_, f32, ()>.parse_peek("inf").unwrap(); assert!(inf.is_infinite()); - let (_i, inf) = float::<_, f32, ()>("infinite").unwrap(); + assert_eq!(i, ""); + let (i, inf) = float::<_, f32, ()>.parse_peek("infinity").unwrap(); assert!(inf.is_infinite()); + assert_eq!(i, ""); } #[cfg(feature = "std")] fn parse_f64(i: &str) -> IResult<&str, f64, ()> { - #[allow(deprecated)] // will just become `pub(crate)` later - match super::recognize_float_or_exceptions(i) { + match super::recognize_float_or_exceptions.parse_peek(i) { Err(e) => Err(e), Ok((i, s)) => { if s.is_empty() { @@ -559,7 +610,7 @@ mod complete { fn floats(s in "\\PC*") { println!("testing {}", s); let res1 = parse_f64(&s); - let res2 = float::<_, f64, ()>(s.as_str()); + let res2 = float::<_, f64, ()>.parse_peek(s.as_str()); assert_eq!(res1, res2); } } @@ -571,7 +622,7 @@ mod complete { fn escaped_string(input: &str) -> IResult<&str, &str> { use crate::ascii::alpha0; use crate::token::one_of; - escaped(alpha0, '\\', one_of("n")).parse_next(input) + escaped(alpha0, '\\', one_of(['n'])).parse_peek(input) } escaped_string("7").unwrap(); @@ -588,10 +639,14 @@ mod complete { delimited( '"', - escaped(opt(none_of(r#"\""#)), '\\', one_of(r#"\"rnt"#)), + escaped( + opt(none_of(['\\', '"'])), + '\\', + one_of(['\\', '"', 'r', 'n', 't']), + ), '"', ) - .parse_next(input) + .parse_peek(input) } assert_eq!(unquote(r#""""#), Ok(("", ""))); @@ -605,7 +660,7 @@ mod complete { use crate::token::one_of; fn esc(i: &[u8]) -> IResult<&[u8], &[u8]> { - escaped(alpha, '\\', one_of("\"n\\")).parse_next(i) + escaped(alpha, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i) } assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], &b"abcd"[..]))); assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], &b"ab\\\"cd"[..]))); @@ -615,21 +670,21 @@ mod complete { assert_eq!( esc(&b"AB\\"[..]), Err(ErrMode::Backtrack(error_position!( - &b""[..], + &&b""[..], ErrorKind::Token ))) ); assert_eq!( esc(&b"AB\\A"[..]), Err(ErrMode::Backtrack(error_node_position!( - &b"AB\\A"[..], + &&b"AB\\A"[..], ErrorKind::Token, - error_position!(&b"A"[..], ErrorKind::Verify) + error_position!(&&b"A"[..], ErrorKind::Verify) ))) ); fn esc2(i: &[u8]) -> IResult<&[u8], &[u8]> { - escaped(digit, '\\', one_of("\"n\\")).parse_next(i) + escaped(digit, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i) } assert_eq!(esc2(&b"12\\nnn34"[..]), Ok((&b"nn34"[..], &b"12\\n"[..]))); } @@ -641,7 +696,7 @@ mod complete { use crate::token::one_of; fn esc(i: &str) -> IResult<&str, &str> { - escaped(alpha, '\\', one_of("\"n\\")).parse_next(i) + escaped(alpha, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i) } assert_eq!(esc("abcd;"), Ok((";", "abcd"))); assert_eq!(esc("ab\\\"cd;"), Ok((";", "ab\\\"cd"))); @@ -650,24 +705,24 @@ mod complete { assert_eq!(esc("ab\\\"12"), Ok(("12", "ab\\\""))); assert_eq!( esc("AB\\"), - Err(ErrMode::Backtrack(error_position!("", ErrorKind::Token))) + Err(ErrMode::Backtrack(error_position!(&"", ErrorKind::Token))) ); assert_eq!( esc("AB\\A"), Err(ErrMode::Backtrack(error_node_position!( - "AB\\A", + &"AB\\A", ErrorKind::Token, - error_position!("A", ErrorKind::Verify) + error_position!(&"A", ErrorKind::Verify) ))) ); fn esc2(i: &str) -> IResult<&str, &str> { - escaped(digit, '\\', one_of("\"n\\")).parse_next(i) + escaped(digit, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i) } assert_eq!(esc2("12\\nnn34"), Ok(("nn34", "12\\n"))); fn esc3(i: &str) -> IResult<&str, &str> { - escaped(alpha, '\u{241b}', one_of("\"n")).parse_next(i) + escaped(alpha, '\u{241b}', one_of(['\"', 'n'])).parse_peek(i) } assert_eq!(esc3("ab␛ncd;"), Ok((";", "ab␛ncd"))); } @@ -676,7 +731,7 @@ mod complete { fn test_escaped_error() { fn esc(s: &str) -> IResult<&str, &str> { use crate::ascii::digit1; - escaped(digit1, '\\', one_of("\"n\\")).parse_next(s) + escaped(digit1, '\\', one_of(['\"', 'n', '\\'])).parse_peek(s) } assert_eq!(esc("abcd"), Ok(("abcd", ""))); @@ -703,7 +758,7 @@ mod complete { )), ) .map(to_s) - .parse_next(i) + .parse_peek(i) } assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], String::from("abcd")))); @@ -723,16 +778,16 @@ mod complete { assert_eq!( esc(&b"AB\\"[..]), Err(ErrMode::Backtrack(error_position!( - &b""[..], + &&b""[..], ErrorKind::Tag ))) ); assert_eq!( esc(&b"AB\\A"[..]), Err(ErrMode::Backtrack(error_node_position!( - &b"AB\\A"[..], + &&b"AB\\A"[..], ErrorKind::Eof, - error_position!(&b"A"[..], ErrorKind::Tag) + error_position!(&&b"A"[..], ErrorKind::Tag) ))) ); @@ -746,7 +801,7 @@ mod complete { )), ) .map(to_s) - .parse_next(i) + .parse_peek(i) } assert_eq!( esc2(&b"abèDEF;"[..]), @@ -769,7 +824,7 @@ mod complete { '\\', alt(("\\".value("\\"), "\"".value("\""), "n".value("\n"))), ) - .parse_next(i) + .parse_peek(i) } assert_eq!(esc("abcd;"), Ok((";", String::from("abcd")))); @@ -779,14 +834,14 @@ mod complete { assert_eq!(esc("ab\\\"12"), Ok(("12", String::from("ab\"")))); assert_eq!( esc("AB\\"), - Err(ErrMode::Backtrack(error_position!("", ErrorKind::Tag))) + Err(ErrMode::Backtrack(error_position!(&"", ErrorKind::Tag))) ); assert_eq!( esc("AB\\A"), Err(ErrMode::Backtrack(error_node_position!( - "AB\\A", + &"AB\\A", ErrorKind::Eof, - error_position!("A", ErrorKind::Tag) + error_position!(&"A", ErrorKind::Tag) ))) ); @@ -796,7 +851,7 @@ mod complete { '&', alt(("egrave;".value("è"), "agrave;".value("à"))), ) - .parse_next(i) + .parse_peek(i) } assert_eq!(esc2("abèDEF;"), Ok((";", String::from("abèDEF")))); assert_eq!( @@ -805,7 +860,7 @@ mod complete { ); fn esc3(i: &str) -> IResult<&str, String> { - escaped_transform(alpha, '␛', alt(("0".value("\0"), "n".value("\n")))).parse_next(i) + escaped_transform(alpha, '␛', alt(("0".value("\0"), "n".value("\n")))).parse_peek(i) } assert_eq!(esc3("a␛0bc␛n"), Ok(("", String::from("a\0bc\n")))); } @@ -815,7 +870,7 @@ mod complete { fn test_escaped_transform_error() { fn esc_trans(s: &str) -> IResult<&str, String> { use crate::ascii::digit1; - escaped_transform(digit1, '\\', "n").parse_next(s) + escaped_transform(digit1, '\\', "n").parse_peek(s) } assert_eq!(esc_trans("abcd"), Ok(("abcd", String::new()))); @@ -825,8 +880,8 @@ mod complete { mod partial { use super::*; use crate::combinator::opt; - use crate::error::Error; use crate::error::ErrorKind; + use crate::error::InputError; use crate::error::{ErrMode, Needed}; use crate::stream::ParseSlice; use crate::IResult; @@ -835,7 +890,7 @@ mod partial { macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::IResult<_, _, Error<_>> = $left; + let res: $crate::IResult<_, _, InputError<_>> = $left; assert_eq!(res, $right); }; ); @@ -850,116 +905,116 @@ mod partial { let f: &[u8] = b" ;"; //assert_eq!(alpha1::<_, Error<_>>(a), Err(ErrMode::Incomplete(Needed::new(1)))); assert_parse!( - alpha1(Partial::new(a)), + alpha1.parse_peek(Partial::new(a)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - alpha1(Partial::new(b)), - Err(ErrMode::Backtrack(Error::new( + alpha1.parse_peek(Partial::new(b)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(b), ErrorKind::Slice ))) ); assert_eq!( - alpha1::<_, Error<_>>(Partial::new(c)), + alpha1::<_, InputError<_>>.parse_peek(Partial::new(c)), Ok((Partial::new(&c[1..]), &b"a"[..])) ); assert_eq!( - alpha1::<_, Error<_>>(Partial::new(d)), + alpha1::<_, InputError<_>>.parse_peek(Partial::new(d)), Ok((Partial::new("é12".as_bytes()), &b"az"[..])) ); assert_eq!( - digit1(Partial::new(a)), - Err(ErrMode::Backtrack(Error::new( + digit1.parse_peek(Partial::new(a)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(a), ErrorKind::Slice ))) ); assert_eq!( - digit1::<_, Error<_>>(Partial::new(b)), + digit1::<_, InputError<_>>.parse_peek(Partial::new(b)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - digit1(Partial::new(c)), - Err(ErrMode::Backtrack(Error::new( + digit1.parse_peek(Partial::new(c)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(c), ErrorKind::Slice ))) ); assert_eq!( - digit1(Partial::new(d)), - Err(ErrMode::Backtrack(Error::new( + digit1.parse_peek(Partial::new(d)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(d), ErrorKind::Slice ))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(a)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(a)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(b)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(b)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(c)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(c)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(d)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(d)), Ok((Partial::new("zé12".as_bytes()), &b"a"[..])) ); assert_eq!( - hex_digit1(Partial::new(e)), - Err(ErrMode::Backtrack(Error::new( + hex_digit1.parse_peek(Partial::new(e)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(e), ErrorKind::Slice ))) ); assert_eq!( - oct_digit1(Partial::new(a)), - Err(ErrMode::Backtrack(Error::new( + oct_digit1.parse_peek(Partial::new(a)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(a), ErrorKind::Slice ))) ); assert_eq!( - oct_digit1::<_, Error<_>>(Partial::new(b)), + oct_digit1::<_, InputError<_>>.parse_peek(Partial::new(b)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - oct_digit1(Partial::new(c)), - Err(ErrMode::Backtrack(Error::new( + oct_digit1.parse_peek(Partial::new(c)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(c), ErrorKind::Slice ))) ); assert_eq!( - oct_digit1(Partial::new(d)), - Err(ErrMode::Backtrack(Error::new( + oct_digit1.parse_peek(Partial::new(d)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(d), ErrorKind::Slice ))) ); assert_eq!( - alphanumeric1::<_, Error<_>>(Partial::new(a)), + alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(a)), Err(ErrMode::Incomplete(Needed::new(1))) ); //assert_eq!(fix_error!(b,(), alphanumeric1), Ok((empty, b))); assert_eq!( - alphanumeric1::<_, Error<_>>(Partial::new(c)), + alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(c)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - alphanumeric1::<_, Error<_>>(Partial::new(d)), + alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(d)), Ok((Partial::new("é12".as_bytes()), &b"az"[..])) ); assert_eq!( - space1::<_, Error<_>>(Partial::new(e)), + space1::<_, InputError<_>>.parse_peek(Partial::new(e)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - space1::<_, Error<_>>(Partial::new(f)), + space1::<_, InputError<_>>.parse_peek(Partial::new(f)), Ok((Partial::new(&b";"[..]), &b" "[..])) ); } @@ -973,112 +1028,112 @@ mod partial { let d = "azé12"; let e = " "; assert_eq!( - alpha1::<_, Error<_>>(Partial::new(a)), + alpha1::<_, InputError<_>>.parse_peek(Partial::new(a)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - alpha1(Partial::new(b)), - Err(ErrMode::Backtrack(Error::new( + alpha1.parse_peek(Partial::new(b)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(b), ErrorKind::Slice ))) ); assert_eq!( - alpha1::<_, Error<_>>(Partial::new(c)), + alpha1::<_, InputError<_>>.parse_peek(Partial::new(c)), Ok((Partial::new(&c[1..]), "a")) ); assert_eq!( - alpha1::<_, Error<_>>(Partial::new(d)), + alpha1::<_, InputError<_>>.parse_peek(Partial::new(d)), Ok((Partial::new("é12"), "az")) ); assert_eq!( - digit1(Partial::new(a)), - Err(ErrMode::Backtrack(Error::new( + digit1.parse_peek(Partial::new(a)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(a), ErrorKind::Slice ))) ); assert_eq!( - digit1::<_, Error<_>>(Partial::new(b)), + digit1::<_, InputError<_>>.parse_peek(Partial::new(b)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - digit1(Partial::new(c)), - Err(ErrMode::Backtrack(Error::new( + digit1.parse_peek(Partial::new(c)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(c), ErrorKind::Slice ))) ); assert_eq!( - digit1(Partial::new(d)), - Err(ErrMode::Backtrack(Error::new( + digit1.parse_peek(Partial::new(d)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(d), ErrorKind::Slice ))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(a)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(a)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(b)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(b)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(c)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(c)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - hex_digit1::<_, Error<_>>(Partial::new(d)), + hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(d)), Ok((Partial::new("zé12"), "a")) ); assert_eq!( - hex_digit1(Partial::new(e)), - Err(ErrMode::Backtrack(Error::new( + hex_digit1.parse_peek(Partial::new(e)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(e), ErrorKind::Slice ))) ); assert_eq!( - oct_digit1(Partial::new(a)), - Err(ErrMode::Backtrack(Error::new( + oct_digit1.parse_peek(Partial::new(a)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(a), ErrorKind::Slice ))) ); assert_eq!( - oct_digit1::<_, Error<_>>(Partial::new(b)), + oct_digit1::<_, InputError<_>>.parse_peek(Partial::new(b)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - oct_digit1(Partial::new(c)), - Err(ErrMode::Backtrack(Error::new( + oct_digit1.parse_peek(Partial::new(c)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(c), ErrorKind::Slice ))) ); assert_eq!( - oct_digit1(Partial::new(d)), - Err(ErrMode::Backtrack(Error::new( + oct_digit1.parse_peek(Partial::new(d)), + Err(ErrMode::Backtrack(InputError::new( Partial::new(d), ErrorKind::Slice ))) ); assert_eq!( - alphanumeric1::<_, Error<_>>(Partial::new(a)), + alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(a)), Err(ErrMode::Incomplete(Needed::new(1))) ); //assert_eq!(fix_error!(b,(), alphanumeric1), Ok((empty, b))); assert_eq!( - alphanumeric1::<_, Error<_>>(Partial::new(c)), + alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(c)), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_eq!( - alphanumeric1::<_, Error<_>>(Partial::new(d)), + alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(d)), Ok((Partial::new("é12"), "az")) ); assert_eq!( - space1::<_, Error<_>>(Partial::new(e)), + space1::<_, InputError<_>>.parse_peek(Partial::new(e)), Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -1093,52 +1148,52 @@ mod partial { let e = &b" \t\r\n;"[..]; let f = &b"123abcDEF;"[..]; - match alpha1::<_, Error<_>>(Partial::new(a)) { + match alpha1::<_, InputError<_>>.parse_peek(Partial::new(a)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(a.offset_to(i) + i.len(), a.len()); + assert_eq!(i.offset_from(&a) + i.len(), a.len()); } _ => panic!("wrong return type in offset test for alpha"), } - match digit1::<_, Error<_>>(Partial::new(b)) { + match digit1::<_, InputError<_>>.parse_peek(Partial::new(b)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(b.offset_to(i) + i.len(), b.len()); + assert_eq!(i.offset_from(&b) + i.len(), b.len()); } _ => panic!("wrong return type in offset test for digit"), } - match alphanumeric1::<_, Error<_>>(Partial::new(c)) { + match alphanumeric1::<_, InputError<_>>.parse_peek(Partial::new(c)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(c.offset_to(i) + i.len(), c.len()); + assert_eq!(i.offset_from(&c) + i.len(), c.len()); } _ => panic!("wrong return type in offset test for alphanumeric"), } - match space1::<_, Error<_>>(Partial::new(d)) { + match space1::<_, InputError<_>>.parse_peek(Partial::new(d)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(d.offset_to(i) + i.len(), d.len()); + assert_eq!(i.offset_from(&d) + i.len(), d.len()); } _ => panic!("wrong return type in offset test for space"), } - match multispace1::<_, Error<_>>(Partial::new(e)) { + match multispace1::<_, InputError<_>>.parse_peek(Partial::new(e)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(e.offset_to(i) + i.len(), e.len()); + assert_eq!(i.offset_from(&e) + i.len(), e.len()); } _ => panic!("wrong return type in offset test for multispace"), } - match hex_digit1::<_, Error<_>>(Partial::new(f)) { + match hex_digit1::<_, InputError<_>>.parse_peek(Partial::new(f)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(f.offset_to(i) + i.len(), f.len()); + assert_eq!(i.offset_from(&f) + i.len(), f.len()); } _ => panic!("wrong return type in offset test for hex_digit"), } - match oct_digit1::<_, Error<_>>(Partial::new(f)) { + match oct_digit1::<_, InputError<_>>.parse_peek(Partial::new(f)) { Ok((i, _)) => { let i = i.into_inner(); - assert_eq!(f.offset_to(i) + i.len(), f.len()); + assert_eq!(i.offset_from(&f) + i.len(), f.len()); } _ => panic!("wrong return type in offset test for oct_digit"), } @@ -1148,26 +1203,26 @@ mod partial { fn is_not_line_ending_bytes() { let a: &[u8] = b"ab12cd\nefgh"; assert_eq!( - not_line_ending::<_, Error<_>>(Partial::new(a)), + not_line_ending::<_, InputError<_>>.parse_peek(Partial::new(a)), Ok((Partial::new(&b"\nefgh"[..]), &b"ab12cd"[..])) ); let b: &[u8] = b"ab12cd\nefgh\nijkl"; assert_eq!( - not_line_ending::<_, Error<_>>(Partial::new(b)), + not_line_ending::<_, InputError<_>>.parse_peek(Partial::new(b)), Ok((Partial::new(&b"\nefgh\nijkl"[..]), &b"ab12cd"[..])) ); let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; assert_eq!( - not_line_ending::<_, Error<_>>(Partial::new(c)), + not_line_ending::<_, InputError<_>>.parse_peek(Partial::new(c)), Ok((Partial::new(&b"\r\nefgh\nijkl"[..]), &b"ab12cd"[..])) ); let d: &[u8] = b"ab12cd"; assert_eq!( - not_line_ending::<_, Error<_>>(Partial::new(d)), - Err(ErrMode::Incomplete(Needed::Unknown)) + not_line_ending::<_, InputError<_>>.parse_peek(Partial::new(d)), + Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -1175,17 +1230,17 @@ mod partial { fn is_not_line_ending_str() { let f = "βèƒôřè\rÂßÇáƒƭèř"; assert_eq!( - not_line_ending(Partial::new(f)), - Err(ErrMode::Backtrack(Error::new( - Partial::new(f), + not_line_ending.parse_peek(Partial::new(f)), + Err(ErrMode::Backtrack(InputError::new( + Partial::new(&f[12..]), ErrorKind::Tag ))) ); let g2: &str = "ab12cd"; assert_eq!( - not_line_ending::<_, Error<_>>(Partial::new(g2)), - Err(ErrMode::Incomplete(Needed::Unknown)) + not_line_ending::<_, InputError<_>>.parse_peek(Partial::new(g2)), + Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -1193,24 +1248,24 @@ mod partial { fn hex_digit_test() { let i = &b"0123456789abcdefABCDEF;"[..]; assert_parse!( - hex_digit1(Partial::new(i)), + hex_digit1.parse_peek(Partial::new(i)), Ok((Partial::new(&b";"[..]), &i[..i.len() - 1])) ); let i = &b"g"[..]; assert_parse!( - hex_digit1(Partial::new(i)), + hex_digit1.parse_peek(Partial::new(i)), Err(ErrMode::Backtrack(error_position!( - Partial::new(i), + &Partial::new(i), ErrorKind::Slice ))) ); let i = &b"G"[..]; assert_parse!( - hex_digit1(Partial::new(i)), + hex_digit1.parse_peek(Partial::new(i)), Err(ErrMode::Backtrack(error_position!( - Partial::new(i), + &Partial::new(i), ErrorKind::Slice ))) ); @@ -1233,15 +1288,15 @@ mod partial { fn oct_digit_test() { let i = &b"01234567;"[..]; assert_parse!( - oct_digit1(Partial::new(i)), + oct_digit1.parse_peek(Partial::new(i)), Ok((Partial::new(&b";"[..]), &i[..i.len() - 1])) ); let i = &b"8"[..]; assert_parse!( - oct_digit1(Partial::new(i)), + oct_digit1.parse_peek(Partial::new(i)), Err(ErrMode::Backtrack(error_position!( - Partial::new(i), + &Partial::new(i), ErrorKind::Slice ))) ); @@ -1262,7 +1317,7 @@ mod partial { fn full_line_windows() { #[allow(clippy::type_complexity)] fn take_full_line(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (&[u8], &[u8])> { - (not_line_ending, line_ending).parse_next(i) + (not_line_ending, line_ending).parse_peek(i) } let input = b"abc\r\n"; let output = take_full_line(Partial::new(input)); @@ -1276,7 +1331,7 @@ mod partial { fn full_line_unix() { #[allow(clippy::type_complexity)] fn take_full_line(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (&[u8], &[u8])> { - (not_line_ending, line_ending).parse_next(i) + (not_line_ending, line_ending).parse_peek(i) } let input = b"abc\n"; let output = take_full_line(Partial::new(input)); @@ -1289,44 +1344,47 @@ mod partial { #[test] fn check_windows_lineending() { let input = b"\r\n"; - let output = line_ending(Partial::new(&input[..])); + let output = line_ending.parse_peek(Partial::new(&input[..])); assert_parse!(output, Ok((Partial::new(&b""[..]), &b"\r\n"[..]))); } #[test] fn check_unix_lineending() { let input = b"\n"; - let output = line_ending(Partial::new(&input[..])); + let output = line_ending.parse_peek(Partial::new(&input[..])); assert_parse!(output, Ok((Partial::new(&b""[..]), &b"\n"[..]))); } #[test] fn cr_lf() { assert_parse!( - crlf(Partial::new(&b"\r\na"[..])), + crlf.parse_peek(Partial::new(&b"\r\na"[..])), Ok((Partial::new(&b"a"[..]), &b"\r\n"[..])) ); assert_parse!( - crlf(Partial::new(&b"\r"[..])), + crlf.parse_peek(Partial::new(&b"\r"[..])), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_parse!( - crlf(Partial::new(&b"\ra"[..])), + crlf.parse_peek(Partial::new(&b"\ra"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"\ra"[..]), + &Partial::new(&b"\ra"[..]), ErrorKind::Tag ))) ); - assert_parse!(crlf(Partial::new("\r\na")), Ok((Partial::new("a"), "\r\n"))); assert_parse!( - crlf(Partial::new("\r")), + crlf.parse_peek(Partial::new("\r\na")), + Ok((Partial::new("a"), "\r\n")) + ); + assert_parse!( + crlf.parse_peek(Partial::new("\r")), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_parse!( - crlf(Partial::new("\ra")), + crlf.parse_peek(Partial::new("\ra")), Err(ErrMode::Backtrack(error_position!( - Partial::new("\ra"), + &Partial::new("\ra"), ErrorKind::Tag ))) ); @@ -1335,41 +1393,41 @@ mod partial { #[test] fn end_of_line() { assert_parse!( - line_ending(Partial::new(&b"\na"[..])), + line_ending.parse_peek(Partial::new(&b"\na"[..])), Ok((Partial::new(&b"a"[..]), &b"\n"[..])) ); assert_parse!( - line_ending(Partial::new(&b"\r\na"[..])), + line_ending.parse_peek(Partial::new(&b"\r\na"[..])), Ok((Partial::new(&b"a"[..]), &b"\r\n"[..])) ); assert_parse!( - line_ending(Partial::new(&b"\r"[..])), + line_ending.parse_peek(Partial::new(&b"\r"[..])), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_parse!( - line_ending(Partial::new(&b"\ra"[..])), + line_ending.parse_peek(Partial::new(&b"\ra"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"\ra"[..]), + &Partial::new(&b"\ra"[..]), ErrorKind::Tag ))) ); assert_parse!( - line_ending(Partial::new("\na")), + line_ending.parse_peek(Partial::new("\na")), Ok((Partial::new("a"), "\n")) ); assert_parse!( - line_ending(Partial::new("\r\na")), + line_ending.parse_peek(Partial::new("\r\na")), Ok((Partial::new("a"), "\r\n")) ); assert_parse!( - line_ending(Partial::new("\r")), + line_ending.parse_peek(Partial::new("\r")), Err(ErrMode::Incomplete(Needed::new(1))) ); assert_parse!( - line_ending(Partial::new("\ra")), + line_ending.parse_peek(Partial::new("\ra")), Err(ErrMode::Backtrack(error_position!( - Partial::new("\ra"), + &Partial::new("\ra"), ErrorKind::Tag ))) ); @@ -1377,14 +1435,14 @@ mod partial { fn digit_to_i16(input: Partial<&str>) -> IResult<Partial<&str>, i16> { let i = input; - let (i, opt_sign) = opt(one_of("+-")).parse_next(i)?; + let (i, opt_sign) = opt(one_of(['+', '-'])).parse_peek(i)?; let sign = match opt_sign { Some('+') | None => true, Some('-') => false, _ => unreachable!(), }; - let (i, s) = digit1::<_, crate::error::Error<_>>(i)?; + let (i, s) = digit1::<_, InputError<_>>.parse_peek(i)?; match s.parse_slice() { Some(n) => { if sign { @@ -1393,15 +1451,15 @@ mod partial { Ok((i, -n)) } } - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), + None => Err(ErrMode::from_error_kind(&i, ErrorKind::Verify)), } } fn digit_to_u32(i: Partial<&str>) -> IResult<Partial<&str>, u32> { - let (i, s) = digit1(i)?; + let (i, s) = digit1.parse_peek(i)?; match s.parse_slice() { Some(n) => Ok((i, n)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), + None => Err(ErrMode::from_error_kind(&i, ErrorKind::Verify)), } } @@ -1410,7 +1468,7 @@ mod partial { #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 fn ints(s in "\\PC*") { let res1 = digit_to_i16(Partial::new(&s)); - let res2 = dec_int(Partial::new(s.as_str())); + let res2 = dec_int.parse_peek(Partial::new(s.as_str())); assert_eq!(res1, res2); } @@ -1418,7 +1476,7 @@ mod partial { #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253 fn uints(s in "\\PC*") { let res1 = digit_to_u32(Partial::new(&s)); - let res2 = dec_uint(Partial::new(s.as_str())); + let res2 = dec_uint.parse_peek(Partial::new(s.as_str())); assert_eq!(res1, res2); } } @@ -1426,13 +1484,13 @@ mod partial { #[test] fn hex_uint_tests() { fn hex_u32(input: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { - hex_uint(input) + hex_uint.parse_peek(input) } assert_parse!( hex_u32(Partial::new(&b";"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b";"[..]), + &Partial::new(&b";"[..]), ErrorKind::Slice ))) ); @@ -1455,14 +1513,14 @@ mod partial { assert_parse!( hex_u32(Partial::new(&b"00c5a31be2;"[..])), // overflow Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"00c5a31be2;"[..]), + &Partial::new(&b"00c5a31be2;"[..]), ErrorKind::Verify ))) ); assert_parse!( hex_u32(Partial::new(&b"c5a31be201;"[..])), // overflow Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"c5a31be201;"[..]), + &Partial::new(&b"c5a31be201;"[..]), ErrorKind::Verify ))) ); @@ -1473,14 +1531,14 @@ mod partial { assert_parse!( hex_u32(Partial::new(&b"ffffffffffffffff;"[..])), // overflow Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"ffffffffffffffff;"[..]), + &Partial::new(&b"ffffffffffffffff;"[..]), ErrorKind::Verify ))) ); assert_parse!( hex_u32(Partial::new(&b"ffffffffffffffff"[..])), // overflow Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"ffffffffffffffff"[..]), + &Partial::new(&b"ffffffffffffffff"[..]), ErrorKind::Verify ))) ); diff --git a/vendor/winnow/src/binary/bits/mod.rs b/vendor/winnow/src/binary/bits/mod.rs index 5400e3308..334c6bf00 100644 --- a/vendor/winnow/src/binary/bits/mod.rs +++ b/vendor/winnow/src/binary/bits/mod.rs @@ -4,11 +4,11 @@ #[cfg(test)] mod tests; -use crate::error::{ErrMode, ErrorConvert, ErrorKind, Needed, ParseError}; +use crate::error::{ErrMode, ErrorConvert, ErrorKind, Needed, ParserError}; use crate::lib::std::ops::{AddAssign, Div, Shl, Shr}; use crate::stream::{AsBytes, Stream, StreamIsPartial, ToUsize}; use crate::trace::trace; -use crate::{IResult, Parser}; +use crate::{unpeek, IResult, PResult, Parser}; /// Converts a byte-level input to a bit-level input /// @@ -19,7 +19,7 @@ use crate::{IResult, Parser}; /// use winnow::prelude::*; /// use winnow::Bytes; /// use winnow::binary::bits::{bits, take}; -/// use winnow::error::Error; +/// use winnow::error::InputError; /// /// type Stream<'i> = &'i Bytes; /// @@ -28,7 +28,7 @@ use crate::{IResult, Parser}; /// } /// /// fn parse(input: Stream<'_>) -> IResult<Stream<'_>, (u8, u8)> { -/// bits::<_, _, Error<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_next(input) +/// bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input) /// } /// /// let input = stream(&[0x12, 0x34, 0xff, 0xff]); @@ -45,25 +45,28 @@ use crate::{IResult, Parser}; /// ``` pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl Parser<I, O, E2> where - E1: ParseError<(I, usize)> + ErrorConvert<E2>, - E2: ParseError<I>, - I: Stream, + E1: ParserError<(I, usize)> + ErrorConvert<E2>, + E2: ParserError<I>, + I: Stream + Clone, P: Parser<(I, usize), O, E1>, { - trace("bits", move |input: I| { - match parser.parse_next((input, 0)) { - Ok(((rest, offset), result)) => { - // If the next byte has been partially read, it will be sliced away as well. - // The parser functions might already slice away all fully read bytes. - // That's why `offset / 8` isn't necessarily needed at all times. - let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 }; - let (input, _) = rest.next_slice(remaining_bytes_index); - Ok((input, result)) + trace( + "bits", + unpeek(move |input: I| { + match parser.parse_peek((input, 0)) { + Ok(((rest, offset), result)) => { + // If the next byte has been partially read, it will be sliced away as well. + // The parser functions might already slice away all fully read bytes. + // That's why `offset / 8` isn't necessarily needed at all times. + let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 }; + let (input, _) = rest.peek_slice(remaining_bytes_index); + Ok((input, result)) + } + Err(ErrMode::Incomplete(n)) => Err(ErrMode::Incomplete(n.map(|u| u.get() / 8 + 1))), + Err(e) => Err(e.convert()), } - Err(ErrMode::Incomplete(n)) => Err(ErrMode::Incomplete(n.map(|u| u.get() / 8 + 1))), - Err(e) => Err(e.convert()), - } - }) + }), + ) } /// Convert a [`bits`] stream back into a byte stream @@ -76,7 +79,7 @@ where /// use winnow::Bytes; /// use winnow::binary::bits::{bits, bytes, take}; /// use winnow::combinator::rest; -/// use winnow::error::Error; +/// use winnow::error::InputError; /// /// type Stream<'i> = &'i Bytes; /// @@ -85,11 +88,11 @@ where /// } /// /// fn parse(input: Stream<'_>) -> IResult<Stream<'_>, (u8, u8, &[u8])> { -/// bits::<_, _, Error<(_, usize)>, _, _>(( +/// bits::<_, _, InputError<(_, usize)>, _, _>(( /// take(4usize), /// take(8usize), -/// bytes::<_, _, Error<_>, _, _>(rest) -/// )).parse_next(input) +/// bytes::<_, _, InputError<_>, _, _>(rest) +/// )).parse_peek(input) /// } /// /// let input = stream(&[0x12, 0x34, 0xff, 0xff]); @@ -98,31 +101,36 @@ where /// ``` pub fn bytes<I, O, E1, E2, P>(mut parser: P) -> impl Parser<(I, usize), O, E2> where - E1: ParseError<I> + ErrorConvert<E2>, - E2: ParseError<(I, usize)>, - I: Stream<Token = u8>, + E1: ParserError<I> + ErrorConvert<E2>, + E2: ParserError<(I, usize)>, + I: Stream<Token = u8> + Clone, P: Parser<I, O, E1>, { - trace("bytes", move |(input, offset): (I, usize)| { - let (inner, _) = if offset % 8 != 0 { - input.next_slice(1 + offset / 8) - } else { - input.next_slice(offset / 8) - }; - let i = (input, offset); - match parser.parse_next(inner) { - Ok((rest, res)) => Ok(((rest, 0), res)), - Err(ErrMode::Incomplete(Needed::Unknown)) => Err(ErrMode::Incomplete(Needed::Unknown)), - Err(ErrMode::Incomplete(Needed::Size(sz))) => Err(match sz.get().checked_mul(8) { - Some(v) => ErrMode::Incomplete(Needed::new(v)), - None => ErrMode::Cut(E2::assert( - i, - "overflow in turning needed bytes into needed bits", - )), - }), - Err(e) => Err(e.convert()), - } - }) + trace( + "bytes", + unpeek(move |(input, offset): (I, usize)| { + let (inner, _) = if offset % 8 != 0 { + input.peek_slice(1 + offset / 8) + } else { + input.peek_slice(offset / 8) + }; + let i = (input, offset); + match parser.parse_peek(inner) { + Ok((rest, res)) => Ok(((rest, 0), res)), + Err(ErrMode::Incomplete(Needed::Unknown)) => { + Err(ErrMode::Incomplete(Needed::Unknown)) + } + Err(ErrMode::Incomplete(Needed::Size(sz))) => Err(match sz.get().checked_mul(8) { + Some(v) => ErrMode::Incomplete(Needed::new(v)), + None => ErrMode::Cut(E2::assert( + &i, + "overflow in turning needed bytes into needed bits", + )), + }), + Err(e) => Err(e.convert()), + } + }), + ) } /// Parse taking `count` bits @@ -131,7 +139,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::Bytes; -/// # use winnow::error::{Error, ErrorKind}; +/// # use winnow::error::{InputError, ErrorKind}; /// use winnow::binary::bits::take; /// /// type Stream<'i> = &'i Bytes; @@ -141,7 +149,7 @@ where /// } /// /// fn parser(input: (Stream<'_>, usize), count: usize)-> IResult<(Stream<'_>, usize), u8> { -/// take(count).parse_next(input) +/// take(count).parse_peek(input) /// } /// /// // Consumes 0 bits, returns 0 @@ -154,32 +162,35 @@ where /// assert_eq!(parser((stream(&[0b00010010]), 4), 4), Ok(((stream(&[]), 0), 0b00000010))); /// /// // Tries to consume 12 bits but only 8 are available -/// assert_eq!(parser((stream(&[0b00010010]), 0), 12), Err(winnow::error::ErrMode::Backtrack(Error{input: (stream(&[0b00010010]), 0), kind: ErrorKind::Eof }))); +/// assert_eq!(parser((stream(&[0b00010010]), 0), 12), Err(winnow::error::ErrMode::Backtrack(InputError::new((stream(&[0b00010010]), 0), ErrorKind::Eof)))); /// ``` #[inline(always)] -pub fn take<I, O, C, E: ParseError<(I, usize)>>(count: C) -> impl Parser<(I, usize), O, E> +pub fn take<I, O, C, E: ParserError<(I, usize)>>(count: C) -> impl Parser<(I, usize), O, E> where - I: Stream<Token = u8> + AsBytes + StreamIsPartial, + I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone, C: ToUsize, O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>, { let count = count.to_usize(); - trace("take", move |input: (I, usize)| { - if <I as StreamIsPartial>::is_partial_supported() { - take_::<_, _, _, true>(input, count) - } else { - take_::<_, _, _, false>(input, count) - } - }) + trace( + "take", + unpeek(move |input: (I, usize)| { + if <I as StreamIsPartial>::is_partial_supported() { + take_::<_, _, _, true>(input, count) + } else { + take_::<_, _, _, false>(input, count) + } + }), + ) } -fn take_<I, O, E: ParseError<(I, usize)>, const PARTIAL: bool>( +fn take_<I, O, E: ParserError<(I, usize)>, const PARTIAL: bool>( (input, bit_offset): (I, usize), count: usize, ) -> IResult<(I, usize), O, E> where I: StreamIsPartial, - I: Stream<Token = u8> + AsBytes, + I: Stream<Token = u8> + AsBytes + Clone, O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>, { if count == 0 { @@ -191,7 +202,7 @@ where Err(ErrMode::Incomplete(Needed::new(count))) } else { Err(ErrMode::from_error_kind( - (input, bit_offset), + &(input, bit_offset), ErrorKind::Eof, )) } @@ -221,7 +232,7 @@ where offset = 0; } } - let (input, _) = input.next_slice(cnt); + let (input, _) = input.peek_slice(cnt); Ok(((input, end_offset), acc)) } } @@ -234,7 +245,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::Bytes; -/// # use winnow::error::{Error, ErrorKind}; +/// # use winnow::error::{InputError, ErrorKind}; /// use winnow::binary::bits::tag; /// /// type Stream<'i> = &'i Bytes; @@ -247,7 +258,7 @@ where /// /// Return Ok and the matching section of `input` if there's a match. /// /// Return Err if there's no match. /// fn parser(pattern: u8, count: u8, input: (Stream<'_>, usize)) -> IResult<(Stream<'_>, usize), u8> { -/// tag(pattern, count).parse_next(input) +/// tag(pattern, count).parse_peek(input) /// } /// /// // The lowest 4 bits of 0b00001111 match the lowest 4 bits of 0b11111111. @@ -265,42 +276,46 @@ where /// // The lowest 2 bits of 0b11111111 and 0b00000001 are different. /// assert_eq!( /// parser(0b000000_01, 2, (stream(&[0b111111_11]), 0)), -/// Err(winnow::error::ErrMode::Backtrack(Error { -/// input: (stream(&[0b11111111]), 0), -/// kind: ErrorKind::Tag -/// })) +/// Err(winnow::error::ErrMode::Backtrack(InputError::new( +/// (stream(&[0b11111111]), 0), +/// ErrorKind::Tag +/// ))) /// ); /// /// // The lowest 8 bits of 0b11111111 and 0b11111110 are different. /// assert_eq!( /// parser(0b11111110, 8, (stream(&[0b11111111]), 0)), -/// Err(winnow::error::ErrMode::Backtrack(Error { -/// input: (stream(&[0b11111111]), 0), -/// kind: ErrorKind::Tag -/// })) +/// Err(winnow::error::ErrMode::Backtrack(InputError::new( +/// (stream(&[0b11111111]), 0), +/// ErrorKind::Tag +/// ))) /// ); /// ``` #[inline(always)] #[doc(alias = "literal")] #[doc(alias = "just")] -pub fn tag<I, O, C, E: ParseError<(I, usize)>>( +pub fn tag<I, O, C, E: ParserError<(I, usize)>>( pattern: O, count: C, ) -> impl Parser<(I, usize), O, E> where - I: Stream<Token = u8> + AsBytes + StreamIsPartial, + I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone, C: ToUsize, O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq, { let count = count.to_usize(); - trace("tag", move |input: (I, usize)| { - let inp = input.clone(); + trace("tag", move |input: &mut (I, usize)| { + let start = input.checkpoint(); - take(count).parse_next(input).and_then(|(i, o)| { + take(count).parse_next(input).and_then(|o| { if pattern == o { - Ok((i, o)) + Ok(o) } else { - Err(ErrMode::Backtrack(E::from_error_kind(inp, ErrorKind::Tag))) + input.reset(start); + Err(ErrMode::Backtrack(E::from_error_kind( + input, + ErrorKind::Tag, + ))) } }) }) @@ -313,7 +328,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::Bytes; -/// # use winnow::error::{Error, ErrorKind}; +/// # use winnow::error::{InputError, ErrorKind}; /// use winnow::binary::bits::bool; /// /// type Stream<'i> = &'i Bytes; @@ -323,20 +338,20 @@ where /// } /// /// fn parse(input: (Stream<'_>, usize)) -> IResult<(Stream<'_>, usize), bool> { -/// bool.parse_next(input) +/// bool.parse_peek(input) /// } /// /// assert_eq!(parse((stream(&[0b10000000]), 0)), Ok(((stream(&[0b10000000]), 1), true))); /// assert_eq!(parse((stream(&[0b10000000]), 1)), Ok(((stream(&[0b10000000]), 2), false))); /// ``` #[doc(alias = "any")] -pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E> +pub fn bool<I, E: ParserError<(I, usize)>>(input: &mut (I, usize)) -> PResult<bool, E> where - I: Stream<Token = u8> + AsBytes + StreamIsPartial, + I: Stream<Token = u8> + AsBytes + StreamIsPartial + Clone, { - trace("bool", |input: (I, usize)| { - let (res, bit): (_, u32) = take(1usize).parse_next(input)?; - Ok((res, bit != 0)) + trace("bool", |input: &mut (I, usize)| { + let bit: u32 = take(1usize).parse_next(input)?; + Ok(bit != 0) }) .parse_next(input) } diff --git a/vendor/winnow/src/binary/bits/tests.rs b/vendor/winnow/src/binary/bits/tests.rs index 61dba2c31..41207c624 100644 --- a/vendor/winnow/src/binary/bits/tests.rs +++ b/vendor/winnow/src/binary/bits/tests.rs @@ -1,5 +1,5 @@ use super::*; -use crate::error::Error; +use crate::error::InputError; use crate::Partial; #[test] @@ -10,8 +10,8 @@ fn test_complete_byte_consumption_bits() { // Take 3 bit slices with sizes [4, 8, 4]. let result: IResult<&[u8], (u8, u8, u8)> = - bits::<_, _, Error<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize))) - .parse_next(input); + bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize))) + .parse_peek(input); let output = result.expect("We take 2 bytes and the input is longer than 2 bytes"); @@ -34,7 +34,8 @@ fn test_partial_byte_consumption_bits() { // Take bit slices with sizes [4, 8]. let result: IResult<&[u8], (u8, u8)> = - bits::<_, _, Error<(&[u8], usize)>, _, _>((take(4usize), take(8usize))).parse_next(input); + bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize))) + .parse_peek(input); let output = result.expect("We take 1.5 bytes and the input is longer than 2 bytes"); @@ -54,7 +55,7 @@ fn test_incomplete_bits() { // Take bit slices with sizes [4, 8]. let result: IResult<_, (u8, u8)> = - bits::<_, _, Error<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_next(input); + bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input); assert!(result.is_err()); let error = result.err().unwrap(); @@ -68,7 +69,7 @@ fn test_take_complete_0() { assert_eq!(count, 0usize); let offset = 0usize; - let result: crate::IResult<(&[u8], usize), usize> = take(count).parse_next((input, offset)); + let result: crate::IResult<(&[u8], usize), usize> = take(count).parse_peek((input, offset)); assert_eq!(result, Ok(((input, offset), 0))); } @@ -77,14 +78,14 @@ fn test_take_complete_0() { fn test_take_complete_eof() { let input = &[0b00010010][..]; - let result: crate::IResult<(&[u8], usize), usize> = take(1usize).parse_next((input, 8)); + let result: crate::IResult<(&[u8], usize), usize> = take(1usize).parse_peek((input, 8)); assert_eq!( result, - Err(crate::error::ErrMode::Backtrack(crate::error::Error { - input: (input, 8), - kind: ErrorKind::Eof - })) + Err(crate::error::ErrMode::Backtrack(InputError::new( + (input, 8), + ErrorKind::Eof + ))) ); } @@ -92,7 +93,7 @@ fn test_take_complete_eof() { fn test_take_complete_span_over_multiple_bytes() { let input = &[0b00010010, 0b00110100, 0b11111111, 0b11111111][..]; - let result: crate::IResult<(&[u8], usize), usize> = take(24usize).parse_next((input, 4)); + let result: crate::IResult<(&[u8], usize), usize> = take(24usize).parse_peek((input, 4)); assert_eq!( result, @@ -107,7 +108,7 @@ fn test_take_partial_0() { assert_eq!(count, 0usize); let offset = 0usize; - let result: crate::IResult<(_, usize), usize> = take(count).parse_next((input, offset)); + let result: crate::IResult<(_, usize), usize> = take(count).parse_peek((input, offset)); assert_eq!(result, Ok(((input, offset), 0))); } @@ -120,7 +121,7 @@ fn test_tag_partial_ok() { let value_to_tag = 0b0001; let result: crate::IResult<(_, usize), usize> = - tag(value_to_tag, bits_to_take).parse_next((input, offset)); + tag(value_to_tag, bits_to_take).parse_peek((input, offset)); assert_eq!(result, Ok(((input, bits_to_take), value_to_tag))); } @@ -133,14 +134,14 @@ fn test_tag_partial_err() { let value_to_tag = 0b1111; let result: crate::IResult<(_, usize), usize> = - tag(value_to_tag, bits_to_take).parse_next((input, offset)); + tag(value_to_tag, bits_to_take).parse_peek((input, offset)); assert_eq!( result, - Err(crate::error::ErrMode::Backtrack(crate::error::Error { - input: (input, offset), - kind: ErrorKind::Tag - })) + Err(crate::error::ErrMode::Backtrack(InputError::new( + (input, offset), + ErrorKind::Tag + ))) ); } @@ -148,7 +149,7 @@ fn test_tag_partial_err() { fn test_bool_0_complete() { let input = [0b10000000].as_ref(); - let result: crate::IResult<(&[u8], usize), bool> = bool((input, 0)); + let result: crate::IResult<(&[u8], usize), bool> = bool.parse_peek((input, 0)); assert_eq!(result, Ok(((input, 1), true))); } @@ -157,14 +158,14 @@ fn test_bool_0_complete() { fn test_bool_eof_complete() { let input = [0b10000000].as_ref(); - let result: crate::IResult<(&[u8], usize), bool> = bool((input, 8)); + let result: crate::IResult<(&[u8], usize), bool> = bool.parse_peek((input, 8)); assert_eq!( result, - Err(crate::error::ErrMode::Backtrack(crate::error::Error { - input: (input, 8), - kind: ErrorKind::Eof - })) + Err(crate::error::ErrMode::Backtrack(InputError::new( + (input, 8), + ErrorKind::Eof + ))) ); } @@ -172,7 +173,7 @@ fn test_bool_eof_complete() { fn test_bool_0_partial() { let input = Partial::new([0b10000000].as_ref()); - let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool((input, 0)); + let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool.parse_peek((input, 0)); assert_eq!(result, Ok(((input, 1), true))); } @@ -181,7 +182,7 @@ fn test_bool_0_partial() { fn test_bool_eof_partial() { let input = Partial::new([0b10000000].as_ref()); - let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool.parse_next((input, 8)); + let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool.parse_peek((input, 8)); assert_eq!( result, diff --git a/vendor/winnow/src/binary/mod.rs b/vendor/winnow/src/binary/mod.rs index 80435e359..8b2ee74ee 100644 --- a/vendor/winnow/src/binary/mod.rs +++ b/vendor/winnow/src/binary/mod.rs @@ -11,14 +11,14 @@ use crate::combinator::repeat; use crate::error::ErrMode; use crate::error::ErrorKind; use crate::error::Needed; -use crate::error::ParseError; +use crate::error::ParserError; use crate::lib::std::ops::{Add, Shl}; use crate::stream::Accumulate; use crate::stream::{AsBytes, Stream, StreamIsPartial}; use crate::stream::{ToUsize, UpdateSlice}; use crate::token::take; use crate::trace::trace; -use crate::IResult; +use crate::PResult; use crate::Parser; /// Configurable endianness @@ -41,34 +41,34 @@ pub enum Endianness { /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u8; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u8> { -/// be_u8.parse_next(s) +/// be_u8.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token)))); +/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&[][..], ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_u8; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> { -/// be_u8.parse_next(s) +/// be_u8.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> +pub fn be_u8<I, E: ParserError<I>>(input: &mut I) -> PResult<u8, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -85,40 +85,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u16; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u16> { -/// be_u16.parse_next(s) +/// be_u16.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_u16; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { -/// be_u16.parse_next(s) +/// be_u16.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E> +pub fn be_u16<I, E: ParserError<I>>(input: &mut I) -> PResult<u16, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_u16", move |input: I| be_uint(input, 2)).parse_next(input) + trace("be_u16", move |input: &mut I| be_uint(input, 2)).parse_next(input) } /// Recognizes a big endian unsigned 3 byte integer. @@ -130,40 +130,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u24; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u32> { -/// be_u24.parse_next(s) +/// be_u24.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_u24; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { -/// be_u24.parse_next(s) +/// be_u24.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x000102))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> +pub fn be_u24<I, E: ParserError<I>>(input: &mut I) -> PResult<u32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_u23", move |input: I| be_uint(input, 3)).parse_next(input) + trace("be_u23", move |input: &mut I| be_uint(input, 3)).parse_next(input) } /// Recognizes a big endian unsigned 4 bytes integer. @@ -175,40 +175,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u32; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u32> { -/// be_u32.parse_next(s) +/// be_u32.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_u32; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { -/// be_u32.parse_next(s) +/// be_u32.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> +pub fn be_u32<I, E: ParserError<I>>(input: &mut I) -> PResult<u32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_u32", move |input: I| be_uint(input, 4)).parse_next(input) + trace("be_u32", move |input: &mut I| be_uint(input, 4)).parse_next(input) } /// Recognizes a big endian unsigned 8 bytes integer. @@ -220,40 +220,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u64; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u64> { -/// be_u64.parse_next(s) +/// be_u64.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_u64; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { -/// be_u64.parse_next(s) +/// be_u64.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001020304050607))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E> +pub fn be_u64<I, E: ParserError<I>>(input: &mut I) -> PResult<u64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_u64", move |input: I| be_uint(input, 8)).parse_next(input) + trace("be_u64", move |input: &mut I| be_uint(input, 8)).parse_next(input) } /// Recognizes a big endian unsigned 16 bytes integer. @@ -265,44 +265,44 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u128; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u128> { -/// be_u128.parse_next(s) +/// be_u128.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_u128; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u128> { -/// be_u128.parse_next(s) +/// be_u128.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203040506070809101112131415))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E> +pub fn be_u128<I, E: ParserError<I>>(input: &mut I) -> PResult<u128, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_u128", move |input: I| be_uint(input, 16)).parse_next(input) + trace("be_u128", move |input: &mut I| be_uint(input, 16)).parse_next(input) } #[inline] -fn be_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E> +fn be_uint<I, Uint, E: ParserError<I>>(input: &mut I, bound: usize) -> PResult<Uint, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -337,34 +337,34 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i8; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i8> { -/// be_i8.parse_next(s) +/// be_i8.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token)))); +/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&[][..], ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_i8; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i8> { -/// be_i8.parse_next(s) +/// be_i8.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> +pub fn be_i8<I, E: ParserError<I>>(input: &mut I) -> PResult<i8, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -381,41 +381,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i16; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i16> { -/// be_i16.parse_next(s) +/// be_i16.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_i16; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { -/// be_i16.parse_next(s) +/// be_i16.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E> +pub fn be_i16<I, E: ParserError<I>>(input: &mut I) -> PResult<i16, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_i16", move |input: I| { - be_uint::<_, u16, _>(input, 2).map(|(i, n)| (i, n as i16)) + trace("be_i16", move |input: &mut I| { + be_uint::<_, u16, _>(input, 2).map(|n| n as i16) }) .parse_next(input) } @@ -429,48 +429,48 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i24; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i32> { -/// be_i24.parse_next(s) +/// be_i24.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_i24; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { -/// be_i24.parse_next(s) +/// be_i24.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x000102))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> +pub fn be_i24<I, E: ParserError<I>>(input: &mut I) -> PResult<i32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_i24", move |input: I| { - be_uint::<_, u32, _>(input, 3).map(|(i, n)| { + trace("be_i24", move |input: &mut I| { + be_uint::<_, u32, _>(input, 3).map(|n| { // Same as the unsigned version but we need to sign-extend manually here let n = if n & 0x80_00_00 != 0 { (n | 0xff_00_00_00) as i32 } else { n as i32 }; - (i, n) + n }) }) .parse_next(input) @@ -485,41 +485,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i32; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i32> { -/// be_i32.parse_next(s) +/// be_i32.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_i32; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { -/// be_i32.parse_next(s) +/// be_i32.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(4)))); /// ``` #[inline(always)] -pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> +pub fn be_i32<I, E: ParserError<I>>(input: &mut I) -> PResult<i32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_i32", move |input: I| { - be_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, n as i32)) + trace("be_i32", move |input: &mut I| { + be_uint::<_, u32, _>(input, 4).map(|n| n as i32) }) .parse_next(input) } @@ -533,41 +533,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i64; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i64> { -/// be_i64.parse_next(s) +/// be_i64.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_i64; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { -/// be_i64.parse_next(s) +/// be_i64.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0001020304050607))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E> +pub fn be_i64<I, E: ParserError<I>>(input: &mut I) -> PResult<i64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_i64", move |input: I| { - be_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, n as i64)) + trace("be_i64", move |input: &mut I| { + be_uint::<_, u64, _>(input, 8).map(|n| n as i64) }) .parse_next(input) } @@ -581,41 +581,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i128; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i128> { -/// be_i128.parse_next(s) +/// be_i128.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_i128; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i128> { -/// be_i128.parse_next(s) +/// be_i128.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x00010203040506070809101112131415))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E> +pub fn be_i128<I, E: ParserError<I>>(input: &mut I) -> PResult<i128, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_i128", move |input: I| { - be_uint::<_, u128, _>(input, 16).map(|(i, n)| (i, n as i128)) + trace("be_i128", move |input: &mut I| { + be_uint::<_, u128, _>(input, 16).map(|n| n as i128) }) .parse_next(input) } @@ -629,34 +629,34 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u8; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u8> { -/// le_u8.parse_next(s) +/// le_u8.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token)))); +/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&[][..], ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_u8; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> { -/// le_u8.parse_next(s) +/// le_u8.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> +pub fn le_u8<I, E: ParserError<I>>(input: &mut I) -> PResult<u8, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -673,40 +673,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u16; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u16> { -/// le_u16.parse_next(s) +/// le_u16.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_u16; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { -/// le_u16::<_, Error<_>>.parse_next(s) +/// le_u16::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E> +pub fn le_u16<I, E: ParserError<I>>(input: &mut I) -> PResult<u16, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_u16", move |input: I| le_uint(input, 2)).parse_next(input) + trace("le_u16", move |input: &mut I| le_uint(input, 2)).parse_next(input) } /// Recognizes a little endian unsigned 3 byte integer. @@ -718,40 +718,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u24; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u32> { -/// le_u24.parse_next(s) +/// le_u24.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_u24; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { -/// le_u24::<_, Error<_>>.parse_next(s) +/// le_u24::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> +pub fn le_u24<I, E: ParserError<I>>(input: &mut I) -> PResult<u32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_u24", move |input: I| le_uint(input, 3)).parse_next(input) + trace("le_u24", move |input: &mut I| le_uint(input, 3)).parse_next(input) } /// Recognizes a little endian unsigned 4 bytes integer. @@ -763,40 +763,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u32; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u32> { -/// le_u32.parse_next(s) +/// le_u32.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_u32; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { -/// le_u32::<_, Error<_>>.parse_next(s) +/// le_u32::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x03020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> +pub fn le_u32<I, E: ParserError<I>>(input: &mut I) -> PResult<u32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_u32", move |input: I| le_uint(input, 4)).parse_next(input) + trace("le_u32", move |input: &mut I| le_uint(input, 4)).parse_next(input) } /// Recognizes a little endian unsigned 8 bytes integer. @@ -808,40 +808,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u64; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u64> { -/// le_u64.parse_next(s) +/// le_u64.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_u64; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { -/// le_u64::<_, Error<_>>.parse_next(s) +/// le_u64::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0706050403020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E> +pub fn le_u64<I, E: ParserError<I>>(input: &mut I) -> PResult<u64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_u64", move |input: I| le_uint(input, 8)).parse_next(input) + trace("le_u64", move |input: &mut I| le_uint(input, 8)).parse_next(input) } /// Recognizes a little endian unsigned 16 bytes integer. @@ -853,44 +853,44 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u128; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u128> { -/// le_u128.parse_next(s) +/// le_u128.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_u128; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u128> { -/// le_u128::<_, Error<_>>.parse_next(s) +/// le_u128::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x15141312111009080706050403020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E> +pub fn le_u128<I, E: ParserError<I>>(input: &mut I) -> PResult<u128, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_u128", move |input: I| le_uint(input, 16)).parse_next(input) + trace("le_u128", move |input: &mut I| le_uint(input, 16)).parse_next(input) } #[inline] -fn le_uint<I, Uint, E: ParseError<I>>(input: I, bound: usize) -> IResult<I, Uint, E> +fn le_uint<I, Uint, E: ParserError<I>>(input: &mut I, bound: usize) -> PResult<Uint, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -924,34 +924,34 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i8; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i8> { -/// le_i8.parse_next(s) +/// le_i8.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token)))); +/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&[][..], ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_i8; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i8> { -/// le_i8.parse_next(s) +/// le_i8.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"\x01abcd"[..]), 0x00))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> +pub fn le_i8<I, E: ParserError<I>>(input: &mut I) -> PResult<i8, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -968,41 +968,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i16; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i16> { -/// le_i16.parse_next(s) +/// le_i16.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_i16; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { -/// le_i16::<_, Error<_>>.parse_next(s) +/// le_i16::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E> +pub fn le_i16<I, E: ParserError<I>>(input: &mut I) -> PResult<i16, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_i16", move |input: I| { - le_uint::<_, u16, _>(input, 2).map(|(i, n)| (i, n as i16)) + trace("le_i16", move |input: &mut I| { + le_uint::<_, u16, _>(input, 2).map(|n| n as i16) }) .parse_next(input) } @@ -1016,48 +1016,48 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i24; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i32> { -/// le_i24.parse_next(s) +/// le_i24.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_i24; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { -/// le_i24::<_, Error<_>>.parse_next(s) +/// le_i24::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> +pub fn le_i24<I, E: ParserError<I>>(input: &mut I) -> PResult<i32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_i24", move |input: I| { - le_uint::<_, u32, _>(input, 3).map(|(i, n)| { + trace("le_i24", move |input: &mut I| { + le_uint::<_, u32, _>(input, 3).map(|n| { // Same as the unsigned version but we need to sign-extend manually here let n = if n & 0x80_00_00 != 0 { (n | 0xff_00_00_00) as i32 } else { n as i32 }; - (i, n) + n }) }) .parse_next(input) @@ -1072,41 +1072,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i32; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i32> { -/// le_i32.parse_next(s) +/// le_i32.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_i32; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { -/// le_i32::<_, Error<_>>.parse_next(s) +/// le_i32::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x03020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> +pub fn le_i32<I, E: ParserError<I>>(input: &mut I) -> PResult<i32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_i32", move |input: I| { - le_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, n as i32)) + trace("le_i32", move |input: &mut I| { + le_uint::<_, u32, _>(input, 4).map(|n| n as i32) }) .parse_next(input) } @@ -1120,41 +1120,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i64; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i64> { -/// le_i64.parse_next(s) +/// le_i64.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_i64; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { -/// le_i64::<_, Error<_>>.parse_next(s) +/// le_i64::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0706050403020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E> +pub fn le_i64<I, E: ParserError<I>>(input: &mut I) -> PResult<i64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_i64", move |input: I| { - le_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, n as i64)) + trace("le_i64", move |input: &mut I| { + le_uint::<_, u64, _>(input, 8).map(|n| n as i64) }) .parse_next(input) } @@ -1168,41 +1168,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i128; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i128> { -/// le_i128.parse_next(s) +/// le_i128.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); -/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_i128; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i128> { -/// le_i128::<_, Error<_>>.parse_next(s) +/// le_i128::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x15141312111009080706050403020100))); /// assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E> +pub fn le_i128<I, E: ParserError<I>>(input: &mut I) -> PResult<i128, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_i128", move |input: I| { - le_uint::<_, u128, _>(input, 16).map(|(i, n)| (i, n as i128)) + trace("le_i128", move |input: &mut I| { + le_uint::<_, u128, _>(input, 16).map(|n| n as i128) }) .parse_next(input) } @@ -1218,40 +1218,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::u8; /// /// fn parser(s: &[u8]) -> IResult<&[u8], u8> { -/// u8.parse_next(s) +/// u8.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token)))); +/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&[][..], ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::u8; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> { -/// u8::<_, Error<_>>.parse_next(s) +/// u8::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"\x03abcefg"[..]), 0x00))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> +pub fn u8<I, E: ParserError<I>>(input: &mut I) -> PResult<u8, E> where I: StreamIsPartial, I: Stream<Token = u8>, { - trace("u8", move |input: I| { + trace("u8", move |input: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { u8_::<_, _, true>(input) } else { @@ -1261,7 +1261,7 @@ where .parse_next(input) } -fn u8_<I, E: ParseError<I>, const PARTIAL: bool>(input: I) -> IResult<I, u8, E> +fn u8_<I, E: ParserError<I>, const PARTIAL: bool>(input: &mut I) -> PResult<u8, E> where I: StreamIsPartial, I: Stream<Token = u8>, @@ -1287,55 +1287,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::u16; /// /// let be_u16 = |s| { -/// u16(winnow::binary::Endianness::Big).parse_next(s) +/// u16(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); -/// assert_eq!(be_u16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_u16(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_u16 = |s| { -/// u16(winnow::binary::Endianness::Little).parse_next(s) +/// u16(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300))); -/// assert_eq!(le_u16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_u16(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::u16; /// /// let be_u16 = |s| { -/// u16::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// u16::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003))); /// assert_eq!(be_u16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// let le_u16 = |s| { -/// u16::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// u16::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0300))); /// assert_eq!(le_u16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn u16<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u16, E> +pub fn u16<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, u16, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_u16, Endianness::Little => le_u16, @@ -1359,55 +1359,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::u24; /// /// let be_u24 = |s| { -/// u24(winnow::binary::Endianness::Big).parse_next(s) +/// u24(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); -/// assert_eq!(be_u24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_u24(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_u24 = |s| { -/// u24(winnow::binary::Endianness::Little).parse_next(s) +/// u24(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300))); -/// assert_eq!(le_u24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_u24(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::u24; /// /// let be_u24 = |s| { -/// u24::<_,Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// u24::<_,InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x000305))); /// assert_eq!(be_u24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// /// let le_u24 = |s| { -/// u24::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// u24::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x050300))); /// assert_eq!(le_u24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn u24<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u32, E> +pub fn u24<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, u32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_u24, Endianness::Little => le_u24, @@ -1431,55 +1431,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::u32; /// /// let be_u32 = |s| { -/// u32(winnow::binary::Endianness::Big).parse_next(s) +/// u32(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); -/// assert_eq!(be_u32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_u32(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_u32 = |s| { -/// u32(winnow::binary::Endianness::Little).parse_next(s) +/// u32(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300))); -/// assert_eq!(le_u32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_u32(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::u32; /// /// let be_u32 = |s| { -/// u32::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// u32::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00030507))); /// assert_eq!(be_u32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// /// let le_u32 = |s| { -/// u32::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// u32::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07050300))); /// assert_eq!(le_u32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn u32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u32, E> +pub fn u32<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, u32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_u32, Endianness::Little => le_u32, @@ -1503,55 +1503,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::u64; /// /// let be_u64 = |s| { -/// u64(winnow::binary::Endianness::Big).parse_next(s) +/// u64(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); -/// assert_eq!(be_u64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_u64(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_u64 = |s| { -/// u64(winnow::binary::Endianness::Little).parse_next(s) +/// u64(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100))); -/// assert_eq!(le_u64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_u64(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::u64; /// /// let be_u64 = |s| { -/// u64::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// u64::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0001020304050607))); /// assert_eq!(be_u64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// /// let le_u64 = |s| { -/// u64::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// u64::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0706050403020100))); /// assert_eq!(le_u64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn u64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u64, E> +pub fn u64<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, u64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_u64, Endianness::Little => le_u64, @@ -1575,55 +1575,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::u128; /// /// let be_u128 = |s| { -/// u128(winnow::binary::Endianness::Big).parse_next(s) +/// u128(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); -/// assert_eq!(be_u128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_u128(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_u128 = |s| { -/// u128(winnow::binary::Endianness::Little).parse_next(s) +/// u128(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); -/// assert_eq!(le_u128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_u128(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::u128; /// /// let be_u128 = |s| { -/// u128::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// u128::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_u128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00010203040506070001020304050607))); /// assert_eq!(be_u128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// /// let le_u128 = |s| { -/// u128::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// u128::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_u128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07060504030201000706050403020100))); /// assert_eq!(le_u128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn u128<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, u128, E> +pub fn u128<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, u128, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_u128, Endianness::Little => le_u128, @@ -1646,46 +1646,46 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::i8; /// /// fn parser(s: &[u8]) -> IResult<&[u8], i8> { -/// i8.parse_next(s) +/// i8.parse_peek(s) /// } /// /// assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"\x03abcefg"[..], 0x00))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&[][..], ErrorKind::Token)))); +/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&[][..], ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::i8; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i8> { -/// i8.parse_next(s) +/// i8.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"\x03abcefg"[..]), 0x00))); /// assert_eq!(parser(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> +pub fn i8<I, E: ParserError<I>>(input: &mut I) -> PResult<i8, E> where I: StreamIsPartial, I: Stream<Token = u8>, { - trace("i8", move |input: I| { + trace("i8", move |input: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { u8_::<_, _, true>(input) } else { u8_::<_, _, false>(input) } - .map(|(i, n)| (i, n as i8)) + .map(|n| n as i8) }) .parse_next(input) } @@ -1702,55 +1702,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::i16; /// /// let be_i16 = |s| { -/// i16(winnow::binary::Endianness::Big).parse_next(s) +/// i16(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); -/// assert_eq!(be_i16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_i16(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_i16 = |s| { -/// i16(winnow::binary::Endianness::Little).parse_next(s) +/// i16(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i16(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300))); -/// assert_eq!(le_i16(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_i16(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::i16; /// /// let be_i16 = |s| { -/// i16::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// i16::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003))); /// assert_eq!(be_i16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// let le_i16 = |s| { -/// i16::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// i16::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i16(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0300))); /// assert_eq!(le_i16(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn i16<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i16, E> +pub fn i16<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, i16, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_i16, Endianness::Little => le_i16, @@ -1774,55 +1774,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::i24; /// /// let be_i24 = |s| { -/// i24(winnow::binary::Endianness::Big).parse_next(s) +/// i24(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); -/// assert_eq!(be_i24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_i24(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_i24 = |s| { -/// i24(winnow::binary::Endianness::Little).parse_next(s) +/// i24(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i24(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x050300))); -/// assert_eq!(le_i24(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_i24(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::i24; /// /// let be_i24 = |s| { -/// i24::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// i24::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x000305))); /// assert_eq!(be_i24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// /// let le_i24 = |s| { -/// i24::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// i24::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i24(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x050300))); /// assert_eq!(le_i24(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn i24<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i32, E> +pub fn i24<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, i32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_i24, Endianness::Little => le_i24, @@ -1846,55 +1846,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::i32; /// /// let be_i32 = |s| { -/// i32(winnow::binary::Endianness::Big).parse_next(s) +/// i32(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); -/// assert_eq!(be_i32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_i32(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_i32 = |s| { -/// i32(winnow::binary::Endianness::Little).parse_next(s) +/// i32(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i32(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07050300))); -/// assert_eq!(le_i32(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_i32(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::i32; /// /// let be_i32 = |s| { -/// i32::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// i32::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00030507))); /// assert_eq!(be_i32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// /// let le_i32 = |s| { -/// i32::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// i32::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i32(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07050300))); /// assert_eq!(le_i32(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn i32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i32, E> +pub fn i32<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, i32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_i32, Endianness::Little => le_i32, @@ -1918,55 +1918,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::i64; /// /// let be_i64 = |s| { -/// i64(winnow::binary::Endianness::Big).parse_next(s) +/// i64(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); -/// assert_eq!(be_i64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_i64(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_i64 = |s| { -/// i64(winnow::binary::Endianness::Little).parse_next(s) +/// i64(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i64(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0706050403020100))); -/// assert_eq!(le_i64(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_i64(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::i64; /// /// let be_i64 = |s| { -/// i64::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// i64::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0001020304050607))); /// assert_eq!(be_i64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// /// let le_i64 = |s| { -/// i64::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// i64::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i64(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0706050403020100))); /// assert_eq!(le_i64(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn i64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i64, E> +pub fn i64<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, i64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_i64, Endianness::Little => le_i64, @@ -1990,55 +1990,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::i128; /// /// let be_i128 = |s| { -/// i128(winnow::binary::Endianness::Big).parse_next(s) +/// i128(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); -/// assert_eq!(be_i128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(be_i128(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// /// let le_i128 = |s| { -/// i128(winnow::binary::Endianness::Little).parse_next(s) +/// i128(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i128(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); -/// assert_eq!(le_i128(&b"\x01"[..]), Err(ErrMode::Backtrack(Error::new(&[0x01][..], ErrorKind::Slice)))); +/// assert_eq!(le_i128(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::i128; /// /// let be_i128 = |s| { -/// i128::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// i128::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_i128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00010203040506070001020304050607))); /// assert_eq!(be_i128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// /// let le_i128 = |s| { -/// i128::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// i128::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_i128(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x07060504030201000706050403020100))); /// assert_eq!(le_i128(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn i128<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, i128, E> +pub fn i128<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, i128, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_i128, Endianness::Little => le_i128, @@ -2059,42 +2059,42 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_f32; /// /// fn parser(s: &[u8]) -> IResult<&[u8], f32> { -/// be_f32.parse_next(s) +/// be_f32.parse_peek(s) /// } /// /// assert_eq!(parser(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_f32; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f32> { -/// be_f32.parse_next(s) +/// be_f32.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&[0x40, 0x29, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 2.640625))); /// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E> +pub fn be_f32<I, E: ParserError<I>>(input: &mut I) -> PResult<f32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_f32", move |input: I| { - be_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, f32::from_bits(n))) + trace("be_f32", move |input: &mut I| { + be_uint::<_, u32, _>(input, 4).map(f32::from_bits) }) .parse_next(input) } @@ -2108,41 +2108,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::be_f64; /// /// fn parser(s: &[u8]) -> IResult<&[u8], f64> { -/// be_f64.parse_next(s) +/// be_f64.parse_peek(s) /// } /// /// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::be_f64; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f64> { -/// be_f64::<_, Error<_>>.parse_next(s) +/// be_f64::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E> +pub fn be_f64<I, E: ParserError<I>>(input: &mut I) -> PResult<f64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_f64", move |input: I| { - be_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, f64::from_bits(n))) + trace("be_f64", move |input: &mut I| { + be_uint::<_, u64, _>(input, 8).map(f64::from_bits) }) .parse_next(input) } @@ -2156,41 +2156,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_f32; /// /// fn parser(s: &[u8]) -> IResult<&[u8], f32> { -/// le_f32.parse_next(s) +/// le_f32.parse_peek(s) /// } /// /// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_f32; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f32> { -/// le_f32::<_, Error<_>>.parse_next(s) +/// le_f32::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&[0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E> +pub fn le_f32<I, E: ParserError<I>>(input: &mut I) -> PResult<f32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("le_f32", move |input: I| { - le_uint::<_, u32, _>(input, 4).map(|(i, n)| (i, f32::from_bits(n))) + trace("le_f32", move |input: &mut I| { + le_uint::<_, u32, _>(input, 4).map(f32::from_bits) }) .parse_next(input) } @@ -2204,41 +2204,41 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::le_f64; /// /// fn parser(s: &[u8]) -> IResult<&[u8], f64> { -/// le_f64.parse_next(s) +/// le_f64.parse_peek(s) /// } /// /// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(parser(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::binary::le_f64; /// /// fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, f64> { -/// le_f64::<_, Error<_>>.parse_next(s) +/// le_f64::<_, InputError<_>>.parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 3145728.0))); /// assert_eq!(parser(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E> +pub fn le_f64<I, E: ParserError<I>>(input: &mut I) -> PResult<f64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - trace("be_f64", move |input: I| { - le_uint::<_, u64, _>(input, 8).map(|(i, n)| (i, f64::from_bits(n))) + trace("be_f64", move |input: &mut I| { + le_uint::<_, u64, _>(input, 8).map(f64::from_bits) }) .parse_next(input) } @@ -2255,55 +2255,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::f32; /// /// let be_f32 = |s| { -/// f32(winnow::binary::Endianness::Big).parse_next(s) +/// f32(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(be_f32(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(be_f32(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// /// let le_f32 = |s| { -/// f32(winnow::binary::Endianness::Little).parse_next(s) +/// f32(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(le_f32(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(le_f32(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::f32; /// /// let be_f32 = |s| { -/// f32::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// f32::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_f32(Partial::new(&[0x41, 0x48, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(be_f32(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// let le_f32 = |s| { -/// f32::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// f32::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_f32(Partial::new(&[0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(le_f32(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn f32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, f32, E> +pub fn f32<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, f32, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_f32, Endianness::Little => le_f32, @@ -2327,55 +2327,55 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::binary::f64; /// /// let be_f64 = |s| { -/// f64(winnow::binary::Endianness::Big).parse_next(s) +/// f64(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(be_f64(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(be_f64(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// /// let le_f64 = |s| { -/// f64(winnow::binary::Endianness::Little).parse_next(s) +/// f64(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5))); -/// assert_eq!(le_f64(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(le_f64(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// # use winnow::Partial; /// use winnow::binary::f64; /// /// let be_f64 = |s| { -/// f64::<_, Error<_>>(winnow::binary::Endianness::Big).parse_next(s) +/// f64::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s) /// }; /// /// assert_eq!(be_f64(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(be_f64(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5)))); /// /// let le_f64 = |s| { -/// f64::<_, Error<_>>(winnow::binary::Endianness::Little).parse_next(s) +/// f64::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s) /// }; /// /// assert_eq!(le_f64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(le_f64(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5)))); /// ``` #[inline(always)] -pub fn f64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, f64, E> +pub fn f64<I, E: ParserError<I>>(endian: Endianness) -> impl Parser<I, f64, E> where I: StreamIsPartial, I: Stream<Token = u8>, <I as Stream>::Slice: AsBytes, { - move |input: I| { + move |input: &mut I| { match endian { Endianness::Big => be_f64, Endianness::Little => le_f64, @@ -2414,7 +2414,7 @@ where /// } /// /// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, &[u8]> { -/// length_data(be_u16).parse_next(s) +/// length_data(be_u16).parse_peek(s) /// } /// /// assert_eq!(parser(stream(b"\x00\x03abcefg")), Ok((stream(&b"efg"[..]), &b"abc"[..]))); @@ -2426,10 +2426,10 @@ where I: Stream, N: ToUsize, F: Parser<I, N, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("length_data", move |i: I| { - let (i, length) = f.parse_next(i)?; + trace("length_data", move |i: &mut I| { + let length = f.parse_next(i)?; crate::token::take(length).parse_next(i) }) @@ -2452,7 +2452,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed, stream::{Partial, StreamIsPartial}}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed, stream::{Partial, StreamIsPartial}}; /// # use winnow::prelude::*; /// use winnow::Bytes; /// use winnow::binary::be_u16; @@ -2472,28 +2472,28 @@ where /// } /// /// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, &[u8]> { -/// length_value(be_u16, "abc").parse_next(s) +/// length_value(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(Error::new(complete_stream(&b"123"[..]), ErrorKind::Tag)))); +/// 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> where I: StreamIsPartial, - I: Stream + UpdateSlice, + I: Stream + UpdateSlice + Clone, N: ToUsize, F: Parser<I, N, E>, G: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("length_value", move |i: I| { - let (i, data) = length_data(f.by_ref()).parse_next(i)?; + trace("length_value", move |i: &mut I| { + let data = length_data(f.by_ref()).parse_next(i)?; let mut data = I::update_slice(i.clone(), data); let _ = data.complete(); - let (_, o) = g.by_ref().complete_err().parse_next(data)?; - Ok((i, o)) + let o = g.by_ref().complete_err().parse_next(&mut data)?; + Ok(o) }) } @@ -2509,7 +2509,7 @@ where /// ```rust /// # #[cfg(feature = "std")] { /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::Bytes; /// use winnow::binary::u8; @@ -2526,11 +2526,11 @@ where /// length_count(u8.map(|i| { /// println!("got number: {}", i); /// i -/// }), "abc").parse_next(s) +/// }), "abc").parse_peek(s) /// } /// /// assert_eq!(parser(stream(b"\x02abcabcabc")), Ok((stream(b"abc"), vec![&b"abc"[..], &b"abc"[..]]))); -/// assert_eq!(parser(stream(b"\x03123123123")), Err(ErrMode::Backtrack(Error::new(stream(b"123123123"), ErrorKind::Tag)))); +/// assert_eq!(parser(stream(b"\x03123123123")), Err(ErrMode::Backtrack(InputError::new(stream(b"123123123"), ErrorKind::Tag)))); /// # } /// ``` pub fn length_count<I, O, C, N, E, F, G>(mut f: F, mut g: G) -> impl Parser<I, C, E> @@ -2540,10 +2540,10 @@ where C: Accumulate<O>, F: Parser<I, N, E>, G: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("length_count", move |i: I| { - let (i, n) = f.parse_next(i)?; + trace("length_count", move |i: &mut I| { + let n = f.parse_next(i)?; let n = n.to_usize(); repeat(n, g.by_ref()).parse_next(i) }) diff --git a/vendor/winnow/src/binary/tests.rs b/vendor/winnow/src/binary/tests.rs index 4307d88fe..5d92055ac 100644 --- a/vendor/winnow/src/binary/tests.rs +++ b/vendor/winnow/src/binary/tests.rs @@ -1,70 +1,96 @@ use super::*; +use crate::unpeek; +use crate::IResult; mod complete { use super::*; - use crate::error::Error; + use crate::error::InputError; macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::IResult<_, _, Error<_>> = $left; + let res: $crate::IResult<_, _, InputError<_>> = $left; assert_eq!(res, $right); }; ); #[test] fn i8_tests() { - assert_parse!(i8(&[0x00][..]), Ok((&b""[..], 0))); - assert_parse!(i8(&[0x7f][..]), Ok((&b""[..], 127))); - assert_parse!(i8(&[0xff][..]), Ok((&b""[..], -1))); - assert_parse!(i8(&[0x80][..]), Ok((&b""[..], -128))); + assert_parse!(i8.parse_peek(&[0x00][..]), Ok((&b""[..], 0))); + assert_parse!(i8.parse_peek(&[0x7f][..]), Ok((&b""[..], 127))); + assert_parse!(i8.parse_peek(&[0xff][..]), Ok((&b""[..], -1))); + assert_parse!(i8.parse_peek(&[0x80][..]), Ok((&b""[..], -128))); } #[test] fn be_i8_tests() { - assert_parse!(be_i8(&[0x00][..]), Ok((&b""[..], 0))); - assert_parse!(be_i8(&[0x7f][..]), Ok((&b""[..], 127))); - assert_parse!(be_i8(&[0xff][..]), Ok((&b""[..], -1))); - assert_parse!(be_i8(&[0x80][..]), Ok((&b""[..], -128))); + assert_parse!(be_i8.parse_peek(&[0x00][..]), Ok((&b""[..], 0))); + assert_parse!(be_i8.parse_peek(&[0x7f][..]), Ok((&b""[..], 127))); + assert_parse!(be_i8.parse_peek(&[0xff][..]), Ok((&b""[..], -1))); + assert_parse!(be_i8.parse_peek(&[0x80][..]), Ok((&b""[..], -128))); } #[test] fn be_i16_tests() { - assert_parse!(be_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0))); - assert_parse!(be_i16(&[0x7f, 0xff][..]), Ok((&b""[..], 32_767_i16))); - assert_parse!(be_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1))); - assert_parse!(be_i16(&[0x80, 0x00][..]), Ok((&b""[..], -32_768_i16))); + assert_parse!(be_i16.parse_peek(&[0x00, 0x00][..]), Ok((&b""[..], 0))); + assert_parse!( + be_i16.parse_peek(&[0x7f, 0xff][..]), + Ok((&b""[..], 32_767_i16)) + ); + assert_parse!(be_i16.parse_peek(&[0xff, 0xff][..]), Ok((&b""[..], -1))); + assert_parse!( + be_i16.parse_peek(&[0x80, 0x00][..]), + Ok((&b""[..], -32_768_i16)) + ); } #[test] fn be_u24_tests() { - assert_parse!(be_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0))); - assert_parse!(be_u24(&[0x00, 0xFF, 0xFF][..]), Ok((&b""[..], 65_535_u32))); assert_parse!( - be_u24(&[0x12, 0x34, 0x56][..]), + be_u24.parse_peek(&[0x00, 0x00, 0x00][..]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_u24.parse_peek(&[0x00, 0xFF, 0xFF][..]), + Ok((&b""[..], 65_535_u32)) + ); + assert_parse!( + be_u24.parse_peek(&[0x12, 0x34, 0x56][..]), Ok((&b""[..], 1_193_046_u32)) ); } #[test] fn be_i24_tests() { - assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32))); - assert_parse!(be_i24(&[0xFF, 0x00, 0x00][..]), Ok((&b""[..], -65_536_i32))); assert_parse!( - be_i24(&[0xED, 0xCB, 0xAA][..]), + be_i24.parse_peek(&[0xFF, 0xFF, 0xFF][..]), + Ok((&b""[..], -1_i32)) + ); + assert_parse!( + be_i24.parse_peek(&[0xFF, 0x00, 0x00][..]), + Ok((&b""[..], -65_536_i32)) + ); + assert_parse!( + be_i24.parse_peek(&[0xED, 0xCB, 0xAA][..]), Ok((&b""[..], -1_193_046_i32)) ); } #[test] fn be_i32_tests() { - assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0))); assert_parse!( - be_i32(&[0x7f, 0xff, 0xff, 0xff][..]), + be_i32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i32.parse_peek(&[0x7f, 0xff, 0xff, 0xff][..]), Ok((&b""[..], 2_147_483_647_i32)) ); - assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1))); assert_parse!( - be_i32(&[0x80, 0x00, 0x00, 0x00][..]), + be_i32.parse_peek(&[0xff, 0xff, 0xff, 0xff][..]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i32.parse_peek(&[0x80, 0x00, 0x00, 0x00][..]), Ok((&b""[..], -2_147_483_648_i32)) ); } @@ -72,19 +98,19 @@ mod complete { #[test] fn be_i64_tests() { assert_parse!( - be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), + be_i64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)) ); assert_parse!( - be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), + be_i64.parse_peek(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], 9_223_372_036_854_775_807_i64)) ); assert_parse!( - be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), + be_i64.parse_peek(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)) ); assert_parse!( - be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), + be_i64.parse_peek(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], -9_223_372_036_854_775_808_i64)) ); } @@ -92,7 +118,7 @@ mod complete { #[test] fn be_i128_tests() { assert_parse!( - be_i128( + be_i128.parse_peek( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -101,7 +127,7 @@ mod complete { Ok((&b""[..], 0)) ); assert_parse!( - be_i128( + be_i128.parse_peek( &[ 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -113,7 +139,7 @@ mod complete { )) ); assert_parse!( - be_i128( + be_i128.parse_peek( &[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -122,7 +148,7 @@ mod complete { Ok((&b""[..], -1)) ); assert_parse!( - be_i128( + be_i128.parse_peek( &[ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -137,50 +163,74 @@ mod complete { #[test] fn le_i8_tests() { - assert_parse!(le_i8(&[0x00][..]), Ok((&b""[..], 0))); - assert_parse!(le_i8(&[0x7f][..]), Ok((&b""[..], 127))); - assert_parse!(le_i8(&[0xff][..]), Ok((&b""[..], -1))); - assert_parse!(le_i8(&[0x80][..]), Ok((&b""[..], -128))); + assert_parse!(le_i8.parse_peek(&[0x00][..]), Ok((&b""[..], 0))); + assert_parse!(le_i8.parse_peek(&[0x7f][..]), Ok((&b""[..], 127))); + assert_parse!(le_i8.parse_peek(&[0xff][..]), Ok((&b""[..], -1))); + assert_parse!(le_i8.parse_peek(&[0x80][..]), Ok((&b""[..], -128))); } #[test] fn le_i16_tests() { - assert_parse!(le_i16(&[0x00, 0x00][..]), Ok((&b""[..], 0))); - assert_parse!(le_i16(&[0xff, 0x7f][..]), Ok((&b""[..], 32_767_i16))); - assert_parse!(le_i16(&[0xff, 0xff][..]), Ok((&b""[..], -1))); - assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16))); + assert_parse!(le_i16.parse_peek(&[0x00, 0x00][..]), Ok((&b""[..], 0))); + assert_parse!( + le_i16.parse_peek(&[0xff, 0x7f][..]), + Ok((&b""[..], 32_767_i16)) + ); + assert_parse!(le_i16.parse_peek(&[0xff, 0xff][..]), Ok((&b""[..], -1))); + assert_parse!( + le_i16.parse_peek(&[0x00, 0x80][..]), + Ok((&b""[..], -32_768_i16)) + ); } #[test] fn le_u24_tests() { - assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0))); - assert_parse!(le_u24(&[0xFF, 0xFF, 0x00][..]), Ok((&b""[..], 65_535_u32))); assert_parse!( - le_u24(&[0x56, 0x34, 0x12][..]), + le_u24.parse_peek(&[0x00, 0x00, 0x00][..]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_u24.parse_peek(&[0xFF, 0xFF, 0x00][..]), + Ok((&b""[..], 65_535_u32)) + ); + assert_parse!( + le_u24.parse_peek(&[0x56, 0x34, 0x12][..]), Ok((&b""[..], 1_193_046_u32)) ); } #[test] fn le_i24_tests() { - assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF][..]), Ok((&b""[..], -1_i32))); - assert_parse!(le_i24(&[0x00, 0x00, 0xFF][..]), Ok((&b""[..], -65_536_i32))); assert_parse!( - le_i24(&[0xAA, 0xCB, 0xED][..]), + le_i24.parse_peek(&[0xFF, 0xFF, 0xFF][..]), + Ok((&b""[..], -1_i32)) + ); + assert_parse!( + le_i24.parse_peek(&[0x00, 0x00, 0xFF][..]), + Ok((&b""[..], -65_536_i32)) + ); + assert_parse!( + le_i24.parse_peek(&[0xAA, 0xCB, 0xED][..]), Ok((&b""[..], -1_193_046_i32)) ); } #[test] fn le_i32_tests() { - assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0))); assert_parse!( - le_i32(&[0xff, 0xff, 0xff, 0x7f][..]), + le_i32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i32.parse_peek(&[0xff, 0xff, 0xff, 0x7f][..]), Ok((&b""[..], 2_147_483_647_i32)) ); - assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1))); assert_parse!( - le_i32(&[0x00, 0x00, 0x00, 0x80][..]), + le_i32.parse_peek(&[0xff, 0xff, 0xff, 0xff][..]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i32.parse_peek(&[0x00, 0x00, 0x00, 0x80][..]), Ok((&b""[..], -2_147_483_648_i32)) ); } @@ -188,19 +238,19 @@ mod complete { #[test] fn le_i64_tests() { assert_parse!( - le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), + le_i64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)) ); assert_parse!( - le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]), + le_i64.parse_peek(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]), Ok((&b""[..], 9_223_372_036_854_775_807_i64)) ); assert_parse!( - le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), + le_i64.parse_peek(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), Ok((&b""[..], -1)) ); assert_parse!( - le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]), + le_i64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]), Ok((&b""[..], -9_223_372_036_854_775_808_i64)) ); } @@ -208,7 +258,7 @@ mod complete { #[test] fn le_i128_tests() { assert_parse!( - le_i128( + le_i128.parse_peek( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -217,7 +267,7 @@ mod complete { Ok((&b""[..], 0)) ); assert_parse!( - le_i128( + le_i128.parse_peek( &[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f @@ -229,7 +279,7 @@ mod complete { )) ); assert_parse!( - le_i128( + le_i128.parse_peek( &[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -238,7 +288,7 @@ mod complete { Ok((&b""[..], -1)) ); assert_parse!( - le_i128( + le_i128.parse_peek( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 @@ -253,9 +303,12 @@ mod complete { #[test] fn be_f32_tests() { - assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32))); assert_parse!( - be_f32(&[0x4d, 0x31, 0x1f, 0xd8][..]), + be_f32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), + Ok((&b""[..], 0_f32)) + ); + assert_parse!( + be_f32.parse_peek(&[0x4d, 0x31, 0x1f, 0xd8][..]), Ok((&b""[..], 185_728_380_f32)) ); } @@ -263,20 +316,23 @@ mod complete { #[test] fn be_f64_tests() { assert_parse!( - be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), + be_f64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f64)) ); assert_parse!( - be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]), + be_f64.parse_peek(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 185_728_392_f64)) ); } #[test] fn le_f32_tests() { - assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f32))); assert_parse!( - le_f32(&[0xd8, 0x1f, 0x31, 0x4d][..]), + le_f32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), + Ok((&b""[..], 0_f32)) + ); + assert_parse!( + le_f32.parse_peek(&[0xd8, 0x1f, 0x31, 0x4d][..]), Ok((&b""[..], 185_728_380_f32)) ); } @@ -284,11 +340,11 @@ mod complete { #[test] fn le_f64_tests() { assert_parse!( - le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), + le_f64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 0_f64)) ); assert_parse!( - le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]), + le_f64.parse_peek(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]), Ok((&b""[..], 185_728_392_f64)) ); } @@ -298,19 +354,19 @@ mod complete { use crate::binary::Endianness; fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> { - u16(Endianness::Big).parse_next(i) + u16(Endianness::Big).parse_peek(i) } fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> { - u16(Endianness::Little).parse_next(i) + u16(Endianness::Little).parse_peek(i) } assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16))); assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16))); fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> { - u32(Endianness::Big).parse_next(i) + u32(Endianness::Big).parse_peek(i) } fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> { - u32(Endianness::Little).parse_next(i) + u32(Endianness::Little).parse_peek(i) } assert_eq!( be_tst32(&[0x12, 0x00, 0x60, 0x00]), @@ -322,10 +378,10 @@ mod complete { ); fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> { - u64(Endianness::Big).parse_next(i) + u64(Endianness::Big).parse_peek(i) } fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> { - u64(Endianness::Little).parse_next(i) + u64(Endianness::Little).parse_peek(i) } assert_eq!( be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), @@ -337,19 +393,19 @@ mod complete { ); fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> { - i16(Endianness::Big).parse_next(i) + i16(Endianness::Big).parse_peek(i) } fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> { - i16(Endianness::Little).parse_next(i) + i16(Endianness::Little).parse_peek(i) } assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16))); assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> { - i32(Endianness::Big).parse_next(i) + i32(Endianness::Big).parse_peek(i) } fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> { - i32(Endianness::Little).parse_next(i) + i32(Endianness::Little).parse_peek(i) } assert_eq!( be_tsti32(&[0x00, 0x12, 0x60, 0x00]), @@ -361,10 +417,10 @@ mod complete { ); fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> { - i64(Endianness::Big).parse_next(i) + i64(Endianness::Big).parse_peek(i) } fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> { - i64(Endianness::Little).parse_next(i) + i64(Endianness::Little).parse_peek(i) } assert_eq!( be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), @@ -380,7 +436,7 @@ mod complete { mod partial { use super::*; use crate::error::ErrMode; - use crate::error::Error; + use crate::error::InputError; use crate::error::Needed; #[cfg(feature = "alloc")] use crate::lib::std::vec::Vec; @@ -395,7 +451,7 @@ mod partial { macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::IResult<_, _, Error<_>> = $left; + let res: $crate::IResult<_, _, InputError<_>> = $left; assert_eq!(res, $right); }; ); @@ -403,23 +459,23 @@ mod partial { #[test] fn i8_tests() { assert_parse!( - be_i8(Partial::new(&[0x00][..])), + be_i8.parse_peek(Partial::new(&[0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - be_i8(Partial::new(&[0x7f][..])), + be_i8.parse_peek(Partial::new(&[0x7f][..])), Ok((Partial::new(&b""[..]), 127)) ); assert_parse!( - be_i8(Partial::new(&[0xff][..])), + be_i8.parse_peek(Partial::new(&[0xff][..])), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - be_i8(Partial::new(&[0x80][..])), + be_i8.parse_peek(Partial::new(&[0x80][..])), Ok((Partial::new(&b""[..]), -128)) ); assert_parse!( - be_i8(Partial::new(&[][..])), + be_i8.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -427,27 +483,27 @@ mod partial { #[test] fn i16_tests() { assert_parse!( - be_i16(Partial::new(&[0x00, 0x00][..])), + be_i16.parse_peek(Partial::new(&[0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - be_i16(Partial::new(&[0x7f, 0xff][..])), + be_i16.parse_peek(Partial::new(&[0x7f, 0xff][..])), Ok((Partial::new(&b""[..]), 32_767_i16)) ); assert_parse!( - be_i16(Partial::new(&[0xff, 0xff][..])), + be_i16.parse_peek(Partial::new(&[0xff, 0xff][..])), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - be_i16(Partial::new(&[0x80, 0x00][..])), + be_i16.parse_peek(Partial::new(&[0x80, 0x00][..])), Ok((Partial::new(&b""[..]), -32_768_i16)) ); assert_parse!( - be_i16(Partial::new(&[][..])), + be_i16.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(2))) ); assert_parse!( - be_i16(Partial::new(&[0x00][..])), + be_i16.parse_peek(Partial::new(&[0x00][..])), Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -455,27 +511,27 @@ mod partial { #[test] fn u24_tests() { assert_parse!( - be_u24(Partial::new(&[0x00, 0x00, 0x00][..])), + be_u24.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - be_u24(Partial::new(&[0x00, 0xFF, 0xFF][..])), + be_u24.parse_peek(Partial::new(&[0x00, 0xFF, 0xFF][..])), Ok((Partial::new(&b""[..]), 65_535_u32)) ); assert_parse!( - be_u24(Partial::new(&[0x12, 0x34, 0x56][..])), + be_u24.parse_peek(Partial::new(&[0x12, 0x34, 0x56][..])), Ok((Partial::new(&b""[..]), 1_193_046_u32)) ); assert_parse!( - be_u24(Partial::new(&[][..])), + be_u24.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(3))) ); assert_parse!( - be_u24(Partial::new(&[0x00][..])), + be_u24.parse_peek(Partial::new(&[0x00][..])), Err(ErrMode::Incomplete(Needed::new(2))) ); assert_parse!( - be_u24(Partial::new(&[0x00, 0x00][..])), + be_u24.parse_peek(Partial::new(&[0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -483,27 +539,27 @@ mod partial { #[test] fn i24_tests() { assert_parse!( - be_i24(Partial::new(&[0xFF, 0xFF, 0xFF][..])), + be_i24.parse_peek(Partial::new(&[0xFF, 0xFF, 0xFF][..])), Ok((Partial::new(&b""[..]), -1_i32)) ); assert_parse!( - be_i24(Partial::new(&[0xFF, 0x00, 0x00][..])), + be_i24.parse_peek(Partial::new(&[0xFF, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), -65_536_i32)) ); assert_parse!( - be_i24(Partial::new(&[0xED, 0xCB, 0xAA][..])), + be_i24.parse_peek(Partial::new(&[0xED, 0xCB, 0xAA][..])), Ok((Partial::new(&b""[..]), -1_193_046_i32)) ); assert_parse!( - be_i24(Partial::new(&[][..])), + be_i24.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(3))) ); assert_parse!( - be_i24(Partial::new(&[0x00][..])), + be_i24.parse_peek(Partial::new(&[0x00][..])), Err(ErrMode::Incomplete(Needed::new(2))) ); assert_parse!( - be_i24(Partial::new(&[0x00, 0x00][..])), + be_i24.parse_peek(Partial::new(&[0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -511,35 +567,35 @@ mod partial { #[test] fn i32_tests() { assert_parse!( - be_i32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), + be_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - be_i32(Partial::new(&[0x7f, 0xff, 0xff, 0xff][..])), + be_i32.parse_peek(Partial::new(&[0x7f, 0xff, 0xff, 0xff][..])), Ok((Partial::new(&b""[..]), 2_147_483_647_i32)) ); assert_parse!( - be_i32(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), + be_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - be_i32(Partial::new(&[0x80, 0x00, 0x00, 0x00][..])), + be_i32.parse_peek(Partial::new(&[0x80, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), -2_147_483_648_i32)) ); assert_parse!( - be_i32(Partial::new(&[][..])), + be_i32.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(4))) ); assert_parse!( - be_i32(Partial::new(&[0x00][..])), + be_i32.parse_peek(Partial::new(&[0x00][..])), Err(ErrMode::Incomplete(Needed::new(3))) ); assert_parse!( - be_i32(Partial::new(&[0x00, 0x00][..])), + be_i32.parse_peek(Partial::new(&[0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(2))) ); assert_parse!( - be_i32(Partial::new(&[0x00, 0x00, 0x00][..])), + be_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(1))) ); } @@ -547,59 +603,59 @@ mod partial { #[test] fn i64_tests() { assert_parse!( - be_i64(Partial::new( + be_i64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - be_i64(Partial::new( + be_i64.parse_peek(Partial::new( &[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] )), Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64)) ); assert_parse!( - be_i64(Partial::new( + be_i64.parse_peek(Partial::new( &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] )), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - be_i64(Partial::new( + be_i64.parse_peek(Partial::new( &[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64)) ); assert_parse!( - be_i64(Partial::new(&[][..])), + be_i64.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(8))) ); assert_parse!( - be_i64(Partial::new(&[0x00][..])), + be_i64.parse_peek(Partial::new(&[0x00][..])), Err(ErrMode::Incomplete(Needed::new(7))) ); assert_parse!( - be_i64(Partial::new(&[0x00, 0x00][..])), + be_i64.parse_peek(Partial::new(&[0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(6))) ); assert_parse!( - be_i64(Partial::new(&[0x00, 0x00, 0x00][..])), + be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(5))) ); assert_parse!( - be_i64(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), + be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(4))) ); assert_parse!( - be_i64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])), + be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(3))) ); assert_parse!( - be_i64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), + be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(2))) ); assert_parse!( - be_i64(Partial::new( + be_i64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(1))) @@ -609,7 +665,7 @@ mod partial { #[test] fn i128_tests() { assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -618,7 +674,7 @@ mod partial { Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[ 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -630,7 +686,7 @@ mod partial { )) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -639,7 +695,7 @@ mod partial { Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -651,77 +707,77 @@ mod partial { )) ); assert_parse!( - be_i128(Partial::new(&[][..])), + be_i128.parse_peek(Partial::new(&[][..])), Err(ErrMode::Incomplete(Needed::new(16))) ); assert_parse!( - be_i128(Partial::new(&[0x00][..])), + be_i128.parse_peek(Partial::new(&[0x00][..])), Err(ErrMode::Incomplete(Needed::new(15))) ); assert_parse!( - be_i128(Partial::new(&[0x00, 0x00][..])), + be_i128.parse_peek(Partial::new(&[0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(14))) ); assert_parse!( - be_i128(Partial::new(&[0x00, 0x00, 0x00][..])), + be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(13))) ); assert_parse!( - be_i128(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), + be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(12))) ); assert_parse!( - be_i128(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])), + be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(11))) ); assert_parse!( - be_i128(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), + be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Err(ErrMode::Incomplete(Needed::new(10))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(9))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(8))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(7))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(6))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(5))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(4))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Err(ErrMode::Incomplete(Needed::new(3))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -730,7 +786,7 @@ mod partial { Err(ErrMode::Incomplete(Needed::new(2))) ); assert_parse!( - be_i128(Partial::new( + be_i128.parse_peek(Partial::new( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -743,19 +799,19 @@ mod partial { #[test] fn le_i8_tests() { assert_parse!( - le_i8(Partial::new(&[0x00][..])), + le_i8.parse_peek(Partial::new(&[0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - le_i8(Partial::new(&[0x7f][..])), + le_i8.parse_peek(Partial::new(&[0x7f][..])), Ok((Partial::new(&b""[..]), 127)) ); assert_parse!( - le_i8(Partial::new(&[0xff][..])), + le_i8.parse_peek(Partial::new(&[0xff][..])), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - le_i8(Partial::new(&[0x80][..])), + le_i8.parse_peek(Partial::new(&[0x80][..])), Ok((Partial::new(&b""[..]), -128)) ); } @@ -763,19 +819,19 @@ mod partial { #[test] fn le_i16_tests() { assert_parse!( - le_i16(Partial::new(&[0x00, 0x00][..])), + le_i16.parse_peek(Partial::new(&[0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - le_i16(Partial::new(&[0xff, 0x7f][..])), + le_i16.parse_peek(Partial::new(&[0xff, 0x7f][..])), Ok((Partial::new(&b""[..]), 32_767_i16)) ); assert_parse!( - le_i16(Partial::new(&[0xff, 0xff][..])), + le_i16.parse_peek(Partial::new(&[0xff, 0xff][..])), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - le_i16(Partial::new(&[0x00, 0x80][..])), + le_i16.parse_peek(Partial::new(&[0x00, 0x80][..])), Ok((Partial::new(&b""[..]), -32_768_i16)) ); } @@ -783,15 +839,15 @@ mod partial { #[test] fn le_u24_tests() { assert_parse!( - le_u24(Partial::new(&[0x00, 0x00, 0x00][..])), + le_u24.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - le_u24(Partial::new(&[0xFF, 0xFF, 0x00][..])), + le_u24.parse_peek(Partial::new(&[0xFF, 0xFF, 0x00][..])), Ok((Partial::new(&b""[..]), 65_535_u32)) ); assert_parse!( - le_u24(Partial::new(&[0x56, 0x34, 0x12][..])), + le_u24.parse_peek(Partial::new(&[0x56, 0x34, 0x12][..])), Ok((Partial::new(&b""[..]), 1_193_046_u32)) ); } @@ -799,15 +855,15 @@ mod partial { #[test] fn le_i24_tests() { assert_parse!( - le_i24(Partial::new(&[0xFF, 0xFF, 0xFF][..])), + le_i24.parse_peek(Partial::new(&[0xFF, 0xFF, 0xFF][..])), Ok((Partial::new(&b""[..]), -1_i32)) ); assert_parse!( - le_i24(Partial::new(&[0x00, 0x00, 0xFF][..])), + le_i24.parse_peek(Partial::new(&[0x00, 0x00, 0xFF][..])), Ok((Partial::new(&b""[..]), -65_536_i32)) ); assert_parse!( - le_i24(Partial::new(&[0xAA, 0xCB, 0xED][..])), + le_i24.parse_peek(Partial::new(&[0xAA, 0xCB, 0xED][..])), Ok((Partial::new(&b""[..]), -1_193_046_i32)) ); } @@ -815,19 +871,19 @@ mod partial { #[test] fn le_i32_tests() { assert_parse!( - le_i32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), + le_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - le_i32(Partial::new(&[0xff, 0xff, 0xff, 0x7f][..])), + le_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0x7f][..])), Ok((Partial::new(&b""[..]), 2_147_483_647_i32)) ); assert_parse!( - le_i32(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), + le_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - le_i32(Partial::new(&[0x00, 0x00, 0x00, 0x80][..])), + le_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x80][..])), Ok((Partial::new(&b""[..]), -2_147_483_648_i32)) ); } @@ -835,25 +891,25 @@ mod partial { #[test] fn le_i64_tests() { assert_parse!( - le_i64(Partial::new( + le_i64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - le_i64(Partial::new( + le_i64.parse_peek(Partial::new( &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..] )), Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64)) ); assert_parse!( - le_i64(Partial::new( + le_i64.parse_peek(Partial::new( &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] )), Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - le_i64(Partial::new( + le_i64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..] )), Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64)) @@ -863,7 +919,7 @@ mod partial { #[test] fn le_i128_tests() { assert_parse!( - le_i128(Partial::new( + le_i128.parse_peek(Partial::new( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -872,7 +928,7 @@ mod partial { Ok((Partial::new(&b""[..]), 0)) ); assert_parse!( - le_i128(Partial::new( + le_i128.parse_peek(Partial::new( &[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f @@ -884,7 +940,7 @@ mod partial { )) ); assert_parse!( - le_i128(Partial::new( + le_i128.parse_peek(Partial::new( &[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -893,7 +949,7 @@ mod partial { Ok((Partial::new(&b""[..]), -1)) ); assert_parse!( - le_i128(Partial::new( + le_i128.parse_peek(Partial::new( &[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 @@ -909,11 +965,11 @@ mod partial { #[test] fn be_f32_tests() { assert_parse!( - be_f32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), + be_f32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0_f32)) ); assert_parse!( - be_f32(Partial::new(&[0x4d, 0x31, 0x1f, 0xd8][..])), + be_f32.parse_peek(Partial::new(&[0x4d, 0x31, 0x1f, 0xd8][..])), Ok((Partial::new(&b""[..]), 185_728_380_f32)) ); } @@ -921,13 +977,13 @@ mod partial { #[test] fn be_f64_tests() { assert_parse!( - be_f64(Partial::new( + be_f64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Ok((Partial::new(&b""[..]), 0_f64)) ); assert_parse!( - be_f64(Partial::new( + be_f64.parse_peek(Partial::new( &[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..] )), Ok((Partial::new(&b""[..]), 185_728_392_f64)) @@ -937,11 +993,11 @@ mod partial { #[test] fn le_f32_tests() { assert_parse!( - le_f32(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), + le_f32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 0_f32)) ); assert_parse!( - le_f32(Partial::new(&[0xd8, 0x1f, 0x31, 0x4d][..])), + le_f32.parse_peek(Partial::new(&[0xd8, 0x1f, 0x31, 0x4d][..])), Ok((Partial::new(&b""[..]), 185_728_380_f32)) ); } @@ -949,13 +1005,13 @@ mod partial { #[test] fn le_f64_tests() { assert_parse!( - le_f64(Partial::new( + le_f64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] )), Ok((Partial::new(&b""[..]), 0_f64)) ); assert_parse!( - le_f64(Partial::new( + le_f64.parse_peek(Partial::new( &[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..] )), Ok((Partial::new(&b""[..]), 185_728_392_f64)) @@ -967,10 +1023,10 @@ mod partial { use crate::binary::Endianness; fn be_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { - u16(Endianness::Big).parse_next(i) + u16(Endianness::Big).parse_peek(i) } fn le_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { - u16(Endianness::Little).parse_next(i) + u16(Endianness::Little).parse_peek(i) } assert_eq!( be_tst16(Partial::new(&[0x80, 0x00])), @@ -982,10 +1038,10 @@ mod partial { ); fn be_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { - u32(Endianness::Big).parse_next(i) + u32(Endianness::Big).parse_peek(i) } fn le_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { - u32(Endianness::Little).parse_next(i) + u32(Endianness::Little).parse_peek(i) } assert_eq!( be_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])), @@ -997,10 +1053,10 @@ mod partial { ); fn be_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { - u64(Endianness::Big).parse_next(i) + u64(Endianness::Big).parse_peek(i) } fn le_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { - u64(Endianness::Little).parse_next(i) + u64(Endianness::Little).parse_peek(i) } assert_eq!( be_tst64(Partial::new(&[ @@ -1016,10 +1072,10 @@ mod partial { ); fn be_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { - i16(Endianness::Big).parse_next(i) + i16(Endianness::Big).parse_peek(i) } fn le_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { - i16(Endianness::Little).parse_next(i) + i16(Endianness::Little).parse_peek(i) } assert_eq!( be_tsti16(Partial::new(&[0x00, 0x80])), @@ -1031,10 +1087,10 @@ mod partial { ); fn be_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { - i32(Endianness::Big).parse_next(i) + i32(Endianness::Big).parse_peek(i) } fn le_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { - i32(Endianness::Little).parse_next(i) + i32(Endianness::Little).parse_peek(i) } assert_eq!( be_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])), @@ -1046,10 +1102,10 @@ mod partial { ); fn be_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { - i64(Endianness::Big).parse_next(i) + i64(Endianness::Big).parse_peek(i) } fn le_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { - i64(Endianness::Little).parse_next(i) + i64(Endianness::Little).parse_peek(i) } assert_eq!( be_tsti64(Partial::new(&[ @@ -1072,11 +1128,11 @@ mod partial { digit .try_map(str::from_utf8) .try_map(FromStr::from_str) - .parse_next(i) + .parse_peek(i) } fn cnt(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - length_count(number, "abc").parse_next(i) + length_count(unpeek(number), "abc").parse_peek(i) } assert_eq!( @@ -1094,14 +1150,14 @@ mod partial { assert_eq!( cnt(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Slice ))) ); assert_eq!( cnt(Partial::new(&b"2abcxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -1113,11 +1169,11 @@ mod partial { digit .try_map(str::from_utf8) .try_map(FromStr::from_str) - .parse_next(i) + .parse_peek(i) } fn take(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - length_data(number).parse_next(i) + length_data(unpeek(number)).parse_peek(i) } assert_eq!( @@ -1131,7 +1187,7 @@ mod partial { assert_eq!( take(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Slice ))) ); @@ -1146,10 +1202,10 @@ mod partial { use crate::stream::StreamIsPartial; fn length_value_1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { - length_value(be_u8, be_u16).parse_next(i) + length_value(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_next(i) + length_value(be_u8, (be_u8, be_u8)).parse_peek(i) } let mut empty_complete = Partial::new(&b""[..]); @@ -1159,14 +1215,14 @@ mod partial { assert_eq!( length_value_1(Partial::new(&i1)), Err(ErrMode::Backtrack(error_position!( - empty_complete, + &empty_complete, ErrorKind::Slice ))) ); assert_eq!( length_value_2(Partial::new(&i1)), Err(ErrMode::Backtrack(error_position!( - empty_complete, + &empty_complete, ErrorKind::Token ))) ); @@ -1178,14 +1234,14 @@ mod partial { assert_eq!( length_value_1(Partial::new(&i2)), Err(ErrMode::Backtrack(error_position!( - middle_complete, + &middle_complete, ErrorKind::Slice ))) ); assert_eq!( length_value_2(Partial::new(&i2)), Err(ErrMode::Backtrack(error_position!( - empty_complete, + &empty_complete, ErrorKind::Token ))) ); diff --git a/vendor/winnow/src/bits.rs b/vendor/winnow/src/bits.rs deleted file mode 100644 index 6b4981f30..000000000 --- a/vendor/winnow/src/bits.rs +++ /dev/null @@ -1,71 +0,0 @@ -//! Deprecated, see [`binary::bits`] -#![deprecated(since = "0.4.2", note = "Replaced with `binary::bits`")] - -use crate::binary; -use crate::error::{ErrorConvert, ParseError}; -use crate::lib::std::ops::{AddAssign, Shl, Shr}; -use crate::stream::{AsBytes, Stream, StreamIsPartial, ToUsize}; -use crate::{IResult, Parser}; - -/// Deprecated, replaced with [`binary::bits::bits`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::bits::bits`")] -#[inline(always)] -pub fn bits<I, O, E1, E2, P>(parser: P) -> impl Parser<I, O, E2> -where - E1: ParseError<(I, usize)> + ErrorConvert<E2>, - E2: ParseError<I>, - I: Stream, - P: Parser<(I, usize), O, E1>, -{ - binary::bits::bits(parser) -} - -/// Deprecated, replaced with [`binary::bits::bytes`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::bits::bytes`")] -#[inline(always)] -pub fn bytes<I, O, E1, E2, P>(parser: P) -> impl Parser<(I, usize), O, E2> -where - E1: ParseError<I> + ErrorConvert<E2>, - E2: ParseError<(I, usize)>, - I: Stream<Token = u8>, - P: Parser<I, O, E1>, -{ - binary::bits::bytes(parser) -} - -/// Deprecated, replaced with [`binary::bits::take`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::bits::take`")] -#[inline(always)] -pub fn take<I, O, C, E: ParseError<(I, usize)>>(count: C) -> impl Parser<(I, usize), O, E> -where - I: Stream<Token = u8> + AsBytes + StreamIsPartial, - C: ToUsize, - O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>, -{ - binary::bits::take(count) -} - -/// Deprecated, replaced with [`binary::bits::tag`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::bits::tag`")] -#[inline(always)] -pub fn tag<I, O, C, E: ParseError<(I, usize)>>( - pattern: O, - count: C, -) -> impl Parser<(I, usize), O, E> -where - I: Stream<Token = u8> + AsBytes + StreamIsPartial, - C: ToUsize, - O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq, -{ - binary::bits::tag(pattern, count) -} - -/// Deprecated, replaced with [`binary::bits::bool`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::bits::bool`")] -#[inline(always)] -pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E> -where - I: Stream<Token = u8> + AsBytes + StreamIsPartial, -{ - binary::bits::bool(input) -} diff --git a/vendor/winnow/src/branch.rs b/vendor/winnow/src/branch.rs deleted file mode 100644 index 0783262b4..000000000 --- a/vendor/winnow/src/branch.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! Deprecated, see [`combinator`] -#![deprecated(since = "0.4.2", note = "Replaced with `combinator`")] - -use crate::combinator; - -pub use combinator::alt; -pub use combinator::dispatch; -pub use combinator::permutation; -pub use combinator::Alt; -pub use combinator::Permutation; diff --git a/vendor/winnow/src/bytes.rs b/vendor/winnow/src/bytes.rs deleted file mode 100644 index 9f57a56d2..000000000 --- a/vendor/winnow/src/bytes.rs +++ /dev/null @@ -1,26 +0,0 @@ -//! Deprecated, see [`token`] -#![deprecated(since = "0.4.2", note = "Replaced with `token`")] - -use crate::error::ParseError; -use crate::stream::StreamIsPartial; -use crate::stream::{ContainsToken, Stream}; -use crate::token; -use crate::Parser; - -pub use crate::token::*; - -/// Deprecated, see [`token::take_while`] -#[deprecated(since = "0.4.2", note = "Replaced with `token::take_while`")] -#[inline(always)] -pub fn take_while_m_n<T, I, Error: ParseError<I>>( - m: usize, - n: usize, - list: T, -) -> impl Parser<I, <I as Stream>::Slice, Error> -where - I: StreamIsPartial, - I: Stream, - T: ContainsToken<<I as Stream>::Token>, -{ - token::take_while(m..=n, list) -} diff --git a/vendor/winnow/src/character.rs b/vendor/winnow/src/character.rs deleted file mode 100644 index a2f685971..000000000 --- a/vendor/winnow/src/character.rs +++ /dev/null @@ -1,342 +0,0 @@ -//! Deprecated, see [`ascii`] -#![deprecated(since = "0.4.2", note = "Replaced with `ascii`")] - -use crate::ascii; -use crate::error::ParseError; -use crate::stream::Compare; -use crate::stream::ContainsToken; -use crate::stream::{AsBStr, AsChar, Offset, ParseSlice, Stream, StreamIsPartial}; -use crate::IResult; -use crate::Parser; - -/// Deprecated, replaced by [`ascii::crlf`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::crlf`")] -#[inline(always)] -pub fn crlf<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - I: Compare<&'static str>, -{ - ascii::crlf.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::not_line_ending`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::not_line_ending`")] -#[inline(always)] -pub fn not_line_ending<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream + AsBStr, - I: Compare<&'static str>, - <I as Stream>::Token: AsChar, -{ - ascii::not_line_ending.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::line_ending`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::line_ending`")] -#[inline(always)] -pub fn line_ending<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - I: Compare<&'static str>, -{ - ascii::line_ending.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::newline`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::newline`")] -#[inline(always)] -pub fn newline<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, -{ - ascii::newline.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::tab`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::tab`")] -#[inline(always)] -pub fn tab<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, -{ - ascii::tab.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::alpha0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::alpha0`")] -#[inline(always)] -pub fn alpha0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::alpha0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::alpha1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::alpha1`")] -#[inline(always)] -pub fn alpha1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::alpha1.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::digit0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::digit0`")] -#[inline(always)] -pub fn digit0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::digit0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::digit1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::digit1`")] -#[inline(always)] -pub fn digit1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::digit1.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::hex_digit0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::hex_digit0`")] -#[inline(always)] -pub fn hex_digit0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::hex_digit0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::hex_digit1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::hex_digit1`")] -#[inline(always)] -pub fn hex_digit1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::hex_digit1.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::oct_digit0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::oct_digit0`")] -#[inline(always)] -pub fn oct_digit0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::oct_digit0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::oct_digit1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::oct_digit1`")] -#[inline(always)] -pub fn oct_digit1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::oct_digit0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::alphanumeric0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::alphanumeric0`")] -#[inline(always)] -pub fn alphanumeric0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::alphanumeric0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::alphanumeric1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::alphanumeric1`")] -#[inline(always)] -pub fn alphanumeric1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar, -{ - ascii::alphanumeric1.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::space0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::space0`")] -#[inline(always)] -pub fn space0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, -{ - ascii::space0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::space1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::space1`")] -#[inline(always)] -pub fn space1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, -{ - ascii::space1.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::multispace0`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::multispace0`")] -#[inline(always)] -pub fn multispace0<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, -{ - ascii::multispace0.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::multispace1`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::multispace1`")] -#[inline(always)] -pub fn multispace1<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, -{ - ascii::multispace1.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::dec_uint`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::dec_uint`")] -#[inline(always)] -pub fn dec_uint<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, - O: Uint, -{ - ascii::dec_uint.parse_next(input) -} - -pub use ascii::Uint; - -/// Deprecated, replaced by [`ascii::dec_int`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::dec_int`")] -#[inline(always)] -pub fn dec_int<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> -where - I: StreamIsPartial, - I: Stream, - <I as Stream>::Token: AsChar + Copy, - O: Int, -{ - ascii::dec_int.parse_next(input) -} - -pub use ascii::Int; - -/// Deprecated, replaced by [`ascii::hex_uint`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::hex_uint`")] -#[inline(always)] -pub fn hex_uint<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> -where - I: StreamIsPartial, - I: Stream, - O: HexUint, - <I as Stream>::Token: AsChar, - <I as Stream>::Slice: AsBStr, -{ - ascii::hex_uint.parse_next(input) -} - -pub use ascii::HexUint; - -/// Deprecated, replaced by [`ascii::float`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::float`")] -#[inline(always)] -pub fn float<I, O, E: ParseError<I>>(input: I) -> IResult<I, O, E> -where - I: StreamIsPartial, - I: Stream, - I: Offset + Compare<&'static str>, - <I as Stream>::Slice: ParseSlice<O>, - <I as Stream>::Token: AsChar + Copy, - <I as Stream>::IterOffsets: Clone, - I: AsBStr, - &'static str: ContainsToken<<I as Stream>::Token>, -{ - ascii::float.parse_next(input) -} - -/// Deprecated, replaced by [`ascii::escaped`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::escaped`")] -#[inline(always)] -pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>( - normal: F, - control_char: char, - escapable: G, -) -> impl Parser<I, <I as Stream>::Slice, Error> -where - I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, - F: Parser<I, O1, Error>, - G: Parser<I, O2, Error>, - Error: ParseError<I>, -{ - ascii::escaped(normal, control_char, escapable) -} - -#[cfg(feature = "alloc")] -/// Deprecated, replaced by [`ascii::escaped_transform`] -#[deprecated(since = "0.4.2", note = "Replaced with `ascii::escaped_transform`")] -#[inline(always)] -pub fn escaped_transform<I, Error, F, G, Output>( - normal: F, - control_char: char, - transform: G, -) -> impl Parser<I, Output, Error> -where - I: StreamIsPartial, - I: Stream + Offset, - <I as Stream>::Token: crate::stream::AsChar, - Output: crate::stream::Accumulate<<I as Stream>::Slice>, - F: Parser<I, <I as Stream>::Slice, Error>, - G: Parser<I, <I as Stream>::Slice, Error>, - Error: ParseError<I>, -{ - ascii::escaped_transform(normal, control_char, transform) -} diff --git a/vendor/winnow/src/combinator/branch.rs b/vendor/winnow/src/combinator/branch.rs index 042b27d53..2a712ec1d 100644 --- a/vendor/winnow/src/combinator/branch.rs +++ b/vendor/winnow/src/combinator/branch.rs @@ -1,4 +1,4 @@ -use crate::error::{ErrMode, ErrorKind, ParseError}; +use crate::error::{ErrMode, ErrorKind, ParserError}; use crate::stream::Stream; use crate::trace::trace; use crate::*; @@ -11,7 +11,7 @@ pub use crate::dispatch; /// This trait is implemented for tuples of up to 21 elements pub trait Alt<I, O, E> { /// Tests each parser in the tuple and returns the result of the first one that succeeds - fn choice(&mut self, input: I) -> IResult<I, O, E>; + fn choice(&mut self, input: &mut I) -> PResult<O, E>; } /// Pick the first successful parser @@ -25,13 +25,13 @@ pub trait Alt<I, O, E> { /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::Error,error::ErrorKind, error::Needed}; +/// # use winnow::{error::ErrMode, error::InputError,error::ErrorKind, error::Needed}; /// # use winnow::prelude::*; /// use winnow::ascii::{alpha1, digit1}; /// use winnow::combinator::alt; /// # fn main() { /// fn parser(input: &str) -> IResult<&str, &str> { -/// alt((alpha1, digit1)).parse_next(input) +/// alt((alpha1, digit1)).parse_peek(input) /// }; /// /// // the first parser, alpha1, recognizes the input @@ -41,14 +41,14 @@ pub trait Alt<I, O, E> { /// assert_eq!(parser("123456"), Ok(("", "123456"))); /// /// // both parsers failed, and with the default error type, alt will return the last error -/// assert_eq!(parser(" "), Err(ErrMode::Backtrack(Error::new(" ", ErrorKind::Slice)))); +/// assert_eq!(parser(" "), Err(ErrMode::Backtrack(InputError::new(" ", ErrorKind::Slice)))); /// # } /// ``` #[doc(alias = "choice")] -pub fn alt<I: Stream, O, E: ParseError<I>, List: Alt<I, O, E>>( +pub fn alt<I: Stream, O, E: ParserError<I>, List: Alt<I, O, E>>( mut l: List, ) -> impl Parser<I, O, E> { - trace("alt", move |i: I| l.choice(i)) + trace("alt", move |i: &mut I| l.choice(i)) } /// Helper trait for the [permutation()] combinator. @@ -56,7 +56,7 @@ pub fn alt<I: Stream, O, E: ParseError<I>, List: Alt<I, O, E>>( /// This trait is implemented for tuples of up to 21 elements pub trait Permutation<I, O, E> { /// Tries to apply all parsers in the tuple in various orders until all of them succeed - fn permutation(&mut self, input: I) -> IResult<I, O, E>; + fn permutation(&mut self, input: &mut I) -> PResult<O, E>; } /// Applies a list of parsers in any order. @@ -66,13 +66,13 @@ pub trait Permutation<I, O, E> { /// tuple of the parser results. /// /// ```rust -/// # use winnow::{error::ErrMode,error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode,error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::ascii::{alpha1, digit1}; /// use winnow::combinator::permutation; /// # fn main() { /// fn parser(input: &str) -> IResult<&str, (&str, &str)> { -/// permutation((alpha1, digit1)).parse_next(input) +/// permutation((alpha1, digit1)).parse_peek(input) /// } /// /// // permutation recognizes alphabetic characters then digit @@ -82,20 +82,20 @@ pub trait Permutation<I, O, E> { /// assert_eq!(parser("123abc"), Ok(("", ("abc", "123")))); /// /// // it will fail if one of the parsers failed -/// assert_eq!(parser("abc;"), Err(ErrMode::Backtrack(Error::new(";", ErrorKind::Slice)))); +/// assert_eq!(parser("abc;"), Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Slice)))); /// # } /// ``` /// /// The parsers are applied greedily: if there are multiple unapplied parsers /// that could parse the next slice of input, the first one is used. /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}}; /// # use winnow::prelude::*; /// use winnow::combinator::permutation; /// use winnow::token::any; /// /// fn parser(input: &str) -> IResult<&str, (char, char)> { -/// permutation((any, 'a')).parse_next(input) +/// permutation((any, 'a')).parse_peek(input) /// } /// /// // any parses 'b', then char('a') parses 'a' @@ -103,13 +103,13 @@ pub trait Permutation<I, O, E> { /// /// // any parses 'a', then char('a') fails on 'b', /// // even though char('a') followed by any would succeed -/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(Error::new("b", ErrorKind::Verify)))); +/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(InputError::new("b", ErrorKind::Verify)))); /// ``` /// -pub fn permutation<I: Stream, O, E: ParseError<I>, List: Permutation<I, O, E>>( +pub fn permutation<I: Stream, O, E: ParserError<I>, List: Permutation<I, O, E>>( mut l: List, ) -> impl Parser<I, O, E> { - trace("permutation", move |i: I| l.permutation(i)) + trace("permutation", move |i: &mut I| l.permutation(i)) } macro_rules! alt_trait( @@ -130,13 +130,14 @@ macro_rules! alt_trait( macro_rules! alt_trait_impl( ($($id:ident)+) => ( impl< - I: Clone, Output, Error: ParseError<I>, + I: Stream, Output, Error: ParserError<I>, $($id: Parser<I, Output, Error>),+ > Alt<I, Output, Error> for ( $($id),+ ) { - fn choice(&mut self, input: I) -> IResult<I, Output, Error> { - match self.0.parse_next(input.clone()) { - Err(ErrMode::Backtrack(e)) => alt_trait_inner!(1, self, input, e, $($id)+), + fn choice(&mut self, input: &mut I) -> PResult<Output, Error> { + let start = input.checkpoint(); + match self.0.parse_next(input) { + Err(ErrMode::Backtrack(e)) => alt_trait_inner!(1, self, input, start, e, $($id)+), res => res, } } @@ -145,25 +146,26 @@ macro_rules! alt_trait_impl( ); macro_rules! alt_trait_inner( - ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident $($id:ident)+) => ( - match $self.$it.parse_next($input.clone()) { + ($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident $($id:ident)+) => ({ + $input.reset($start.clone()); + match $self.$it.parse_next($input) { Err(ErrMode::Backtrack(e)) => { let err = $err.or(e); - succ!($it, alt_trait_inner!($self, $input, err, $($id)+)) + succ!($it, alt_trait_inner!($self, $input, $start, err, $($id)+)) } res => res, } - ); - ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => ( + }); + ($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident) => ({ Err(ErrMode::Backtrack($err.append($input, ErrorKind::Alt))) - ); + }); ); alt_trait!(Alt2 Alt3 Alt4 Alt5 Alt6 Alt7 Alt8 Alt9 Alt10 Alt11 Alt12 Alt13 Alt14 Alt15 Alt16 Alt17 Alt18 Alt19 Alt20 Alt21 Alt22); // Manually implement Alt for (A,), the 1-tuple type -impl<I, O, E: ParseError<I>, A: Parser<I, O, E>> Alt<I, O, E> for (A,) { - fn choice(&mut self, input: I) -> IResult<I, O, E> { +impl<I, O, E: ParserError<I>, A: Parser<I, O, E>> Alt<I, O, E> for (A,) { + fn choice(&mut self, input: &mut I) -> PResult<O, E> { self.0.parse_next(input) } } @@ -191,27 +193,29 @@ macro_rules! permutation_trait( macro_rules! permutation_trait_impl( ($($name:ident $ty:ident $item:ident),+) => ( impl< - I: Clone, $($ty),+ , Error: ParseError<I>, + I: Stream, $($ty),+ , Error: ParserError<I>, $($name: Parser<I, $ty, Error>),+ > Permutation<I, ( $($ty),+ ), Error> for ( $($name),+ ) { - fn permutation(&mut self, mut input: I) -> IResult<I, ( $($ty),+ ), Error> { + fn permutation(&mut self, input: &mut I) -> PResult<( $($ty),+ ), Error> { let mut res = ($(Option::<$ty>::None),+); loop { let mut err: Option<Error> = None; - permutation_trait_inner!(0, self, input, res, err, $($name)+); + let start = input.checkpoint(); + permutation_trait_inner!(0, self, input, start, res, err, $($name)+); // If we reach here, every iterator has either been applied before, // or errored on the remaining input if let Some(err) = err { // There are remaining parsers, and all errored on the remaining input + input.reset(start.clone()); return Err(ErrMode::Backtrack(err.append(input, ErrorKind::Alt))); } // All parsers were applied match res { - ($(Some($item)),+) => return Ok((input, ($($item),+))), + ($(Some($item)),+) => return Ok(($($item),+)), _ => unreachable!(), } } @@ -221,11 +225,11 @@ macro_rules! permutation_trait_impl( ); macro_rules! permutation_trait_inner( - ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr, $head:ident $($id:ident)*) => ( + ($it:tt, $self:expr, $input:ident, $start:ident, $res:expr, $err:expr, $head:ident $($id:ident)*) => ( if $res.$it.is_none() { - match $self.$it.parse_next($input.clone()) { - Ok((i, o)) => { - $input = i; + $input.reset($start.clone()); + match $self.$it.parse_next($input) { + Ok(o) => { $res.$it = Some(o); continue; } @@ -238,9 +242,9 @@ macro_rules! permutation_trait_inner( Err(e) => return Err(e), }; } - succ!($it, permutation_trait_inner!($self, $input, $res, $err, $($id)*)); + succ!($it, permutation_trait_inner!($self, $input, $start, $res, $err, $($id)*)); ); - ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr,) => (); + ($it:tt, $self:expr, $input:ident, $start:ident, $res:expr, $err:expr,) => (); ); permutation_trait!( diff --git a/vendor/winnow/src/combinator/core.rs b/vendor/winnow/src/combinator/core.rs index 75551d7c5..d784b4e9e 100644 --- a/vendor/winnow/src/combinator/core.rs +++ b/vendor/winnow/src/combinator/core.rs @@ -1,4 +1,4 @@ -use crate::error::{ErrMode, ErrorKind, Needed, ParseError}; +use crate::error::{ErrMode, ErrorKind, Needed, ParserError}; use crate::stream::Stream; use crate::trace::trace; use crate::*; @@ -8,21 +8,19 @@ use crate::*; /// # Example /// /// ```rust +/// # use winnow::prelude::*; /// # use winnow::error::ErrorKind; -/// # use winnow::error::Error; +/// # use winnow::error::InputError; /// use winnow::combinator::rest; -/// assert_eq!(rest::<_,Error<_>>("abc"), Ok(("", "abc"))); -/// assert_eq!(rest::<_,Error<_>>(""), Ok(("", ""))); +/// assert_eq!(rest::<_,InputError<_>>.parse_peek("abc"), Ok(("", "abc"))); +/// assert_eq!(rest::<_,InputError<_>>.parse_peek(""), Ok(("", ""))); /// ``` #[inline] -pub fn rest<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn rest<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: Stream, { - trace("rest", move |input: I| { - Ok(input.next_slice(input.eof_offset())) - }) - .parse_next(input) + trace("rest", move |input: &mut I| Ok(input.finish())).parse_next(input) } /// Return the length of the remaining input. @@ -32,20 +30,21 @@ where /// # Example /// /// ```rust +/// # use winnow::prelude::*; /// # use winnow::error::ErrorKind; -/// # use winnow::error::Error; +/// # use winnow::error::InputError; /// use winnow::combinator::rest_len; -/// assert_eq!(rest_len::<_,Error<_>>("abc"), Ok(("abc", 3))); -/// assert_eq!(rest_len::<_,Error<_>>(""), Ok(("", 0))); +/// assert_eq!(rest_len::<_,InputError<_>>.parse_peek("abc"), Ok(("abc", 3))); +/// assert_eq!(rest_len::<_,InputError<_>>.parse_peek(""), Ok(("", 0))); /// ``` #[inline] -pub fn rest_len<I, E: ParseError<I>>(input: I) -> IResult<I, usize, E> +pub fn rest_len<I, E: ParserError<I>>(input: &mut I) -> PResult<usize, E> where I: Stream, { - trace("rest_len", move |input: I| { + trace("rest_len", move |input: &mut I| { let len = input.eof_offset(); - Ok((input, len)) + Ok(len) }) .parse_next(input) } @@ -57,29 +56,32 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// use winnow::combinator::opt; /// use winnow::ascii::alpha1; /// # fn main() { /// /// fn parser(i: &str) -> IResult<&str, Option<&str>> { -/// opt(alpha1).parse_next(i) +/// opt(alpha1).parse_peek(i) /// } /// /// assert_eq!(parser("abcd;"), Ok((";", Some("abcd")))); /// assert_eq!(parser("123;"), Ok(("123;", None))); /// # } /// ``` -pub fn opt<I: Stream, O, E: ParseError<I>, F>(mut f: F) -> impl Parser<I, Option<O>, E> +pub fn opt<I: Stream, O, E: ParserError<I>, F>(mut f: F) -> impl Parser<I, Option<O>, E> where F: Parser<I, O, E>, { - trace("opt", move |input: I| { - let i = input.clone(); + trace("opt", move |input: &mut I| { + let start = input.checkpoint(); match f.parse_next(input) { - Ok((i, o)) => Ok((i, Some(o))), - Err(ErrMode::Backtrack(_)) => Ok((i, None)), + Ok(o) => Ok(Some(o)), + Err(ErrMode::Backtrack(_)) => { + input.reset(start); + Ok(None) + } Err(e) => Err(e), } }) @@ -90,35 +92,32 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, IResult}; /// # use winnow::prelude::*; /// use winnow::combinator::cond; /// use winnow::ascii::alpha1; /// # fn main() { /// /// fn parser(b: bool, i: &str) -> IResult<&str, Option<&str>> { -/// cond(b, alpha1).parse_next(i) +/// cond(b, alpha1).parse_peek(i) /// } /// /// assert_eq!(parser(true, "abcd;"), Ok((";", Some("abcd")))); /// assert_eq!(parser(false, "abcd;"), Ok(("abcd;", None))); -/// assert_eq!(parser(true, "123;"), Err(ErrMode::Backtrack(Error::new("123;", ErrorKind::Slice)))); +/// assert_eq!(parser(true, "123;"), Err(ErrMode::Backtrack(InputError::new("123;", ErrorKind::Slice)))); /// assert_eq!(parser(false, "123;"), Ok(("123;", None))); /// # } /// ``` -pub fn cond<I, O, E: ParseError<I>, F>(b: bool, mut f: F) -> impl Parser<I, Option<O>, E> +pub fn cond<I, O, E: ParserError<I>, F>(b: bool, mut f: F) -> impl Parser<I, Option<O>, E> where I: Stream, F: Parser<I, O, E>, { - trace("cond", move |input: I| { + trace("cond", move |input: &mut I| { if b { - match f.parse_next(input) { - Ok((i, o)) => Ok((i, Some(o))), - Err(e) => Err(e), - } + f.parse_next(input).map(Some) } else { - Ok((input, None)) + Ok(None) } }) } @@ -128,7 +127,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, IResult}; /// # use winnow::prelude::*; /// use winnow::combinator::peek; /// use winnow::ascii::alpha1; @@ -136,22 +135,21 @@ where /// /// let mut parser = peek(alpha1); /// -/// assert_eq!(parser.parse_next("abcd;"), Ok(("abcd;", "abcd"))); -/// assert_eq!(parser.parse_next("123;"), Err(ErrMode::Backtrack(Error::new("123;", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("abcd;"), Ok(("abcd;", "abcd"))); +/// assert_eq!(parser.parse_peek("123;"), Err(ErrMode::Backtrack(InputError::new("123;", ErrorKind::Slice)))); /// # } /// ``` #[doc(alias = "look_ahead")] #[doc(alias = "rewind")] -pub fn peek<I: Stream, O, E: ParseError<I>, F>(mut f: F) -> impl Parser<I, O, E> +pub fn peek<I: Stream, O, E: ParserError<I>, F>(mut f: F) -> impl Parser<I, O, E> where F: Parser<I, O, E>, { - trace("peek", move |input: I| { - let i = input.clone(); - match f.parse_next(input) { - Ok((_, o)) => Ok((i, o)), - Err(e) => Err(e), - } + trace("peek", move |input: &mut I| { + let start = input.checkpoint(); + let res = f.parse_next(input); + input.reset(start); + res }) } @@ -163,21 +161,21 @@ where /// /// ```rust /// # use std::str; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::combinator::eof; /// # use winnow::prelude::*; /// /// let mut parser = eof; -/// assert_eq!(parser.parse_next("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Eof)))); -/// assert_eq!(parser.parse_next(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Eof)))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` #[doc(alias = "end")] #[doc(alias = "eoi")] -pub fn eof<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn eof<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: Stream, { - trace("eof", move |input: I| { + trace("eof", move |input: &mut I| { if input.eof_offset() == 0 { Ok(input.next_slice(0)) } else { @@ -194,7 +192,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, IResult}; /// # use winnow::prelude::*; /// use winnow::combinator::not; /// use winnow::ascii::alpha1; @@ -202,19 +200,21 @@ where /// /// let mut parser = not(alpha1); /// -/// assert_eq!(parser.parse_next("123"), Ok(("123", ()))); -/// assert_eq!(parser.parse_next("abcd"), Err(ErrMode::Backtrack(Error::new("abcd", ErrorKind::Not)))); +/// assert_eq!(parser.parse_peek("123"), Ok(("123", ()))); +/// assert_eq!(parser.parse_peek("abcd"), Err(ErrMode::Backtrack(InputError::new("abcd", ErrorKind::Not)))); /// # } /// ``` -pub fn not<I: Stream, O, E: ParseError<I>, F>(mut parser: F) -> impl Parser<I, (), E> +pub fn not<I: Stream, O, E: ParserError<I>, F>(mut parser: F) -> impl Parser<I, (), E> where F: Parser<I, O, E>, { - trace("not", move |input: I| { - let i = input.clone(); - match parser.parse_next(input) { - Ok(_) => Err(ErrMode::from_error_kind(i, ErrorKind::Not)), - Err(ErrMode::Backtrack(_)) => Ok((i, ())), + trace("not", move |input: &mut I| { + let start = input.checkpoint(); + let res = parser.parse_next(input); + input.reset(start); + match res { + Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Not)), + Err(ErrMode::Backtrack(_)) => Ok(()), Err(e) => Err(e), } }) @@ -229,7 +229,7 @@ where /// /// Without `cut_err`: /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::token::one_of; /// # use winnow::ascii::digit1; /// # use winnow::combinator::rest; @@ -240,9 +240,9 @@ where /// /// fn parser(input: &str) -> IResult<&str, &str> { /// alt(( -/// preceded(one_of("+-"), digit1), +/// preceded(one_of(['+', '-']), digit1), /// rest -/// )).parse_next(input) +/// )).parse_peek(input) /// } /// /// assert_eq!(parser("+10 ab"), Ok((" ab", "10"))); @@ -253,7 +253,7 @@ where /// /// With `cut_err`: /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// # use winnow::token::one_of; /// # use winnow::ascii::digit1; @@ -265,22 +265,22 @@ where /// /// fn parser(input: &str) -> IResult<&str, &str> { /// alt(( -/// preceded(one_of("+-"), cut_err(digit1)), +/// preceded(one_of(['+', '-']), cut_err(digit1)), /// rest -/// )).parse_next(input) +/// )).parse_peek(input) /// } /// /// assert_eq!(parser("+10 ab"), Ok((" ab", "10"))); /// assert_eq!(parser("ab"), Ok(("", "ab"))); -/// assert_eq!(parser("+"), Err(ErrMode::Cut(Error { input: "", kind: ErrorKind::Slice }))); +/// assert_eq!(parser("+"), Err(ErrMode::Cut(InputError::new("", ErrorKind::Slice )))); /// # } /// ``` -pub fn cut_err<I, O, E: ParseError<I>, F>(mut parser: F) -> impl Parser<I, O, E> +pub fn cut_err<I, O, E: ParserError<I>, F>(mut parser: F) -> impl Parser<I, O, E> where I: Stream, F: Parser<I, O, E>, { - trace("cut_err", move |input: I| { + trace("cut_err", move |input: &mut I| { parser.parse_next(input).map_err(|e| e.cut()) }) } @@ -289,12 +289,12 @@ where /// /// This attempts the parse, allowing other parsers to be tried on failure, like with /// [`winnow::combinator::alt`][crate::combinator::alt]. -pub fn backtrack_err<I, O, E: ParseError<I>, F>(mut parser: F) -> impl Parser<I, O, E> +pub fn backtrack_err<I, O, E: ParserError<I>, F>(mut parser: F) -> impl Parser<I, O, E> where I: Stream, F: Parser<I, O, E>, { - trace("backtrack_err", move |input: I| { + trace("backtrack_err", move |input: &mut I| { parser.parse_next(input).map_err(|e| e.backtrack()) }) } @@ -313,17 +313,17 @@ where /// # use winnow::prelude::*; /// # use winnow::combinator::todo; /// -/// fn parser(input: &str) -> IResult<&str, u64> { +/// fn parser(input: &mut &str) -> PResult<u64> { /// todo(input) /// } /// ``` #[track_caller] -pub fn todo<I, O, E>(input: I) -> IResult<I, O, E> +pub fn todo<I, O, E>(input: &mut I) -> PResult<O, E> where I: Stream, { #![allow(clippy::todo)] - trace("todo", move |_input: I| todo!("unimplemented parse")).parse_next(input) + trace("todo", move |_input: &mut I| todo!("unimplemented parse")).parse_next(input) } /// Repeats the embedded parser, lazily returning the results @@ -352,7 +352,7 @@ pub fn iterator<I, O, E, F>(input: I, parser: F) -> ParserIterator<F, I, O, E> where F: Parser<I, O, E>, I: Stream, - E: ParseError<I>, + E: ParserError<I>, { ParserIterator { parser, @@ -380,7 +380,7 @@ where I: Stream, { /// Returns the remaining input if parsing was successful, or the error if we encountered an error. - pub fn finish(mut self) -> IResult<I, (), E> { + pub fn finish(mut self) -> PResult<(I, ()), E> { match self.state.take().unwrap() { State::Running | State::Done => Ok((self.input, ())), State::Failure(e) => Err(ErrMode::Cut(e)), @@ -398,15 +398,15 @@ where fn next(&mut self) -> Option<Self::Item> { if let State::Running = self.state.take().unwrap() { - let input = self.input.clone(); + let start = self.input.checkpoint(); - match self.parser.parse_next(input) { - Ok((i, o)) => { - self.input = i; + match self.parser.parse_next(&mut self.input) { + Ok(o) => { self.state = Some(State::Running); Some(o) } Err(ErrMode::Backtrack(_)) => { + self.input.reset(start); self.state = Some(State::Done); None } @@ -442,20 +442,20 @@ enum State<E> { /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// use winnow::combinator::alt; /// use winnow::combinator::success; /// -/// let mut parser = success::<_,_,Error<_>>(10); -/// assert_eq!(parser.parse_next("xyz"), Ok(("xyz", 10))); +/// let mut parser = success::<_,_,InputError<_>>(10); +/// assert_eq!(parser.parse_peek("xyz"), Ok(("xyz", 10))); /// /// fn sign(input: &str) -> IResult<&str, isize> { /// alt(( /// '-'.value(-1), /// '+'.value(1), -/// success::<_,_,Error<_>>(1) -/// )).parse_next(input) +/// success::<_,_,InputError<_>>(1) +/// )).parse_peek(input) /// } /// assert_eq!(sign("+10"), Ok(("10", 1))); /// assert_eq!(sign("-10"), Ok(("10", -1))); @@ -463,8 +463,8 @@ enum State<E> { /// ``` #[doc(alias = "value")] #[doc(alias = "empty")] -pub fn success<I: Stream, O: Clone, E: ParseError<I>>(val: O) -> impl Parser<I, O, E> { - trace("success", move |input: I| Ok((input, val.clone()))) +pub fn success<I: Stream, O: Clone, E: ParserError<I>>(val: O) -> impl Parser<I, O, E> { + trace("success", move |_input: &mut I| Ok(val.clone())) } /// A parser which always fails. @@ -475,15 +475,16 @@ pub fn success<I: Stream, O: Clone, E: ParseError<I>>(val: O) -> impl Parser<I, /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, IResult}; +/// # use winnow::prelude::*; /// use winnow::combinator::fail; /// /// let s = "string"; -/// assert_eq!(fail::<_, &str, _>(s), Err(ErrMode::Backtrack(Error::new(s, ErrorKind::Fail)))); +/// assert_eq!(fail::<_, &str, _>.parse_peek(s), Err(ErrMode::Backtrack(InputError::new(s, ErrorKind::Fail)))); /// ``` #[doc(alias = "unexpected")] -pub fn fail<I: Stream, O, E: ParseError<I>>(i: I) -> IResult<I, O, E> { - trace("fail", |i| { +pub fn fail<I: Stream, O, E: ParserError<I>>(i: &mut I) -> PResult<O, E> { + trace("fail", |i: &mut I| { Err(ErrMode::from_error_kind(i, ErrorKind::Fail)) }) .parse_next(i) diff --git a/vendor/winnow/src/combinator/multi.rs b/vendor/winnow/src/combinator/multi.rs index 950432601..1fdb7535b 100644 --- a/vendor/winnow/src/combinator/multi.rs +++ b/vendor/winnow/src/combinator/multi.rs @@ -2,12 +2,12 @@ use crate::error::ErrMode; use crate::error::ErrorKind; -use crate::error::ParseError; +use crate::error::ParserError; use crate::stream::Accumulate; use crate::stream::Range; use crate::stream::Stream; use crate::trace::trace; -use crate::IResult; +use crate::PResult; use crate::Parser; /// [`Accumulate`] the output of a parser into a container, like `Vec` @@ -37,7 +37,7 @@ use crate::Parser; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(0.., "abc").parse_next(s) +/// repeat(0.., "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -50,38 +50,38 @@ use crate::Parser; /// One or more reptitions: /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(1.., "abc").parse_next(s) +/// repeat(1.., "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); /// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// # } /// ``` /// /// Fixed number of repeitions: /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(2, "abc").parse_next(s) +/// repeat(2, "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); -/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); /// # } /// ``` @@ -95,7 +95,7 @@ use crate::Parser; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(0..=2, "abc").parse_next(s) +/// repeat(0..=2, "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -120,13 +120,13 @@ where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { let Range { start_inclusive, end_inclusive, } = range.into(); - trace("repeat", move |i: I| { + trace("repeat", move |i: &mut I| { match (start_inclusive, end_inclusive) { (0, None) => repeat0_(&mut f, i), (1, None) => repeat1_(&mut f, i), @@ -136,84 +136,63 @@ where }) } -/// Deprecated, replaced by [`repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `repeat`")] -#[inline(always)] -pub fn repeat0<I, O, C, E, F>(f: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - repeat(0.., f) -} - -fn repeat0_<I, O, C, E, F>(f: &mut F, mut i: I) -> IResult<I, C, E> +fn repeat0_<I, O, C, E, F>(f: &mut F, i: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { let mut acc = C::initial(None); loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match f.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, acc)), + match f.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(acc); + } Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } - i = i1; acc.accumulate(o); } } } } -/// Deprecated, replaced by [`repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `repeat`")] -#[inline(always)] -pub fn repeat1<I, O, C, E, F>(f: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - repeat(1.., f) -} - -fn repeat1_<I, O, C, E, F>(f: &mut F, mut i: I) -> IResult<I, C, E> +fn repeat1_<I, O, C, E, F>(f: &mut F, i: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - match f.parse_next(i.clone()) { + match f.parse_next(i) { Err(e) => Err(e.append(i, ErrorKind::Many)), - Ok((i1, o)) => { + Ok(o) => { let mut acc = C::initial(None); acc.accumulate(o); - i = i1; loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match f.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, acc)), + match f.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(acc); + } Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } - i = i1; acc.accumulate(o); } } @@ -235,19 +214,19 @@ where /// /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::repeat_till0; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> { -/// repeat_till0("abc", "end").parse_next(s) +/// repeat_till0("abc", "end").parse_peek(s) /// }; /// /// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end")))); -/// assert_eq!(parser("abc123end"), Err(ErrMode::Backtrack(Error::new("123end", ErrorKind::Tag)))); -/// assert_eq!(parser("123123end"), Err(ErrMode::Backtrack(Error::new("123123end", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("abc123end"), Err(ErrMode::Backtrack(InputError::new("123end", ErrorKind::Tag)))); +/// assert_eq!(parser("123123end"), Err(ErrMode::Backtrack(InputError::new("123123end", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end")))); /// # } /// ``` @@ -258,20 +237,22 @@ where C: Accumulate<O>, F: Parser<I, O, E>, G: Parser<I, P, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("repeat_till0", move |mut i: I| { + trace("repeat_till0", move |i: &mut I| { let mut res = C::initial(None); loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match g.parse_next(i.clone()) { - Ok((i1, o)) => return Ok((i1, (res, o))), + match g.parse_next(i) { + Ok(o) => return Ok((res, o)), Err(ErrMode::Backtrack(_)) => { - match f.parse_next(i.clone()) { + i.reset(start); + match f.parse_next(i) { Err(e) => return Err(e.append(i, ErrorKind::Many)), - Ok((i1, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert( i, "`repeat` parsers must always consume", @@ -279,7 +260,6 @@ where } res.accumulate(o); - i = i1; } } } @@ -308,7 +288,7 @@ where /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// separated0("abc", "|").parse_next(s) +/// separated0("abc", "|").parse_peek(s) /// } /// /// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); @@ -326,37 +306,46 @@ where C: Accumulate<O>, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("separated0", move |mut i: I| { + trace("separated0", move |i: &mut I| { let mut res = C::initial(None); - match parser.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + let start = i.checkpoint(); + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { res.accumulate(o); - i = i1; } } loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match sep.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match sep.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i1, _)) => { + Ok(_) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "sep parsers must always consume")); } - match parser.parse_next(i1.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i2, o)) => { + Ok(o) => { res.accumulate(o); - i = i2; } } } @@ -380,20 +369,20 @@ where /// /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::separated1; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// separated1("abc", "|").parse_next(s) +/// separated1("abc", "|").parse_peek(s) /// } /// /// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); /// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); /// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Tag)))); /// # } /// ``` #[doc(alias = "sep_by1")] @@ -404,37 +393,42 @@ where C: Accumulate<O>, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("separated1", move |mut i: I| { + trace("separated1", move |i: &mut I| { let mut res = C::initial(None); // Parse the first element - match parser.parse_next(i.clone()) { + match parser.parse_next(i) { Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { res.accumulate(o); - i = i1; } } loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match sep.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match sep.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i1, _)) => { + Ok(_) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "sep parsers must always consume")); } - match parser.parse_next(i1.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i2, o)) => { + Ok(o) => { res.accumulate(o); - i = i2; } } } @@ -451,18 +445,18 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::separated_foldl1; /// use winnow::ascii::dec_int; /// /// fn parser(s: &str) -> IResult<&str, i32> { -/// separated_foldl1(dec_int, "-", |l, _, r| l - r).parse_next(s) +/// separated_foldl1(dec_int, "-", |l, _, r| l - r).parse_peek(s) /// } /// /// assert_eq!(parser("9-3-5"), Ok(("", 1))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); -/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Slice)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); +/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Slice)))); /// ``` pub fn separated_foldl1<I, O, O2, E, P, S, Op>( mut parser: P, @@ -473,29 +467,35 @@ where I: Stream, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, Op: Fn(O, O2, O) -> O, { - trace("separated_foldl1", move |i: I| { - let (mut i, mut ol) = parser.parse_next(i)?; + trace("separated_foldl1", move |i: &mut I| { + let mut ol = parser.parse_next(i)?; loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match sep.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, ol)), + match sep.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(ol); + } Err(e) => return Err(e), - Ok((i1, s)) => { + Ok(s) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } - match parser.parse_next(i1.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, ol)), + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(ol); + } Err(e) => return Err(e), - Ok((i2, or)) => { + Ok(or) => { ol = op(ol, s, or); - i = i2; } } } @@ -512,19 +512,19 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::separated_foldr1; /// use winnow::ascii::dec_uint; /// /// fn parser(s: &str) -> IResult<&str, u32> { -/// separated_foldr1(dec_uint, "^", |l: u32, _, r: u32| l.pow(r)).parse_next(s) +/// separated_foldr1(dec_uint, "^", |l: u32, _, r: u32| l.pow(r)).parse_peek(s) /// } /// /// assert_eq!(parser("2^3^2"), Ok(("", 512))); /// assert_eq!(parser("2"), Ok(("", 2))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); -/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Slice)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); +/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Slice)))); /// ``` #[cfg(feature = "alloc")] pub fn separated_foldr1<I, O, O2, E, P, S, Op>( @@ -536,12 +536,12 @@ where I: Stream, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, Op: Fn(O, O2, O) -> O, { - trace("separated_foldr1", move |i: I| { - let (i, ol) = parser.parse_next(i)?; - let (i, all): (_, crate::lib::std::vec::Vec<(O2, O)>) = + trace("separated_foldr1", move |i: &mut I| { + let ol = parser.parse_next(i)?; + let all: crate::lib::std::vec::Vec<(O2, O)> = repeat(0.., (sep.by_ref(), parser.by_ref())).parse_next(i)?; if let Some((s, or)) = all .into_iter() @@ -549,24 +549,19 @@ where .reduce(|(sr, or), (sl, ol)| (sl, op(ol, sr, or))) { let merged = op(ol, s, or); - Ok((i, merged)) + Ok(merged) } else { - Ok((i, ol)) + Ok(ol) } }) } -fn repeat_m_n_<I, O, C, E, F>( - min: usize, - max: usize, - parse: &mut F, - mut input: I, -) -> IResult<I, C, E> +fn repeat_m_n_<I, O, C, E, F>(min: usize, max: usize, parse: &mut F, input: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { if min > max { return Err(ErrMode::Cut(E::from_error_kind(input, ErrorKind::Many))); @@ -574,11 +569,12 @@ where let mut res = C::initial(Some(min)); for count in 0..max { + let start = input.checkpoint(); let len = input.eof_offset(); - match parse.parse_next(input.clone()) { - Ok((tail, value)) => { + match parse.parse_next(input) { + Ok(value) => { // infinite loop check: the parser must always consume - if tail.eof_offset() == len { + if input.eof_offset() == len { return Err(ErrMode::assert( input, "`repeat` parsers must always consume", @@ -586,13 +582,13 @@ where } res.accumulate(value); - input = tail; } Err(ErrMode::Backtrack(e)) => { if count < min { return Err(ErrMode::Backtrack(e.append(input, ErrorKind::Many))); } else { - return Ok((input, res)); + input.reset(start); + return Ok(res); } } Err(e) => { @@ -601,38 +597,22 @@ where } } - Ok((input, res)) + Ok(res) } -/// Deprecated, replaced by [`repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `repeat`")] -#[inline(always)] -pub fn count<I, O, C, E, F>(f: F, count: usize) -> impl Parser<I, C, E> +fn repeat_n_<I, O, C, E, F>(count: usize, f: &mut F, i: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - repeat(count, f) -} - -fn repeat_n_<I, O, C, E, F>(count: usize, f: &mut F, i: I) -> IResult<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - let mut input = i.clone(); let mut res = C::initial(Some(count)); for _ in 0..count { - let input_ = input.clone(); - match f.parse_next(input_) { - Ok((i, o)) => { + match f.parse_next(i) { + Ok(o) => { res.accumulate(o); - input = i; } Err(e) => { return Err(e.append(i, ErrorKind::Many)); @@ -640,7 +620,7 @@ where } } - Ok((input, res)) + Ok(res) } /// Repeats the embedded parser, filling the given slice with results. @@ -654,38 +634,34 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::fill; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, [&str; 2]> { /// let mut buf = ["", ""]; -/// let (rest, ()) = fill("abc", &mut buf).parse_next(s)?; +/// let (rest, ()) = fill("abc", &mut buf).parse_peek(s)?; /// Ok((rest, buf)) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", ["abc", "abc"]))); -/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", ["abc", "abc"]))); /// ``` pub fn fill<'a, I, O, E, F>(mut f: F, buf: &'a mut [O]) -> impl Parser<I, (), E> + 'a where I: Stream + 'a, F: Parser<I, O, E> + 'a, - E: ParseError<I> + 'a, + E: ParserError<I> + 'a, { - trace("fill", move |i: I| { - let mut input = i.clone(); - + trace("fill", move |i: &mut I| { for elem in buf.iter_mut() { - let input_ = input.clone(); - match f.parse_next(input_) { - Ok((i, o)) => { + match f.parse_next(i) { + Ok(o) => { *elem = o; - input = i; } Err(e) => { return Err(e.append(i, ErrorKind::Many)); @@ -693,7 +669,7 @@ where } } - Ok((input, ())) + Ok(()) }) } @@ -732,7 +708,7 @@ where /// acc.push(item); /// acc /// } -/// ).parse_next(s) +/// ).parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -743,7 +719,7 @@ where /// /// One or more repetitions: /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::fold_repeat; /// use winnow::token::tag; @@ -757,13 +733,13 @@ where /// acc.push(item); /// acc /// } -/// ).parse_next(s) +/// ).parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); /// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Many)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Many)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Many)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Many)))); /// ``` /// /// Arbitrary number of repetitions: @@ -782,7 +758,7 @@ where /// acc.push(item); /// acc /// } -/// ).parse_next(s) +/// ).parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -806,13 +782,13 @@ where F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { let Range { start_inclusive, end_inclusive, } = range.into(); - trace("fold_repeat", move |i: I| { + trace("fold_repeat", move |i: &mut I| { match (start_inclusive, end_inclusive) { (0, None) => fold_repeat0_(&mut f, &mut init, &mut g, i), (1, None) => fold_repeat1_(&mut f, &mut init, &mut g, i), @@ -828,48 +804,39 @@ where }) } -/// Deprecated, replaced by [`fold_repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `fold_repeat`")] -#[inline(always)] -pub fn fold_repeat0<I, O, E, F, G, H, R>(mut f: F, mut init: H, mut g: G) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - trace("fold_repeat0", move |i: I| { - fold_repeat0_(&mut f, &mut init, &mut g, i) - }) -} - -fn fold_repeat0_<I, O, E, F, G, H, R>(f: &mut F, init: &mut H, g: &mut G, i: I) -> IResult<I, R, E> +fn fold_repeat0_<I, O, E, F, G, H, R>( + f: &mut F, + init: &mut H, + g: &mut G, + input: &mut I, +) -> PResult<R, E> where I: Stream, F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { let mut res = init(); - let mut input = i; loop { - let i_ = input.clone(); + let start = input.checkpoint(); let len = input.eof_offset(); - match f.parse_next(i_) { - Ok((i, o)) => { + match f.parse_next(input) { + Ok(o) => { // infinite loop check: the parser must always consume - if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + if input.eof_offset() == len { + return Err(ErrMode::assert( + input, + "`repeat` parsers must always consume", + )); } res = g(res, o); - input = i; } Err(ErrMode::Backtrack(_)) => { - return Ok((input, res)); + input.reset(start); + return Ok(res); } Err(e) => { return Err(e); @@ -878,60 +845,50 @@ where } } -/// Deprecated, replaced by [`fold_repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `fold_repeat`")] -#[inline(always)] -pub fn fold_repeat1<I, O, E, F, G, H, R>(mut f: F, mut init: H, mut g: G) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - trace("fold_repeat1", move |i: I| { - fold_repeat1_(&mut f, &mut init, &mut g, i) - }) -} - -fn fold_repeat1_<I, O, E, F, G, H, R>(f: &mut F, init: &mut H, g: &mut G, i: I) -> IResult<I, R, E> +fn fold_repeat1_<I, O, E, F, G, H, R>( + f: &mut F, + init: &mut H, + g: &mut G, + input: &mut I, +) -> PResult<R, E> where I: Stream, F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { - let _i = i.clone(); let init = init(); - match f.parse_next(_i) { - Err(ErrMode::Backtrack(_)) => Err(ErrMode::from_error_kind(i, ErrorKind::Many)), + match f.parse_next(input) { + Err(ErrMode::Backtrack(_)) => Err(ErrMode::from_error_kind(input, ErrorKind::Many)), Err(e) => Err(e), - Ok((i1, o1)) => { + Ok(o1) => { let mut acc = g(init, o1); - let mut input = i1; loop { - let _input = input.clone(); + let start = input.checkpoint(); let len = input.eof_offset(); - match f.parse_next(_input) { + match f.parse_next(input) { Err(ErrMode::Backtrack(_)) => { + input.reset(start); break; } Err(e) => return Err(e), - Ok((i, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + if input.eof_offset() == len { + return Err(ErrMode::assert( + input, + "`repeat` parsers must always consume", + )); } acc = g(acc, o); - input = i; } } } - Ok((input, acc)) + Ok(acc) } } } @@ -942,14 +899,14 @@ fn fold_repeat_m_n_<I, O, E, F, G, H, R>( parse: &mut F, init: &mut H, fold: &mut G, - mut input: I, -) -> IResult<I, R, E> + input: &mut I, +) -> PResult<R, E> where I: Stream, F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { if min > max { return Err(ErrMode::Cut(E::from_error_kind(input, ErrorKind::Many))); @@ -957,11 +914,12 @@ where let mut acc = init(); for count in 0..max { + let start = input.checkpoint(); let len = input.eof_offset(); - match parse.parse_next(input.clone()) { - Ok((tail, value)) => { + match parse.parse_next(input) { + Ok(value) => { // infinite loop check: the parser must always consume - if tail.eof_offset() == len { + if input.eof_offset() == len { return Err(ErrMode::assert( input, "`repeat` parsers must always consume", @@ -969,13 +927,13 @@ where } acc = fold(acc, value); - input = tail; } //FInputXMError: handle failure properly Err(ErrMode::Backtrack(err)) => { if count < min { return Err(ErrMode::Backtrack(err.append(input, ErrorKind::Many))); } else { + input.reset(start); break; } } @@ -983,5 +941,5 @@ where } } - Ok((input, acc)) + Ok(acc) } diff --git a/vendor/winnow/src/combinator/parser.rs b/vendor/winnow/src/combinator/parser.rs index 12b223a2e..969c2d525 100644 --- a/vendor/winnow/src/combinator/parser.rs +++ b/vendor/winnow/src/combinator/parser.rs @@ -1,8 +1,8 @@ -use crate::error::{ContextError, ErrMode, ErrorKind, FromExternalError, ParseError}; +use crate::error::{AddContext, ErrMode, ErrorKind, FromExternalError, ParserError}; use crate::lib::std::borrow::Borrow; use crate::lib::std::ops::Range; +use crate::stream::StreamIsPartial; use crate::stream::{Location, Stream}; -use crate::stream::{Offset, StreamIsPartial}; use crate::trace::trace; use crate::trace::trace_result; use crate::*; @@ -20,7 +20,8 @@ impl<'p, P> ByRef<'p, P> { } impl<'p, I, O, E, P: Parser<I, O, E>> Parser<I, O, E> for ByRef<'p, P> { - fn parse_next(&mut self, i: I) -> IResult<I, O, E> { + #[inline(always)] + fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { self.p.parse_next(i) } } @@ -62,24 +63,22 @@ where F: Parser<I, O, E>, G: Fn(O) -> O2, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { match self.parser.parse_next(i) { Err(e) => Err(e), - Ok((i, o)) => Ok((i, (self.map)(o))), + Ok(o) => Ok((self.map)(o)), } } } -#[deprecated(since = "0.4.2", note = "Replaced with `TryMap`")] -pub use TryMap as MapRes; - /// Implementation of [`Parser::try_map`] #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] pub struct TryMap<F, G, I, O, O2, E, E2> where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { parser: F, @@ -95,7 +94,7 @@ impl<F, G, I, O, O2, E, E2> TryMap<F, G, I, O, O2, E, E2> where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { pub(crate) fn new(parser: F, map: G) -> Self { @@ -115,16 +114,17 @@ impl<F, G, I, O, O2, E, E2> Parser<I, O2, E> for TryMap<F, G, I, O, O2, E, E2> where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { - fn parse_next(&mut self, input: I) -> IResult<I, O2, E> { - let i = input.clone(); - let (input, o) = self.parser.parse_next(input)?; - let res = match (self.map)(o) { - Ok(o2) => Ok((input, o2)), - Err(e) => Err(ErrMode::from_external_error(i, ErrorKind::Verify, e)), - }; + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { + let start = input.checkpoint(); + let o = self.parser.parse_next(input)?; + let res = (self.map)(o).map_err(|err| { + input.reset(start); + ErrMode::from_external_error(input, ErrorKind::Verify, err) + }); trace_result("verify", &res); res } @@ -136,8 +136,8 @@ pub struct VerifyMap<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, + I: Stream, + E: ParserError<I>, { parser: F, map: G, @@ -151,8 +151,8 @@ impl<F, G, I, O, O2, E> VerifyMap<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, + I: Stream, + E: ParserError<I>, { pub(crate) fn new(parser: F, map: G) -> Self { Self { @@ -170,16 +170,17 @@ impl<F, G, I, O, O2, E> Parser<I, O2, E> for VerifyMap<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, -{ - fn parse_next(&mut self, input: I) -> IResult<I, O2, E> { - let i = input.clone(); - let (input, o) = self.parser.parse_next(input)?; - let res = match (self.map)(o) { - Some(o2) => Ok((input, o2)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), - }; + I: Stream, + E: ParserError<I>, +{ + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { + let start = input.checkpoint(); + let o = self.parser.parse_next(input)?; + let res = (self.map)(o).ok_or_else(|| { + input.reset(start); + ErrMode::from_error_kind(input, ErrorKind::Verify) + }); trace_result("verify", &res); res } @@ -192,6 +193,7 @@ where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { outer: F, inner: G, @@ -206,6 +208,7 @@ where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { pub(crate) fn new(outer: F, inner: G) -> Self { Self { @@ -224,12 +227,18 @@ where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - let (i, mut o) = self.outer.parse_next(i)?; + #[inline(always)] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + let start = i.checkpoint(); + let mut o = self.outer.parse_next(i)?; let _ = o.complete(); - let (_, o2) = self.inner.parse_next(o)?; - Ok((i, o2)) + let o2 = self.inner.parse_next(&mut o).map_err(|err| { + i.reset(start); + err + })?; + Ok(o2) } } @@ -240,7 +249,7 @@ where P: Parser<I, O, E>, I: Stream, O: crate::stream::ParseSlice<O2>, - E: ParseError<I>, + E: ParserError<I>, { p: P, i: core::marker::PhantomData<I>, @@ -254,7 +263,7 @@ where P: Parser<I, O, E>, I: Stream, O: crate::stream::ParseSlice<O2>, - E: ParseError<I>, + E: ParserError<I>, { pub(crate) fn new(p: P) -> Self { Self { @@ -272,17 +281,18 @@ where P: Parser<I, O, E>, I: Stream, O: crate::stream::ParseSlice<O2>, - E: ParseError<I>, -{ - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - let input = i.clone(); - let (i, o) = self.p.parse_next(i)?; - - let res = o - .parse_slice() - .ok_or_else(|| ErrMode::from_error_kind(input, ErrorKind::Verify)); + E: ParserError<I>, +{ + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + let start = i.checkpoint(); + let o = self.p.parse_next(i)?; + let res = o.parse_slice().ok_or_else(|| { + i.reset(start); + ErrMode::from_error_kind(i, ErrorKind::Verify) + }); trace_result("verify", &res); - Ok((i, res?)) + res } } @@ -328,8 +338,9 @@ where G: FnMut(O) -> H, H: Parser<I, O2, E>, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - let (i, o) = self.f.parse_next(i)?; + #[inline(always)] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + let o = self.f.parse_next(i)?; (self.g)(o).parse_next(i) } } @@ -350,14 +361,14 @@ impl<F, I, O, E> Parser<I, O, E> for CompleteErr<F> where I: Stream, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - fn parse_next(&mut self, input: I) -> IResult<I, O, E> { - trace("complete_err", |input: I| { - let i = input.clone(); + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O, E> { + trace("complete_err", |input: &mut I| { match (self.f).parse_next(input) { Err(ErrMode::Incomplete(_)) => { - Err(ErrMode::from_error_kind(i, ErrorKind::Complete)) + Err(ErrMode::from_error_kind(input, ErrorKind::Complete)) } rest => rest, } @@ -372,10 +383,10 @@ pub struct Verify<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: Borrow<O2>, O2: ?Sized, - E: ParseError<I>, + E: ParserError<I>, { parser: F, filter: G, @@ -389,10 +400,10 @@ impl<F, G, I, O, O2, E> Verify<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: Borrow<O2>, O2: ?Sized, - E: ParseError<I>, + E: ParserError<I>, { pub(crate) fn new(parser: F, filter: G) -> Self { Self { @@ -410,20 +421,19 @@ impl<F, G, I, O, O2, E> Parser<I, O, E> for Verify<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: Borrow<O2>, O2: ?Sized, - E: ParseError<I>, -{ - fn parse_next(&mut self, input: I) -> IResult<I, O, E> { - let i = input.clone(); - let (input, o) = self.parser.parse_next(input)?; - - let res = if (self.filter)(o.borrow()) { - Ok((input, o)) - } else { - Err(ErrMode::from_error_kind(i, ErrorKind::Verify)) - }; + E: ParserError<I>, +{ + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O, E> { + let start = input.checkpoint(); + let o = self.parser.parse_next(input)?; + let res = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| { + input.reset(start); + ErrMode::from_error_kind(input, ErrorKind::Verify) + }); trace_result("verify", &res); res } @@ -464,10 +474,9 @@ where F: Parser<I, O, E>, O2: Clone, { - fn parse_next(&mut self, input: I) -> IResult<I, O2, E> { - (self.parser) - .parse_next(input) - .map(|(i, _)| (i, self.val.clone())) + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { + (self.parser).parse_next(input).map(|_| self.val.clone()) } } @@ -501,8 +510,9 @@ impl<F, I, O, E> Parser<I, (), E> for Void<F, I, O, E> where F: Parser<I, O, E>, { - fn parse_next(&mut self, input: I) -> IResult<I, (), E> { - (self.parser).parse_next(input).map(|(i, _)| (i, ())) + #[inline(always)] + fn parse_next(&mut self, input: &mut I) -> PResult<(), E> { + (self.parser).parse_next(input).map(|_| ()) } } @@ -511,7 +521,7 @@ where pub struct Recognize<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { parser: F, i: core::marker::PhantomData<I>, @@ -522,7 +532,7 @@ where impl<F, I, O, E> Recognize<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { pub(crate) fn new(parser: F) -> Self { Self { @@ -537,14 +547,17 @@ where impl<I, O, E, F> Parser<I, <I as Stream>::Slice, E> for Recognize<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { - fn parse_next(&mut self, input: I) -> IResult<I, <I as Stream>::Slice, E> { - let i = input.clone(); - match (self.parser).parse_next(i) { - Ok((i, _)) => { - let offset = input.offset_to(&i); - Ok(input.next_slice(offset)) + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<<I as Stream>::Slice, E> { + let checkpoint = input.checkpoint(); + match (self.parser).parse_next(input) { + Ok(_) => { + let offset = input.offset_from(&checkpoint); + input.reset(checkpoint); + let recognized = input.next_slice(offset); + Ok(recognized) } Err(e) => Err(e), } @@ -556,7 +569,7 @@ where pub struct WithRecognized<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { parser: F, i: core::marker::PhantomData<I>, @@ -567,7 +580,7 @@ where impl<F, I, O, E> WithRecognized<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { pub(crate) fn new(parser: F) -> Self { Self { @@ -582,15 +595,17 @@ where impl<F, I, O, E> Parser<I, (O, <I as Stream>::Slice), E> for WithRecognized<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { - fn parse_next(&mut self, input: I) -> IResult<I, (O, <I as Stream>::Slice), E> { - let i = input.clone(); - match (self.parser).parse_next(i) { - Ok((remaining, result)) => { - let offset = input.offset_to(&remaining); - let (remaining, recognized) = input.next_slice(offset); - Ok((remaining, (result, recognized))) + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<(O, <I as Stream>::Slice), E> { + let checkpoint = input.checkpoint(); + match (self.parser).parse_next(input) { + Ok(result) => { + let offset = input.offset_from(&checkpoint); + input.reset(checkpoint); + let recognized = input.next_slice(offset); + Ok((result, recognized)) } Err(e) => Err(e), } @@ -602,7 +617,7 @@ where pub struct Span<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { parser: F, i: core::marker::PhantomData<I>, @@ -613,7 +628,7 @@ where impl<F, I, O, E> Span<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { pub(crate) fn new(parser: F) -> Self { Self { @@ -628,13 +643,14 @@ where impl<I, O, E, F> Parser<I, Range<usize>, E> for Span<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { - fn parse_next(&mut self, input: I) -> IResult<I, Range<usize>, E> { + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<Range<usize>, E> { let start = input.location(); - self.parser.parse_next(input).map(move |(remaining, _)| { - let end = remaining.location(); - (remaining, (start..end)) + self.parser.parse_next(input).map(move |_| { + let end = input.location(); + start..end }) } } @@ -644,7 +660,7 @@ where pub struct WithSpan<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { parser: F, i: core::marker::PhantomData<I>, @@ -655,7 +671,7 @@ where impl<F, I, O, E> WithSpan<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { pub(crate) fn new(parser: F) -> Self { Self { @@ -670,16 +686,15 @@ where impl<F, I, O, E> Parser<I, (O, Range<usize>), E> for WithSpan<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { - fn parse_next(&mut self, input: I) -> IResult<I, (O, Range<usize>), E> { + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<(O, Range<usize>), E> { let start = input.location(); - self.parser - .parse_next(input) - .map(move |(remaining, output)| { - let end = remaining.location(); - (remaining, (output, (start..end))) - }) + self.parser.parse_next(input).map(move |output| { + let end = input.location(); + (output, (start..end)) + }) } } @@ -718,11 +733,9 @@ where F: Parser<I, O, E>, O: Into<O2>, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - match self.parser.parse_next(i) { - Ok((i, o)) => Ok((i, o.into())), - Err(err) => Err(err), - } + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + self.parser.parse_next(i).map(|o| o.into()) } } @@ -761,7 +774,8 @@ where F: Parser<I, O, E>, E: Into<E2>, { - fn parse_next(&mut self, i: I) -> IResult<I, O, E2> { + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O, E2> { match self.parser.parse_next(i) { Ok(ok) => Ok(ok), Err(ErrMode::Backtrack(e)) => Err(ErrMode::Backtrack(e.into())), @@ -777,7 +791,7 @@ pub struct Context<F, I, O, E, C> where F: Parser<I, O, E>, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { parser: F, @@ -791,7 +805,7 @@ impl<F, I, O, E, C> Context<F, I, O, E, C> where F: Parser<I, O, E>, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { pub(crate) fn new(parser: F, context: C) -> Self { @@ -809,17 +823,18 @@ impl<F, I, O, E, C> Parser<I, O, E> for Context<F, I, O, E, C> where F: Parser<I, O, E>, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { - fn parse_next(&mut self, i: I) -> IResult<I, O, E> { + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { #[cfg(feature = "debug")] let name = format!("context={:?}", self.context); #[cfg(not(feature = "debug"))] let name = "context"; - trace(name, move |i: I| { + trace(name, move |i: &mut I| { (self.parser) - .parse_next(i.clone()) + .parse_next(i) .map_err(|err| err.map(|err| err.add_context(i, self.context.clone()))) }) .parse_next(i) diff --git a/vendor/winnow/src/combinator/sequence.rs b/vendor/winnow/src/combinator/sequence.rs index 89c29a548..5cfeb9cb4 100644 --- a/vendor/winnow/src/combinator/sequence.rs +++ b/vendor/winnow/src/combinator/sequence.rs @@ -1,4 +1,4 @@ -use crate::error::ParseError; +use crate::error::ParserError; use crate::stream::Stream; use crate::trace::trace; use crate::*; @@ -12,7 +12,7 @@ use crate::*; /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::combinator::preceded; @@ -20,13 +20,13 @@ use crate::*; /// /// let mut parser = preceded("abc", "efg"); /// -/// assert_eq!(parser.parse_next("abcefg"), Ok(("", "efg"))); -/// assert_eq!(parser.parse_next("abcefghij"), Ok(("hij", "efg"))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("abcefg"), Ok(("", "efg"))); +/// assert_eq!(parser.parse_peek("abcefghij"), Ok(("hij", "efg"))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` #[doc(alias = "ignore_then")] -pub fn preceded<I, O1, O2, E: ParseError<I>, F, G>( +pub fn preceded<I, O1, O2, E: ParserError<I>, F, G>( mut first: F, mut second: G, ) -> impl Parser<I, O2, E> @@ -35,8 +35,8 @@ where F: Parser<I, O1, E>, G: Parser<I, O2, E>, { - trace("preceded", move |input: I| { - let (input, _) = first.parse_next(input)?; + trace("preceded", move |input: &mut I| { + let _ = first.parse_next(input)?; second.parse_next(input) }) } @@ -50,7 +50,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::combinator::terminated; @@ -58,13 +58,13 @@ where /// /// let mut parser = terminated("abc", "efg"); /// -/// assert_eq!(parser.parse_next("abcefg"), Ok(("", "abc"))); -/// assert_eq!(parser.parse_next("abcefghij"), Ok(("hij", "abc"))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("abcefg"), Ok(("", "abc"))); +/// assert_eq!(parser.parse_peek("abcefghij"), Ok(("hij", "abc"))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` #[doc(alias = "then_ignore")] -pub fn terminated<I, O1, O2, E: ParseError<I>, F, G>( +pub fn terminated<I, O1, O2, E: ParserError<I>, F, G>( mut first: F, mut second: G, ) -> impl Parser<I, O1, E> @@ -73,9 +73,9 @@ where F: Parser<I, O1, E>, G: Parser<I, O2, E>, { - trace("terminated", move |input: I| { - let (input, o1) = first.parse_next(input)?; - second.parse_next(input).map(|(i, _)| (i, o1)) + trace("terminated", move |input: &mut I| { + let o1 = first.parse_next(input)?; + second.parse_next(input).map(|_| o1) }) } @@ -89,7 +89,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::error::Needed::Size; /// # use winnow::prelude::*; /// use winnow::combinator::separated_pair; @@ -97,12 +97,12 @@ where /// /// let mut parser = separated_pair("abc", "|", "efg"); /// -/// assert_eq!(parser.parse_next("abc|efg"), Ok(("", ("abc", "efg")))); -/// assert_eq!(parser.parse_next("abc|efghij"), Ok(("hij", ("abc", "efg")))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("abc|efg"), Ok(("", ("abc", "efg")))); +/// assert_eq!(parser.parse_peek("abc|efghij"), Ok(("hij", ("abc", "efg")))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` -pub fn separated_pair<I, O1, O2, O3, E: ParseError<I>, F, G, H>( +pub fn separated_pair<I, O1, O2, O3, E: ParserError<I>, F, G, H>( mut first: F, mut sep: G, mut second: H, @@ -113,10 +113,10 @@ where G: Parser<I, O2, E>, H: Parser<I, O3, E>, { - trace("separated_pair", move |input: I| { - let (input, o1) = first.parse_next(input)?; - let (input, _) = sep.parse_next(input)?; - second.parse_next(input).map(|(i, o2)| (i, (o1, o2))) + trace("separated_pair", move |input: &mut I| { + let o1 = first.parse_next(input)?; + let _ = sep.parse_next(input)?; + second.parse_next(input).map(|o2| (o1, o2)) }) } @@ -130,7 +130,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::error::Needed::Size; /// # use winnow::prelude::*; /// use winnow::combinator::delimited; @@ -138,14 +138,14 @@ where /// /// let mut parser = delimited("(", "abc", ")"); /// -/// assert_eq!(parser.parse_next("(abc)"), Ok(("", "abc"))); -/// assert_eq!(parser.parse_next("(abc)def"), Ok(("def", "abc"))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("(abc)"), Ok(("", "abc"))); +/// assert_eq!(parser.parse_peek("(abc)def"), Ok(("def", "abc"))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` #[doc(alias = "between")] #[doc(alias = "padded")] -pub fn delimited<I, O1, O2, O3, E: ParseError<I>, F, G, H>( +pub fn delimited<I, O1, O2, O3, E: ParserError<I>, F, G, H>( mut first: F, mut second: G, mut third: H, @@ -156,9 +156,9 @@ where G: Parser<I, O2, E>, H: Parser<I, O3, E>, { - trace("delimited", move |input: I| { - let (input, _) = first.parse_next(input)?; - let (input, o2) = second.parse_next(input)?; - third.parse_next(input).map(|(i, _)| (i, o2)) + trace("delimited", move |input: &mut I| { + let _ = first.parse_next(input)?; + let o2 = second.parse_next(input)?; + third.parse_next(input).map(|_| o2) }) } diff --git a/vendor/winnow/src/combinator/tests.rs b/vendor/winnow/src/combinator/tests.rs index cdb787760..62dc420e5 100644 --- a/vendor/winnow/src/combinator/tests.rs +++ b/vendor/winnow/src/combinator/tests.rs @@ -5,11 +5,13 @@ use crate::binary::u16; use crate::binary::u8; use crate::binary::Endianness; use crate::error::ErrMode; -use crate::error::Error; use crate::error::ErrorKind; +use crate::error::InputError; use crate::error::Needed; -use crate::error::ParseError; +use crate::error::ParserError; +use crate::stream::Stream; use crate::token::take; +use crate::unpeek; use crate::IResult; use crate::Parser; use crate::Partial; @@ -19,7 +21,7 @@ use crate::lib::std::vec::Vec; macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::IResult<_, _, Error<_>> = $left; + let res: $crate::IResult<_, _, InputError<_>> = $left; assert_eq!(res, $right); }; ); @@ -29,16 +31,16 @@ fn eof_on_slices() { let not_over: &[u8] = &b"Hello, world!"[..]; let is_over: &[u8] = &b""[..]; - let res_not_over = eof(not_over); + let res_not_over = eof.parse_peek(not_over); assert_parse!( res_not_over, Err(ErrMode::Backtrack(error_position!( - not_over, + ¬_over, ErrorKind::Eof ))) ); - let res_over = eof(is_over); + let res_over = eof.parse_peek(is_over); assert_parse!(res_over, Ok((is_over, is_over))); } @@ -47,16 +49,16 @@ fn eof_on_strs() { let not_over: &str = "Hello, world!"; let is_over: &str = ""; - let res_not_over = eof(not_over); + let res_not_over = eof.parse_peek(not_over); assert_parse!( res_not_over, Err(ErrMode::Backtrack(error_position!( - not_over, + ¬_over, ErrorKind::Eof ))) ); - let res_over = eof(is_over); + let res_over = eof.parse_peek(is_over); assert_parse!(res_over, Ok((is_over, is_over))); } @@ -64,20 +66,20 @@ fn eof_on_strs() { fn rest_on_slices() { let input: &[u8] = &b"Hello, world!"[..]; let empty: &[u8] = &b""[..]; - assert_parse!(rest(input), Ok((empty, input))); + assert_parse!(rest.parse_peek(input), Ok((empty, input))); } #[test] fn rest_on_strs() { let input: &str = "Hello, world!"; let empty: &str = ""; - assert_parse!(rest(input), Ok((empty, input))); + assert_parse!(rest.parse_peek(input), Ok((empty, input))); } #[test] fn rest_len_on_slices() { let input: &[u8] = &b"Hello, world!"[..]; - assert_parse!(rest_len(input), Ok((input, input.len()))); + assert_parse!(rest_len.parse_peek(input), Ok((input, input.len()))); } use crate::lib::std::convert::From; @@ -87,12 +89,12 @@ impl From<u32> for CustomError { } } -impl<I> ParseError<I> for CustomError { - fn from_error_kind(_: I, _: ErrorKind) -> Self { +impl<I> ParserError<I> for CustomError { + fn from_error_kind(_: &I, _: ErrorKind) -> Self { CustomError } - fn append(self, _: I, _: ErrorKind) -> Self { + fn append(self, _: &I, _: ErrorKind) -> Self { CustomError } } @@ -101,14 +103,14 @@ struct CustomError; #[allow(dead_code)] fn custom_error(input: &[u8]) -> IResult<&[u8], &[u8], CustomError> { //fix_error!(input, CustomError<_>, alphanumeric) - crate::ascii::alphanumeric1(input) + crate::ascii::alphanumeric1.parse_peek(input) } #[test] fn test_parser_flat_map() { let input: &[u8] = &[3, 100, 101, 102, 103, 104][..]; assert_parse!( - u8.flat_map(take).parse_next(input), + u8.flat_map(take).parse_peek(input), Ok((&[103, 104][..], &[100, 101, 102][..])) ); } @@ -116,7 +118,7 @@ fn test_parser_flat_map() { #[allow(dead_code)] fn test_closure_compiles_195(input: &[u8]) -> IResult<&[u8], ()> { u8.flat_map(|num| repeat(num as usize, u16(Endianness::Big))) - .parse_next(input) + .parse_peek(input) } #[test] @@ -124,15 +126,15 @@ fn test_parser_verify_map() { let input: &[u8] = &[50][..]; assert_parse!( u8.verify_map(|u| if u < 20 { Some(u) } else { None }) - .parse_next(input), - Err(ErrMode::Backtrack(Error { - input: &[50][..], - kind: ErrorKind::Verify - })) + .parse_peek(input), + Err(ErrMode::Backtrack(InputError::new( + &[50][..], + ErrorKind::Verify + ))) ); assert_parse!( u8.verify_map(|u| if u > 20 { Some(u) } else { None }) - .parse_next(input), + .parse_peek(input), Ok((&[][..], 50)) ); } @@ -141,7 +143,7 @@ fn test_parser_verify_map() { fn test_parser_map_parser() { let input: &[u8] = &[100, 101, 102, 103, 104][..]; assert_parse!( - take(4usize).and_then(take(2usize)).parse_next(input), + take(4usize).and_then(take(2usize)).parse_peek(input), Ok((&[104][..], &[100, 101][..])) ); } @@ -149,11 +151,11 @@ fn test_parser_map_parser() { #[test] #[cfg(feature = "std")] fn test_parser_into() { - use crate::error::Error; + use crate::error::InputError; use crate::token::take; - let mut parser = take::<_, _, Error<_>>(3u8).output_into(); - let result: IResult<&[u8], Vec<u8>> = parser.parse_next(&b"abcdefg"[..]); + let mut parser = take::<_, _, InputError<_>>(3u8).output_into(); + let result: IResult<&[u8], Vec<u8>> = parser.parse_peek(&b"abcdefg"[..]); assert_eq!(result, Ok((&b"defg"[..], vec![97, 98, 99]))); } @@ -161,7 +163,7 @@ fn test_parser_into() { #[test] fn opt_test() { fn opt_abcd(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Option<&[u8]>> { - opt("abcd").parse_next(i) + opt("abcd").parse_peek(i) } let a = &b"abcdef"[..]; @@ -184,7 +186,7 @@ fn opt_test() { #[test] fn peek_test() { fn peek_tag(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - peek("abcd").parse_next(i) + peek("abcd").parse_peek(i) } assert_eq!( @@ -198,7 +200,7 @@ fn peek_test() { assert_eq!( peek_tag(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -207,13 +209,13 @@ fn peek_test() { #[test] fn not_test() { fn not_aaa(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, ()> { - not("aaa").parse_next(i) + not("aaa").parse_peek(i) } assert_eq!( not_aaa(Partial::new(&b"aaa"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"aaa"[..]), + &Partial::new(&b"aaa"[..]), ErrorKind::Not ))) ); @@ -234,7 +236,7 @@ fn test_parser_verify() { fn test(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { take(5u8) .verify(|slice: &[u8]| slice[0] == b'a') - .parse_next(i) + .parse_peek(i) } assert_eq!( test(Partial::new(&b"bcd"[..])), @@ -243,7 +245,7 @@ fn test_parser_verify() { assert_eq!( test(Partial::new(&b"bcdefg"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"bcdefg"[..]), + &Partial::new(&b"bcdefg"[..]), ErrorKind::Verify ))) ); @@ -261,21 +263,21 @@ fn test_parser_verify_ref() { let mut parser1 = take(3u8).verify(|s: &[u8]| s == &b"abc"[..]); assert_eq!( - parser1.parse_next(&b"abcd"[..]), + parser1.parse_peek(&b"abcd"[..]), Ok((&b"d"[..], &b"abc"[..])) ); assert_eq!( - parser1.parse_next(&b"defg"[..]), - Err(ErrMode::Backtrack(Error { - input: &b"defg"[..], - kind: ErrorKind::Verify - })) + parser1.parse_peek(&b"defg"[..]), + Err(ErrMode::Backtrack(InputError::new( + &b"defg"[..], + ErrorKind::Verify + ))) ); fn parser2(i: &[u8]) -> IResult<&[u8], u32> { crate::binary::be_u32 .verify(|val: &u32| *val < 3) - .parse_next(i) + .parse_peek(i) } } @@ -288,15 +290,15 @@ fn test_parser_verify_alloc() { .verify(|s: &[u8]| s == &b"abc"[..]); assert_eq!( - parser1.parse_next(&b"abcd"[..]), + parser1.parse_peek(&b"abcd"[..]), Ok((&b"d"[..], b"abc".to_vec())) ); assert_eq!( - parser1.parse_next(&b"defg"[..]), - Err(ErrMode::Backtrack(Error { - input: &b"defg"[..], - kind: ErrorKind::Verify - })) + parser1.parse_peek(&b"defg"[..]), + Err(ErrMode::Backtrack(InputError::new( + &b"defg"[..], + ErrorKind::Verify + ))) ); } @@ -306,26 +308,20 @@ fn fail_test() { let b = "another string"; assert_eq!( - fail::<_, &str, _>(a), - Err(ErrMode::Backtrack(Error { - input: a, - kind: ErrorKind::Fail - })) + fail::<_, &str, _>.parse_peek(a), + Err(ErrMode::Backtrack(InputError::new(a, ErrorKind::Fail))) ); assert_eq!( - fail::<_, &str, _>(b), - Err(ErrMode::Backtrack(Error { - input: b, - kind: ErrorKind::Fail - })) + fail::<_, &str, _>.parse_peek(b), + Err(ErrMode::Backtrack(InputError::new(b, ErrorKind::Fail))) ); } #[test] fn complete() { fn err_test(i: &[u8]) -> IResult<&[u8], &[u8]> { - let (i, _) = "ijkl".parse_next(i)?; - "mnop".parse_next(i) + let (i, _) = "ijkl".parse_peek(i)?; + "mnop".parse_peek(i) } let a = &b"ijklmn"[..]; @@ -333,7 +329,7 @@ fn complete() { assert_eq!( res_a, Err(ErrMode::Backtrack(error_position!( - &b"mn"[..], + &&b"mn"[..], ErrorKind::Tag ))) ); @@ -343,7 +339,7 @@ fn complete() { fn separated_pair_test() { #[allow(clippy::type_complexity)] fn sep_pair_abc_def(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (&[u8], &[u8])> { - separated_pair("abc", ",", "def").parse_next(i) + separated_pair("abc", ",", "def").parse_peek(i) } assert_eq!( @@ -361,21 +357,21 @@ fn separated_pair_test() { assert_eq!( sep_pair_abc_def(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( sep_pair_abc_def(Partial::new(&b"xxx,def"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx,def"[..]), + &Partial::new(&b"xxx,def"[..]), ErrorKind::Tag ))) ); assert_eq!( sep_pair_abc_def(Partial::new(&b"abc,xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -384,7 +380,7 @@ fn separated_pair_test() { #[test] fn preceded_test() { fn preceded_abcd_efgh(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - preceded("abcd", "efgh").parse_next(i) + preceded("abcd", "efgh").parse_peek(i) } assert_eq!( @@ -402,21 +398,21 @@ fn preceded_test() { assert_eq!( preceded_abcd_efgh(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( preceded_abcd_efgh(Partial::new(&b"xxxxdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxxdef"[..]), + &Partial::new(&b"xxxxdef"[..]), ErrorKind::Tag ))) ); assert_eq!( preceded_abcd_efgh(Partial::new(&b"abcdxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -425,7 +421,7 @@ fn preceded_test() { #[test] fn terminated_test() { fn terminated_abcd_efgh(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - terminated("abcd", "efgh").parse_next(i) + terminated("abcd", "efgh").parse_peek(i) } assert_eq!( @@ -443,21 +439,21 @@ fn terminated_test() { assert_eq!( terminated_abcd_efgh(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( terminated_abcd_efgh(Partial::new(&b"xxxxdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxxdef"[..]), + &Partial::new(&b"xxxxdef"[..]), ErrorKind::Tag ))) ); assert_eq!( terminated_abcd_efgh(Partial::new(&b"abcdxxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxx"[..]), + &Partial::new(&b"xxxx"[..]), ErrorKind::Tag ))) ); @@ -466,7 +462,7 @@ fn terminated_test() { #[test] fn delimited_test() { fn delimited_abc_def_ghi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - delimited("abc", "def", "ghi").parse_next(i) + delimited("abc", "def", "ghi").parse_peek(i) } assert_eq!( @@ -488,28 +484,28 @@ fn delimited_test() { assert_eq!( delimited_abc_def_ghi(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( delimited_abc_def_ghi(Partial::new(&b"xxxdefghi"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxdefghi"[..]), + &Partial::new(&b"xxxdefghi"[..]), ErrorKind::Tag ),)) ); assert_eq!( delimited_abc_def_ghi(Partial::new(&b"abcxxxghi"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxghi"[..]), + &Partial::new(&b"xxxghi"[..]), ErrorKind::Tag ))) ); assert_eq!( delimited_abc_def_ghi(Partial::new(&b"abcdefxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -520,7 +516,7 @@ fn delimited_test() { fn alt_test() { #[cfg(feature = "alloc")] use crate::{ - error::ParseError, + error::ParserError, lib::std::{ fmt::Debug, string::{String, ToString}, @@ -546,12 +542,12 @@ fn alt_test() { } #[cfg(feature = "alloc")] - impl<I: Debug> ParseError<I> for ErrorStr { - fn from_error_kind(input: I, kind: ErrorKind) -> Self { + impl<I: Debug> ParserError<I> for ErrorStr { + fn from_error_kind(input: &I, kind: ErrorKind) -> Self { ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind)) } - fn append(self, input: I, kind: ErrorKind) -> Self { + fn append(self, input: &I, kind: ErrorKind) -> Self { ErrorStr(format!( "custom error message: ({:?}, {:?}) - {:?}", input, kind, self @@ -560,7 +556,7 @@ fn alt_test() { } fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - Ok((&b""[..], input)) + Ok(input.peek_finish()) } #[allow(unused_variables)] @@ -573,13 +569,19 @@ fn alt_test() { } fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, dont_work)).parse_next(i) + alt((unpeek(dont_work), unpeek(dont_work))).parse_peek(i) } fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, work)).parse_next(i) + alt((unpeek(dont_work), unpeek(work))).parse_peek(i) } fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, dont_work, work2, dont_work)).parse_next(i) + alt(( + unpeek(dont_work), + unpeek(dont_work), + unpeek(work2), + unpeek(dont_work), + )) + .parse_peek(i) } //named!(alt1, alt!(dont_work | dont_work)); //named!(alt2, alt!(dont_work | work)); @@ -589,7 +591,7 @@ fn alt_test() { assert_eq!( alt1(a), Err(ErrMode::Backtrack(error_node_position!( - a, + &a, ErrorKind::Alt, ErrorStr("abcd".to_string()) ))) @@ -598,7 +600,7 @@ fn alt_test() { assert_eq!(alt3(a), Ok((a, &b""[..]))); fn alt4(i: &[u8]) -> IResult<&[u8], &[u8]> { - alt(("abcd", "efgh")).parse_next(i) + alt(("abcd", "efgh")).parse_peek(i) } let b = &b"efgh"[..]; assert_eq!(alt4(a), Ok((&b""[..], a))); @@ -608,7 +610,7 @@ fn alt_test() { #[test] fn alt_incomplete() { fn alt1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - alt(("a", "bc", "def")).parse_next(i) + alt(("a", "bc", "def")).parse_peek(i) } let a = &b""[..]; @@ -630,7 +632,7 @@ fn alt_incomplete() { assert_eq!( alt1(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(a), + &Partial::new(a), ErrorKind::Tag ))) ); @@ -650,7 +652,7 @@ fn alt_incomplete() { fn permutation_test() { #[allow(clippy::type_complexity)] fn perm(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (&[u8], &[u8], &[u8])> { - permutation(("abcd", "efg", "hi")).parse_next(i) + permutation(("abcd", "efg", "hi")).parse_peek(i) } let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); @@ -675,9 +677,9 @@ fn permutation_test() { assert_eq!( perm(Partial::new(d)), Err(ErrMode::Backtrack(error_node_position!( - Partial::new(&b"efgxyzabcdefghi"[..]), + &Partial::new(&b"efgxyzabcdefghi"[..]), ErrorKind::Alt, - error_position!(Partial::new(&b"xyzabcdefghi"[..]), ErrorKind::Tag) + error_position!(&Partial::new(&b"xyzabcdefghi"[..]), ErrorKind::Tag) ))) ); @@ -692,13 +694,13 @@ fn permutation_test() { #[cfg(feature = "alloc")] fn separated0_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("abcd", ",").parse_next(i) + separated0("abcd", ",").parse_peek(i) } fn multi_empty(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("", ",").parse_next(i) + separated0("", ",").parse_peek(i) } fn multi_longsep(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("abcd", "..").parse_next(i) + separated0("abcd", "..").parse_peek(i) } let a = &b"abcdef"[..]; @@ -748,7 +750,7 @@ fn separated0_test() { #[cfg_attr(debug_assertions, should_panic)] fn separated0_empty_sep_test() { fn empty_sep(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("abc", "").parse_next(i) + separated0("abc", "").parse_peek(i) } let i = &b"abcabc"[..]; @@ -757,7 +759,7 @@ fn separated0_empty_sep_test() { assert_eq!( empty_sep(Partial::new(i)), Err(ErrMode::Backtrack(error_position!( - Partial::new(i_err_pos), + &Partial::new(i_err_pos), ErrorKind::Assert ))) ); @@ -767,10 +769,10 @@ fn separated0_empty_sep_test() { #[cfg(feature = "alloc")] fn separated1_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated1("abcd", ",").parse_next(i) + separated1("abcd", ",").parse_peek(i) } fn multi_longsep(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated1("abcd", "..").parse_next(i) + separated1("abcd", "..").parse_peek(i) } let a = &b"abcdef"[..]; @@ -789,7 +791,7 @@ fn separated1_test() { assert_eq!( multi(Partial::new(c)), Err(ErrMode::Backtrack(error_position!( - Partial::new(c), + &Partial::new(c), ErrorKind::Tag ))) ); @@ -817,7 +819,7 @@ fn separated1_test() { #[cfg(feature = "alloc")] fn repeat0_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(0.., "abcd").parse_next(i) + repeat(0.., "abcd").parse_peek(i) } assert_eq!( @@ -851,13 +853,13 @@ fn repeat0_test() { #[cfg_attr(debug_assertions, should_panic)] fn repeat0_empty_test() { fn multi_empty(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(0.., "").parse_next(i) + repeat(0.., "").parse_peek(i) } assert_eq!( multi_empty(Partial::new(&b"abcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"abcdef"[..]), + &Partial::new(&b"abcdef"[..]), ErrorKind::Assert ))) ); @@ -867,7 +869,7 @@ fn repeat0_empty_test() { #[cfg(feature = "alloc")] fn repeat1_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(1.., "abcd").parse_next(i) + repeat(1.., "abcd").parse_peek(i) } let a = &b"abcdef"[..]; @@ -885,7 +887,7 @@ fn repeat1_test() { assert_eq!( multi(Partial::new(c)), Err(ErrMode::Backtrack(error_position!( - Partial::new(c), + &Partial::new(c), ErrorKind::Tag ))) ); @@ -900,7 +902,7 @@ fn repeat1_test() { fn repeat_till_test() { #[allow(clippy::type_complexity)] fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> { - repeat_till0("abcd", "efgh").parse_next(i) + repeat_till0("abcd", "efgh").parse_peek(i) } let a = b"abcdabcdefghabcd"; @@ -914,9 +916,9 @@ fn repeat_till_test() { assert_eq!( multi(&c[..]), Err(ErrMode::Backtrack(error_node_position!( - &c[..], + &&c[..], ErrorKind::Many, - error_position!(&c[..], ErrorKind::Tag) + error_position!(&&c[..], ErrorKind::Tag) ))) ); } @@ -926,23 +928,23 @@ fn repeat_till_test() { fn infinite_many() { fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { println!("input: {:?}", input); - Err(ErrMode::Backtrack(error_position!(input, ErrorKind::Tag))) + Err(ErrMode::Backtrack(error_position!(&input, ErrorKind::Tag))) } // should not go into an infinite loop fn multi0(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - repeat(0.., tst).parse_next(i) + repeat(0.., unpeek(tst)).parse_peek(i) } let a = &b"abcdef"[..]; assert_eq!(multi0(a), Ok((a, Vec::new()))); fn multi1(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - repeat(1.., tst).parse_next(i) + repeat(1.., unpeek(tst)).parse_peek(i) } let a = &b"abcdef"[..]; assert_eq!( multi1(a), - Err(ErrMode::Backtrack(error_position!(a, ErrorKind::Tag))) + Err(ErrMode::Backtrack(error_position!(&a, ErrorKind::Tag))) ); } @@ -950,7 +952,7 @@ fn infinite_many() { #[cfg(feature = "alloc")] fn repeat_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(2..=4, "Abcd").parse_next(i) + repeat(2..=4, "Abcd").parse_peek(i) } let a = &b"Abcdef"[..]; @@ -962,7 +964,7 @@ fn repeat_test() { assert_eq!( multi(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"ef"[..]), + &Partial::new(&b"ef"[..]), ErrorKind::Tag ))) ); @@ -992,7 +994,7 @@ fn repeat_test() { fn count_test() { const TIMES: usize = 2; fn cnt_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(TIMES, "abc").parse_next(i) + repeat(TIMES, "abc").parse_peek(i) } assert_eq!( @@ -1010,21 +1012,21 @@ fn count_test() { assert_eq!( cnt_2(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( cnt_2(Partial::new(&b"xxxabcabcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxabcabcdef"[..]), + &Partial::new(&b"xxxabcabcdef"[..]), ErrorKind::Tag ))) ); assert_eq!( cnt_2(Partial::new(&b"abcxxxabcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxabcdef"[..]), + &Partial::new(&b"xxxabcdef"[..]), ErrorKind::Tag ))) ); @@ -1035,7 +1037,7 @@ fn count_test() { fn count_zero() { const TIMES: usize = 0; fn counter_2(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - repeat(TIMES, "abc").parse_next(i) + repeat(TIMES, "abc").parse_peek(i) } let done = &b"abcabcabcdef"[..]; @@ -1078,11 +1080,11 @@ impl<I> From<(I, ErrorKind)> for NilError { } } -impl<I> ParseError<I> for NilError { - fn from_error_kind(_: I, _: ErrorKind) -> NilError { +impl<I> ParserError<I> for NilError { + fn from_error_kind(_: &I, _: ErrorKind) -> NilError { NilError } - fn append(self, _: I, _: ErrorKind) -> NilError { + fn append(self, _: &I, _: ErrorKind) -> NilError { NilError } } @@ -1095,7 +1097,7 @@ fn fold_repeat0_test() { acc } fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(0.., "abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(0.., "abcd", Vec::new, fold_into_vec).parse_peek(i) } assert_eq!( @@ -1133,13 +1135,13 @@ fn fold_repeat0_empty_test() { acc } fn multi_empty(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(0.., "", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(0.., "", Vec::new, fold_into_vec).parse_peek(i) } assert_eq!( multi_empty(Partial::new(&b"abcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"abcdef"[..]), + &Partial::new(&b"abcdef"[..]), ErrorKind::Assert ))) ); @@ -1153,7 +1155,7 @@ fn fold_repeat1_test() { acc } fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(1.., "abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(1.., "abcd", Vec::new, fold_into_vec).parse_peek(i) } let a = &b"abcdef"[..]; @@ -1171,7 +1173,7 @@ fn fold_repeat1_test() { assert_eq!( multi(Partial::new(c)), Err(ErrMode::Backtrack(error_position!( - Partial::new(c), + &Partial::new(c), ErrorKind::Many ))) ); @@ -1189,7 +1191,7 @@ fn fold_repeat_test() { acc } fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(2..=4, "Abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(2..=4, "Abcd", Vec::new, fold_into_vec).parse_peek(i) } let a = &b"Abcdef"[..]; @@ -1201,7 +1203,7 @@ fn fold_repeat_test() { assert_eq!( multi(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"ef"[..]), + &Partial::new(&b"ef"[..]), ErrorKind::Tag ))) ); @@ -1229,7 +1231,7 @@ fn fold_repeat_test() { #[test] fn repeat0_count_test() { fn count0_nums(i: &[u8]) -> IResult<&[u8], usize> { - repeat(0.., (digit, ",")).parse_next(i) + repeat(0.., (digit, ",")).parse_peek(i) } assert_eq!(count0_nums(&b"123,junk"[..]), Ok((&b"junk"[..], 1))); @@ -1247,7 +1249,7 @@ fn repeat0_count_test() { #[test] fn repeat1_count_test() { fn count1_nums(i: &[u8]) -> IResult<&[u8], usize> { - repeat(1.., (digit, ",")).parse_next(i) + repeat(1.., (digit, ",")).parse_peek(i) } assert_eq!(count1_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); @@ -1260,7 +1262,7 @@ fn repeat1_count_test() { assert_eq!( count1_nums(&b"hello"[..]), Err(ErrMode::Backtrack(error_position!( - &b"hello"[..], + &&b"hello"[..], ErrorKind::Slice ))) ); diff --git a/vendor/winnow/src/error.rs b/vendor/winnow/src/error.rs index c92228621..d40e8244b 100644 --- a/vendor/winnow/src/error.rs +++ b/vendor/winnow/src/error.rs @@ -8,14 +8,15 @@ //! - Can be modified according to the user's needs, because some languages need a lot more information //! - Help thread-through the [stream][crate::stream] //! -//! To abstract these needs away from the user, generally `winnow` parsers use the [`IResult`] -//! alias, rather than [`Result`][std::result::Result]. [`finish`][FinishIResult::finish] is -//! provided for top-level parsers to integrate with your application's error reporting. +//! To abstract these needs away from the user, generally `winnow` parsers use the [`PResult`] +//! alias, rather than [`Result`][std::result::Result]. [`Parser::parse`] is a top-level operation +//! that can help convert to a `Result` for integrating with your application's error reporting. //! //! Error types include: //! - `()` -//! - [`Error`] -//! - [`VerboseError`] +//! - [`ErrorKind`] +//! - [`InputError`] (mostly for testing) +//! - [`ContextError`] //! - [Custom errors][crate::_topic::error] #[cfg(feature = "alloc")] @@ -23,8 +24,8 @@ use crate::lib::std::borrow::ToOwned; use crate::lib::std::fmt; use core::num::NonZeroUsize; +use crate::stream::AsBStr; use crate::stream::Stream; -use crate::stream::StreamIsPartial; #[allow(unused_imports)] // Here for intra-doc links use crate::Parser; @@ -33,100 +34,26 @@ use crate::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 [`Error`] +/// By default, the error type (`E`) is [`InputError`] /// -/// At the top-level of your parser, you can use the [`FinishIResult::finish`] method to convert -/// it to a more common result type -pub type IResult<I, O, E = Error<I>> = Result<(I, O), ErrMode<E>>; - -/// Extension trait to convert a parser's [`IResult`] to a more manageable type -#[deprecated(since = "0.4.0", note = "Replaced with `Parser::parse`")] -pub trait FinishIResult<I, O, E> { - /// Converts the parser's [`IResult`] to a type that is more consumable by callers. - /// - /// Errors if the parser is not at the [end of input][crate::combinator::eof]. See - /// [`FinishIResult::finish_err`] if the remaining input is needed. - /// - /// # Panic - /// - /// If the result is `Err(ErrMode::Incomplete(_))`, this method will panic. - /// - **Complete parsers:** It will not be an issue, `Incomplete` is never used - /// - **Partial parsers:** `Incomplete` will be returned if there's not enough data - /// for the parser to decide, and you should gather more data before parsing again. - /// Once the parser returns either `Ok(_)`, `Err(ErrMode::Backtrack(_))` or `Err(ErrMode::Cut(_))`, - /// you can get out of the parsing loop and call `finish_err()` on the parser's result - /// - /// # Example - /// - /// ```rust - /// # #[cfg(feature = "std")] { - /// use winnow::prelude::*; - /// use winnow::ascii::hex_uint; - /// use winnow::error::Error; - /// - /// struct Hex(u64); - /// - /// fn parse(value: &str) -> Result<Hex, Error<String>> { - /// hex_uint.map(Hex).parse_next(value).finish().map_err(Error::into_owned) - /// } - /// # } - /// ``` - #[deprecated(since = "0.4.0", note = "Replaced with `Parser::parse`")] - fn finish(self) -> Result<O, E>; - - /// Converts the parser's [`IResult`] to a type that is more consumable by errors. - /// - /// It keeps the same `Ok` branch, and merges `ErrMode::Backtrack` and `ErrMode::Cut` into the `Err` - /// side. - /// - /// # Panic - /// - /// If the result is `Err(ErrMode::Incomplete(_))`, this method will panic as [`ErrMode::Incomplete`] - /// should only be set when the input is [`StreamIsPartial<false>`] which this isn't implemented - /// for. - #[deprecated(since = "0.4.0", note = "Replaced with `Parser::parse`")] - fn finish_err(self) -> Result<(I, O), E>; -} - -#[allow(deprecated)] -impl<I, O, E> FinishIResult<I, O, E> for IResult<I, O, E> -where - I: Stream, - // Force users to deal with `Incomplete` when `StreamIsPartial<true>` - I: StreamIsPartial, - I: Clone, - E: ParseError<I>, -{ - fn finish(self) -> Result<O, E> { - debug_assert!( - !I::is_partial_supported(), - "partial streams need to handle `ErrMode::Incomplete`" - ); +/// [`Parser::parse`] is a top-level operation that can help convert to a `Result` for integrating +/// with your application's error reporting. +pub type IResult<I, O, E = InputError<I>> = PResult<(I, O), E>; - let (i, o) = self.finish_err()?; - crate::combinator::eof(i).finish_err()?; - Ok(o) - } - - fn finish_err(self) -> Result<(I, O), E> { - debug_assert!( - !I::is_partial_supported(), - "partial streams need to handle `ErrMode::Incomplete`" - ); - - match self { - Ok(res) => Ok(res), - Err(ErrMode::Backtrack(e)) | Err(ErrMode::Cut(e)) => Err(e), - Err(ErrMode::Incomplete(_)) => { - panic!("complete parsers should not report `Err(ErrMode::Incomplete(_))`") - } - } - } -} +/// Holds the result of [`Parser`] +/// +/// - `Ok(O)` is the parsed value +/// - [`Err(ErrMode<E>)`][ErrMode] is the error along with how to respond to it +/// +/// By default, the error type (`E`) is [`ErrorKind`]. +/// +/// [`Parser::parse`] is a top-level operation that can help convert to a `Result` for integrating +/// with your application's error reporting. +pub type PResult<O, E = ContextError> = Result<O, ErrMode<E>>; /// Contains information on needed data if a parser returned `Incomplete` /// -/// **Note:** This is only possible for `Stream` that are [partial][`StreamIsPartial`], +/// **Note:** This is only possible for `Stream` that are [partial][`crate::stream::StreamIsPartial`], /// like [`Partial`][crate::Partial]. #[derive(Debug, PartialEq, Eq, Clone, Copy)] #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] @@ -169,7 +96,7 @@ pub enum ErrMode<E> { /// /// More data needs to be buffered before retrying the parse. /// - /// This must only be set when the [`Stream`] is [partial][`StreamIsPartial`], like with + /// This must only be set when the [`Stream`][crate::stream::Stream] is [partial][`crate::stream::StreamIsPartial`], like with /// [`Partial`][crate::Partial] /// /// Convert this into an `Backtrack` with [`Parser::complete_err`] @@ -232,22 +159,33 @@ impl<E> ErrMode<E> { { self.map(ErrorConvert::convert) } + + /// Unwrap the mode, returning the underlying error + /// + /// Returns `None` for [`ErrMode::Incomplete`] + #[cfg_attr(debug_assertions, track_caller)] + pub fn into_inner(self) -> Option<E> { + match self { + ErrMode::Backtrack(e) | ErrMode::Cut(e) => Some(e), + ErrMode::Incomplete(_) => None, + } + } } -impl<I, E: ParseError<I>> ParseError<I> for ErrMode<E> { - fn from_error_kind(input: I, kind: ErrorKind) -> Self { +impl<I, E: ParserError<I>> ParserError<I> for ErrMode<E> { + fn from_error_kind(input: &I, kind: ErrorKind) -> Self { ErrMode::Backtrack(E::from_error_kind(input, kind)) } #[cfg_attr(debug_assertions, track_caller)] - fn assert(input: I, message: &'static str) -> Self + fn assert(input: &I, message: &'static str) -> Self where I: crate::lib::std::fmt::Debug, { ErrMode::Backtrack(E::assert(input, message)) } - fn append(self, input: I, kind: ErrorKind) -> Self { + fn append(self, input: &I, kind: ErrorKind) -> Self { match self { ErrMode::Backtrack(e) => ErrMode::Backtrack(e.append(input, kind)), e => e, @@ -267,24 +205,24 @@ impl<I, EXT, E> FromExternalError<I, EXT> for ErrMode<E> where E: FromExternalError<I, EXT>, { - fn from_external_error(input: I, kind: ErrorKind, e: EXT) -> Self { + fn from_external_error(input: &I, kind: ErrorKind, e: EXT) -> Self { ErrMode::Backtrack(E::from_external_error(input, kind, e)) } } -impl<T> ErrMode<Error<T>> { - /// Maps `ErrMode<Error<T>>` to `ErrMode<Error<U>>` with the given `F: T -> U` - pub fn map_input<U, F>(self, f: F) -> ErrMode<Error<U>> +impl<T: Clone> ErrMode<InputError<T>> { + /// Maps `ErrMode<InputError<T>>` to `ErrMode<InputError<U>>` with the given `F: T -> U` + pub fn map_input<U: Clone, F>(self, f: F) -> ErrMode<InputError<U>> where F: FnOnce(T) -> U, { match self { ErrMode::Incomplete(n) => ErrMode::Incomplete(n), - ErrMode::Cut(Error { input, kind }) => ErrMode::Cut(Error { + ErrMode::Cut(InputError { input, kind }) => ErrMode::Cut(InputError { input: f(input), kind, }), - ErrMode::Backtrack(Error { input, kind }) => ErrMode::Backtrack(Error { + ErrMode::Backtrack(InputError { input, kind }) => ErrMode::Backtrack(InputError { input: f(input), kind, }), @@ -312,13 +250,13 @@ where /// /// It provides methods to create an error from some combinators, /// and combine existing errors in combinators like `alt`. -pub trait ParseError<I>: Sized { +pub trait ParserError<I>: Sized { /// Creates an error from the input position and an [`ErrorKind`] - fn from_error_kind(input: I, kind: ErrorKind) -> Self; + fn from_error_kind(input: &I, kind: ErrorKind) -> Self; /// Process a parser assertion #[cfg_attr(debug_assertions, track_caller)] - fn assert(input: I, _message: &'static str) -> Self + fn assert(input: &I, _message: &'static str) -> Self where I: crate::lib::std::fmt::Debug, { @@ -328,11 +266,11 @@ pub trait ParseError<I>: Sized { Self::from_error_kind(input, ErrorKind::Assert) } - /// Like [`ParseError::from_error_kind`] but merges it with the existing error. + /// Like [`ParserError::from_error_kind`] but merges it with the existing error. /// /// This is useful when backtracking through a parse tree, accumulating error context on the /// way. - fn append(self, input: I, kind: ErrorKind) -> Self; + fn append(self, input: &I, kind: ErrorKind) -> Self; /// Combines errors from two different parse branches. /// @@ -346,12 +284,13 @@ pub trait ParseError<I>: Sized { /// Used by [`Parser::context`] to add custom data to error while backtracking /// /// May be implemented multiple times for different kinds of context. -pub trait ContextError<I, C = &'static str>: Sized { +pub trait AddContext<I, C = &'static str>: Sized { /// Append to an existing error custom data /// /// This is used mainly by [`Parser::context`], to add user friendly information /// to errors when backtracking through a parse tree - fn add_context(self, _input: I, _ctx: C) -> Self { + #[inline] + fn add_context(self, _input: &I, _ctx: C) -> Self { self } } @@ -360,8 +299,8 @@ pub trait ContextError<I, C = &'static str>: Sized { /// /// This trait is required by the [`Parser::try_map`] combinator. pub trait FromExternalError<I, E> { - /// Like [`ParseError::from_error_kind`] but also include an external error. - fn from_external_error(input: I, kind: ErrorKind, e: E) -> Self; + /// Like [`ParserError::from_error_kind`] but also include an external error. + fn from_external_error(input: &I, kind: ErrorKind, e: E) -> Self; } /// Equivalent of `From` implementation to avoid orphan rules in bits parsers @@ -370,70 +309,85 @@ pub trait ErrorConvert<E> { fn convert(self) -> E; } -/// Default error type, only contains the error' location and kind +/// Capture input on error +/// +/// This is useful for testing of generic parsers to ensure the error happens at the right +/// location. /// -/// This is a low-overhead error that only provides basic information. For less overhead, see -/// `()`. Fore more information, see [`VerboseError`]. -///:w /// **Note:** [context][Parser::context] and inner errors (like from [`Parser::try_map`]) will be /// dropped. #[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct Error<I> { +pub struct InputError<I: Clone> { /// The input stream, pointing to the location where the error occurred pub input: I, /// A rudimentary error kind pub kind: ErrorKind, } -impl<I> Error<I> { +impl<I: Clone> InputError<I> { /// Creates a new basic error - pub fn new(input: I, kind: ErrorKind) -> Error<I> { - Error { input, kind } + #[inline] + pub fn new(input: I, kind: ErrorKind) -> Self { + Self { input, kind } } } #[cfg(feature = "alloc")] -impl<'i, I: ToOwned + ?Sized> Error<&'i I> { +impl<'i, I: Clone + ToOwned + ?Sized> InputError<&'i I> +where + <I as ToOwned>::Owned: Clone, +{ /// Obtaining ownership - pub fn into_owned(self) -> Error<<I as ToOwned>::Owned> { - Error { + pub fn into_owned(self) -> InputError<<I as ToOwned>::Owned> { + InputError { input: self.input.to_owned(), kind: self.kind, } } } -impl<I> ParseError<I> for Error<I> { - fn from_error_kind(input: I, kind: ErrorKind) -> Self { - Error { input, kind } +impl<I: Clone> ParserError<I> for InputError<I> { + #[inline] + fn from_error_kind(input: &I, kind: ErrorKind) -> Self { + Self { + input: input.clone(), + kind, + } } - fn append(self, _: I, _: ErrorKind) -> Self { + #[inline] + fn append(self, _: &I, _: ErrorKind) -> Self { self } } -impl<I, C> ContextError<I, C> for Error<I> {} +impl<I: Clone, C> AddContext<I, C> for InputError<I> {} -impl<I, E> FromExternalError<I, E> for Error<I> { +impl<I: Clone, E> FromExternalError<I, E> for InputError<I> { /// Create a new error from an input position and an external error - fn from_external_error(input: I, kind: ErrorKind, _e: E) -> Self { - Error { input, kind } + #[inline] + fn from_external_error(input: &I, kind: ErrorKind, _e: E) -> Self { + Self { + input: input.clone(), + kind, + } } } -impl<I> ErrorConvert<Error<(I, usize)>> for Error<I> { - fn convert(self) -> Error<(I, usize)> { - Error { +impl<I: Clone> ErrorConvert<InputError<(I, usize)>> for InputError<I> { + #[inline] + fn convert(self) -> InputError<(I, usize)> { + InputError { input: (self.input, 0), kind: self.kind, } } } -impl<I> ErrorConvert<Error<I>> for Error<(I, usize)> { - fn convert(self) -> Error<I> { - Error { +impl<I: Clone> ErrorConvert<InputError<I>> for InputError<(I, usize)> { + #[inline] + fn convert(self) -> InputError<I> { + InputError { input: self.input.0, kind: self.kind, } @@ -441,217 +395,254 @@ impl<I> ErrorConvert<Error<I>> for Error<(I, usize)> { } /// The Display implementation allows the `std::error::Error` implementation -impl<I: fmt::Display> fmt::Display for Error<I> { +impl<I: Clone + fmt::Display> fmt::Display for InputError<I> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "error {:?} at: {}", self.kind, self.input) + write!(f, "{} error starting at: {}", self.kind, self.input) } } #[cfg(feature = "std")] -impl<I: fmt::Debug + fmt::Display + Sync + Send + 'static> std::error::Error for Error<I> {} +impl<I: Clone + fmt::Debug + fmt::Display + Sync + Send + 'static> std::error::Error + for InputError<I> +{ +} -impl<I> ParseError<I> for () { - fn from_error_kind(_: I, _: ErrorKind) -> Self {} +impl<I> ParserError<I> for () { + #[inline] + fn from_error_kind(_: &I, _: ErrorKind) -> Self {} - fn append(self, _: I, _: ErrorKind) -> Self {} + #[inline] + fn append(self, _: &I, _: ErrorKind) -> Self {} } -impl<I, C> ContextError<I, C> for () {} +impl<I, C> AddContext<I, C> for () {} impl<I, E> FromExternalError<I, E> for () { - fn from_external_error(_input: I, _kind: ErrorKind, _e: E) -> Self {} + #[inline] + fn from_external_error(_input: &I, _kind: ErrorKind, _e: E) -> Self {} } impl ErrorConvert<()> for () { + #[inline] fn convert(self) {} } -/// Accumulates error information while backtracking -/// -/// For less overhead (and information), see [`Error`]. -/// -/// [`convert_error`] provides an example of how to render this for end-users. -/// -/// **Note:** This will only capture the last failed branch for combinators like -/// [`alt`][crate::combinator::alt]. -#[cfg(feature = "alloc")] -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct VerboseError<I> { - /// Accumulated error information - pub errors: crate::lib::std::vec::Vec<(I, VerboseErrorKind)>, +/// Accumulate context while backtracking errors +#[derive(Debug)] +pub struct ContextError<C = StrContext> { + #[cfg(feature = "alloc")] + context: crate::lib::std::vec::Vec<C>, + #[cfg(not(feature = "alloc"))] + context: core::marker::PhantomData<C>, + #[cfg(feature = "std")] + cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>, } -#[cfg(feature = "alloc")] -impl<'i, I: ToOwned + ?Sized> VerboseError<&'i I> { - /// Obtaining ownership - pub fn into_owned(self) -> VerboseError<<I as ToOwned>::Owned> { - #[allow(clippy::redundant_clone)] // false positive - VerboseError { - errors: self - .errors - .into_iter() - .map(|(i, k)| (i.to_owned(), k)) - .collect(), +impl<C> ContextError<C> { + /// Create an empty error + #[inline] + pub fn new() -> Self { + Self { + context: Default::default(), + #[cfg(feature = "std")] + cause: None, } } + + /// Access context from [`Parser::context`] + #[inline] + #[cfg(feature = "alloc")] + pub fn context(&self) -> impl Iterator<Item = &C> { + self.context.iter() + } + + /// Originating [`std::error::Error`] + #[inline] + #[cfg(feature = "std")] + pub fn cause(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> { + self.cause.as_deref() + } } -#[cfg(feature = "alloc")] -#[derive(Clone, Debug, Eq, PartialEq)] -/// Error context for `VerboseError` -pub enum VerboseErrorKind { - /// Static string added by the `context` function - Context(&'static str), - /// Error kind given by various parsers - Winnow(ErrorKind), +impl<C> Default for ContextError<C> { + #[inline] + fn default() -> Self { + Self::new() + } } -#[cfg(feature = "alloc")] -impl<I> ParseError<I> for VerboseError<I> { - fn from_error_kind(input: I, kind: ErrorKind) -> Self { - VerboseError { - errors: vec![(input, VerboseErrorKind::Winnow(kind))], - } +impl<I, C> ParserError<I> for ContextError<C> { + #[inline] + fn from_error_kind(_input: &I, _kind: ErrorKind) -> Self { + Self::new() } - fn append(mut self, input: I, kind: ErrorKind) -> Self { - self.errors.push((input, VerboseErrorKind::Winnow(kind))); + #[inline] + fn append(self, _input: &I, _kind: ErrorKind) -> Self { self } -} -#[cfg(feature = "alloc")] -impl<I> ContextError<I, &'static str> for VerboseError<I> { - fn add_context(mut self, input: I, ctx: &'static str) -> Self { - self.errors.push((input, VerboseErrorKind::Context(ctx))); - self + #[inline] + fn or(self, other: Self) -> Self { + other } } -#[cfg(feature = "alloc")] -impl<I, E> FromExternalError<I, E> for VerboseError<I> { - /// Create a new error from an input position and an external error - fn from_external_error(input: I, kind: ErrorKind, _e: E) -> Self { - Self::from_error_kind(input, kind) +impl<C, I> AddContext<I, C> for ContextError<C> { + #[inline] + fn add_context(mut self, _input: &I, ctx: C) -> Self { + #[cfg(feature = "alloc")] + self.context.push(ctx); + self } } -#[cfg(feature = "alloc")] -impl<I> ErrorConvert<VerboseError<I>> for VerboseError<(I, usize)> { - fn convert(self) -> VerboseError<I> { - VerboseError { - errors: self.errors.into_iter().map(|(i, e)| (i.0, e)).collect(), +#[cfg(feature = "std")] +impl<C, I, E: std::error::Error + Send + Sync + 'static> FromExternalError<I, E> + for ContextError<C> +{ + #[inline] + fn from_external_error(_input: &I, _kind: ErrorKind, e: E) -> Self { + let mut err = Self::new(); + { + err.cause = Some(Box::new(e)); } + err } } -#[cfg(feature = "alloc")] -impl<I> ErrorConvert<VerboseError<(I, usize)>> for VerboseError<I> { - fn convert(self) -> VerboseError<(I, usize)> { - VerboseError { - errors: self.errors.into_iter().map(|(i, e)| ((i, 0), e)).collect(), - } +// HACK: This is more general than `std`, making the features non-additive +#[cfg(not(feature = "std"))] +impl<C, I, E: Send + Sync + 'static> FromExternalError<I, E> for ContextError<C> { + #[inline] + fn from_external_error(_input: &I, _kind: ErrorKind, _e: E) -> Self { + let err = Self::new(); + err } } -#[cfg(feature = "alloc")] -impl<I: fmt::Display> fmt::Display for VerboseError<I> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - writeln!(f, "Parse error:")?; - for (input, error) in &self.errors { - match error { - VerboseErrorKind::Winnow(e) => writeln!(f, "{:?} at: {}", e, input)?, - VerboseErrorKind::Context(s) => writeln!(f, "in section '{}', at: {}", s, input)?, +// For tests +impl<C: core::cmp::PartialEq> core::cmp::PartialEq for ContextError<C> { + fn eq(&self, other: &Self) -> bool { + #[cfg(feature = "alloc")] + { + if self.context != other.context { + return false; + } + } + #[cfg(feature = "std")] + { + if self.cause.as_ref().map(ToString::to_string) + != other.cause.as_ref().map(ToString::to_string) + { + return false; } } - Ok(()) + true } } -#[cfg(feature = "std")] -impl<I: fmt::Debug + fmt::Display + Sync + Send + 'static> std::error::Error for VerboseError<I> {} +impl crate::lib::std::fmt::Display for ContextError<StrContext> { + fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result { + #[cfg(feature = "alloc")] + { + let expression = self.context().find_map(|c| match c { + StrContext::Label(c) => Some(c), + _ => None, + }); + let expected = self + .context() + .filter_map(|c| match c { + StrContext::Expected(c) => Some(c), + _ => None, + }) + .collect::<crate::lib::std::vec::Vec<_>>(); -/// Transforms a `VerboseError` into a trace with input position information -#[cfg(feature = "alloc")] -pub fn convert_error<I: core::ops::Deref<Target = str>>( - input: I, - e: VerboseError<I>, -) -> crate::lib::std::string::String { - use crate::lib::std::fmt::Write; - use crate::stream::Offset; + let mut newline = false; - let mut result = crate::lib::std::string::String::new(); + if let Some(expression) = expression { + newline = true; - for (i, (substring, kind)) in e.errors.iter().enumerate() { - let offset = input.offset_to(substring); + write!(f, "invalid {}", expression)?; + } - if input.is_empty() { - match kind { - VerboseErrorKind::Context(s) => { - write!(&mut result, "{}: in {}, got empty input\n\n", i, s) + if !expected.is_empty() { + if newline { + writeln!(f)?; } - VerboseErrorKind::Winnow(e) => { - write!(&mut result, "{}: in {:?}, got empty input\n\n", i, e) + newline = true; + + write!(f, "expected ")?; + for (i, expected) in expected.iter().enumerate() { + if i != 0 { + write!(f, ", ")?; + } + write!(f, "{}", expected)?; } } - } else { - let prefix = &input.as_bytes()[..offset]; - - // Count the number of newlines in the first `offset` bytes of input - let line_number = prefix.iter().filter(|&&b| b == b'\n').count() + 1; - - // Find the line that includes the subslice: - // Find the *last* newline before the substring starts - let line_begin = prefix - .iter() - .rev() - .position(|&b| b == b'\n') - .map(|pos| offset - pos) - .unwrap_or(0); - - // Find the full line after that newline - let line = input[line_begin..] - .lines() - .next() - .unwrap_or(&input[line_begin..]) - .trim_end(); - - // The (1-indexed) column number is the offset of our substring into that line - let column_number = line.offset_to(substring) + 1; - - match kind { - VerboseErrorKind::Context(s) => write!( - &mut result, - "{i}: at line {line_number}, in {context}:\n\ - {line}\n\ - {caret:>column$}\n\n", - i = i, - line_number = line_number, - context = s, - line = line, - caret = '^', - column = column_number, - ), - VerboseErrorKind::Winnow(e) => write!( - &mut result, - "{i}: at line {line_number}, in {kind:?}:\n\ - {line}\n\ - {caret:>column$}\n\n", - i = i, - line_number = line_number, - kind = e, - line = line, - caret = '^', - column = column_number, - ), + #[cfg(feature = "std")] + { + if let Some(cause) = self.cause() { + if newline { + writeln!(f)?; + } + write!(f, "{}", cause)?; + } } } - // Because `write!` to a `String` is infallible, this `unwrap` is fine. - .unwrap(); + + Ok(()) + } +} + +/// Additional parse context for [`ContextError`] added via [`Parser::context`] +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum StrContext { + /// Description of what is currently being parsed + Label(&'static str), + /// Grammar item that was expected + Expected(StrContextValue), +} + +/// See [`StrContext`] +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum StrContextValue { + /// A [`char`] token + CharLiteral(char), + /// A [`&str`] token + StringLiteral(&'static str), + /// A description of what was being parsed + Description(&'static str), +} + +impl From<char> for StrContextValue { + fn from(inner: char) -> Self { + Self::CharLiteral(inner) } +} - result +impl From<&'static str> for StrContextValue { + fn from(inner: &'static str) -> Self { + Self::StringLiteral(inner) + } +} + +impl crate::lib::std::fmt::Display for StrContextValue { + fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result { + match self { + Self::CharLiteral('\n') => "newline".fmt(f), + Self::CharLiteral('`') => "'`'".fmt(f), + Self::CharLiteral(c) if c.is_ascii_control() => { + write!(f, "`{}`", c.escape_debug()) + } + Self::CharLiteral(c) => write!(f, "`{}`", c), + Self::StringLiteral(c) => write!(f, "`{}`", c), + Self::Description(c) => write!(f, "{}", c), + } + } } /// Provide some minor debug context for errors @@ -692,31 +683,255 @@ impl ErrorKind { } } +impl<I> ParserError<I> for ErrorKind { + fn from_error_kind(_input: &I, kind: ErrorKind) -> Self { + kind + } + + fn append(self, _: &I, _: ErrorKind) -> Self { + self + } +} + +impl<I, C> AddContext<I, C> for ErrorKind {} + +impl<I, E> FromExternalError<I, E> for ErrorKind { + /// Create a new error from an input position and an external error + fn from_external_error(_input: &I, kind: ErrorKind, _e: E) -> Self { + kind + } +} + +/// The Display implementation allows the `std::error::Error` implementation +impl fmt::Display for ErrorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "error {:?}", self) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for ErrorKind {} + +/// See [`Parser::parse`] +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ParseError<I, E> { + input: I, + offset: usize, + inner: E, +} + +impl<I: Stream, E: ParserError<I>> ParseError<I, E> { + pub(crate) fn new(mut input: I, start: I::Checkpoint, inner: E) -> Self { + let offset = input.offset_from(&start); + input.reset(start); + Self { + input, + offset, + inner, + } + } +} + +impl<I, E> ParseError<I, E> { + /// The [`Stream`] at the initial location when parsing started + #[inline] + pub fn input(&self) -> &I { + &self.input + } + + /// The location in [`ParseError::input`] where parsing failed + #[inline] + pub fn offset(&self) -> usize { + self.offset + } + + /// The original [`ParserError`] + #[inline] + pub fn inner(&self) -> &E { + &self.inner + } +} + +impl<I, E> core::fmt::Display for ParseError<I, E> +where + I: AsBStr, + E: core::fmt::Display, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let input = self.input.as_bstr(); + let span_start = self.offset; + let span_end = span_start; + #[cfg(feature = "std")] + if input.contains(&b'\n') { + let (line_idx, col_idx) = translate_position(input, span_start); + let line_num = line_idx + 1; + let col_num = col_idx + 1; + let gutter = line_num.to_string().len(); + let content = input + .split(|c| *c == b'\n') + .nth(line_idx) + .expect("valid line number"); + + writeln!(f, "parse error at line {}, column {}", line_num, col_num)?; + // | + for _ in 0..=gutter { + write!(f, " ")?; + } + writeln!(f, "|")?; + + // 1 | 00:32:00.a999999 + write!(f, "{} | ", line_num)?; + writeln!(f, "{}", String::from_utf8_lossy(content))?; + + // | ^ + for _ in 0..=gutter { + write!(f, " ")?; + } + write!(f, "|")?; + for _ in 0..=col_idx { + write!(f, " ")?; + } + // The span will be empty at eof, so we need to make sure we always print at least + // one `^` + write!(f, "^")?; + for _ in (span_start + 1)..(span_end.min(span_start + content.len())) { + write!(f, "^")?; + } + writeln!(f)?; + } else { + let content = input; + writeln!(f, "{}", String::from_utf8_lossy(content))?; + for _ in 0..=span_start { + write!(f, " ")?; + } + // The span will be empty at eof, so we need to make sure we always print at least + // one `^` + write!(f, "^")?; + for _ in (span_start + 1)..(span_end.min(span_start + content.len())) { + write!(f, "^")?; + } + writeln!(f)?; + } + write!(f, "{}", self.inner)?; + + Ok(()) + } +} + +#[cfg(feature = "std")] +fn translate_position(input: &[u8], index: usize) -> (usize, usize) { + if input.is_empty() { + return (0, index); + } + + let safe_index = index.min(input.len() - 1); + let column_offset = index - safe_index; + let index = safe_index; + + let nl = input[0..index] + .iter() + .rev() + .enumerate() + .find(|(_, b)| **b == b'\n') + .map(|(nl, _)| index - nl - 1); + let line_start = match nl { + Some(nl) => nl + 1, + None => 0, + }; + let line = input[0..line_start].iter().filter(|b| **b == b'\n').count(); + let line = line; + + // HACK: This treats byte offset and column offsets the same + let column = std::str::from_utf8(&input[line_start..=index]) + .map(|s| s.chars().count() - 1) + .unwrap_or_else(|_| index - line_start); + let column = column + column_offset; + + (line, column) +} + +#[cfg(test)] +#[cfg(feature = "std")] +mod test_translate_position { + use super::*; + + #[test] + fn empty() { + let input = b""; + let index = 0; + let position = translate_position(&input[..], index); + assert_eq!(position, (0, 0)); + } + + #[test] + fn start() { + let input = b"Hello"; + let index = 0; + let position = translate_position(&input[..], index); + assert_eq!(position, (0, 0)); + } + + #[test] + fn end() { + let input = b"Hello"; + let index = input.len() - 1; + let position = translate_position(&input[..], index); + assert_eq!(position, (0, input.len() - 1)); + } + + #[test] + fn after() { + let input = b"Hello"; + let index = input.len(); + let position = translate_position(&input[..], index); + assert_eq!(position, (0, input.len())); + } + + #[test] + fn first_line() { + let input = b"Hello\nWorld\n"; + let index = 2; + let position = translate_position(&input[..], index); + assert_eq!(position, (0, 2)); + } + + #[test] + fn end_of_line() { + let input = b"Hello\nWorld\n"; + let index = 5; + let position = translate_position(&input[..], index); + assert_eq!(position, (0, 5)); + } + + #[test] + fn start_of_second_line() { + let input = b"Hello\nWorld\n"; + let index = 6; + let position = translate_position(&input[..], index); + assert_eq!(position, (1, 0)); + } + + #[test] + fn second_line() { + let input = b"Hello\nWorld\n"; + let index = 8; + let position = translate_position(&input[..], index); + assert_eq!(position, (1, 2)); + } +} + /// Creates a parse error from a [`ErrorKind`] /// and the position in the input #[cfg(test)] macro_rules! error_position( ($input:expr, $code:expr) => ({ - $crate::error::ParseError::from_error_kind($input, $code) + $crate::error::ParserError::from_error_kind($input, $code) }); ); #[cfg(test)] macro_rules! error_node_position( ($input:expr, $code:expr, $next:expr) => ({ - $crate::error::ParseError::append($next, $input, $code) + $crate::error::ParserError::append($next, $input, $code) }); ); - -#[cfg(test)] -#[cfg(feature = "alloc")] -mod tests { - use super::*; - - #[test] - fn convert_error_panic() { - let input = ""; - - let _result: IResult<_, _, VerboseError<&str>> = 'x'.parse_next(input); - } -} diff --git a/vendor/winnow/src/lib.rs b/vendor/winnow/src/lib.rs index 3b6066230..5614b7f11 100644 --- a/vendor/winnow/src/lib.rs +++ b/vendor/winnow/src/lib.rs @@ -137,7 +137,7 @@ #![allow(clippy::unnested_or_patterns)] #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] #[cfg(feature = "alloc")] -#[macro_use] +#[cfg_attr(test, macro_use)] extern crate alloc; #[cfg(doctest)] extern crate doc_comment; @@ -201,14 +201,7 @@ pub mod stream; pub mod ascii; pub mod binary; -pub mod bits; -pub mod branch; -pub mod bytes; -pub mod character; pub mod combinator; -pub mod multi; -pub mod number; -pub mod sequence; pub mod token; pub mod trace; @@ -220,7 +213,7 @@ pub mod _tutorial; /// Core concepts available for glob import /// /// Including -/// - [`FinishIResult`] +/// - [`StreamIsPartial`][crate::stream::StreamIsPartial] /// - [`Parser`] /// /// ## Example @@ -228,7 +221,7 @@ pub mod _tutorial; /// ```rust /// use winnow::prelude::*; /// -/// fn parse_data(input: &str) -> IResult<&str, u64> { +/// fn parse_data(input: &mut &str) -> PResult<u64> { /// // ... /// # winnow::ascii::dec_uint(input) /// } @@ -240,15 +233,13 @@ pub mod _tutorial; /// ``` pub mod prelude { pub use crate::stream::StreamIsPartial as _; - #[allow(deprecated)] - pub use crate::FinishIResult as _; pub use crate::IResult; + pub use crate::PResult; pub use crate::Parser; } -#[allow(deprecated)] -pub use error::FinishIResult; pub use error::IResult; +pub use error::PResult; pub use parser::*; pub use stream::BStr; pub use stream::Bytes; diff --git a/vendor/winnow/src/macros.rs b/vendor/winnow/src/macros.rs index 8a38ef25c..b3078c605 100644 --- a/vendor/winnow/src/macros.rs +++ b/vendor/winnow/src/macros.rs @@ -17,11 +17,11 @@ /// # use winnow::combinator::success; /// # use winnow::combinator::fail; /// -/// fn escaped(input: &str) -> IResult<&str, char> { +/// fn escaped(input: &mut &str) -> PResult<char> { /// preceded('\\', escape_seq_char).parse_next(input) /// } /// -/// fn escape_seq_char(input: &str) -> IResult<&str, char> { +/// fn escape_seq_char(input: &mut &str) -> PResult<char> { /// dispatch! {any; /// 'b' => success('\u{8}'), /// 'f' => success('\u{c}'), @@ -35,15 +35,15 @@ /// .parse_next(input) /// } /// -/// assert_eq!(escaped.parse_next("\\nHello"), Ok(("Hello", '\n'))); +/// assert_eq!(escaped.parse_peek("\\nHello"), Ok(("Hello", '\n'))); /// ``` #[macro_export] macro_rules! dispatch { ($match_parser: expr; $( $pat:pat $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => { - $crate::trace::trace("dispatch", move |i| + $crate::trace::trace("dispatch", move |i: &mut _| { use $crate::Parser; - let (i, initial) = $match_parser.parse_next(i)?; + let initial = $match_parser.parse_next(i)?; match initial { $( $pat $(if $pred)? => $expr.parse_next(i), diff --git a/vendor/winnow/src/multi.rs b/vendor/winnow/src/multi.rs deleted file mode 100644 index 3e92f70be..000000000 --- a/vendor/winnow/src/multi.rs +++ /dev/null @@ -1,124 +0,0 @@ -//! Deprecated, see [`combinator`] -#![deprecated(since = "0.4.2", note = "Replaced with `combinator`")] - -use crate::binary; -use crate::combinator; -use crate::error::ParseError; -use crate::stream::Accumulate; -use crate::stream::Stream; -use crate::Parser; - -/// Deprecated, replaced by [`combinator::repeat`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat`")] -#[inline(always)] -pub fn many0<I, O, C, E, F>(f: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - combinator::repeat(0.., f) -} - -/// Deprecated, replaced by [`combinator::repeat`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat`")] -#[inline(always)] -pub fn many1<I, O, C, E, F>(f: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - combinator::repeat(1.., f) -} - -/// Deprecated, replaced by [`combinator::repeat_till0`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat_till0`")] -#[inline(always)] -pub fn many_till0<I, O, C, P, E, F, G>(f: F, g: G) -> impl Parser<I, (C, P), E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - G: Parser<I, P, E>, - E: ParseError<I>, -{ - combinator::repeat_till0(f, g) -} - -pub use combinator::separated0; -pub use combinator::separated1; -pub use combinator::separated_foldl1; -#[cfg(feature = "alloc")] -pub use combinator::separated_foldr1; - -/// Deprecated, replaced by [`combinator::repeat`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat`")] -#[inline(always)] -pub fn many_m_n<I, O, C, E, F>(min: usize, max: usize, parse: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - combinator::repeat(min..=max, parse) -} - -#[allow(deprecated)] -pub use combinator::count; -pub use combinator::fill; - -/// Deprecated, replaced by [`combinator::fold_repeat`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_repeat`")] -#[inline(always)] -pub fn fold_many0<I, O, E, F, G, H, R>(f: F, init: H, g: G) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - combinator::fold_repeat(0.., f, init, g) -} - -/// Deprecated, replaced by [`combinator::fold_repeat`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_repeat`")] -#[inline(always)] -pub fn fold_many1<I, O, E, F, G, H, R>(f: F, init: H, g: G) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - combinator::fold_repeat(1.., f, init, g) -} - -/// Deprecated, replaced by [`combinator::fold_repeat`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_repeat`")] -#[inline(always)] -pub fn fold_many_m_n<I, O, E, F, G, H, R>( - min: usize, - max: usize, - parse: F, - init: H, - fold: G, -) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - combinator::fold_repeat(min..=max, parse, init, fold) -} - -pub use binary::length_count; -pub use binary::length_data; -pub use binary::length_value; diff --git a/vendor/winnow/src/number.rs b/vendor/winnow/src/number.rs deleted file mode 100644 index 2aa8da425..000000000 --- a/vendor/winnow/src/number.rs +++ /dev/null @@ -1,509 +0,0 @@ -//! Deprecated, see [`binary`] -#![deprecated(since = "0.4.2", note = "Replaced with `binary`")] -#![allow(clippy::match_same_arms)] - -use crate::binary; -use crate::error::ParseError; -use crate::stream::{AsBytes, Stream, StreamIsPartial}; -use crate::IResult; -use crate::Parser; - -pub use binary::Endianness; - -/// Deprecated, see [`binary::be_u8`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_u8`")] -#[inline(always)] -pub fn be_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, -{ - binary::be_u8(input) -} - -/// Deprecated, see [`binary::be_u16`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_u16`")] -#[inline(always)] -pub fn be_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_u16(input) -} - -/// Deprecated, see [`binary::be_u24`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_u24`")] -#[inline(always)] -pub fn be_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_u24(input) -} - -/// Deprecated, see [`binary::be_u32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_u32`")] -#[inline(always)] -pub fn be_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_u32(input) -} - -/// Deprecated, see [`binary::be_u64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_u64`")] -#[inline(always)] -pub fn be_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_u64(input) -} - -/// Deprecated, see [`binary::be_u128`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_u128`")] -#[inline(always)] -pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_u128(input) -} - -/// Deprecated, see [`binary::be_i8`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_i8`")] -#[inline(always)] -pub fn be_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, -{ - binary::be_i8(input) -} - -/// Deprecated, see [`binary::be_i16`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_i16`")] -#[inline(always)] -pub fn be_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_i16(input) -} - -/// Deprecated, see [`binary::be_i24`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_i24`")] -#[inline(always)] -pub fn be_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_i24(input) -} - -/// Deprecated, see [`binary::be_i32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_i32`")] -#[inline(always)] -pub fn be_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_i32(input) -} - -/// Deprecated, see [`binary::be_i64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_i64`")] -#[inline(always)] -pub fn be_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_i64(input) -} - -/// Deprecated, see [`binary::be_i128`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_i128`")] -#[inline(always)] -pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_i128(input) -} - -/// Deprecated, see [`binary::le_u8`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_u8`")] -#[inline(always)] -pub fn le_u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, -{ - binary::le_u8(input) -} - -/// Deprecated, see [`binary::le_u16`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_u16`")] -#[inline(always)] -pub fn le_u16<I, E: ParseError<I>>(input: I) -> IResult<I, u16, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_u16(input) -} - -/// Deprecated, see [`binary::le_u24`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_u24`")] -#[inline(always)] -pub fn le_u24<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_u24(input) -} - -/// Deprecated, see [`binary::le_u32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_u32`")] -#[inline(always)] -pub fn le_u32<I, E: ParseError<I>>(input: I) -> IResult<I, u32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_u32(input) -} - -/// Deprecated, see [`binary::le_u64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_u64`")] -#[inline(always)] -pub fn le_u64<I, E: ParseError<I>>(input: I) -> IResult<I, u64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_u64(input) -} - -/// Deprecated, see [`binary::le_u128`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_u128`")] -#[inline(always)] -pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_u128(input) -} - -/// Deprecated, see [`binary::le_i8`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_i8`")] -#[inline(always)] -pub fn le_i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, -{ - binary::le_i8(input) -} - -/// Deprecated, see [`binary::le_i16`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_i16`")] -#[inline(always)] -pub fn le_i16<I, E: ParseError<I>>(input: I) -> IResult<I, i16, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_i16(input) -} - -/// Deprecated, see [`binary::le_i24`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_i24`")] -#[inline(always)] -pub fn le_i24<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_i24(input) -} - -/// Deprecated, see [`binary::le_i32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_i32`")] -#[inline(always)] -pub fn le_i32<I, E: ParseError<I>>(input: I) -> IResult<I, i32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_i32(input) -} - -/// Deprecated, see [`binary::le_i64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_i64`")] -#[inline(always)] -pub fn le_i64<I, E: ParseError<I>>(input: I) -> IResult<I, i64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_i64(input) -} - -/// Deprecated, see [`binary::le_i128`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_i128`")] -#[inline(always)] -pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_i128(input) -} - -/// Deprecated, see [`binary::u8`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::u8`")] -#[inline(always)] -pub fn u8<I, E: ParseError<I>>(input: I) -> IResult<I, u8, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, -{ - binary::u8.parse_next(input) -} - -/// Deprecated, see [`binary::u16`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::u16`")] -#[inline(always)] -pub fn u16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, u16, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::u16(endian) -} - -/// Deprecated, see [`binary::u24`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::u24`")] -#[inline(always)] -pub fn u24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, u32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::u24(endian) -} - -/// Deprecated, see [`binary::u32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::u32`")] -#[inline(always)] -pub fn u32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, u32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::u32(endian) -} - -/// Deprecated, see [`binary::u64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::u64`")] -#[inline(always)] -pub fn u64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, u64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::u64(endian) -} - -/// Deprecated, see [`binary::u128`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::u128`")] -#[inline(always)] -pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, u128, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::u128(endian) -} - -/// Deprecated, see [`binary::i8`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::i8`")] -#[inline(always)] -pub fn i8<I, E: ParseError<I>>(input: I) -> IResult<I, i8, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, -{ - binary::i8.parse_next(input) -} - -/// Deprecated, see [`binary::i16`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::i16`")] -#[inline(always)] -pub fn i16<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, i16, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::i16(endian) -} - -/// Deprecated, see [`binary::i24`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::i24`")] -#[inline(always)] -pub fn i24<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, i32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::i24(endian) -} - -/// Deprecated, see [`binary::i32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::i32`")] -#[inline(always)] -pub fn i32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, i32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::i32(endian) -} - -/// Deprecated, see [`binary::i64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::i64`")] -#[inline(always)] -pub fn i64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, i64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::i64(endian) -} - -/// Deprecated, see [`binary::i128`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::i128`")] -#[inline(always)] -pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, i128, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::i128(endian) -} - -/// Deprecated, see [`binary::be_f32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_f32`")] -#[inline(always)] -pub fn be_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_f32(input) -} - -/// Deprecated, see [`binary::be_f64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::be_f64`")] -#[inline(always)] -pub fn be_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::be_f64(input) -} - -/// Deprecated, see [`binary::le_f32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_f32`")] -#[inline(always)] -pub fn le_f32<I, E: ParseError<I>>(input: I) -> IResult<I, f32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_f32(input) -} - -/// Deprecated, see [`binary::le_f64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::le_f64`")] -#[inline(always)] -pub fn le_f64<I, E: ParseError<I>>(input: I) -> IResult<I, f64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::le_f64(input) -} - -/// Deprecated, see [`binary::f32`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::f32`")] -#[inline(always)] -pub fn f32<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, f32, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::f32(endian) -} - -/// Deprecated, see [`binary::f64`] -#[deprecated(since = "0.4.2", note = "Replaced with `binary::f64`")] -#[inline(always)] -pub fn f64<I, E: ParseError<I>>(endian: crate::number::Endianness) -> impl Parser<I, f64, E> -where - I: StreamIsPartial, - I: Stream<Token = u8>, - <I as Stream>::Slice: AsBytes, -{ - binary::f64(endian) -} diff --git a/vendor/winnow/src/parser.rs b/vendor/winnow/src/parser.rs index 15d892864..d160a8ef8 100644 --- a/vendor/winnow/src/parser.rs +++ b/vendor/winnow/src/parser.rs @@ -1,8 +1,8 @@ //! Basic types to build the parsers use crate::combinator::*; -use crate::error::{ContextError, FromExternalError, IResult, ParseError}; -use crate::stream::{AsChar, Compare, Location, Offset, ParseSlice, Stream, StreamIsPartial}; +use crate::error::{AddContext, FromExternalError, IResult, PResult, ParseError, ParserError}; +use crate::stream::{AsChar, Compare, Location, ParseSlice, Stream, StreamIsPartial}; /// Core trait for parsing /// @@ -10,12 +10,12 @@ use crate::stream::{AsChar, Compare, Location, Offset, ParseSlice, Stream, Strea /// ```rust /// use winnow::prelude::*; /// -/// fn success(input: &str) -> IResult<&str, ()> { +/// fn success(input: &mut &str) -> PResult<()> { /// let output = (); -/// Ok((input, output)) +/// Ok(output) /// } /// -/// let (input, output) = success.parse_next("Hello").unwrap(); +/// let (input, output) = success.parse_peek("Hello").unwrap(); /// assert_eq!(input, "Hello"); // We didn't consume any input /// ``` /// @@ -23,14 +23,14 @@ use crate::stream::{AsChar, Compare, Location, Offset, ParseSlice, Stream, Strea /// ```rust /// use winnow::prelude::*; /// -/// fn success<O: Clone>(output: O) -> impl FnMut(&str) -> IResult<&str, O> { -/// move |input: &str| { +/// fn success<O: Clone>(output: O) -> impl FnMut(&mut &str) -> PResult<O> { +/// move |input: &mut &str| { /// let output = output.clone(); -/// Ok((input, output)) +/// Ok(output) /// } /// } /// -/// let (input, output) = success("World").parse_next("Hello").unwrap(); +/// let (input, output) = success("World").parse_peek("Hello").unwrap(); /// assert_eq!(input, "Hello"); // We didn't consume any input /// assert_eq!(output, "World"); /// ``` @@ -41,23 +41,49 @@ use crate::stream::{AsChar, Compare, Location, Offset, ParseSlice, Stream, Strea pub trait Parser<I, O, E> { /// Parse all of `input`, generating `O` from it #[inline] - fn parse(&mut self, input: I) -> Result<O, E> + fn parse(&mut self, mut input: I) -> Result<O, ParseError<I, E>> where + Self: core::marker::Sized, I: Stream, // Force users to deal with `Incomplete` when `StreamIsPartial<true>` I: StreamIsPartial, I: Clone, - E: ParseError<I>, + E: ParserError<I>, { - #![allow(deprecated)] - use crate::error::FinishIResult; - self.parse_next(input).finish() + debug_assert!( + !I::is_partial_supported(), + "partial streams need to handle `ErrMode::Incomplete`" + ); + + let start = input.checkpoint(); + let (o, _) = (self.by_ref(), crate::combinator::eof) + .parse_next(&mut input) + .map_err(|e| { + let e = e + .into_inner() + .expect("complete parsers should not report `ErrMode::Incomplete(_)`"); + ParseError::new(input, start, e) + })?; + Ok(o) } /// Take tokens from the [`Stream`], turning it into the output /// /// This includes advancing the [`Stream`] to the next location. - fn parse_next(&mut self, input: I) -> IResult<I, O, E>; + /// + /// On error, `input` will be left pointing at the error location. + fn parse_next(&mut self, input: &mut I) -> PResult<O, E>; + + /// Take tokens from the [`Stream`], turning it into the output + /// + /// This includes advancing the [`Stream`] to the next location. + #[inline(always)] + fn parse_peek(&mut self, mut input: I) -> IResult<I, O, E> { + match self.parse_next(&mut input) { + Ok(o) => Ok((input, o)), + Err(err) => Err(err), + } + } /// Treat `&mut Self` as a parser /// @@ -70,18 +96,17 @@ pub trait Parser<I, O, E> { /// [`Parser::complete_err`]: /// ```rust,compile_fail /// # use winnow::prelude::*; - /// # use winnow::IResult; /// # use winnow::Parser; - /// # use winnow::error::ParseError; + /// # use winnow::error::ParserError; /// # use winnow::binary::length_data; - /// pub fn length_value<'i, O, E: ParseError<&'i [u8]>>( + /// pub fn length_value<'i, O, E: ParserError<&'i [u8]>>( /// mut f: impl Parser<&'i [u8], usize, E>, /// mut g: impl Parser<&'i [u8], O, E> - /// ) -> impl FnMut(&'i [u8]) -> IResult<&'i [u8], O, E> { - /// move |i: &'i [u8]| { - /// let (i, data) = length_data(f).parse_next(i)?; - /// let (_, o) = g.complete().parse_next(data)?; - /// Ok((i, o)) + /// ) -> impl Parser<&'i [u8], O, E> { + /// move |i: &mut &'i [u8]| { + /// let mut data = length_data(f).parse_next(i)?; + /// let o = g.complete_err().parse_next(&mut data)?; + /// Ok(o) /// } /// } /// ``` @@ -89,18 +114,17 @@ pub trait Parser<I, O, E> { /// By adding `by_ref`, we can make this work: /// ```rust /// # use winnow::prelude::*; - /// # use winnow::IResult; /// # use winnow::Parser; - /// # use winnow::error::ParseError; + /// # use winnow::error::ParserError; /// # use winnow::binary::length_data; - /// pub fn length_value<'i, O, E: ParseError<&'i [u8]>>( + /// pub fn length_value<'i, O, E: ParserError<&'i [u8]>>( /// mut f: impl Parser<&'i [u8], usize, E>, /// mut g: impl Parser<&'i [u8], O, E> - /// ) -> impl FnMut(&'i [u8]) -> IResult<&'i [u8], O, E> { - /// move |i: &'i [u8]| { - /// let (i, data) = length_data(f.by_ref()).parse_next(i)?; - /// let (_, o) = g.by_ref().complete_err().parse_next(data)?; - /// Ok((i, o)) + /// ) -> impl Parser<&'i [u8], O, E> { + /// move |i: &mut &'i [u8]| { + /// let mut data = length_data(f.by_ref()).parse_next(i)?; + /// let o = g.by_ref().complete_err().parse_next(&mut data)?; + /// Ok(o) /// } /// } /// ``` @@ -116,14 +140,14 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::alpha1; /// # fn main() { /// /// let mut parser = alpha1.value(1234); /// - /// assert_eq!(parser.parse_next("abcd"), Ok(("", 1234))); - /// assert_eq!(parser.parse_next("123abcd;"), Err(ErrMode::Backtrack(Error::new("123abcd;", ErrorKind::Slice)))); + /// assert_eq!(parser.parse_peek("abcd"), Ok(("", 1234))); + /// assert_eq!(parser.parse_peek("123abcd;"), Err(ErrMode::Backtrack(InputError::new("123abcd;", ErrorKind::Slice)))); /// # } /// ``` #[doc(alias = "to")] @@ -140,14 +164,14 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::alpha1; /// # fn main() { /// /// let mut parser = alpha1.void(); /// - /// assert_eq!(parser.parse_next("abcd"), Ok(("", ()))); - /// assert_eq!(parser.parse_next("123abcd;"), Err(ErrMode::Backtrack(Error::new("123abcd;", ErrorKind::Slice)))); + /// assert_eq!(parser.parse_peek("abcd"), Ok(("", ()))); + /// assert_eq!(parser.parse_peek("123abcd;"), Err(ErrMode::Backtrack(InputError::new("123abcd;", ErrorKind::Slice)))); /// # } /// ``` fn void(self) -> Void<Self, I, O, E> @@ -162,19 +186,19 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::IResult; - /// # use winnow::Parser; + /// # use winnow::prelude::*; + /// # use winnow::error::InputError; /// use winnow::ascii::alpha1; /// # fn main() { /// - /// fn parser1(i: &str) -> IResult<&str, &str> { + /// fn parser1<'s>(i: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { /// alpha1(i) /// } /// /// let mut parser2 = parser1.output_into(); /// /// // the parser converts the &str output of the child parser into a Vec<u8> - /// let bytes: IResult<&str, Vec<u8>> = parser2.parse_next("abcd"); + /// let bytes: IResult<&str, Vec<u8>> = parser2.parse_peek("abcd"); /// assert_eq!(bytes, Ok(("", vec![97, 98, 99, 100]))); /// # } /// ``` @@ -191,22 +215,22 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::{alpha1}; /// use winnow::combinator::separated_pair; /// # fn main() { /// /// let mut parser = separated_pair(alpha1, ',', alpha1).recognize(); /// - /// assert_eq!(parser.parse_next("abcd,efgh"), Ok(("", "abcd,efgh"))); - /// assert_eq!(parser.parse_next("abcd;"),Err(ErrMode::Backtrack(Error::new(";", ErrorKind::Verify)))); + /// assert_eq!(parser.parse_peek("abcd,efgh"), Ok(("", "abcd,efgh"))); + /// assert_eq!(parser.parse_peek("abcd;"),Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Verify)))); /// # } /// ``` #[doc(alias = "concat")] fn recognize(self) -> Recognize<Self, I, O, E> where Self: core::marker::Sized, - I: Stream + Offset, + I: Stream, { Recognize::new(self) } @@ -225,36 +249,33 @@ pub trait Parser<I, O, E> { /// /// ```rust /// # use winnow::prelude::*; - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError}; /// use winnow::ascii::{alpha1}; /// use winnow::token::tag; /// use winnow::combinator::separated_pair; /// - /// fn inner_parser(input: &str) -> IResult<&str, bool> { + /// fn inner_parser<'s>(input: &mut &'s str) -> PResult<bool, InputError<&'s str>> { /// "1234".value(true).parse_next(input) /// } /// - /// # fn main() { - /// /// let mut consumed_parser = separated_pair(alpha1, ',', alpha1).value(true).with_recognized(); /// - /// assert_eq!(consumed_parser.parse_next("abcd,efgh1"), Ok(("1", (true, "abcd,efgh")))); - /// assert_eq!(consumed_parser.parse_next("abcd;"),Err(ErrMode::Backtrack(Error::new(";", ErrorKind::Verify)))); + /// assert_eq!(consumed_parser.parse_peek("abcd,efgh1"), Ok(("1", (true, "abcd,efgh")))); + /// assert_eq!(consumed_parser.parse_peek("abcd;"),Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Verify)))); /// /// // the second output (representing the consumed input) /// // should be the same as that of the `recognize` parser. /// let mut recognize_parser = inner_parser.recognize(); /// let mut consumed_parser = inner_parser.with_recognized().map(|(output, consumed)| consumed); /// - /// assert_eq!(recognize_parser.parse_next("1234"), consumed_parser.parse_next("1234")); - /// assert_eq!(recognize_parser.parse_next("abcd"), consumed_parser.parse_next("abcd")); - /// # } + /// assert_eq!(recognize_parser.parse_peek("1234"), consumed_parser.parse_peek("1234")); + /// assert_eq!(recognize_parser.parse_peek("abcd"), consumed_parser.parse_peek("abcd")); /// ``` #[doc(alias = "consumed")] fn with_recognized(self) -> WithRecognized<Self, I, O, E> where Self: core::marker::Sized, - I: Stream + Offset, + I: Stream, { WithRecognized::new(self) } @@ -265,7 +286,7 @@ pub trait Parser<I, O, E> { /// /// ```rust /// # use winnow::prelude::*; - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, stream::Stream}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, stream::Stream}; /// use winnow::stream::Located; /// use winnow::ascii::alpha1; /// use winnow::combinator::separated_pair; @@ -273,7 +294,7 @@ pub trait Parser<I, O, E> { /// let mut parser = separated_pair(alpha1.span(), ',', alpha1.span()); /// /// assert_eq!(parser.parse(Located::new("abcd,efgh")), Ok((0..4, 5..9))); - /// assert_eq!(parser.parse_next(Located::new("abcd;")),Err(ErrMode::Backtrack(Error::new(Located::new("abcd;").next_slice(4).0, ErrorKind::Verify)))); + /// assert_eq!(parser.parse_peek(Located::new("abcd;")),Err(ErrMode::Backtrack(InputError::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Verify)))); /// ``` fn span(self) -> Span<Self, I, O, E> where @@ -297,13 +318,13 @@ pub trait Parser<I, O, E> { /// /// ```rust /// # use winnow::prelude::*; - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, stream::Stream}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, stream::Stream}; /// use winnow::stream::Located; /// use winnow::ascii::alpha1; /// use winnow::token::tag; /// use winnow::combinator::separated_pair; /// - /// fn inner_parser(input: Located<&str>) -> IResult<Located<&str>, bool> { + /// fn inner_parser<'s>(input: &mut Located<&'s str>) -> PResult<bool, InputError<Located<&'s str>>> { /// "1234".value(true).parse_next(input) /// } /// @@ -312,15 +333,15 @@ pub trait Parser<I, O, E> { /// let mut consumed_parser = separated_pair(alpha1.value(1).with_span(), ',', alpha1.value(2).with_span()); /// /// assert_eq!(consumed_parser.parse(Located::new("abcd,efgh")), Ok(((1, 0..4), (2, 5..9)))); - /// assert_eq!(consumed_parser.parse_next(Located::new("abcd;")),Err(ErrMode::Backtrack(Error::new(Located::new("abcd;").next_slice(4).0, ErrorKind::Verify)))); + /// assert_eq!(consumed_parser.parse_peek(Located::new("abcd;")),Err(ErrMode::Backtrack(InputError::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Verify)))); /// /// // the second output (representing the consumed input) /// // should be the same as that of the `span` parser. /// let mut recognize_parser = inner_parser.span(); /// let mut consumed_parser = inner_parser.with_span().map(|(output, consumed)| consumed); /// - /// assert_eq!(recognize_parser.parse_next(Located::new("1234")), consumed_parser.parse_next(Located::new("1234"))); - /// assert_eq!(recognize_parser.parse_next(Located::new("abcd")), consumed_parser.parse_next(Located::new("abcd"))); + /// assert_eq!(recognize_parser.parse_peek(Located::new("1234")), consumed_parser.parse_peek(Located::new("1234"))); + /// assert_eq!(recognize_parser.parse_peek(Located::new("abcd")), consumed_parser.parse_peek(Located::new("abcd"))); /// # } /// ``` fn with_span(self) -> WithSpan<Self, I, O, E> @@ -336,17 +357,17 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult,Parser}; + /// use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::digit1; /// # fn main() { /// /// let mut parser = digit1.map(|s: &str| s.len()); /// /// // the parser will count how many characters were returned by digit1 - /// assert_eq!(parser.parse_next("123456"), Ok(("", 6))); + /// assert_eq!(parser.parse_peek("123456"), Ok(("", 6))); /// /// // this will fail if digit1 fails - /// assert_eq!(parser.parse_next("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Slice)))); + /// assert_eq!(parser.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Slice)))); /// # } /// ``` fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E> @@ -362,63 +383,51 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::digit1; /// # fn main() { /// /// let mut parse = digit1.try_map(|s: &str| s.parse::<u8>()); /// /// // the parser will convert the result of digit1 to a number - /// assert_eq!(parse.parse_next("123"), Ok(("", 123))); + /// assert_eq!(parse.parse_peek("123"), Ok(("", 123))); /// /// // this will fail if digit1 fails - /// assert_eq!(parse.parse_next("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Slice)))); + /// assert_eq!(parse.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Slice)))); /// /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) - /// assert_eq!(parse.parse_next("123456"), Err(ErrMode::Backtrack(Error::new("123456", ErrorKind::Verify)))); + /// assert_eq!(parse.parse_peek("123456"), Err(ErrMode::Backtrack(InputError::new("123456", ErrorKind::Verify)))); /// # } /// ``` fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2> where Self: core::marker::Sized, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { TryMap::new(self, map) } - /// Deprecated, see [`Parser::try_map`] - #[deprecated(since = "0.4.2", note = "Replaced with `Parser::try_map`")] - fn map_res<G, O2, E2>(self, map: G) -> MapRes<Self, G, I, O, O2, E, E2> - where - Self: core::marker::Sized, - G: FnMut(O) -> Result<O2, E2>, - I: Clone, - E: FromExternalError<I, E2>, - { - self.try_map(map) - } - /// Apply both [`Parser::verify`] and [`Parser::map`]. /// /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::digit1; /// # fn main() { /// /// let mut parse = digit1.verify_map(|s: &str| s.parse::<u8>().ok()); /// /// // the parser will convert the result of digit1 to a number - /// assert_eq!(parse.parse_next("123"), Ok(("", 123))); + /// assert_eq!(parse.parse_peek("123"), Ok(("", 123))); /// /// // this will fail if digit1 fails - /// assert_eq!(parse.parse_next("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Slice)))); + /// assert_eq!(parse.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Slice)))); /// /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) - /// assert_eq!(parse.parse_next("123456"), Err(ErrMode::Backtrack(Error::new("123456", ErrorKind::Verify)))); + /// assert_eq!(parse.parse_peek("123456"), Err(ErrMode::Backtrack(InputError::new("123456", ErrorKind::Verify)))); /// # } /// ``` #[doc(alias = "satisfy_map")] @@ -428,8 +437,8 @@ pub trait Parser<I, O, E> { where Self: core::marker::Sized, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, + I: Stream, + E: ParserError<I>, { VerifyMap::new(self, map) } @@ -439,32 +448,32 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, PResult, Parser}; /// use winnow::token::take; /// use winnow::binary::u8; /// - /// fn length_data(input: &[u8]) -> IResult<&[u8], &[u8]> { + /// fn length_data<'s>(input: &mut &'s [u8]) -> PResult<&'s [u8], InputError<&'s [u8]>> { /// u8.flat_map(take).parse_next(input) /// } /// - /// assert_eq!(length_data.parse_next(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..]))); - /// assert_eq!(length_data.parse_next(&[4, 0, 1, 2][..]), Err(ErrMode::Backtrack(Error::new(&[0, 1, 2][..], ErrorKind::Slice)))); + /// assert_eq!(length_data.parse_peek(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..]))); + /// assert_eq!(length_data.parse_peek(&[4, 0, 1, 2][..]), Err(ErrMode::Backtrack(InputError::new(&[0, 1, 2][..], ErrorKind::Slice)))); /// ``` /// /// which is the same as /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, PResult, Parser}; /// use winnow::token::take; /// use winnow::binary::u8; /// - /// fn length_data(input: &[u8]) -> IResult<&[u8], &[u8]> { - /// let (input, length) = u8.parse_next(input)?; - /// let (input, data) = take(length).parse_next(input)?; - /// Ok((input, data)) + /// fn length_data<'s>(input: &mut &'s [u8]) -> PResult<&'s [u8], InputError<&'s [u8]>> { + /// let length = u8.parse_next(input)?; + /// let data = take(length).parse_next(input)?; + /// Ok(data) /// } /// - /// assert_eq!(length_data.parse_next(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..]))); - /// assert_eq!(length_data.parse_next(&[4, 0, 1, 2][..]), Err(ErrMode::Backtrack(Error::new(&[0, 1, 2][..], ErrorKind::Slice)))); + /// assert_eq!(length_data.parse_peek(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..]))); + /// assert_eq!(length_data.parse_peek(&[4, 0, 1, 2][..]), Err(ErrMode::Backtrack(InputError::new(&[0, 1, 2][..], ErrorKind::Slice)))); /// ``` fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E> where @@ -480,16 +489,16 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::digit1; /// use winnow::token::take; /// # fn main() { /// /// let mut digits = take(5u8).and_then(digit1); /// - /// assert_eq!(digits.parse_next("12345"), Ok(("", "12345"))); - /// assert_eq!(digits.parse_next("123ab"), Ok(("", "123"))); - /// assert_eq!(digits.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Slice)))); + /// assert_eq!(digits.parse_peek("12345"), Ok(("", "12345"))); + /// assert_eq!(digits.parse_peek("123ab"), Ok(("", "123"))); + /// assert_eq!(digits.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Slice)))); /// # } /// ``` fn and_then<G, O2>(self, inner: G) -> AndThen<Self, G, I, O, O2, E> @@ -497,6 +506,7 @@ pub trait Parser<I, O, E> { Self: core::marker::Sized, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { AndThen::new(self, inner) } @@ -507,18 +517,18 @@ pub trait Parser<I, O, E> { /// /// ```rust /// # use winnow::prelude::*; - /// use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult,Parser}; + /// use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// use winnow::ascii::digit1; /// - /// fn parser(input: &str) -> IResult<&str, u64> { + /// fn parser<'s>(input: &mut &'s str) -> PResult<u64, InputError<&'s str>> { /// digit1.parse_to().parse_next(input) /// } /// /// // the parser will count how many characters were returned by digit1 - /// assert_eq!(parser.parse_next("123456"), Ok(("", 123456))); + /// assert_eq!(parser.parse_peek("123456"), Ok(("", 123456))); /// /// // this will fail if digit1 fails - /// assert_eq!(parser.parse_next("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Slice)))); + /// assert_eq!(parser.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Slice)))); /// ``` #[doc(alias = "from_str")] fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E> @@ -526,7 +536,7 @@ pub trait Parser<I, O, E> { Self: core::marker::Sized, I: Stream, O: ParseSlice<O2>, - E: ParseError<I>, + E: ParserError<I>, { ParseTo::new(self) } @@ -539,15 +549,15 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, error::InputError, Parser}; /// # use winnow::ascii::alpha1; /// # fn main() { /// /// let mut parser = alpha1.verify(|s: &str| s.len() == 4); /// - /// assert_eq!(parser.parse_next("abcd"), Ok(("", "abcd"))); - /// assert_eq!(parser.parse_next("abcde"), Err(ErrMode::Backtrack(Error::new("abcde", ErrorKind::Verify)))); - /// assert_eq!(parser.parse_next("123abcd;"),Err(ErrMode::Backtrack(Error::new("123abcd;", ErrorKind::Slice)))); + /// assert_eq!(parser.parse_peek("abcd"), Ok(("", "abcd"))); + /// assert_eq!(parser.parse_peek("abcde"), Err(ErrMode::Backtrack(InputError::new("abcde", ErrorKind::Verify)))); + /// assert_eq!(parser.parse_peek("123abcd;"),Err(ErrMode::Backtrack(InputError::new("123abcd;", ErrorKind::Slice)))); /// # } /// ``` #[doc(alias = "satisfy")] @@ -556,10 +566,10 @@ pub trait Parser<I, O, E> { where Self: core::marker::Sized, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: crate::lib::std::borrow::Borrow<O2>, O2: ?Sized, - E: ParseError<I>, + E: ParserError<I>, { Verify::new(self, filter) } @@ -573,7 +583,7 @@ pub trait Parser<I, O, E> { where Self: core::marker::Sized, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { Context::new(self, context) @@ -584,14 +594,14 @@ pub trait Parser<I, O, E> { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult, stream::Partial, Parser}; + /// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, stream::Partial, Parser}; /// # use winnow::token::take; /// # fn main() { /// /// let mut parser = take(5u8).complete_err(); /// - /// assert_eq!(parser.parse_next(Partial::new("abcdefg")), Ok((Partial::new("fg"), "abcde"))); - /// assert_eq!(parser.parse_next(Partial::new("abcd")), Err(ErrMode::Backtrack(Error::new(Partial::new("abcd"), ErrorKind::Complete)))); + /// assert_eq!(parser.parse_peek(Partial::new("abcdefg")), Ok((Partial::new("fg"), "abcde"))); + /// assert_eq!(parser.parse_peek(Partial::new("abcd")), Err(ErrMode::Backtrack(InputError::new(Partial::new("abcd"), ErrorKind::Complete)))); /// # } /// ``` fn complete_err(self) -> CompleteErr<Self> @@ -613,10 +623,11 @@ pub trait Parser<I, O, E> { impl<'a, I, O, E, F> Parser<I, O, E> for F where - F: FnMut(I) -> IResult<I, O, E> + 'a, + F: FnMut(&mut I) -> PResult<O, E> + 'a, + I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, O, E> { + fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { self(i) } } @@ -627,23 +638,23 @@ where /// /// ``` /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{ErrorKind, Error}}; -/// fn parser(i: &[u8]) -> IResult<&[u8], u8> { +/// # use winnow::{error::ErrMode, error::{ErrorKind, InputError}}; +/// fn parser<'s>(i: &mut &'s [u8]) -> PResult<u8, InputError<&'s [u8]>> { /// b'a'.parse_next(i) /// } -/// assert_eq!(parser(&b"abc"[..]), Ok((&b"bc"[..], b'a'))); -/// assert_eq!(parser(&b" abc"[..]), Err(ErrMode::Backtrack(Error::new(&b" abc"[..], ErrorKind::Verify)))); -/// assert_eq!(parser(&b"bc"[..]), Err(ErrMode::Backtrack(Error::new(&b"bc"[..], ErrorKind::Verify)))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&b""[..], ErrorKind::Token)))); +/// assert_eq!(parser.parse_peek(&b"abc"[..]), Ok((&b"bc"[..], b'a'))); +/// assert_eq!(parser.parse_peek(&b" abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b" abc"[..], ErrorKind::Verify)))); +/// assert_eq!(parser.parse_peek(&b"bc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"bc"[..], ErrorKind::Verify)))); +/// assert_eq!(parser.parse_peek(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&b""[..], ErrorKind::Token)))); /// ``` impl<I, E> Parser<I, u8, E> for u8 where I: StreamIsPartial, I: Stream<Token = u8>, - E: ParseError<I>, + E: ParserError<I>, { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, u8, E> { + fn parse_next(&mut self, i: &mut I) -> PResult<u8, E> { crate::token::one_of(*self).parse_next(i) } } @@ -654,24 +665,24 @@ where /// /// ``` /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{ErrorKind, Error}}; -/// fn parser(i: &str) -> IResult<&str, char> { +/// # use winnow::{error::ErrMode, error::{ErrorKind, InputError}}; +/// fn parser<'s>(i: &mut &'s str) -> PResult<char, InputError<&'s str>> { /// 'a'.parse_next(i) /// } -/// assert_eq!(parser("abc"), Ok(("bc", 'a'))); -/// assert_eq!(parser(" abc"), Err(ErrMode::Backtrack(Error::new(" abc", ErrorKind::Verify)))); -/// assert_eq!(parser("bc"), Err(ErrMode::Backtrack(Error::new("bc", ErrorKind::Verify)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(parser.parse_peek("abc"), Ok(("bc", 'a'))); +/// assert_eq!(parser.parse_peek(" abc"), Err(ErrMode::Backtrack(InputError::new(" abc", ErrorKind::Verify)))); +/// assert_eq!(parser.parse_peek("bc"), Err(ErrMode::Backtrack(InputError::new("bc", ErrorKind::Verify)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// ``` impl<I, E> Parser<I, <I as Stream>::Token, E> for char where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: AsChar + Copy, - E: ParseError<I>, + <I as Stream>::Token: AsChar + Clone, + E: ParserError<I>, { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, <I as Stream>::Token, E> { + fn parse_next(&mut self, i: &mut I) -> PResult<<I as Stream>::Token, E> { crate::token::one_of(*self).parse_next(i) } } @@ -681,26 +692,26 @@ where /// # Example /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::combinator::alt; /// # use winnow::token::take; /// -/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8], InputError<&'s [u8]>> { /// alt((&"Hello"[..], take(5usize))).parse_next(s) /// } /// -/// assert_eq!(parser(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..]))); -/// assert_eq!(parser(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..]))); -/// assert_eq!(parser(&b"Some"[..]), Err(ErrMode::Backtrack(Error::new(&b"Some"[..], ErrorKind::Slice)))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&b""[..], ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..]))); +/// assert_eq!(parser.parse_peek(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..]))); +/// assert_eq!(parser.parse_peek(&b"Some"[..]), Err(ErrMode::Backtrack(InputError::new(&b"Some"[..], ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&b""[..], ErrorKind::Slice)))); /// ``` -impl<'s, I, E: ParseError<I>> Parser<I, <I as Stream>::Slice, E> for &'s [u8] +impl<'s, I, E: ParserError<I>> Parser<I, <I as Stream>::Slice, E> for &'s [u8] where I: Compare<&'s [u8]> + StreamIsPartial, I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, <I as Stream>::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> PResult<<I as Stream>::Slice, E> { crate::token::tag(*self).parse_next(i) } } @@ -710,26 +721,26 @@ where /// # Example /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::combinator::alt; /// # use winnow::token::take; /// -/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8], InputError<&'s [u8]>> { /// alt((b"Hello", take(5usize))).parse_next(s) /// } /// -/// assert_eq!(parser(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..]))); -/// assert_eq!(parser(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..]))); -/// assert_eq!(parser(&b"Some"[..]), Err(ErrMode::Backtrack(Error::new(&b"Some"[..], ErrorKind::Slice)))); -/// assert_eq!(parser(&b""[..]), Err(ErrMode::Backtrack(Error::new(&b""[..], ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..]))); +/// assert_eq!(parser.parse_peek(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..]))); +/// assert_eq!(parser.parse_peek(&b"Some"[..]), Err(ErrMode::Backtrack(InputError::new(&b"Some"[..], ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&b""[..], ErrorKind::Slice)))); /// ``` -impl<'s, I, E: ParseError<I>, const N: usize> Parser<I, <I as Stream>::Slice, E> for &'s [u8; N] +impl<'s, I, E: ParserError<I>, const N: usize> Parser<I, <I as Stream>::Slice, E> for &'s [u8; N] where I: Compare<&'s [u8; N]> + StreamIsPartial, I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, <I as Stream>::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> PResult<<I as Stream>::Slice, E> { crate::token::tag(*self).parse_next(i) } } @@ -739,51 +750,51 @@ where /// # Example /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}}; /// # use winnow::combinator::alt; /// # use winnow::token::take; /// -/// fn parser(s: &str) -> IResult<&str, &str> { +/// fn parser<'s>(s: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { /// alt(("Hello", take(5usize))).parse_next(s) /// } /// -/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); -/// assert_eq!(parser("Something"), Ok(("hing", "Somet"))); -/// assert_eq!(parser("Some"), Err(ErrMode::Backtrack(Error::new("Some", ErrorKind::Slice)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser.parse_peek("Something"), Ok(("hing", "Somet"))); +/// assert_eq!(parser.parse_peek("Some"), Err(ErrMode::Backtrack(InputError::new("Some", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` -impl<'s, I, E: ParseError<I>> Parser<I, <I as Stream>::Slice, E> for &'s str +impl<'s, I, E: ParserError<I>> Parser<I, <I as Stream>::Slice, E> for &'s str where I: Compare<&'s str> + StreamIsPartial, I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, <I as Stream>::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> PResult<<I as Stream>::Slice, E> { crate::token::tag(*self).parse_next(i) } } -impl<I, E: ParseError<I>> Parser<I, (), E> for () { +impl<I, E: ParserError<I>> Parser<I, (), E> for () { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, (), E> { - Ok((i, ())) + fn parse_next(&mut self, _i: &mut I) -> PResult<(), E> { + Ok(()) } } macro_rules! impl_parser_for_tuple { ($($parser:ident $output:ident),+) => ( #[allow(non_snake_case)] - impl<I, $($output),+, E: ParseError<I>, $($parser),+> Parser<I, ($($output),+,), E> for ($($parser),+,) + impl<I, $($output),+, E: ParserError<I>, $($parser),+> Parser<I, ($($output),+,), E> for ($($parser),+,) where $($parser: Parser<I, $output, E>),+ { #[inline(always)] - fn parse_next(&mut self, i: I) -> IResult<I, ($($output),+,), E> { + fn parse_next(&mut self, i: &mut I) -> PResult<($($output),+,), E> { let ($(ref mut $parser),+,) = *self; - $(let(i, $output) = $parser.parse_next(i)?;)+ + $(let $output = $parser.parse_next(i)?;)+ - Ok((i, ($($output),+,))) + Ok(($($output),+,)) } } ) @@ -832,8 +843,25 @@ use alloc::boxed::Box; #[cfg(feature = "alloc")] impl<'a, I, O, E> Parser<I, O, E> for Box<dyn Parser<I, O, E> + 'a> { #[inline(always)] - fn parse_next(&mut self, input: I) -> IResult<I, O, E> { - (**self).parse_next(input) + fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { + (**self).parse_next(i) + } +} + +/// Convert a [`Parser::parse_peek`] style parse function to be a [`Parser`] +#[inline(always)] +pub fn unpeek<'a, I, O, E>( + mut peek: impl FnMut(I) -> IResult<I, O, E> + 'a, +) -> impl FnMut(&mut I) -> PResult<O, E> +where + I: Clone, +{ + move |input| match peek((*input).clone()) { + Ok((i, o)) => { + *input = i; + Ok(o) + } + Err(err) => Err(err), } } @@ -842,8 +870,8 @@ mod tests { use super::*; use crate::binary::be_u16; use crate::error::ErrMode; - use crate::error::Error; use crate::error::ErrorKind; + use crate::error::InputError; use crate::error::Needed; use crate::token::take; use crate::Partial; @@ -878,13 +906,13 @@ mod tests { use crate::error::ErrorKind; let mut parser = (alpha1,); - assert_eq!(parser.parse_next("abc123def"), Ok(("123def", ("abc",)))); + assert_eq!(parser.parse_peek("abc123def"), Ok(("123def", ("abc",)))); assert_eq!( - parser.parse_next("123def"), - Err(ErrMode::Backtrack(Error { - input: "123def", - kind: ErrorKind::Slice - })) + parser.parse_peek("123def"), + Err(ErrMode::Backtrack(InputError::new( + "123def", + ErrorKind::Slice + ))) ); } @@ -892,7 +920,7 @@ mod tests { fn tuple_test() { #[allow(clippy::type_complexity)] fn tuple_3(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (u16, &[u8], &[u8])> { - (be_u16, take(3u8), "fg").parse_next(i) + (be_u16, take(3u8), "fg").parse_peek(i) } assert_eq!( @@ -913,7 +941,7 @@ mod tests { assert_eq!( tuple_3(Partial::new(&b"abcdejk"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"jk"[..]), + &Partial::new(&b"jk"[..]), ErrorKind::Tag ))) ); @@ -921,11 +949,11 @@ mod tests { #[test] fn unit_type() { - fn parser(i: &str) -> IResult<&str, ()> { + fn parser(i: &mut &str) -> PResult<()> { ().parse_next(i) } - assert_eq!(parser.parse_next("abxsbsh"), Ok(("abxsbsh", ()))); - assert_eq!(parser.parse_next("sdfjakdsas"), Ok(("sdfjakdsas", ()))); - assert_eq!(parser.parse_next(""), Ok(("", ()))); + assert_eq!(parser.parse_peek("abxsbsh"), Ok(("abxsbsh", ()))); + assert_eq!(parser.parse_peek("sdfjakdsas"), Ok(("sdfjakdsas", ()))); + assert_eq!(parser.parse_peek(""), Ok(("", ()))); } } diff --git a/vendor/winnow/src/sequence.rs b/vendor/winnow/src/sequence.rs deleted file mode 100644 index 2d7af28e9..000000000 --- a/vendor/winnow/src/sequence.rs +++ /dev/null @@ -1,9 +0,0 @@ -//! Deprecated, see [`combinator`] -#![deprecated(since = "0.4.2", note = "Replaced with `combinator`")] - -use crate::combinator; - -pub use combinator::delimited; -pub use combinator::preceded; -pub use combinator::separated_pair; -pub use combinator::terminated; diff --git a/vendor/winnow/src/stream/mod.rs b/vendor/winnow/src/stream/mod.rs index 3d7a41e09..fcacbd78f 100644 --- a/vendor/winnow/src/stream/mod.rs +++ b/vendor/winnow/src/stream/mod.rs @@ -11,13 +11,16 @@ use core::num::NonZeroUsize; -use crate::error::{ErrMode, ErrorKind, Needed, ParseError}; +use crate::error::Needed; use crate::lib::std::iter::{Cloned, Enumerate}; use crate::lib::std::slice::Iter; use crate::lib::std::str::from_utf8; use crate::lib::std::str::CharIndices; use crate::lib::std::str::FromStr; -use crate::IResult; + +#[allow(unused_imports)] +#[cfg(feature = "unstable-doc")] +use crate::error::ErrMode; #[cfg(feature = "alloc")] use crate::lib::std::collections::BTreeMap; @@ -103,7 +106,7 @@ where } fn location(&self) -> usize { - self.initial.offset_to(&self.input) + self.input.offset_from(&self.initial) } } @@ -156,9 +159,9 @@ impl<I: crate::lib::std::fmt::Display> crate::lib::std::fmt::Display for Located /// /// type Stream<'is> = Stateful<&'is str, State<'is>>; /// -/// fn word(i: Stream<'_>) -> IResult<Stream<'_>, &str> { +/// fn word<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { /// i.state.count(); -/// alpha1(i) +/// alpha1.parse_next(i) /// } /// /// let data = "Hello"; @@ -218,48 +221,48 @@ impl<I: crate::lib::std::fmt::Display, S> crate::lib::std::fmt::Display for Stat /// Here is how it works in practice: /// /// ```rust -/// # use winnow::{IResult, error::ErrMode, error::Needed, error::{Error, ErrorKind}, token, ascii, stream::Partial}; +/// # use winnow::{PResult, error::ErrMode, error::Needed, error::{InputError, ErrorKind}, token, ascii, stream::Partial}; /// # use winnow::prelude::*; /// -/// fn take_partial(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { +/// fn take_partial<'s>(i: &mut Partial<&'s [u8]>) -> PResult<&'s [u8], InputError<Partial<&'s [u8]>>> { /// token::take(4u8).parse_next(i) /// } /// -/// fn take_complete(i: &[u8]) -> IResult<&[u8], &[u8]> { +/// fn take_complete<'s>(i: &mut &'s [u8]) -> PResult<&'s [u8], InputError<&'s [u8]>> { /// token::take(4u8).parse_next(i) /// } /// /// // both parsers will take 4 bytes as expected -/// assert_eq!(take_partial(Partial::new(&b"abcde"[..])), Ok((Partial::new(&b"e"[..]), &b"abcd"[..]))); -/// assert_eq!(take_complete(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..]))); +/// assert_eq!(take_partial.parse_peek(Partial::new(&b"abcde"[..])), Ok((Partial::new(&b"e"[..]), &b"abcd"[..]))); +/// assert_eq!(take_complete.parse_peek(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..]))); /// /// // if the input is smaller than 4 bytes, the partial parser /// // will return `Incomplete` to indicate that we need more data -/// assert_eq!(take_partial(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(take_partial.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// // but the complete parser will return an error -/// assert_eq!(take_complete(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice)))); +/// assert_eq!(take_complete.parse_peek(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice)))); /// /// // the alpha0 function recognizes 0 or more alphabetic characters -/// fn alpha0_partial(i: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// ascii::alpha0(i) +/// fn alpha0_partial<'s>(i: &mut Partial<&'s str>) -> PResult<&'s str, InputError<Partial<&'s str>>> { +/// ascii::alpha0.parse_next(i) /// } /// -/// fn alpha0_complete(i: &str) -> IResult<&str, &str> { -/// ascii::alpha0(i) +/// fn alpha0_complete<'s>(i: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { +/// ascii::alpha0.parse_next(i) /// } /// /// // if there's a clear limit to the recognized characters, both parsers work the same way -/// assert_eq!(alpha0_partial(Partial::new("abcd;")), Ok((Partial::new(";"), "abcd"))); -/// assert_eq!(alpha0_complete("abcd;"), Ok((";", "abcd"))); +/// assert_eq!(alpha0_partial.parse_peek(Partial::new("abcd;")), Ok((Partial::new(";"), "abcd"))); +/// assert_eq!(alpha0_complete.parse_peek("abcd;"), Ok((";", "abcd"))); /// /// // but when there's no limit, the partial version returns `Incomplete`, because it cannot /// // know if more input data should be recognized. The whole input could be "abcd;", or /// // "abcde;" -/// assert_eq!(alpha0_partial(Partial::new("abcd")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alpha0_partial.parse_peek(Partial::new("abcd")), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// // while the complete version knows that all of the data is there -/// assert_eq!(alpha0_complete("abcd"), Ok(("", "abcd"))); +/// assert_eq!(alpha0_complete.parse_peek("abcd"), Ok(("", "abcd"))); /// ``` #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Partial<I> { @@ -402,7 +405,7 @@ where } /// Core definition for parser input state -pub trait Stream: Offset + Clone + crate::lib::std::fmt::Debug { +pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::Debug { /// The smallest unit being parsed /// /// Example: `u8` for `&[u8]` or `char` for `&str` @@ -415,13 +418,26 @@ pub trait Stream: Offset + Clone + crate::lib::std::fmt::Debug { /// Iterate with the offset from the current location type IterOffsets: Iterator<Item = (usize, Self::Token)>; + /// A parse location within the stream + type Checkpoint: Offset + Clone + crate::lib::std::fmt::Debug; + /// Iterate with the offset from the current location fn iter_offsets(&self) -> Self::IterOffsets; /// Returns the offaet to the end of the input fn eof_offset(&self) -> usize; /// Split off the next token from the input - fn next_token(&self) -> Option<(Self, Self::Token)>; + fn next_token(&mut self) -> Option<Self::Token>; + /// Split off the next token from the input + #[inline(always)] + fn peek_token(&self) -> Option<(Self, Self::Token)> + where + Self: Clone, + { + let mut peek = self.clone(); + let token = peek.next_token()?; + Some((peek, token)) + } /// Finds the offset of the next matching token fn offset_for<P>(&self, predicate: P) -> Option<usize> @@ -448,7 +464,45 @@ pub trait Stream: Offset + Clone + crate::lib::std::fmt::Debug { /// * Indexes must uphold invariants of the stream, like for `str` they must lie on UTF-8 /// sequence boundaries. /// - fn next_slice(&self, offset: usize) -> (Self, Self::Slice); + fn next_slice(&mut self, offset: usize) -> Self::Slice; + /// Split off a slice of tokens from the input + #[inline(always)] + fn peek_slice(&self, offset: usize) -> (Self, Self::Slice) + where + Self: Clone, + { + let mut peek = self.clone(); + let slice = peek.next_slice(offset); + (peek, slice) + } + + /// Advance to the end of the stream + #[inline(always)] + fn finish(&mut self) -> Self::Slice { + self.next_slice(self.eof_offset()) + } + /// Advance to the end of the stream + #[inline(always)] + fn peek_finish(&self) -> (Self, Self::Slice) + where + Self: Clone, + { + let mut peek = self.clone(); + let slice = peek.finish(); + (peek, slice) + } + + /// Save the current parse location within the stream + fn checkpoint(&self) -> Self::Checkpoint; + /// Revert the stream to a prior [`Self::Checkpoint`] + /// + /// # Panic + /// + /// May panic if an invalid [`Self::Checkpoint`] is provided + fn reset(&mut self, checkpoint: Self::Checkpoint); + + /// Return the inner-most stream + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug; } impl<'i, T> Stream for &'i [T] @@ -460,6 +514,8 @@ where type IterOffsets = Enumerate<Cloned<Iter<'i, T>>>; + type Checkpoint = Checkpoint<Self>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.iter().cloned().enumerate() @@ -470,9 +526,10 @@ where } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { - self.split_first() - .map(|(token, next)| (next, token.clone())) + fn next_token(&mut self) -> Option<Self::Token> { + let (token, next) = self.split_first()?; + *self = next; + Some(token.clone()) } #[inline(always)] @@ -491,9 +548,24 @@ where } } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { + fn next_slice(&mut self, offset: usize) -> Self::Slice { let (slice, next) = self.split_at(offset); - (next, slice) + *self = next; + slice + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(*self) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + *self = checkpoint.0; + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + self } } @@ -503,6 +575,8 @@ impl<'i> Stream for &'i str { type IterOffsets = CharIndices<'i>; + type Checkpoint = Checkpoint<Self>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.char_indices() @@ -513,10 +587,11 @@ impl<'i> Stream for &'i str { } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { + fn next_token(&mut self) -> Option<Self::Token> { let c = self.chars().next()?; let offset = c.len(); - Some((&self[offset..], c)) + *self = &self[offset..]; + Some(c) } #[inline(always)] @@ -548,9 +623,24 @@ impl<'i> Stream for &'i str { } } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { + fn next_slice(&mut self, offset: usize) -> Self::Slice { let (slice, next) = self.split_at(offset); - (next, slice) + *self = next; + slice + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(*self) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + *self = checkpoint.0; + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + self } } @@ -560,6 +650,8 @@ impl<'i> Stream for &'i Bytes { type IterOffsets = Enumerate<Cloned<Iter<'i, u8>>>; + type Checkpoint = Checkpoint<Self>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.iter().cloned().enumerate() @@ -570,11 +662,13 @@ impl<'i> Stream for &'i Bytes { } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { + fn next_token(&mut self) -> Option<Self::Token> { if self.is_empty() { None } else { - Some((Bytes::from_bytes(&self[1..]), self[0])) + let token = self[0]; + *self = &self[1..]; + Some(token) } } @@ -594,9 +688,24 @@ impl<'i> Stream for &'i Bytes { } } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { - let (next, slice) = (&self.0).next_slice(offset); - (Bytes::from_bytes(next), slice) + fn next_slice(&mut self, offset: usize) -> Self::Slice { + let (slice, next) = self.0.split_at(offset); + *self = Bytes::from_bytes(next); + slice + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(*self) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + *self = checkpoint.0; + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + self } } @@ -606,6 +715,8 @@ impl<'i> Stream for &'i BStr { type IterOffsets = Enumerate<Cloned<Iter<'i, u8>>>; + type Checkpoint = Checkpoint<Self>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.iter().cloned().enumerate() @@ -616,11 +727,13 @@ impl<'i> Stream for &'i BStr { } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { + fn next_token(&mut self) -> Option<Self::Token> { if self.is_empty() { None } else { - Some((BStr::from_bytes(&self[1..]), self[0])) + let token = self[0]; + *self = &self[1..]; + Some(token) } } @@ -640,21 +753,38 @@ impl<'i> Stream for &'i BStr { } } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { - let (next, slice) = (&self.0).next_slice(offset); - (BStr::from_bytes(next), slice) + fn next_slice(&mut self, offset: usize) -> Self::Slice { + let (slice, next) = self.0.split_at(offset); + *self = BStr::from_bytes(next); + slice + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(*self) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + *self = checkpoint.0; + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + self } } impl<I> Stream for (I, usize) where - I: Stream<Token = u8>, + I: Stream<Token = u8> + Clone, { type Token = bool; type Slice = (I::Slice, usize, usize); type IterOffsets = BitOffsets<I>; + type Checkpoint = Checkpoint<(I::Checkpoint, usize)>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { BitOffsets { @@ -673,7 +803,7 @@ where } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { + fn next_token(&mut self) -> Option<Self::Token> { next_bit(self) } @@ -683,7 +813,7 @@ where P: Fn(Self::Token) -> bool, { self.iter_offsets() - .find_map(|(o, b)| predicate(b).then(|| o)) + .find_map(|(o, b)| predicate(b).then_some(o)) } #[inline(always)] fn offset_at(&self, tokens: usize) -> Result<usize, Needed> { @@ -697,11 +827,28 @@ where } } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { + fn next_slice(&mut self, offset: usize) -> Self::Slice { let byte_offset = (offset + self.1) / 8; let end_offset = (offset + self.1) % 8; - let (i, s) = self.0.next_slice(byte_offset); - ((i, end_offset), (s, self.1, end_offset)) + let s = self.0.next_slice(byte_offset); + let start_offset = self.1; + self.1 = end_offset; + (s, start_offset, end_offset) + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint((self.0.checkpoint(), self.1)) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + self.0.reset(checkpoint.0 .0); + self.1 = checkpoint.0 .1; + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + &self.0 } } @@ -713,37 +860,40 @@ pub struct BitOffsets<I> { impl<I> Iterator for BitOffsets<I> where - I: Stream<Token = u8>, + I: Stream<Token = u8> + Clone, { type Item = (usize, bool); fn next(&mut self) -> Option<Self::Item> { - let (next, b) = next_bit(&self.i)?; + let b = next_bit(&mut self.i)?; let o = self.o; - self.i = next; self.o += 1; Some((o, b)) } } -fn next_bit<I>(i: &(I, usize)) -> Option<((I, usize), bool)> +fn next_bit<I>(i: &mut (I, usize)) -> Option<bool> where - I: Stream<Token = u8>, + I: Stream<Token = u8> + Clone, { if i.eof_offset() == 0 { return None; } + let offset = i.1; - let i = i.clone(); - let (next_i, byte) = i.0.next_token()?; - let bit = (byte >> i.1) & 0x1 == 0x1; + let mut next_i = i.0.clone(); + let byte = next_i.next_token()?; + let bit = (byte >> offset) & 0x1 == 0x1; - let next_offset = i.1 + 1; + let next_offset = offset + 1; if next_offset == 8 { - Some(((next_i, 0), bit)) + i.0 = next_i; + i.1 = 0; + Some(bit) } else { - Some(((i.0, next_offset), bit)) + i.1 = next_offset; + Some(bit) } } @@ -753,6 +903,8 @@ impl<I: Stream> Stream for Located<I> { type IterOffsets = <I as Stream>::IterOffsets; + type Checkpoint = Checkpoint<I::Checkpoint>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.input.iter_offsets() @@ -763,15 +915,8 @@ impl<I: Stream> Stream for Located<I> { } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { - let (next, token) = self.input.next_token()?; - Some(( - Self { - initial: self.initial.clone(), - input: next, - }, - token, - )) + fn next_token(&mut self) -> Option<Self::Token> { + self.input.next_token() } #[inline(always)] @@ -786,15 +931,22 @@ impl<I: Stream> Stream for Located<I> { self.input.offset_at(tokens) } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { - let (next, slice) = self.input.next_slice(offset); - ( - Self { - initial: self.initial.clone(), - input: next, - }, - slice, - ) + fn next_slice(&mut self, offset: usize) -> Self::Slice { + self.input.next_slice(offset) + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(self.input.checkpoint()) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + self.input.reset(checkpoint.0); + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + &self.input } } @@ -804,6 +956,8 @@ impl<I: Stream, S: Clone + crate::lib::std::fmt::Debug> Stream for Stateful<I, S type IterOffsets = <I as Stream>::IterOffsets; + type Checkpoint = Checkpoint<I::Checkpoint>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.input.iter_offsets() @@ -814,15 +968,8 @@ impl<I: Stream, S: Clone + crate::lib::std::fmt::Debug> Stream for Stateful<I, S } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { - let (next, token) = self.input.next_token()?; - Some(( - Self { - input: next, - state: self.state.clone(), - }, - token, - )) + fn next_token(&mut self) -> Option<Self::Token> { + self.input.next_token() } #[inline(always)] @@ -837,15 +984,22 @@ impl<I: Stream, S: Clone + crate::lib::std::fmt::Debug> Stream for Stateful<I, S self.input.offset_at(tokens) } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { - let (next, slice) = self.input.next_slice(offset); - ( - Self { - input: next, - state: self.state.clone(), - }, - slice, - ) + fn next_slice(&mut self, offset: usize) -> Self::Slice { + self.input.next_slice(offset) + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(self.input.checkpoint()) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + self.input.reset(checkpoint.0); + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + &self.input } } @@ -855,6 +1009,8 @@ impl<I: Stream> Stream for Partial<I> { type IterOffsets = <I as Stream>::IterOffsets; + type Checkpoint = Checkpoint<I::Checkpoint>; + #[inline(always)] fn iter_offsets(&self) -> Self::IterOffsets { self.input.iter_offsets() @@ -865,15 +1021,8 @@ impl<I: Stream> Stream for Partial<I> { } #[inline(always)] - fn next_token(&self) -> Option<(Self, Self::Token)> { - let (next, token) = self.input.next_token()?; - Some(( - Partial { - input: next, - partial: self.partial, - }, - token, - )) + fn next_token(&mut self) -> Option<Self::Token> { + self.input.next_token() } #[inline(always)] @@ -888,15 +1037,22 @@ impl<I: Stream> Stream for Partial<I> { self.input.offset_at(tokens) } #[inline(always)] - fn next_slice(&self, offset: usize) -> (Self, Self::Slice) { - let (next, slice) = self.input.next_slice(offset); - ( - Partial { - input: next, - partial: self.partial, - }, - slice, - ) + fn next_slice(&mut self, offset: usize) -> Self::Slice { + self.input.next_slice(offset) + } + + #[inline(always)] + fn checkpoint(&self) -> Self::Checkpoint { + Checkpoint(self.input.checkpoint()) + } + #[inline(always)] + fn reset(&mut self, checkpoint: Self::Checkpoint) { + self.input.reset(checkpoint.0); + } + + #[inline(always)] + fn raw(&self) -> &dyn crate::lib::std::fmt::Debug { + &self.input } } @@ -1119,24 +1275,16 @@ where } /// Useful functions to calculate the offset between slices and show a hexdump of a slice -pub trait Offset { - /// Offset between the first byte of self and the first byte of the argument - fn offset_to(&self, second: &Self) -> usize; +pub trait Offset<Start = Self> { + /// Offset between the first byte of `start` and the first byte of `self` + fn offset_from(&self, start: &Start) -> usize; } impl<'a, T> Offset for &'a [T] { - #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - (*self).offset_to(*second) - } -} - -/// Convenience implementation to accept `&[T]` instead of `&&[T]` as above -impl<T> Offset for [T] { #[inline] - fn offset_to(&self, second: &Self) -> usize { - let fst = self.as_ptr(); - let snd = second.as_ptr(); + fn offset_from(&self, start: &Self) -> usize { + let fst = (*start).as_ptr(); + let snd = (*self).as_ptr(); debug_assert!( fst <= snd, @@ -1146,46 +1294,55 @@ impl<T> Offset for [T] { } } -impl<'a> Offset for &'a str { +impl<'a, T> Offset<<&'a [T] as Stream>::Checkpoint> for &'a [T] +where + T: Clone + crate::lib::std::fmt::Debug, +{ #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.as_bytes().offset_to(second.as_bytes()) + fn offset_from(&self, other: &<&'a [T] as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } -/// Convenience implementation to accept `&str` instead of `&&str` as above -impl Offset for str { +impl<'a> Offset for &'a str { #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.as_bytes().offset_to(second.as_bytes()) + fn offset_from(&self, start: &Self) -> usize { + self.as_bytes().offset_from(&start.as_bytes()) } } -impl Offset for Bytes { +impl<'a> Offset<<&'a str as Stream>::Checkpoint> for &'a str { #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.as_bytes().offset_to(second.as_bytes()) + fn offset_from(&self, other: &<&'a str as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } impl<'a> Offset for &'a Bytes { #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.as_bytes().offset_to(second.as_bytes()) + fn offset_from(&self, start: &Self) -> usize { + self.as_bytes().offset_from(&start.as_bytes()) } } -impl Offset for BStr { +impl<'a> Offset<<&'a Bytes as Stream>::Checkpoint> for &'a Bytes { #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.as_bytes().offset_to(second.as_bytes()) + fn offset_from(&self, other: &<&'a Bytes as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } impl<'a> Offset for &'a BStr { #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.as_bytes().offset_to(second.as_bytes()) + fn offset_from(&self, start: &Self) -> usize { + self.as_bytes().offset_from(&start.as_bytes()) + } +} + +impl<'a> Offset<<&'a BStr as Stream>::Checkpoint> for &'a BStr { + #[inline(always)] + fn offset_from(&self, other: &<&'a BStr as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } @@ -1194,38 +1351,90 @@ where I: Offset, { #[inline(always)] - fn offset_to(&self, other: &Self) -> usize { - self.0.offset_to(&other.0) * 8 + other.1 - self.1 + fn offset_from(&self, start: &Self) -> usize { + self.0.offset_from(&start.0) * 8 + self.1 - start.1 + } +} + +impl<I> Offset<<(I, usize) as Stream>::Checkpoint> for (I, usize) +where + I: Stream<Token = u8> + Clone, +{ + #[inline(always)] + fn offset_from(&self, other: &<(I, usize) as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } impl<I> Offset for Located<I> where - I: Offset, + I: Stream, { #[inline(always)] - fn offset_to(&self, other: &Self) -> usize { - self.input.offset_to(&other.input) + fn offset_from(&self, other: &Self) -> usize { + self.offset_from(&other.checkpoint()) + } +} + +impl<I> Offset<<Located<I> as Stream>::Checkpoint> for Located<I> +where + I: Stream, +{ + #[inline(always)] + fn offset_from(&self, other: &<Located<I> as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } impl<I, S> Offset for Stateful<I, S> where - I: Offset, + I: Stream, + S: Clone + crate::lib::std::fmt::Debug, +{ + #[inline(always)] + fn offset_from(&self, start: &Self) -> usize { + self.offset_from(&start.checkpoint()) + } +} + +impl<I, S> Offset<<Stateful<I, S> as Stream>::Checkpoint> for Stateful<I, S> +where + I: Stream, + S: Clone + crate::lib::std::fmt::Debug, { #[inline(always)] - fn offset_to(&self, other: &Self) -> usize { - self.input.offset_to(&other.input) + fn offset_from(&self, other: &<Stateful<I, S> as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) } } impl<I> Offset for Partial<I> where + I: Stream, +{ + #[inline(always)] + fn offset_from(&self, start: &Self) -> usize { + self.offset_from(&start.checkpoint()) + } +} + +impl<I> Offset<<Partial<I> as Stream>::Checkpoint> for Partial<I> +where + I: Stream, +{ + #[inline(always)] + fn offset_from(&self, other: &<Partial<I> as Stream>::Checkpoint) -> usize { + self.checkpoint().offset_from(other) + } +} + +impl<I> Offset for Checkpoint<I> +where I: Offset, { #[inline(always)] - fn offset_to(&self, second: &Self) -> usize { - self.input.offset_to(&second.input) + fn offset_from(&self, start: &Self) -> usize { + self.0.offset_from(&start.0) } } @@ -1744,6 +1953,10 @@ where } } +/// Ensure checkpoint details are kept privazte +#[derive(Copy, Clone, Debug)] +pub struct Checkpoint<T>(T); + /// A range bounded inclusively for counting parses performed #[derive(PartialEq, Eq)] pub struct Range { @@ -2257,15 +2470,15 @@ impl<'a> AsChar for &'a char { /// For example, you could implement `hex_digit0` as: /// ``` /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::token::take_while; -/// fn hex_digit1(input: &str) -> IResult<&str, &str> { +/// fn hex_digit1<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> { /// take_while(1.., ('a'..='f', 'A'..='F', '0'..='9')).parse_next(input) /// } /// -/// assert_eq!(hex_digit1("21cZ"), Ok(("Z", "21c"))); -/// assert_eq!(hex_digit1("H2"), Err(ErrMode::Backtrack(Error::new("H2", ErrorKind::Slice)))); -/// assert_eq!(hex_digit1(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(hex_digit1.parse_peek("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(hex_digit1.parse_peek("H2"), Err(ErrMode::Backtrack(InputError::new("H2", ErrorKind::Slice)))); +/// assert_eq!(hex_digit1.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` pub trait ContainsToken<T> { /// Returns true if self contains the token @@ -2415,14 +2628,6 @@ impl<const LEN: usize, C: AsChar> ContainsToken<C> for [char; LEN] { } } -impl<C: AsChar> ContainsToken<C> for &'_ str { - #[inline(always)] - fn contains_token(&self, token: C) -> bool { - let token = token.as_char(); - self.chars().any(|i| i == token) - } -} - impl<T> ContainsToken<T> for () { #[inline(always)] fn contains_token(&self, _token: T) -> bool { @@ -2464,88 +2669,6 @@ impl_contains_token_for_tuples!( F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21 ); -/// Looks for the first element of the input type for which the condition returns true, -/// and returns the input up to this position. -/// -/// *Partial version*: If no element is found matching the condition, this will return `Incomplete` -pub(crate) fn split_at_offset_partial<P, I: Stream, E: ParseError<I>>( - input: &I, - predicate: P, -) -> IResult<I, <I as Stream>::Slice, E> -where - P: Fn(I::Token) -> bool, -{ - let offset = input - .offset_for(predicate) - .ok_or_else(|| ErrMode::Incomplete(Needed::new(1)))?; - Ok(input.next_slice(offset)) -} - -/// Looks for the first element of the input type for which the condition returns true -/// and returns the input up to this position. -/// -/// Fails if the produced slice is empty. -/// -/// *Partial version*: If no element is found matching the condition, this will return `Incomplete` -pub(crate) fn split_at_offset1_partial<P, I: Stream, E: ParseError<I>>( - input: &I, - predicate: P, - e: ErrorKind, -) -> IResult<I, <I as Stream>::Slice, E> -where - P: Fn(I::Token) -> bool, -{ - let offset = input - .offset_for(predicate) - .ok_or_else(|| ErrMode::Incomplete(Needed::new(1)))?; - if offset == 0 { - Err(ErrMode::from_error_kind(input.clone(), e)) - } else { - Ok(input.next_slice(offset)) - } -} - -/// Looks for the first element of the input type for which the condition returns true, -/// and returns the input up to this position. -/// -/// *Complete version*: If no element is found matching the condition, this will return the whole input -pub(crate) fn split_at_offset_complete<P, I: Stream, E: ParseError<I>>( - input: &I, - predicate: P, -) -> IResult<I, <I as Stream>::Slice, E> -where - P: Fn(I::Token) -> bool, -{ - let offset = input - .offset_for(predicate) - .unwrap_or_else(|| input.eof_offset()); - Ok(input.next_slice(offset)) -} - -/// Looks for the first element of the input type for which the condition returns true -/// and returns the input up to this position. -/// -/// Fails if the produced slice is empty. -/// -/// *Complete version*: If no element is found matching the condition, this will return the whole input -pub(crate) fn split_at_offset1_complete<P, I: Stream, E: ParseError<I>>( - input: &I, - predicate: P, - e: ErrorKind, -) -> IResult<I, <I as Stream>::Slice, E> -where - P: Fn(I::Token) -> bool, -{ - let offset = input - .offset_for(predicate) - .unwrap_or_else(|| input.eof_offset()); - if offset == 0 { - Err(ErrMode::from_error_kind(input.clone(), e)) - } else { - Ok(input.next_slice(offset)) - } -} - #[cfg(feature = "simd")] #[inline(always)] fn memchr(token: u8, slice: &[u8]) -> Option<usize> { diff --git a/vendor/winnow/src/stream/tests.rs b/vendor/winnow/src/stream/tests.rs index de61df099..e653ad9e0 100644 --- a/vendor/winnow/src/stream/tests.rs +++ b/vendor/winnow/src/stream/tests.rs @@ -10,9 +10,9 @@ fn test_offset_u8() { let b = &a[2..]; let c = &a[..4]; let d = &a[3..5]; - assert_eq!(a.offset_to(b), 2); - assert_eq!(a.offset_to(c), 0); - assert_eq!(a.offset_to(d), 3); + assert_eq!(b.offset_from(&a), 2); + assert_eq!(c.offset_from(&a), 0); + assert_eq!(d.offset_from(&a), 3); } #[test] @@ -21,9 +21,9 @@ fn test_offset_str() { let b = &a[7..]; let c = &a[..5]; let d = &a[5..9]; - assert_eq!(a.offset_to(b), 7); - assert_eq!(a.offset_to(c), 0); - assert_eq!(a.offset_to(d), 5); + assert_eq!(b.offset_from(&a), 7); + assert_eq!(c.offset_from(&a), 0); + assert_eq!(d.offset_from(&a), 5); } #[test] @@ -37,7 +37,7 @@ fn test_bit_stream_empty() { let actual = i.eof_offset(); assert_eq!(actual, 0); - let actual = i.next_token(); + let actual = i.peek_token(); assert_eq!(actual, None); let actual = i.offset_for(|b| b); @@ -46,7 +46,7 @@ fn test_bit_stream_empty() { let actual = i.offset_at(1); assert_eq!(actual, Err(Needed::new(1))); - let (actual_input, actual_slice) = i.next_slice(0); + let (actual_input, actual_slice) = i.peek_slice(0); assert_eq!(actual_input, (&b""[..], 0)); assert_eq!(actual_slice, (&b""[..], 0, 0)); } @@ -56,7 +56,7 @@ fn test_bit_stream_empty() { fn test_bit_offset_empty() { let i = (&b""[..], 0); - let actual = i.offset_to(&i); + let actual = i.offset_from(&i); assert_eq!(actual, 0); } @@ -80,18 +80,18 @@ fn bit_stream_inner(byte_len: usize, start: usize) { let mut curr_i = i; let mut curr_offset = 0; - while let Some((next_i, _token)) = curr_i.next_token() { - let to_offset = i.offset_to(&curr_i); + while let Some((next_i, _token)) = curr_i.peek_token() { + let to_offset = curr_i.offset_from(&i); assert_eq!(curr_offset, to_offset); - let (slice_i, _) = i.next_slice(curr_offset); + let (slice_i, _) = i.peek_slice(curr_offset); assert_eq!(curr_i, slice_i); let at_offset = i.offset_at(curr_offset).unwrap(); assert_eq!(curr_offset, at_offset); let eof_offset = curr_i.eof_offset(); - let (next_eof_i, eof_slice) = curr_i.next_slice(eof_offset); + let (next_eof_i, eof_slice) = curr_i.peek_slice(eof_offset); assert_eq!(next_eof_i, (&b""[..], 0)); let eof_slice_i = (eof_slice.0, eof_slice.1); assert_eq!(eof_slice_i, curr_i); diff --git a/vendor/winnow/src/token/mod.rs b/vendor/winnow/src/token/mod.rs index 6f99dfa8d..fba019c97 100644 --- a/vendor/winnow/src/token/mod.rs +++ b/vendor/winnow/src/token/mod.rs @@ -6,16 +6,13 @@ mod tests; use crate::error::ErrMode; use crate::error::ErrorKind; use crate::error::Needed; -use crate::error::ParseError; +use crate::error::ParserError; use crate::lib::std::result::Result::Ok; use crate::stream::Range; -use crate::stream::{ - split_at_offset1_complete, split_at_offset1_partial, split_at_offset_complete, - split_at_offset_partial, Compare, CompareResult, ContainsToken, FindSlice, SliceLen, Stream, -}; +use crate::stream::{Compare, CompareResult, ContainsToken, FindSlice, SliceLen, Stream}; use crate::stream::{StreamIsPartial, ToUsize}; use crate::trace::trace; -use crate::IResult; +use crate::PResult; use crate::Parser; /// Matches one token @@ -27,31 +24,31 @@ use crate::Parser; /// # Example /// /// ```rust -/// # use winnow::{token::any, error::ErrMode, error::{Error, ErrorKind}}; +/// # use winnow::{token::any, error::ErrMode, error::{InputError, ErrorKind}}; /// # use winnow::prelude::*; /// fn parser(input: &str) -> IResult<&str, char> { -/// any.parse_next(input) +/// any.parse_peek(input) /// } /// /// assert_eq!(parser("abc"), Ok(("bc",'a'))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// ``` /// /// ```rust -/// # use winnow::{token::any, error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{token::any, error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; -/// assert_eq!(any::<_, Error<_>>.parse_next(Partial::new("abc")), Ok((Partial::new("bc"),'a'))); -/// assert_eq!(any::<_, Error<_>>.parse_next(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(any::<_, InputError<_>>.parse_peek(Partial::new("abc")), Ok((Partial::new("bc"),'a'))); +/// assert_eq!(any::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] #[doc(alias = "token")] -pub fn any<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Token, E> +pub fn any<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Token, E> where I: StreamIsPartial, I: Stream, { - trace("any", move |input: I| { + trace("any", move |input: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { any_::<_, _, true>(input) } else { @@ -61,7 +58,9 @@ where .parse_next(input) } -fn any_<I, E: ParseError<I>, const PARTIAL: bool>(input: I) -> IResult<I, <I as Stream>::Token, E> +fn any_<I, E: ParserError<I>, const PARTIAL: bool>( + input: &mut I, +) -> PResult<<I as Stream>::Token, E> where I: StreamIsPartial, I: Stream, @@ -80,7 +79,7 @@ where /// The input data will be compared to the tag combinator's argument and will return the part of /// the input that matches the argument /// -/// It will return `Err(ErrMode::Backtrack(Error::new(_, ErrorKind::Tag)))` if the input doesn't match the pattern +/// It will return `Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Tag)))` if the input doesn't match the pattern /// /// **Note:** [`Parser`][crate::Parser] is implemented for strings and byte strings as a convenience (complete /// only) @@ -88,44 +87,44 @@ where /// # Example /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, &str> { -/// "Hello".parse_next(s) +/// "Hello".parse_peek(s) /// } /// /// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); -/// assert_eq!(parser("Something"), Err(ErrMode::Backtrack(Error::new("Something", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("Something"), Err(ErrMode::Backtrack(InputError::new("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// ``` /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::Partial; /// use winnow::token::tag; /// /// fn parser(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// "Hello".parse_next(s) +/// "Hello".parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new("Hello, World!")), Ok((Partial::new(", World!"), "Hello"))); -/// assert_eq!(parser(Partial::new("Something")), Err(ErrMode::Backtrack(Error::new(Partial::new("Something"), ErrorKind::Tag)))); -/// assert_eq!(parser(Partial::new("S")), Err(ErrMode::Backtrack(Error::new(Partial::new("S"), ErrorKind::Tag)))); +/// assert_eq!(parser(Partial::new("Something")), Err(ErrMode::Backtrack(InputError::new(Partial::new("Something"), ErrorKind::Tag)))); +/// assert_eq!(parser(Partial::new("S")), Err(ErrMode::Backtrack(InputError::new(Partial::new("S"), ErrorKind::Tag)))); /// assert_eq!(parser(Partial::new("H")), Err(ErrMode::Incomplete(Needed::new(4)))); /// ``` #[inline(always)] #[doc(alias = "literal")] #[doc(alias = "bytes")] #[doc(alias = "just")] -pub fn tag<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, <I as Stream>::Slice, Error> +pub fn tag<T, I, Error: ParserError<I>>(tag: T) -> impl Parser<I, <I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream + Compare<T>, T: SliceLen + Clone, { - trace("tag", move |i: I| { + trace("tag", move |i: &mut I| { let t = tag.clone(); if <I as StreamIsPartial>::is_partial_supported() { tag_::<_, _, _, true>(i, t) @@ -135,10 +134,10 @@ where }) } -fn tag_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - i: I, +fn tag_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + i: &mut I, t: T, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream + Compare<T>, @@ -162,47 +161,47 @@ where /// The input data will be compared to the tag combinator's argument and will return the part of /// the input that matches the argument with no regard to case. /// -/// It will return `Err(ErrMode::Backtrack(Error::new(_, ErrorKind::Tag)))` if the input doesn't match the pattern. +/// It will return `Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Tag)))` if the input doesn't match the pattern. /// /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::tag_no_case; /// /// fn parser(s: &str) -> IResult<&str, &str> { -/// tag_no_case("hello").parse_next(s) +/// tag_no_case("hello").parse_peek(s) /// } /// /// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); /// assert_eq!(parser("hello, World!"), Ok((", World!", "hello"))); /// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO"))); -/// assert_eq!(parser("Something"), Err(ErrMode::Backtrack(Error::new("Something", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("Something"), Err(ErrMode::Backtrack(InputError::new("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::tag_no_case; /// /// fn parser(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// tag_no_case("hello").parse_next(s) +/// tag_no_case("hello").parse_peek(s) /// } /// /// assert_eq!(parser(Partial::new("Hello, World!")), Ok((Partial::new(", World!"), "Hello"))); /// assert_eq!(parser(Partial::new("hello, World!")), Ok((Partial::new(", World!"), "hello"))); /// assert_eq!(parser(Partial::new("HeLlO, World!")), Ok((Partial::new(", World!"), "HeLlO"))); -/// assert_eq!(parser(Partial::new("Something")), Err(ErrMode::Backtrack(Error::new(Partial::new("Something"), ErrorKind::Tag)))); +/// assert_eq!(parser(Partial::new("Something")), Err(ErrMode::Backtrack(InputError::new(Partial::new("Something"), ErrorKind::Tag)))); /// assert_eq!(parser(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(5)))); /// ``` #[inline(always)] #[doc(alias = "literal")] #[doc(alias = "bytes")] #[doc(alias = "just")] -pub fn tag_no_case<T, I, Error: ParseError<I>>( +pub fn tag_no_case<T, I, Error: ParserError<I>>( tag: T, ) -> impl Parser<I, <I as Stream>::Slice, Error> where @@ -210,7 +209,7 @@ where I: Stream + Compare<T>, T: SliceLen + Clone, { - trace("tag_no_case", move |i: I| { + trace("tag_no_case", move |i: &mut I| { let t = tag.clone(); if <I as StreamIsPartial>::is_partial_supported() { tag_no_case_::<_, _, _, true>(i, t) @@ -220,10 +219,10 @@ where }) } -fn tag_no_case_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - i: I, +fn tag_no_case_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + i: &mut I, t: T, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream + Compare<T>, @@ -231,7 +230,7 @@ where { let tag_len = t.slice_len(); - match (i).compare_no_case(t) { + match i.compare_no_case(t) { CompareResult::Ok => Ok(i.next_slice(tag_len)), CompareResult::Incomplete if PARTIAL && i.is_partial() => { Err(ErrMode::Incomplete(Needed::new(tag_len - i.eof_offset()))) @@ -258,50 +257,50 @@ where /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::token::one_of; -/// assert_eq!(one_of::<_, _, Error<_>>("abc").parse_next("b"), Ok(("", 'b'))); -/// assert_eq!(one_of::<_, _, Error<_>>("a").parse_next("bc"), Err(ErrMode::Backtrack(Error::new("bc", ErrorKind::Verify)))); -/// assert_eq!(one_of::<_, _, Error<_>>("a").parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(one_of::<_, _, InputError<_>>(['a', 'b', 'c']).parse_peek("b"), Ok(("", 'b'))); +/// assert_eq!(one_of::<_, _, InputError<_>>('a').parse_peek("bc"), Err(ErrMode::Backtrack(InputError::new("bc", ErrorKind::Verify)))); +/// assert_eq!(one_of::<_, _, InputError<_>>('a').parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// /// fn parser_fn(i: &str) -> IResult<&str, char> { -/// one_of(|c| c == 'a' || c == 'b').parse_next(i) +/// one_of(|c| c == 'a' || c == 'b').parse_peek(i) /// } /// assert_eq!(parser_fn("abc"), Ok(("bc", 'a'))); -/// assert_eq!(parser_fn("cd"), Err(ErrMode::Backtrack(Error::new("cd", ErrorKind::Verify)))); -/// assert_eq!(parser_fn(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(parser_fn("cd"), Err(ErrMode::Backtrack(InputError::new("cd", ErrorKind::Verify)))); +/// assert_eq!(parser_fn(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// ``` /// /// ``` /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// # use winnow::token::one_of; -/// assert_eq!(one_of::<_, _, Error<_>>("abc").parse_next(Partial::new("b")), Ok((Partial::new(""), 'b'))); -/// assert_eq!(one_of::<_, _, Error<_>>("a").parse_next(Partial::new("bc")), Err(ErrMode::Backtrack(Error::new(Partial::new("bc"), ErrorKind::Verify)))); -/// assert_eq!(one_of::<_, _, Error<_>>("a").parse_next(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(one_of::<_, _, InputError<_>>(['a', 'b', 'c']).parse_peek(Partial::new("b")), Ok((Partial::new(""), 'b'))); +/// assert_eq!(one_of::<_, _, InputError<_>>('a').parse_peek(Partial::new("bc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("bc"), ErrorKind::Verify)))); +/// assert_eq!(one_of::<_, _, InputError<_>>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// fn parser_fn(i: Partial<&str>) -> IResult<Partial<&str>, char> { -/// one_of(|c| c == 'a' || c == 'b').parse_next(i) +/// one_of(|c| c == 'a' || c == 'b').parse_peek(i) /// } /// assert_eq!(parser_fn(Partial::new("abc")), Ok((Partial::new("bc"), 'a'))); -/// assert_eq!(parser_fn(Partial::new("cd")), Err(ErrMode::Backtrack(Error::new(Partial::new("cd"), ErrorKind::Verify)))); +/// assert_eq!(parser_fn(Partial::new("cd")), Err(ErrMode::Backtrack(InputError::new(Partial::new("cd"), ErrorKind::Verify)))); /// assert_eq!(parser_fn(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] #[doc(alias = "char")] #[doc(alias = "token")] #[doc(alias = "satisfy")] -pub fn one_of<I, T, Error: ParseError<I>>(list: T) -> impl Parser<I, <I as Stream>::Token, Error> +pub fn one_of<I, T, Error: ParserError<I>>(list: T) -> impl Parser<I, <I as Stream>::Token, Error> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: Copy, + <I as Stream>::Token: Clone, T: ContainsToken<<I as Stream>::Token>, { trace( "one_of", - any.verify(move |t: &<I as Stream>::Token| list.contains_token(*t)), + any.verify(move |t: &<I as Stream>::Token| list.contains_token(t.clone())), ) } @@ -314,40 +313,40 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// # use winnow::token::none_of; -/// assert_eq!(none_of::<_, _, Error<_>>("abc").parse_next("z"), Ok(("", 'z'))); -/// assert_eq!(none_of::<_, _, Error<_>>("ab").parse_next("a"), Err(ErrMode::Backtrack(Error::new("a", ErrorKind::Verify)))); -/// assert_eq!(none_of::<_, _, Error<_>>("a").parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Token)))); +/// assert_eq!(none_of::<_, _, InputError<_>>(['a', 'b', 'c']).parse_peek("z"), Ok(("", 'z'))); +/// assert_eq!(none_of::<_, _, InputError<_>>(['a', 'b']).parse_peek("a"), Err(ErrMode::Backtrack(InputError::new("a", ErrorKind::Verify)))); +/// assert_eq!(none_of::<_, _, InputError<_>>('a').parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token)))); /// ``` /// /// ``` -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// # use winnow::token::none_of; -/// assert_eq!(none_of::<_, _, Error<_>>("abc").parse_next(Partial::new("z")), Ok((Partial::new(""), 'z'))); -/// assert_eq!(none_of::<_, _, Error<_>>("ab").parse_next(Partial::new("a")), Err(ErrMode::Backtrack(Error::new(Partial::new("a"), ErrorKind::Verify)))); -/// assert_eq!(none_of::<_, _, Error<_>>("a").parse_next(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(none_of::<_, _, InputError<_>>(['a', 'b', 'c']).parse_peek(Partial::new("z")), Ok((Partial::new(""), 'z'))); +/// assert_eq!(none_of::<_, _, InputError<_>>(['a', 'b']).parse_peek(Partial::new("a")), Err(ErrMode::Backtrack(InputError::new(Partial::new("a"), ErrorKind::Verify)))); +/// assert_eq!(none_of::<_, _, InputError<_>>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn none_of<I, T, Error: ParseError<I>>(list: T) -> impl Parser<I, <I as Stream>::Token, Error> +pub fn none_of<I, T, Error: ParserError<I>>(list: T) -> impl Parser<I, <I as Stream>::Token, Error> where I: StreamIsPartial, I: Stream, - <I as Stream>::Token: Copy, + <I as Stream>::Token: Clone, T: ContainsToken<<I as Stream>::Token>, { trace( "none_of", - any.verify(move |t: &<I as Stream>::Token| !list.contains_token(*t)), + any.verify(move |t: &<I as Stream>::Token| !list.contains_token(t.clone())), ) } /// Recognize the longest (m <= len <= n) input slice that matches the [pattern][ContainsToken] /// -/// It will return an `ErrMode::Backtrack(Error::new(_, ErrorKind::Slice))` if the pattern wasn't met or is out +/// It will return an `ErrMode::Backtrack(InputError::new(_, ErrorKind::Slice))` if the pattern wasn't met or is out /// of range (m <= len <= n). /// /// *Partial version* will return a `ErrMode::Incomplete(Needed::new(1))` if the pattern reaches the end of the input or is too short. @@ -358,13 +357,13 @@ where /// /// Zero or more tokens: /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// /// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { -/// take_while(0.., AsChar::is_alpha).parse_next(s) +/// take_while(0.., AsChar::is_alpha).parse_peek(s) /// } /// /// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); @@ -374,14 +373,14 @@ where /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// /// fn alpha(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { -/// take_while(0.., AsChar::is_alpha).parse_next(s) +/// take_while(0.., AsChar::is_alpha).parse_peek(s) /// } /// /// assert_eq!(alpha(Partial::new(b"latin123")), Ok((Partial::new(&b"123"[..]), &b"latin"[..]))); @@ -392,47 +391,47 @@ where /// /// One or more tokens: /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// /// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { -/// take_while(1.., AsChar::is_alpha).parse_next(s) +/// take_while(1.., AsChar::is_alpha).parse_peek(s) /// } /// /// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); /// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); -/// assert_eq!(alpha(b"12345"), Err(ErrMode::Backtrack(Error::new(&b"12345"[..], ErrorKind::Slice)))); +/// assert_eq!(alpha(b"12345"), Err(ErrMode::Backtrack(InputError::new(&b"12345"[..], ErrorKind::Slice)))); /// /// fn hex(s: &str) -> IResult<&str, &str> { -/// take_while(1.., "1234567890ABCDEF").parse_next(s) +/// take_while(1.., ('0'..='9', 'A'..='F')).parse_peek(s) /// } /// /// assert_eq!(hex("123 and voila"), Ok((" and voila", "123"))); /// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF"))); /// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE"))); /// assert_eq!(hex("D15EA5E"), Ok(("", "D15EA5E"))); -/// assert_eq!(hex(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(hex(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// /// fn alpha(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { -/// take_while(1.., AsChar::is_alpha).parse_next(s) +/// take_while(1.., AsChar::is_alpha).parse_peek(s) /// } /// /// assert_eq!(alpha(Partial::new(b"latin123")), Ok((Partial::new(&b"123"[..]), &b"latin"[..]))); /// assert_eq!(alpha(Partial::new(b"latin")), Err(ErrMode::Incomplete(Needed::new(1)))); -/// assert_eq!(alpha(Partial::new(b"12345")), Err(ErrMode::Backtrack(Error::new(Partial::new(&b"12345"[..]), ErrorKind::Slice)))); +/// assert_eq!(alpha(Partial::new(b"12345")), Err(ErrMode::Backtrack(InputError::new(Partial::new(&b"12345"[..]), ErrorKind::Slice)))); /// /// fn hex(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take_while(1.., "1234567890ABCDEF").parse_next(s) +/// take_while(1.., ('0'..='9', 'A'..='F')).parse_peek(s) /// } /// /// assert_eq!(hex(Partial::new("123 and voila")), Ok((Partial::new(" and voila"), "123"))); @@ -444,44 +443,44 @@ where /// /// Arbitrary amount of tokens: /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// /// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { -/// take_while(3..=6, AsChar::is_alpha).parse_next(s) +/// take_while(3..=6, AsChar::is_alpha).parse_peek(s) /// } /// /// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); /// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); /// assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); -/// assert_eq!(short_alpha(b"ed"), Err(ErrMode::Backtrack(Error::new(&b"ed"[..], ErrorKind::Slice)))); -/// assert_eq!(short_alpha(b"12345"), Err(ErrMode::Backtrack(Error::new(&b"12345"[..], ErrorKind::Slice)))); +/// assert_eq!(short_alpha(b"ed"), Err(ErrMode::Backtrack(InputError::new(&b"ed"[..], ErrorKind::Slice)))); +/// assert_eq!(short_alpha(b"12345"), Err(ErrMode::Backtrack(InputError::new(&b"12345"[..], ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// /// fn short_alpha(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { -/// take_while(3..=6, AsChar::is_alpha).parse_next(s) +/// take_while(3..=6, AsChar::is_alpha).parse_peek(s) /// } /// /// assert_eq!(short_alpha(Partial::new(b"latin123")), Ok((Partial::new(&b"123"[..]), &b"latin"[..]))); /// assert_eq!(short_alpha(Partial::new(b"lengthy")), Ok((Partial::new(&b"y"[..]), &b"length"[..]))); /// assert_eq!(short_alpha(Partial::new(b"latin")), Err(ErrMode::Incomplete(Needed::new(1)))); /// assert_eq!(short_alpha(Partial::new(b"ed")), Err(ErrMode::Incomplete(Needed::new(1)))); -/// assert_eq!(short_alpha(Partial::new(b"12345")), Err(ErrMode::Backtrack(Error::new(Partial::new(&b"12345"[..]), ErrorKind::Slice)))); +/// assert_eq!(short_alpha(Partial::new(b"12345")), Err(ErrMode::Backtrack(InputError::new(Partial::new(&b"12345"[..]), ErrorKind::Slice)))); /// ``` #[inline(always)] #[doc(alias = "is_a")] #[doc(alias = "take_while0")] #[doc(alias = "take_while1")] -pub fn take_while<T, I, Error: ParseError<I>>( +pub fn take_while<T, I, Error: ParserError<I>>( range: impl Into<Range>, list: T, ) -> impl Parser<I, <I as Stream>::Slice, Error> @@ -494,7 +493,7 @@ where start_inclusive, end_inclusive, } = range.into(); - trace("take_while", move |i: I| { + trace("take_while", move |i: &mut I| { match (start_inclusive, end_inclusive) { (0, None) => { if <I as StreamIsPartial>::is_partial_supported() { @@ -522,73 +521,127 @@ where }) } -/// Deprecated, see [`take_while`] -#[deprecated(since = "0.4.6", note = "Replaced with `take_while`")] -#[inline(always)] -pub fn take_while0<T, I, Error: ParseError<I>>( - list: T, -) -> impl Parser<I, <I as Stream>::Slice, Error> +fn take_while0_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + input: &mut I, + list: &T, +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream, T: ContainsToken<<I as Stream>::Token>, { - take_while(0.., list) + if PARTIAL && input.is_partial() { + take_till0_partial(input, |c| !list.contains_token(c)) + } else { + take_till0_complete(input, |c| !list.contains_token(c)) + } } -/// Deprecated, see [`take_while`] -#[deprecated(since = "0.4.6", note = "Replaced with `take_while`")] -#[inline(always)] -pub fn take_while1<T, I, Error: ParseError<I>>( - list: T, -) -> impl Parser<I, <I as Stream>::Slice, Error> +fn take_while1_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + input: &mut I, + list: &T, +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream, T: ContainsToken<<I as Stream>::Token>, { - take_while(1.., list) + let e: ErrorKind = ErrorKind::Slice; + if PARTIAL && input.is_partial() { + take_till1_partial(input, |c| !list.contains_token(c), e) + } else { + take_till1_complete(input, |c| !list.contains_token(c), e) + } } -fn take_while0_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - input: I, - list: &T, -) -> IResult<I, <I as Stream>::Slice, Error> +/// Looks for the first element of the input type for which the condition returns true, +/// and returns the input up to this position. +/// +/// *Partial version*: If no element is found matching the condition, this will return `Incomplete` +fn take_till0_partial<P, I: Stream, E: ParserError<I>>( + input: &mut I, + predicate: P, +) -> PResult<<I as Stream>::Slice, E> where - I: StreamIsPartial, - I: Stream, - T: ContainsToken<<I as Stream>::Token>, + P: Fn(I::Token) -> bool, { - if PARTIAL && input.is_partial() { - split_at_offset_partial(&input, |c| !list.contains_token(c)) + let offset = input + .offset_for(predicate) + .ok_or_else(|| ErrMode::Incomplete(Needed::new(1)))?; + Ok(input.next_slice(offset)) +} + +/// Looks for the first element of the input type for which the condition returns true +/// and returns the input up to this position. +/// +/// Fails if the produced slice is empty. +/// +/// *Partial version*: If no element is found matching the condition, this will return `Incomplete` +fn take_till1_partial<P, I: Stream, E: ParserError<I>>( + input: &mut I, + predicate: P, + e: ErrorKind, +) -> PResult<<I as Stream>::Slice, E> +where + P: Fn(I::Token) -> bool, +{ + let offset = input + .offset_for(predicate) + .ok_or_else(|| ErrMode::Incomplete(Needed::new(1)))?; + if offset == 0 { + Err(ErrMode::from_error_kind(input, e)) } else { - split_at_offset_complete(&input, |c| !list.contains_token(c)) + Ok(input.next_slice(offset)) } } -fn take_while1_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - input: I, - list: &T, -) -> IResult<I, <I as Stream>::Slice, Error> +/// Looks for the first element of the input type for which the condition returns true, +/// and returns the input up to this position. +/// +/// *Complete version*: If no element is found matching the condition, this will return the whole input +fn take_till0_complete<P, I: Stream, E: ParserError<I>>( + input: &mut I, + predicate: P, +) -> PResult<<I as Stream>::Slice, E> where - I: StreamIsPartial, - I: Stream, - T: ContainsToken<<I as Stream>::Token>, + P: Fn(I::Token) -> bool, { - let e: ErrorKind = ErrorKind::Slice; - if PARTIAL && input.is_partial() { - split_at_offset1_partial(&input, |c| !list.contains_token(c), e) + let offset = input + .offset_for(predicate) + .unwrap_or_else(|| input.eof_offset()); + Ok(input.next_slice(offset)) +} + +/// Looks for the first element of the input type for which the condition returns true +/// and returns the input up to this position. +/// +/// Fails if the produced slice is empty. +/// +/// *Complete version*: If no element is found matching the condition, this will return the whole input +fn take_till1_complete<P, I: Stream, E: ParserError<I>>( + input: &mut I, + predicate: P, + e: ErrorKind, +) -> PResult<<I as Stream>::Slice, E> +where + P: Fn(I::Token) -> bool, +{ + let offset = input + .offset_for(predicate) + .unwrap_or_else(|| input.eof_offset()); + if offset == 0 { + Err(ErrMode::from_error_kind(input, e)) } else { - split_at_offset1_complete(&input, |c| !list.contains_token(c), e) + Ok(input.next_slice(offset)) } } -fn take_while_m_n_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - input: I, +fn take_while_m_n_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + input: &mut I, m: usize, n: usize, list: &T, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -615,7 +668,7 @@ where } if PARTIAL && input.is_partial() { if final_count == n { - Ok(input.next_slice(input.eof_offset())) + Ok(input.finish()) } else { let needed = if m > input.eof_offset() { m - input.eof_offset() @@ -626,7 +679,7 @@ where } } else { if m <= final_count { - Ok(input.next_slice(input.eof_offset())) + Ok(input.finish()) } else { Err(ErrMode::from_error_kind(input, ErrorKind::Slice)) } @@ -641,12 +694,12 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_till0; /// /// fn till_colon(s: &str) -> IResult<&str, &str> { -/// take_till0(|c| c == ':').parse_next(s) +/// take_till0(|c| c == ':').parse_peek(s) /// } /// /// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); @@ -656,13 +709,13 @@ where /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_till0; /// /// fn till_colon(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take_till0(|c| c == ':').parse_next(s) +/// take_till0(|c| c == ':').parse_peek(s) /// } /// /// assert_eq!(till_colon(Partial::new("latin:123")), Ok((Partial::new(":123"), "latin"))); @@ -671,7 +724,7 @@ where /// assert_eq!(till_colon(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn take_till0<T, I, Error: ParseError<I>>( +pub fn take_till0<T, I, Error: ParserError<I>>( list: T, ) -> impl Parser<I, <I as Stream>::Slice, Error> where @@ -679,18 +732,18 @@ where I: Stream, T: ContainsToken<<I as Stream>::Token>, { - trace("take_till0", move |i: I| { + trace("take_till0", move |i: &mut I| { if <I as StreamIsPartial>::is_partial_supported() && i.is_partial() { - split_at_offset_partial(&i, |c| list.contains_token(c)) + take_till0_partial(i, |c| list.contains_token(c)) } else { - split_at_offset_complete(&i, |c| list.contains_token(c)) + take_till0_complete(i, |c| list.contains_token(c)) } }) } /// Recognize the longest (at least 1) input slice till a [pattern][ContainsToken] is met. /// -/// It will return `Err(ErrMode::Backtrack(Error::new(_, ErrorKind::Slice)))` if the input is empty or the +/// It will return `Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Slice)))` if the input is empty or the /// predicate matches the first input. /// /// *Partial version* will return a `ErrMode::Incomplete(Needed::new(1))` if the match reaches the @@ -699,46 +752,46 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_till1; /// /// fn till_colon(s: &str) -> IResult<&str, &str> { -/// take_till1(|c| c == ':').parse_next(s) +/// take_till1(|c| c == ':').parse_peek(s) /// } /// /// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); -/// assert_eq!(till_colon(":empty matched"), Err(ErrMode::Backtrack(Error::new(":empty matched", ErrorKind::Slice)))); +/// assert_eq!(till_colon(":empty matched"), Err(ErrMode::Backtrack(InputError::new(":empty matched", ErrorKind::Slice)))); /// assert_eq!(till_colon("12345"), Ok(("", "12345"))); -/// assert_eq!(till_colon(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(till_colon(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// /// fn not_space(s: &str) -> IResult<&str, &str> { -/// take_till1(" \t\r\n").parse_next(s) +/// take_till1([' ', '\t', '\r', '\n']).parse_peek(s) /// } /// /// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,"))); /// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes"))); /// assert_eq!(not_space("Nospace"), Ok(("", "Nospace"))); -/// assert_eq!(not_space(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(not_space(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_till1; /// /// fn till_colon(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take_till1(|c| c == ':').parse_next(s) +/// take_till1(|c| c == ':').parse_peek(s) /// } /// /// assert_eq!(till_colon(Partial::new("latin:123")), Ok((Partial::new(":123"), "latin"))); -/// assert_eq!(till_colon(Partial::new(":empty matched")), Err(ErrMode::Backtrack(Error::new(Partial::new(":empty matched"), ErrorKind::Slice)))); +/// assert_eq!(till_colon(Partial::new(":empty matched")), Err(ErrMode::Backtrack(InputError::new(Partial::new(":empty matched"), ErrorKind::Slice)))); /// assert_eq!(till_colon(Partial::new("12345")), Err(ErrMode::Incomplete(Needed::new(1)))); /// assert_eq!(till_colon(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// fn not_space(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take_till1(" \t\r\n").parse_next(s) +/// take_till1([' ', '\t', '\r', '\n']).parse_peek(s) /// } /// /// assert_eq!(not_space(Partial::new("Hello, World!")), Ok((Partial::new(" World!"), "Hello,"))); @@ -748,7 +801,7 @@ where /// ``` #[inline(always)] #[doc(alias = "is_not")] -pub fn take_till1<T, I, Error: ParseError<I>>( +pub fn take_till1<T, I, Error: ParserError<I>>( list: T, ) -> impl Parser<I, <I as Stream>::Slice, Error> where @@ -756,19 +809,19 @@ where I: Stream, T: ContainsToken<<I as Stream>::Token>, { - trace("take_till1", move |i: I| { + trace("take_till1", move |i: &mut I| { let e: ErrorKind = ErrorKind::Slice; if <I as StreamIsPartial>::is_partial_supported() && i.is_partial() { - split_at_offset1_partial(&i, |c| list.contains_token(c), e) + take_till1_partial(i, |c| list.contains_token(c), e) } else { - split_at_offset1_complete(&i, |c| list.contains_token(c), e) + take_till1_complete(i, |c| list.contains_token(c), e) } }) } /// Recognize an input slice containing the first N input elements (I[..N]). /// -/// *Complete version*: It will return `Err(ErrMode::Backtrack(Error::new(_, ErrorKind::Slice)))` if the input is shorter than the argument. +/// *Complete version*: It will return `Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Slice)))` if the input is shorter than the argument. /// /// *Partial version*: if the input has less than N elements, `take` will /// return a `ErrMode::Incomplete(Needed::new(M))` where M is the number of @@ -780,18 +833,18 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take; /// /// fn take6(s: &str) -> IResult<&str, &str> { -/// take(6usize).parse_next(s) +/// take(6usize).parse_peek(s) /// } /// /// assert_eq!(take6("1234567"), Ok(("7", "123456"))); /// assert_eq!(take6("things"), Ok(("", "things"))); -/// assert_eq!(take6("short"), Err(ErrMode::Backtrack(Error::new("short", ErrorKind::Slice)))); -/// assert_eq!(take6(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(take6("short"), Err(ErrMode::Backtrack(InputError::new("short", ErrorKind::Slice)))); +/// assert_eq!(take6(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// ``` /// /// The units that are taken will depend on the input type. For example, for a @@ -800,21 +853,21 @@ where /// /// ```rust /// # use winnow::prelude::*; -/// use winnow::error::Error; +/// use winnow::error::InputError; /// use winnow::token::take; /// -/// assert_eq!(take::<_, _, Error<_>>(1usize).parse_next("💙"), Ok(("", "💙"))); -/// assert_eq!(take::<_, _, Error<_>>(1usize).parse_next("💙".as_bytes()), Ok((b"\x9F\x92\x99".as_ref(), b"\xF0".as_ref()))); +/// assert_eq!(take::<_, _, InputError<_>>(1usize).parse_peek("💙"), Ok(("", "💙"))); +/// assert_eq!(take::<_, _, InputError<_>>(1usize).parse_peek("💙".as_bytes()), Ok((b"\x9F\x92\x99".as_ref(), b"\xF0".as_ref()))); /// ``` /// /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::Partial; /// use winnow::token::take; /// /// fn take6(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take(6usize).parse_next(s) +/// take(6usize).parse_peek(s) /// } /// /// assert_eq!(take6(Partial::new("1234567")), Ok((Partial::new("7"), "123456"))); @@ -823,14 +876,14 @@ where /// assert_eq!(take6(Partial::new("short")), Err(ErrMode::Incomplete(Needed::Unknown))); /// ``` #[inline(always)] -pub fn take<C, I, Error: ParseError<I>>(count: C) -> impl Parser<I, <I as Stream>::Slice, Error> +pub fn take<C, I, Error: ParserError<I>>(count: C) -> impl Parser<I, <I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream, C: ToUsize, { let c = count.to_usize(); - trace("take", move |i: I| { + trace("take", move |i: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { take_::<_, _, true>(i, c) } else { @@ -839,10 +892,10 @@ where }) } -fn take_<I, Error: ParseError<I>, const PARTIAL: bool>( - i: I, +fn take_<I, Error: ParserError<I>, const PARTIAL: bool>( + i: &mut I, c: usize, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -858,7 +911,7 @@ where /// /// It doesn't consume the pattern. /// -/// *Complete version*: It will return `Err(ErrMode::Backtrack(Error::new(_, ErrorKind::Slice)))` +/// *Complete version*: It will return `Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Slice)))` /// if the pattern wasn't met. /// /// *Partial version*: will return a `ErrMode::Incomplete(Needed::new(N))` if the input doesn't @@ -867,28 +920,28 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_until0; /// /// fn until_eof(s: &str) -> IResult<&str, &str> { -/// take_until0("eof").parse_next(s) +/// take_until0("eof").parse_peek(s) /// } /// /// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); -/// assert_eq!(until_eof("hello, world"), Err(ErrMode::Backtrack(Error::new("hello, world", ErrorKind::Slice)))); -/// assert_eq!(until_eof(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(until_eof("hello, world"), Err(ErrMode::Backtrack(InputError::new("hello, world", ErrorKind::Slice)))); +/// assert_eq!(until_eof(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1"))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_until0; /// /// fn until_eof(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take_until0("eof").parse_next(s) +/// take_until0("eof").parse_peek(s) /// } /// /// assert_eq!(until_eof(Partial::new("hello, worldeof")), Ok((Partial::new("eof"), "hello, world"))); @@ -897,7 +950,7 @@ where /// assert_eq!(until_eof(Partial::new("1eof2eof")), Ok((Partial::new("eof2eof"), "1"))); /// ``` #[inline(always)] -pub fn take_until0<T, I, Error: ParseError<I>>( +pub fn take_until0<T, I, Error: ParserError<I>>( tag: T, ) -> impl Parser<I, <I as Stream>::Slice, Error> where @@ -905,7 +958,7 @@ where I: Stream + FindSlice<T>, T: SliceLen + Clone, { - trace("take_until0", move |i: I| { + trace("take_until0", move |i: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { take_until0_::<_, _, _, true>(i, tag.clone()) } else { @@ -914,10 +967,10 @@ where }) } -fn take_until0_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - i: I, +fn take_until0_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + i: &mut I, t: T, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice<T>, @@ -934,7 +987,7 @@ where /// /// It doesn't consume the pattern. /// -/// *Complete version*: It will return `Err(ErrMode::Backtrack(Error::new(_, ErrorKind::Slice)))` +/// *Complete version*: It will return `Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Slice)))` /// if the pattern wasn't met. /// /// *Partial version*: will return a `ErrMode::Incomplete(Needed::new(N))` if the input doesn't @@ -943,39 +996,39 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::token::take_until1; /// /// fn until_eof(s: &str) -> IResult<&str, &str> { -/// take_until1("eof").parse_next(s) +/// take_until1("eof").parse_peek(s) /// } /// /// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); -/// assert_eq!(until_eof("hello, world"), Err(ErrMode::Backtrack(Error::new("hello, world", ErrorKind::Slice)))); -/// assert_eq!(until_eof(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); +/// assert_eq!(until_eof("hello, world"), Err(ErrMode::Backtrack(InputError::new("hello, world", ErrorKind::Slice)))); +/// assert_eq!(until_eof(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); /// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1"))); -/// assert_eq!(until_eof("eof"), Err(ErrMode::Backtrack(Error::new("eof", ErrorKind::Slice)))); +/// assert_eq!(until_eof("eof"), Err(ErrMode::Backtrack(InputError::new("eof", ErrorKind::Slice)))); /// ``` /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; /// use winnow::token::take_until1; /// /// fn until_eof(s: Partial<&str>) -> IResult<Partial<&str>, &str> { -/// take_until1("eof").parse_next(s) +/// take_until1("eof").parse_peek(s) /// } /// /// assert_eq!(until_eof(Partial::new("hello, worldeof")), Ok((Partial::new("eof"), "hello, world"))); /// assert_eq!(until_eof(Partial::new("hello, world")), Err(ErrMode::Incomplete(Needed::Unknown))); /// assert_eq!(until_eof(Partial::new("hello, worldeo")), Err(ErrMode::Incomplete(Needed::Unknown))); /// assert_eq!(until_eof(Partial::new("1eof2eof")), Ok((Partial::new("eof2eof"), "1"))); -/// assert_eq!(until_eof(Partial::new("eof")), Err(ErrMode::Backtrack(Error::new(Partial::new("eof"), ErrorKind::Slice)))); +/// assert_eq!(until_eof(Partial::new("eof")), Err(ErrMode::Backtrack(InputError::new(Partial::new("eof"), ErrorKind::Slice)))); /// ``` #[inline(always)] -pub fn take_until1<T, I, Error: ParseError<I>>( +pub fn take_until1<T, I, Error: ParserError<I>>( tag: T, ) -> impl Parser<I, <I as Stream>::Slice, Error> where @@ -983,7 +1036,7 @@ where I: Stream + FindSlice<T>, T: SliceLen + Clone, { - trace("take_until1", move |i: I| { + trace("take_until1", move |i: &mut I| { if <I as StreamIsPartial>::is_partial_supported() { take_until1_::<_, _, _, true>(i, tag.clone()) } else { @@ -992,10 +1045,10 @@ where }) } -fn take_until1_<T, I, Error: ParseError<I>, const PARTIAL: bool>( - i: I, +fn take_until1_<T, I, Error: ParserError<I>, const PARTIAL: bool>( + i: &mut I, t: T, -) -> IResult<I, <I as Stream>::Slice, Error> +) -> PResult<<I as Stream>::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice<T>, diff --git a/vendor/winnow/src/token/tests.rs b/vendor/winnow/src/token/tests.rs index e1c7999c2..d9f364607 100644 --- a/vendor/winnow/src/token/tests.rs +++ b/vendor/winnow/src/token/tests.rs @@ -6,11 +6,12 @@ use proptest::prelude::*; use crate::binary::length_data; use crate::combinator::delimited; use crate::error::ErrMode; -use crate::error::Error; use crate::error::ErrorKind; +use crate::error::InputError; use crate::error::Needed; use crate::stream::AsChar; use crate::token::tag; +use crate::unpeek; use crate::IResult; use crate::Parser; use crate::Partial; @@ -18,13 +19,13 @@ 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_next("øn"); + take_while(1..=4, |c: char| c.is_alphabetic()).parse_peek("øn"); assert_eq!(result, Ok(("", "øn"))); } #[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_next("øn"); + let result: IResult<&str, &str> = take_while(1, |c: char| c.is_alphabetic()).parse_peek("øn"); assert_eq!(result, Ok(("n", "ø"))); } @@ -37,7 +38,7 @@ fn model_complete_take_while_m_n( ) -> IResult<&str, &str> { if n < m { Err(crate::error::ErrMode::from_error_kind( - input, + &input, crate::error::ErrorKind::Slice, )) } else if m <= valid { @@ -45,7 +46,7 @@ fn model_complete_take_while_m_n( Ok((&input[offset..], &input[0..offset])) } else { Err(crate::error::ErrMode::from_error_kind( - input, + &input, crate::error::ErrorKind::Slice, )) } @@ -59,7 +60,7 @@ proptest! { let input = format!("{:a<valid$}{:b<invalid$}", "", "", valid=valid, invalid=invalid); let expected = model_complete_take_while_m_n(m, n, valid, &input); if m <= n { - let actual = take_while(m..=n, |c: char| c == 'a').parse_next(input.as_str()); + let actual = take_while(m..=n, |c: char| c == 'a').parse_peek(input.as_str()); assert_eq!(expected, actual); } } @@ -69,7 +70,7 @@ proptest! { fn partial_any_str() { use super::any; assert_eq!( - any::<_, Error<Partial<&str>>>(Partial::new("Ә")), + any::<_, InputError<Partial<&str>>>.parse_peek(Partial::new("Ә")), Ok((Partial::new(""), 'Ә')) ); } @@ -77,7 +78,7 @@ fn partial_any_str() { #[test] fn partial_one_of_test() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> { - one_of("ab").parse_next(i) + one_of(['a', 'b']).parse_peek(i) } let a = &b"abcd"[..]; @@ -87,13 +88,13 @@ fn partial_one_of_test() { assert_eq!( f(Partial::new(b)), Err(ErrMode::Backtrack(error_position!( - Partial::new(b), + &Partial::new(b), ErrorKind::Verify ))) ); fn utf8(i: Partial<&str>) -> IResult<Partial<&str>, char> { - one_of("+\u{FF0B}").parse_next(i) + one_of(['+', '\u{FF0B}']).parse_peek(i) } assert!(utf8(Partial::new("+")).is_ok()); @@ -103,14 +104,14 @@ fn partial_one_of_test() { #[test] fn char_byteslice() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> { - 'c'.parse_next(i) + 'c'.parse_peek(i) } let a = &b"abcd"[..]; assert_eq!( f(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(a), + &Partial::new(a), ErrorKind::Verify ))) ); @@ -122,14 +123,14 @@ fn char_byteslice() { #[test] fn char_str() { fn f(i: Partial<&str>) -> IResult<Partial<&str>, char> { - 'c'.parse_next(i) + 'c'.parse_peek(i) } let a = "abcd"; assert_eq!( f(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(a), + &Partial::new(a), ErrorKind::Verify ))) ); @@ -141,14 +142,14 @@ fn char_str() { #[test] fn partial_none_of_test() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> { - none_of("ab").parse_next(i) + none_of(['a', 'b']).parse_peek(i) } let a = &b"abcd"[..]; assert_eq!( f(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(a), + &Partial::new(a), ErrorKind::Verify ))) ); @@ -160,7 +161,7 @@ fn partial_none_of_test() { #[test] fn partial_is_a() { fn a_or_b(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_while(1.., "ab").parse_next(i) + take_while(1.., ['a', 'b']).parse_peek(i) } let a = Partial::new(&b"abcd"[..]); @@ -172,7 +173,7 @@ fn partial_is_a() { let c = Partial::new(&b"cdef"[..]); assert_eq!( a_or_b(c), - Err(ErrMode::Backtrack(error_position!(c, ErrorKind::Slice))) + Err(ErrMode::Backtrack(error_position!(&c, ErrorKind::Slice))) ); let d = Partial::new(&b"bacdef"[..]); @@ -182,7 +183,7 @@ fn partial_is_a() { #[test] fn partial_is_not() { fn a_or_b(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_till1("ab").parse_next(i) + take_till1(['a', 'b']).parse_peek(i) } let a = Partial::new(&b"cdab"[..]); @@ -194,7 +195,7 @@ fn partial_is_not() { let c = Partial::new(&b"abab"[..]); assert_eq!( a_or_b(c), - Err(ErrMode::Backtrack(error_position!(c, ErrorKind::Slice))) + Err(ErrMode::Backtrack(error_position!(&c, ErrorKind::Slice))) ); let d = Partial::new(&b"cdefba"[..]); @@ -207,7 +208,7 @@ fn partial_is_not() { #[test] fn partial_take_until_incomplete() { fn y(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_until0("end").parse_next(i) + take_until0("end").parse_peek(i) } assert_eq!( y(Partial::new(&b"nd"[..])), @@ -226,7 +227,7 @@ fn partial_take_until_incomplete() { #[test] fn partial_take_until_incomplete_s() { fn ys(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_until0("end").parse_next(i) + take_until0("end").parse_peek(i) } assert_eq!( ys(Partial::new("123en")), @@ -244,7 +245,7 @@ fn partial_recognize() { fn x(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { delimited("<!--", take(5_usize), "-->") .recognize() - .parse_next(i) + .parse_peek(i) } let r = x(Partial::new(&b"<!-- abc --> aaa"[..])); assert_eq!(r, Ok((Partial::new(&b" aaa"[..]), &b"<!-- abc -->"[..]))); @@ -252,43 +253,43 @@ fn partial_recognize() { let semicolon = &b";"[..]; fn ya(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - alpha.recognize().parse_next(i) + alpha.recognize().parse_peek(i) } let ra = ya(Partial::new(&b"abc;"[..])); assert_eq!(ra, Ok((Partial::new(semicolon), &b"abc"[..]))); fn yd(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - digit.recognize().parse_next(i) + digit.recognize().parse_peek(i) } let rd = yd(Partial::new(&b"123;"[..])); assert_eq!(rd, Ok((Partial::new(semicolon), &b"123"[..]))); fn yhd(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - hex_digit.recognize().parse_next(i) + hex_digit.recognize().parse_peek(i) } let rhd = yhd(Partial::new(&b"123abcDEF;"[..])); assert_eq!(rhd, Ok((Partial::new(semicolon), &b"123abcDEF"[..]))); fn yod(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - oct_digit.recognize().parse_next(i) + oct_digit.recognize().parse_peek(i) } let rod = yod(Partial::new(&b"1234567;"[..])); assert_eq!(rod, Ok((Partial::new(semicolon), &b"1234567"[..]))); fn yan(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - alphanumeric.recognize().parse_next(i) + alphanumeric.recognize().parse_peek(i) } let ran = yan(Partial::new(&b"123abc;"[..])); assert_eq!(ran, Ok((Partial::new(semicolon), &b"123abc"[..]))); fn ys(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - space.recognize().parse_next(i) + space.recognize().parse_peek(i) } let rs = ys(Partial::new(&b" \t;"[..])); assert_eq!(rs, Ok((Partial::new(semicolon), &b" \t"[..]))); fn yms(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - multispace.recognize().parse_next(i) + multispace.recognize().parse_peek(i) } let rms = yms(Partial::new(&b" \t\r\n;"[..])); assert_eq!(rms, Ok((Partial::new(semicolon), &b" \t\r\n"[..]))); @@ -297,7 +298,7 @@ fn partial_recognize() { #[test] fn partial_take_while0() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_while(0.., AsChar::is_alpha).parse_next(i) + take_while(0.., AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"abcd"[..]; @@ -313,7 +314,7 @@ fn partial_take_while0() { #[test] fn partial_take_while1() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_while(1.., AsChar::is_alpha).parse_next(i) + take_while(1.., AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"abcd"[..]; @@ -326,7 +327,7 @@ fn partial_take_while1() { assert_eq!( f(Partial::new(d)), Err(ErrMode::Backtrack(error_position!( - Partial::new(d), + &Partial::new(d), ErrorKind::Slice ))) ); @@ -335,7 +336,7 @@ fn partial_take_while1() { #[test] fn partial_take_while_m_n() { fn x(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_while(2..=4, AsChar::is_alpha).parse_next(i) + take_while(2..=4, AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"a"[..]; @@ -355,7 +356,7 @@ fn partial_take_while_m_n() { assert_eq!( x(Partial::new(f)), Err(ErrMode::Backtrack(error_position!( - Partial::new(f), + &Partial::new(f), ErrorKind::Slice ))) ); @@ -364,7 +365,7 @@ fn partial_take_while_m_n() { #[test] fn partial_take_till0() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_till0(AsChar::is_alpha).parse_next(i) + take_till0(AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"abcd"[..]; @@ -386,7 +387,7 @@ fn partial_take_till0() { #[test] fn partial_take_till1() { fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_till1(AsChar::is_alpha).parse_next(i) + take_till1(AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"abcd"[..]; @@ -397,7 +398,7 @@ fn partial_take_till1() { assert_eq!( f(Partial::new(b)), Err(ErrMode::Backtrack(error_position!( - Partial::new(b), + &Partial::new(b), ErrorKind::Slice ))) ); @@ -411,7 +412,7 @@ fn partial_take_till1() { #[test] fn partial_take_while_utf8() { fn f(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(0.., |c| c != '點').parse_next(i) + take_while(0.., |c| c != '點').parse_peek(i) } assert_eq!( @@ -429,7 +430,7 @@ fn partial_take_while_utf8() { ); fn g(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(0.., |c| c == '點').parse_next(i) + take_while(0.., |c| c == '點').parse_peek(i) } assert_eq!( @@ -446,7 +447,7 @@ fn partial_take_while_utf8() { #[test] fn partial_take_till0_utf8() { fn f(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_till0(|c| c == '點').parse_next(i) + take_till0(|c| c == '點').parse_peek(i) } assert_eq!( @@ -464,7 +465,7 @@ fn partial_take_till0_utf8() { ); fn g(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_till0(|c| c != '點').parse_next(i) + take_till0(|c| c != '點').parse_peek(i) } assert_eq!( @@ -481,7 +482,7 @@ fn partial_take_till0_utf8() { #[test] fn partial_take_utf8() { fn f(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take(3_usize).parse_next(i) + take(3_usize).parse_peek(i) } assert_eq!( @@ -501,7 +502,7 @@ fn partial_take_utf8() { assert_eq!(f(Partial::new("a點b")), Ok((Partial::new(""), "a點b"))); fn g(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(0.., |c| c == '點').parse_next(i) + take_while(0.., |c| c == '點').parse_peek(i) } assert_eq!( @@ -518,7 +519,7 @@ fn partial_take_utf8() { #[test] fn partial_take_while_m_n_utf8_fixed() { fn parser(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(1, |c| c == 'A' || c == '😃').parse_next(i) + take_while(1, |c| c == 'A' || c == '😃').parse_peek(i) } assert_eq!(parser(Partial::new("A!")), Ok((Partial::new("!"), "A"))); assert_eq!(parser(Partial::new("😃!")), Ok((Partial::new("!"), "😃"))); @@ -527,7 +528,7 @@ fn partial_take_while_m_n_utf8_fixed() { #[test] fn partial_take_while_m_n_utf8_range() { fn parser(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(1..=2, |c| c == 'A' || c == '😃').parse_next(i) + take_while(1..=2, |c| c == 'A' || c == '😃').parse_peek(i) } assert_eq!(parser(Partial::new("A!")), Ok((Partial::new("!"), "A"))); assert_eq!(parser(Partial::new("😃!")), Ok((Partial::new("!"), "😃"))); @@ -536,7 +537,7 @@ fn partial_take_while_m_n_utf8_range() { #[test] fn partial_take_while_m_n_utf8_full_match_fixed() { fn parser(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(1, |c: char| c.is_alphabetic()).parse_next(i) + take_while(1, |c: char| c.is_alphabetic()).parse_peek(i) } assert_eq!(parser(Partial::new("øn")), Ok((Partial::new("n"), "ø"))); } @@ -544,7 +545,7 @@ fn partial_take_while_m_n_utf8_full_match_fixed() { #[test] fn partial_take_while_m_n_utf8_full_match_range() { fn parser(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - take_while(1..=2, |c: char| c.is_alphabetic()).parse_next(i) + take_while(1..=2, |c: char| c.is_alphabetic()).parse_peek(i) } assert_eq!(parser(Partial::new("øn")), Ok((Partial::new(""), "øn"))); } @@ -553,10 +554,10 @@ fn partial_take_while_m_n_utf8_full_match_range() { #[cfg(feature = "std")] fn partial_recognize_take_while0() { fn x(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - take_while(0.., AsChar::is_alphanum).parse_next(i) + take_while(0.., AsChar::is_alphanum).parse_peek(i) } fn y(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - x.recognize().parse_next(i) + unpeek(x).recognize().parse_peek(i) } assert_eq!( x(Partial::new(&b"ab."[..])), @@ -573,7 +574,7 @@ fn partial_length_bytes() { use crate::binary::le_u8; fn x(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - length_data(le_u8).parse_next(i) + length_data(le_u8).parse_peek(i) } assert_eq!( x(Partial::new(b"\x02..>>")), @@ -593,8 +594,8 @@ fn partial_length_bytes() { ); fn y(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - let (i, _) = "magic".parse_next(i)?; - length_data(le_u8).parse_next(i) + let (i, _) = "magic".parse_peek(i)?; + length_data(le_u8).parse_peek(i) } assert_eq!( y(Partial::new(b"magic\x02..>>")), @@ -618,7 +619,7 @@ fn partial_length_bytes() { #[test] fn partial_case_insensitive() { fn test(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - tag_no_case("ABcd").parse_next(i) + tag_no_case("ABcd").parse_peek(i) } assert_eq!( test(Partial::new(&b"aBCdefgh"[..])), @@ -639,20 +640,20 @@ fn partial_case_insensitive() { assert_eq!( test(Partial::new(&b"Hello"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"Hello"[..]), + &Partial::new(&b"Hello"[..]), ErrorKind::Tag ))) ); assert_eq!( test(Partial::new(&b"Hel"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"Hel"[..]), + &Partial::new(&b"Hel"[..]), ErrorKind::Tag ))) ); fn test2(i: Partial<&str>) -> IResult<Partial<&str>, &str> { - tag_no_case("ABcd").parse_next(i) + tag_no_case("ABcd").parse_peek(i) } assert_eq!( test2(Partial::new("aBCdefgh")), @@ -673,14 +674,14 @@ fn partial_case_insensitive() { assert_eq!( test2(Partial::new("Hello")), Err(ErrMode::Backtrack(error_position!( - Partial::new("Hello"), + &Partial::new("Hello"), ErrorKind::Tag ))) ); assert_eq!( test2(Partial::new("Hel")), Err(ErrMode::Backtrack(error_position!( - Partial::new("Hel"), + &Partial::new("Hel"), ErrorKind::Tag ))) ); @@ -689,10 +690,10 @@ fn partial_case_insensitive() { #[test] fn partial_tag_fixed_size_array() { fn test(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - tag([0x42]).parse_next(i) + tag([0x42]).parse_peek(i) } fn test2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - tag(&[0x42]).parse_next(i) + tag(&[0x42]).parse_peek(i) } let input = Partial::new(&[0x42, 0x00][..]); assert_eq!(test(input), Ok((Partial::new(&b"\x00"[..]), &b"\x42"[..]))); diff --git a/vendor/winnow/src/trace/internals.rs b/vendor/winnow/src/trace/internals.rs index 3a10204b7..b990ae7c7 100644 --- a/vendor/winnow/src/trace/internals.rs +++ b/vendor/winnow/src/trace/internals.rs @@ -87,17 +87,13 @@ pub fn start<I: Stream>( }; let call_column = format!("{:depth$}> {name}{count}", ""); - let eof_offset = input.eof_offset(); - let offset = input.offset_at(input_width).unwrap_or(eof_offset); - let (_, slice) = input.next_slice(offset); - // The debug version of `slice` might be wider, either due to rendering one byte as two nibbles or // escaping in strings. - let mut debug_slice = format!("{:#?}", slice); + let mut debug_slice = format!("{:#?}", input.raw()); let (debug_slice, eof) = if let Some(debug_offset) = debug_slice .char_indices() .enumerate() - .find_map(|(pos, (offset, _))| (input_width <= pos).then(|| offset)) + .find_map(|(pos, (offset, _))| (input_width <= pos).then_some(offset)) { debug_slice.truncate(debug_offset); let eof = ""; @@ -129,7 +125,7 @@ pub fn end( depth: usize, name: &dyn crate::lib::std::fmt::Display, count: usize, - consumed: Option<usize>, + consumed: usize, severity: Severity, ) { let gutter_style = anstyle::Style::new().bold(); @@ -146,7 +142,7 @@ pub fn end( let (status_style, status) = match severity { Severity::Success => { let style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Green.into())); - let status = format!("+{}", consumed.unwrap_or_default()); + let status = format!("+{}", consumed); (style, status) } Severity::Backtrack => ( diff --git a/vendor/winnow/src/trace/mod.rs b/vendor/winnow/src/trace/mod.rs index e5eaf9451..316733e9a 100644 --- a/vendor/winnow/src/trace/mod.rs +++ b/vendor/winnow/src/trace/mod.rs @@ -26,23 +26,23 @@ compile_error!("`debug` requires `std`"); /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed, IResult}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::token::take_while; /// # use winnow::stream::AsChar; /// # use winnow::prelude::*; /// use winnow::trace::trace; /// -/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// fn short_alpha<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8], InputError<&'s [u8]>> { /// trace("short_alpha", /// take_while(3..=6, AsChar::is_alpha) /// ).parse_next(s) /// } /// -/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); -/// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); -/// assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); -/// assert_eq!(short_alpha(b"ed"), Err(ErrMode::Backtrack(Error::new(&b"ed"[..], ErrorKind::Slice)))); -/// assert_eq!(short_alpha(b"12345"), Err(ErrMode::Backtrack(Error::new(&b"12345"[..], ErrorKind::Slice)))); +/// assert_eq!(short_alpha.parse_peek(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(short_alpha.parse_peek(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); +/// assert_eq!(short_alpha.parse_peek(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(short_alpha.parse_peek(b"ed"), Err(ErrMode::Backtrack(InputError::new(&b"ed"[..], ErrorKind::Slice)))); +/// assert_eq!(short_alpha.parse_peek(b"12345"), Err(ErrMode::Backtrack(InputError::new(&b"12345"[..], ErrorKind::Slice)))); /// ``` #[cfg_attr(not(feature = "debug"), allow(unused_variables))] #[cfg_attr(not(feature = "debug"), allow(unused_mut))] @@ -54,21 +54,14 @@ pub fn trace<I: Stream, O, E>( #[cfg(feature = "debug")] { let mut call_count = 0; - move |i: I| { + move |i: &mut I| { let depth = internals::Depth::new(); - let original = i.clone(); - internals::start(*depth, &name, call_count, &original); + let original = i.checkpoint(); + internals::start(*depth, &name, call_count, i); let res = parser.parse_next(i); - let consumed = res.as_ref().ok().map(|(i, _)| { - if i.eof_offset() == 0 { - // Sometimes, an unrelated empty string is returned which can break `offset_to` - original.eof_offset() - } else { - original.offset_to(i) - } - }); + let consumed = i.offset_from(&original); let severity = internals::Severity::with_result(&res); internals::end(*depth, &name, call_count, consumed, severity); call_count += 1; |