pars/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4//! # Parser-combinator library for Rust.
5//! `pars` is a general purpose parser-combinator library, with support for `no_std`.
6//! `pars` is heavily inspired by [`nom`](https://crates.io/crates/nom), but `pars`
7//! aims to generalize more over different kinds of input and provide more
8//! Rust-idiomatic and ergonomic combinators. `pars` also provides direct support for
9//! higher level concepts such as Unicode and regular expressions.
10//!
11//! # Contents
12//! * [Example](#example) - Jump directly into an example parser implementation
13//! * [Parser Combinators](#parser-combinators) - A brief introduction to
14//!   parser-combinators
15//! * [Parser Input](#parser-input) - An overview of how parseable input works in
16//!   `pars`
17//! * [Defining a Parser](#defining-a-parser) - How to write your own parser
18//! * [Defining a Combinator](#defining-a-combinator) - How to write your own
19//!   combinator
20//! * [Parsing Errors](#parsing-errors) - The `pars` error system and how to define
21//!   your own error types
22//! * [Features](#features) - Available crate features
23//!
24//! # Example
25//! ```
26//! use pars::prelude::*;
27//! use pars::ErrorKind;
28//! use pars::unicode::{
29//!     strict::{char as uchar, verbatim},
30//!     UnicodeInput as UInput, PResult,
31//! };
32//!
33//! #[derive(Debug, PartialEq)]
34//! struct Color {
35//!     red: u8,
36//!     green: u8,
37//!     blue: u8,
38//! }
39//!
40//! fn hex_digit<I: UInput>(input: I) -> PResult<u8, I> {
41//!     uchar.try_map(|ch| match ch {
42//!         '0'..='9' => Ok((ch as u8) - b'0'),
43//!         'a'..='f' => Ok((ch as u8) - b'a' + 10),
44//!         'A'..='F' => Ok((ch as u8) - b'A' + 10),
45//!         _ => Err(ErrorKind::InvalidInput),
46//!     }).parse(input)
47//! }
48//!
49//! fn hex_byte<I: UInput>(input: I) -> PResult<u8, I> {
50//!     hex_digit.array().map(|[d0, d1]| (d0 << 4) | d1).parse(input)
51//! }
52//!
53//! fn hex_color<I: UInput>(input: I) -> PResult<Color, I> {
54//!     prefix(verbatim("#"), hex_byte.array())
55//!         .map(|[r, g, b]| Color {
56//!             red: r,
57//!             green: g,
58//!             blue: b,
59//!         })
60//!         .parse(input)
61//! }
62//!
63//! fn main() {
64//!     assert_eq!(
65//!         hex_color.parse("#2F14DF"),
66//!         Ok(Success(Color {
67//!             red: 0x2f,
68//!             green: 0x14,
69//!             blue: 0xdf,
70//!         }, ""))
71//!     );
72//! }
73//! ```
74//!
75//! # Parser Combinators
76//! Classically, parsers are defined as a grammar that parser generators, such as
77//! `lex` and `yacc`, will transpile to an implementation of a state machine. This
78//! generally produces the most efficient parsers, to be sure. Some popular parser
79//! generators for Rust include [`pest`](https://crates.io/crates/pest),
80//! [`LALRPOP`](https://crates.io/crates/lalrpop), and
81//! [`peg`](https://crates.io/crates/peg).
82//!
83//! A parser combinator, on the other hand, is generally a library of parser building
84//! blocks for building a recursive descent parser. The trade off here is that we
85//! gain the ability to write plain Rust (in the case of `pars`) as well the ability
86//! to easily reuse parsers to build more complex parsers, but at the costs associated
87//! with recursive descent parsers. The downside of recursive descent parsers is that
88//! there is no limit to how far back the parser may need to backtrack in the input
89//! stream during parsing, and thus are generally not as efficient as parser produced
90//! by parser generators. But note that, for example, most practical compilers are
91//! implemented with a recursive descent parser because of their power and
92//! flexibility, despite not being the most efficient approach. Also note that in
93//! some cases, it is possible define a complex parser without any backtracking with
94//! a little extra effort. See the example
95//! [`msgpack_alt.rs`](https://github.com/Vociferix/pars/blob/master/examples/msgpack_alt.rs)
96//! in the crate repo for an example of a parser with no backtracking.
97//!
98//! # Parser Input
99//! In `pars` a parser is defined as a function that accepts a stream of symbols as a
100//! parameter, and returns either a parse success or parse failure. A stream of
101//! symbols is generalized by the [`Input`] trait. [`Input`] is very similar to the
102//! [`Iterator`] trait in the Rust standard library, but it has some extra
103//! properties. [`Input`] must also implement [`Clone`], and it must be possible to
104//! test whether two [`Input`] objects are at the same stream position. Note that
105//! for good performance, the [`Clone`] implementation should be cheap, but it is
106//! not required that it implement [`Copy`].
107//!
108//! [`Input`] is automatically implemented for slice references, `&[T]` and `&str`.
109//! To illustrate the properties of an [`Input`] consider `input: &[u8]`. Advancing
110//! the input is equivalent to `input = &input[1..]`. Testing that two inputs are
111//! at the same position is just testing that the underlying pointers are equal,
112//! `input.as_ptr() == other_input.as_ptr()`. Also, `&[u8]` clearly has a cheap
113//! [`Clone`] implementation.
114//!
115//! See the documentation for [`Input`] for more details.
116//!
117//! `pars` also provides the [`IntoInput`] trait, which is similarly analogous to
118//! the [`IntoIterator`] trait in the Rust standard library. The [`Parse::parse`]
119//! function accepts a value implementing [`IntoInput`] instead of [`Input`] for
120//! the convience of the caller. For example, [`&Vec<u8>`](alloc::vec::Vec),
121//! [`&Box<[u8]>`](alloc::boxed::Box), `&[u8; N]` all implement [`IntoIterator`]
122//! and will convert to the input type [`&[u8]`]([u8]).
123//!
124//! See the documentation for [`IntoInput`] for more details.
125//!
126//! Since [`Input`] does not have any constraints on what a "symbol" can be, `pars`
127//! can be adapted to consume pretty much any kind of input stream. For example, in
128//! some cases it might be desirable to build a lexer (or scanner) to interpret the
129//! source input as a stream of tokens, and parse the token stream with `pars`
130//! instead of the source input. Sufficiently complex parsers, such as compiler
131//! frontends, generally prefer this approach. The lexer just needs to provide an
132//! interface with a type that implements [`Input`]. Note that it will usually
133//! be necessary for the lexer (or whatever the custom input source is) to be
134//! "multi-pass" in order to support parser backtracking.
135//!
136//! TODO: Include a custom input example.
137//!
138//! # Defining a Parser
139//! Most user defined parsers should be implemented as a plain function. Types
140//! implementing [`Fn(I) -> PResult<T, I, E>`](core::ops::Fn), where `I`
141//! implements [`Input`] and `E` implements [`Error<I>`] automatically implement
142//! [`Parse<I, Parsed = T, Error = E>`](Parse). Closures can also be used to
143//! define a parser inline, but note that occaisionally closures may need some
144//! type hinting assistance to compile properly as a parser.
145//!
146//! ```
147//! use pars::{Input, PResult, Success, bytes::Error, Parse};
148//!
149//! // `my_parser` implements `Parse<I, Parsed = i32, Error = Error<I>>`
150//! fn my_parser<I: Input>(input: I) -> PResult<i32, I, Error<I>> {
151//!     Success(42, input).into()
152//! }
153//! ```
154//!
155//! When needed, [`Parse`] can always be implemented manually.
156//!
157//! # Defining a Combinator
158//! Occaisionly, there is a need to implement a custom combinator for reuse. The
159//! simplest way is to define a function returning [`impl Parse`](Parse).
160//!
161//! ```
162//! use pars::{basic::array, Input, Parse};
163//!
164//! // Creates a parser that applies `parser` twice and
165//! // returns the parsed values as an array of length 2.
166//! const fn twice<I, P>(parser: P)
167//!     -> impl Parse<I, Parsed = [P::Parsed; 2], Error = P::Error>
168//! where
169//!     I: Input,
170//!     P: Parse<I>,
171//! {
172//!     array(parser)
173//! }
174//! ```
175//!
176//! It is usually necessary for the [`impl Parse`](Parse) return to explicitly define
177//! the [`Parsed`](Parse::Parsed) and [`Error`](Parse::Error) associated types, in
178//! order for type inference to work properly in conjunction with other combinators.
179//!
180//! # Parsing Errors
181//! `pars` does not define a general purpose error for parsing failures. Instead, it
182//! provides the [`Error`] trait. Users can define their own parsing error types, and
183//! `pars` provides a basic error type for each of the modules
184//! [`bytes`](./bytes/struct.Error.html), [`ascii`](./ascii/struct.Error.html), and
185//! [`unicode`](./unicode/struct.Error.html).
186//!
187//! A parsing error, aside from other error information it communicates, contains the
188//! input position at which the error occurred. The generic parsers and combinators
189//! defined throughout `pars` use the interface described by the [`Error`] trait to
190//! instantiate an error as needed, also providing where the error occurred. Note
191//! that the input position described by an [`Error`] may be different from the input
192//! position described by a [`Failure`] within a [`PResult`]. The [`Failure`] always
193//! describes the remaining input that has not been successfully parsed, but the
194//! error may have occurred further into the input and caused the parser to
195//! backtrack.
196//!
197//! An important partner to the [`Error`] trait is the [`ErrorSeed`] trait.
198//! Essentially, a type implementing [`ErrorSeed`] is a parsing error without the
199//! accompanying input position of the error. Such a type can be combined with an
200//! input position via the [`ErrorSeed`] trait to create an actual error.
201//!
202//! Many combinators defined in `pars`, particularly those named `try_*`, require the
203//! user to provide a function or closure that returns a [`Result`]. The `Err`
204//! variant of that [`Result`] must be convertible to an [`Error`] via [`ErrorSeed`].
205//! The reason for this is combinators generally abstract away the input stream,
206//! letting the user focus more on how parsers fit together and produce a parsed
207//! value. When some intermediate operation is fallible, being able to return an
208//! [`ErrorSeed`] simplifies matters by letting the combinator implementation deal
209//! with tracking input positions.
210//!
211//! A typical user defined error will be defined as a pair of types: One implementing
212//! [`ErrorSeed`] and another implementing [`Error`].
213//!
214//! ```
215//! # use pars::{Input, ErrorSeed, Error};
216//! #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
217//! struct MyErrorKind(&'static str);
218//!
219//! #[derive(Debug, Clone, PartialEq, Eq, Hash)]
220//! struct MyError<I: Input> {
221//!     msg: &'static str,
222//!     pos: I,
223//! }
224//!
225//! impl<I: Input> ErrorSeed<I, MyError<I>> for MyErrorKind {
226//!     fn into_error(self, pos: I) -> MyError<I> {
227//!         MyError {
228//!             msg: self.0,
229//!             pos,
230//!         }
231//!     }
232//! }
233//!
234//! impl<I: Input> Error<I> for MyError<I> {
235//!     fn need_more_input(pos: I) -> Self {
236//!         MyErrorKind("need more input").into_error(pos)
237//!     }
238//!
239//!     fn expected_eof(pos: I) -> Self {
240//!         MyErrorKind("expected end of input").into_error(pos)
241//!     }
242//!
243//!     fn invalid_input(pos: I) -> Self {
244//!         MyErrorKind("invalid input").into_error(pos)
245//!     }
246//!
247//!     fn position(&self) -> &I { &self.pos }
248//! }
249//! ```
250//!
251//! For convenience, [`ErrorKind`] is available for use as an [`ErrorSeed`] type that
252//! can be converted to any type implementing [`Error`].
253//!
254//! When combining a user defined error with parsers that use a different error type,
255//! such as [`bytes::u8`](./bytes/fn.u8.html), there are a few ways this can be dealt with.
256//! The simplest way is to use [`Parse::err_into`].
257//!
258//! ```
259//! # use pars::{bytes, Input, ErrorSeed, Error, Parse, PResult};
260//! # #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
261//! # struct MyErrorKind(&'static str);
262//! #
263//! # #[derive(Debug, Clone, PartialEq, Eq, Hash)]
264//! # struct MyError<I: Input> {
265//! #     msg: &'static str,
266//! #     pos: I,
267//! # }
268//! #
269//! # impl<I: Input> ErrorSeed<I, MyError<I>> for MyErrorKind {
270//! #     fn into_error(self, pos: I) -> MyError<I> {
271//! #         MyError {
272//! #             msg: self.0,
273//! #             pos,
274//! #         }
275//! #     }
276//! # }
277//! #
278//! # impl<I: Input> Error<I> for MyError<I> {
279//! #     fn need_more_input(pos: I) -> Self {
280//! #         MyErrorKind("need more input").into_error(pos)
281//! #     }
282//! #
283//! #     fn expected_eof(pos: I) -> Self {
284//! #         MyErrorKind("expected end of input").into_error(pos)
285//! #     }
286//! #
287//! #     fn invalid_input(pos: I) -> Self {
288//! #         MyErrorKind("invalid input").into_error(pos)
289//! #     }
290//! #
291//! #     fn position(&self) -> &I { &self.pos }
292//! # }
293//! // Given `MyError` defined previously.
294//! impl<I: Input> From<bytes::Error<I>> for MyError<I> {
295//!     fn from(err: bytes::Error<I>) -> Self {
296//!         match err.kind() {
297//!             bytes::ErrorKind::NeedMoreInput => Self::need_more_input(err.position().clone()),
298//!             bytes::ErrorKind::ExpectedEof => Self::expected_eof(err.position().clone()),
299//!             _ => Self::invalid_input(err.position().clone()),
300//!         }
301//!     }
302//! }
303//!
304//! fn my_u8<I: bytes::ByteInput>(input: I) -> PResult<u8, I, MyError<I>> {
305//!     // Changes `bytes::u8` to return `MyError` instead of `bytes::Error`
306//!     bytes::u8.err_into().parse(input)
307//! }
308//! ```
309//!
310//! When relying on a [`From`] implementation doesn't work [`Parse::map_err`] and
311//! [`Parse::map_res`] are also available to define error conversions separately for each
312//! instance.
313//!
314//! # Features
315//! Note that there is no `std` feature, since `pars` never makes use of types that are
316//! only available in `std`.
317//!
318//! * `alloc` - Enable usage of the [`alloc`] crate. This enables [`IntoInput`] impls
319//! for types such as [`&Vec<[T]>`](alloc::vec::Vec) and
320//! [`&String`](alloc::string::String). Enabled by default.
321//! * `bytes` - Enable the [`bytes`](./bytes) module, which provides parsers and utilities
322//! for byte streams.
323//! * `unicode` - Enable the [`unicode`](./unicode) module, which provides parsers and
324//! utilities for UTF-8, UTF-16, and UTF-32 streams.
325//! * `ascii` - Enable the [`ascii`](./ascii) module, which provides parsers and utilities
326//! for ASCII character streams.
327
328#[cfg(any(doc, feature = "alloc"))]
329extern crate alloc;
330
331mod input;
332mod span;
333
334pub mod basic;
335
336#[cfg(feature = "ascii")]
337pub mod ascii;
338#[cfg(feature = "bytes")]
339pub mod bytes;
340#[cfg(feature = "unicode")]
341pub mod unicode;
342
343pub use input::*;
344pub use span::*;
345
346extern crate self as pars;
347
348/// The `pars` prelude.
349pub mod prelude {
350    pub use super::basic::{
351        alt, constant, delimited, either, eof, error, pair, permutation, pop, prefix, remaining,
352        remaining_len, separated_pair, seq, suffix, take,
353    };
354    pub use super::{
355        Error as _, ErrorSeed as _, Failure, Input, IntoInput, PResultExt, Parse, Span, Success,
356    };
357}
358
359/// A parsing error.
360pub trait Error<I: Input>: Sized {
361    /// Creates an error representing the need for more input.
362    ///
363    /// Generic parsers will call this method to generate an error when parsing failed
364    /// due to reaching the end of input.
365    fn need_more_input(pos: I) -> Self;
366
367    /// Creates an error representing that the end of input was expected but not reached.
368    ///
369    /// Generic parsers will call this method to generate an error when parsing failed
370    /// because it expected to reach the end of the input, but there was more input
371    /// available after parsing was finished.
372    fn expected_eof(pos: I) -> Self;
373
374    /// Creates an error representing that the input is unparsable.
375    ///
376    /// Generic parsers will call this method to generate an error when parsing failed
377    /// due to a failed verification of a symbol or parsed value.
378    fn invalid_input(pos: I) -> Self;
379
380    /// Gets the input position where the error occured.
381    fn position(&self) -> &I;
382}
383
384/// A parsing error descriptor that can be used to build a parsing error.
385///
386/// Most types implementing [`ErrorSeed`] are typically an "error kind" `enum. The most
387/// generic [`ErrorSeed`] is [`ErrorKind`]. However, an [`ErrorSeed`] type can be
388/// anything as long as it can be combined with some [`Input`] to create an [`Error`].
389///
390/// An [`ErrorSeed`] is generally used in fallible combinators (i.e. any `Parse::try_*`
391/// combinator) to represent an error with the input stream abstracted away. For
392/// example:
393/// ```
394/// # use pars::{bytes, Parse, ErrorKind};
395/// # fn my_parser(input: &[u8]) -> pars::PResult<u8, &[u8], bytes::Error<&[u8]>> {
396/// bytes::u8.try_map(|byte| {
397///     if byte < 0x80 {
398///         Ok(byte)
399///     } else {
400///         // `try_map` converts this to a `bytes::Error`
401///         Err(ErrorKind::InvalidInput)
402///     }
403/// })
404/// # .parse(input)
405/// # }
406/// ```
407///
408/// [`ErrorSeed`] is generic over both the input type and error type, so any
409/// [`ErrorSeed`] can convert multiple error types if desired. [`ErrorKind`], for
410/// example, can convert to any type implementing [`Error`] for any input type.
411pub trait ErrorSeed<I: Input, E: Error<I>> {
412    /// Combines this error seed with input to create a parsing error.
413    fn into_error(self, pos: I) -> E;
414}
415
416/// A type implementing [`ErrorSeed`] for all error and input types.
417#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
418pub enum ErrorKind {
419    /// Converts to an error using [`Error::need_more_input`].
420    NeedMoreInput,
421    /// Converts to an error using [`Error::expected_eof`].
422    ExpectedEof,
423    /// Converts to an error using [`Error::invalid_input`].
424    InvalidInput,
425}
426
427/// Type returned by a parser when parsing succeeds.
428///
429/// [`Success`] is returned in the [`Ok`] variant of a [`PResult`] to signal that
430/// parsing succeeded. [`Success`] is a named tuple containing the parsed value
431/// (see [`Parse::Parsed`]) and the remaining unparsed input.
432#[derive(Debug, Clone)]
433pub struct Success<T, I>(
434    /// The parsed value
435    pub T,
436    /// The remaining unparsed input
437    pub I,
438);
439
440/// Type returned by a parser when parsing fails.
441///
442/// [`Failure`] is returned in the [`Err`] variant of a [`PResult`] to signal that
443/// parsing failed. [`Failure`] is a named tuple containing the parsing error
444/// (see [`Error`] and [`Parse::Error`]) and the input that failed to parse. The
445/// input member should always have the same position as the input that was
446/// originally provided to the parser - failure to do so is a bug, but not UB.
447#[derive(Debug, Clone)]
448pub struct Failure<E, I>(
449    /// The parsing error
450    pub E,
451    /// The input that failed to parse
452    pub I,
453);
454
455/// The [`Result`] type returned by a parser.
456///
457/// All parsers return either a [`Success`] when parsing succeeds, or a
458/// [`Failure`] when parsing fails. The input type that is returned is the same
459/// in either case, and is the same type as was provided to the parser to parse.
460///
461/// Unlike a typical [`Result`] alias in rust, [`PResult`] has three generic
462/// parameters: The parsed value type, the input type, and the error type. Since
463/// this differs from the standard convention, the alias has a slightly different
464/// name to clearly indicate the break from convention.
465pub type PResult<T, I, E> = Result<Success<T, I>, Failure<E, I>>;
466
467/// Trait implemented by all parsers.
468///
469/// The [`Parse`] trait is where the logic of parsing is implemented. However,
470/// most `pars` users will not implement [`Parse`] directly. See the crate level
471/// documentation for examples of user defined parsers.
472pub trait Parse<I: Input> {
473    /// The value type that is produced by the parser on success.
474    type Parsed: Sized;
475
476    /// The error type that is produced by the parser on failure.
477    type Error: Error<I>;
478
479    /// Parses the provided input.
480    fn parse<N>(&self, input: N) -> PResult<Self::Parsed, I, Self::Error>
481    where
482        N: IntoInput<Input = I>;
483
484    /// Creates a parser whose parsed result is transformed.
485    ///
486    /// The provided function, `map_fn`, is applied to the parsed result of
487    /// `self` if it parses successfully. The value returned from `map_fn`
488    /// is the parsed result of the new parser.
489    ///
490    /// If the the mapping operation could fail, use [`Parse::try_map`]
491    /// instead.
492    ///
493    /// See also [`basic::map`].
494    ///
495    /// # Example
496    /// ```
497    /// # use pars::prelude::*;
498    /// # use pars::bytes::{self, PResult};
499    /// fn inv_byte(input: &[u8]) -> PResult<u8, &[u8]> {
500    ///     bytes::u8.map(|byte| !byte).parse(input)
501    /// }
502    ///
503    /// assert_eq!(inv_byte.parse(b"\x0f").unwrap().0, 0xf0);
504    /// ```
505    #[inline]
506    fn map<F, R>(self, map_fn: F) -> impl Parse<I, Parsed = R, Error = Self::Error>
507    where
508        Self: Sized,
509        F: Fn(Self::Parsed) -> R,
510    {
511        basic::map(self, map_fn)
512    }
513
514    /// Creates a parser whose parsed result is fallibly transformed.
515    ///
516    /// The provided function, `map_fn`, is applied to the parsed result of
517    /// `self` if it parses successfully. `map_fn` is permitted to fail,
518    /// which is signaled with an [`Err`] return value. In thise case the
519    /// new parser fails, and the contained error is converted to an
520    /// [`Error`] via [`ErrorSeed`]. If an [`Ok`] value is returned, the
521    /// contained value becomes the parsed result of the new parser.
522    ///
523    /// If the mapping function cannot fail, use [`Parse::map`] instead.
524    ///
525    /// See also [`basic::try_map`].
526    ///
527    /// # Example
528    /// ```
529    /// # use pars::prelude::*;
530    /// # use pars::bytes::{self, PResult};
531    /// # use pars::ErrorKind;
532    /// fn inv_byte(input: &[u8]) -> PResult<u8, &[u8]> {
533    ///     bytes::u8.try_map(|byte| {
534    ///         if byte < 0x80 {
535    ///             Ok(!byte)
536    ///         } else {
537    ///             Err(ErrorKind::InvalidInput)
538    ///         }
539    ///     }).parse(input)
540    /// }
541    ///
542    /// assert_eq!(inv_byte.parse(b"\x0f").unwrap().0, 0xf0);
543    /// assert!(inv_byte.parse(b"\xf0").is_err());
544    /// ```
545    #[inline]
546    fn try_map<F, R, S>(self, map_fn: F) -> impl Parse<I, Parsed = R, Error = Self::Error>
547    where
548        Self: Sized,
549        S: ErrorSeed<I, Self::Error>,
550        F: Fn(Self::Parsed) -> Result<R, S>,
551    {
552        basic::try_map(self, map_fn)
553    }
554
555    /// Creates a parser that transforms a parsing error.
556    ///
557    /// The provided function, `map_err_fn`, is applied the parsing error of
558    /// `self` if its parsing fails. The value returned from `map_err_fn`
559    /// becomes the parsing error of the new parser. Otherwise, if `self`
560    /// succeeds, its parsed result is the parsed result of the new parser.
561    ///
562    /// This combinator is most useful for converting between error types.
563    ///
564    /// See also [`basic::map_err`].
565    ///
566    /// # Example
567    /// ```
568    /// # use pars::prelude::*;
569    /// # use pars::{bytes, unicode, PResult};
570    /// fn my_parser(input: &[u8]) -> PResult<u8, &[u8], unicode::Error<&[u8]>> {
571    ///     bytes::u8.map_err(|err: bytes::Error<&[u8]>| {
572    ///         match err.kind() {
573    ///             bytes::ErrorKind::NeedMoreInput => unicode::ErrorKind::NeedMoreInput,
574    ///             bytes::ErrorKind::ExpectedEof => unicode::ErrorKind::ExpectedEof,
575    ///             _ => unicode::ErrorKind::InvalidInput,
576    ///         }.into_error(*err.position())
577    ///     }).parse(input)
578    /// }
579    ///
580    /// assert!(my_parser.parse(b"hello") == Ok(Success(b'h', b"ello")));
581    /// assert!(my_parser.parse(b"") == Err(Failure(
582    ///     unicode::ErrorKind::NeedMoreInput.into_error(b"".as_slice()), b"")));
583    /// ```
584    #[inline]
585    fn map_err<F, R>(self, map_err_fn: F) -> impl Parse<I, Parsed = Self::Parsed, Error = R>
586    where
587        Self: Sized,
588        F: Fn(Self::Error) -> R,
589        R: Error<I>,
590    {
591        basic::map_err(self, map_err_fn)
592    }
593
594    /// Creates a parser whose result is transformed.
595    ///
596    /// The provided function, `map_res_fn`, accepts a [`Result`] containing either the
597    /// successfully parsed value of `self` or the parsing error returned by `self`.
598    /// `map_res_fn` then returns a new [`Result`], which will contain either the new
599    /// successful parsed value or a new parsing error.
600    ///
601    /// Unlike [`Parse::map`] or [`Parse::map_err`], the mapping function may choose to
602    /// convert an [`Ok`] into and [`Err`] or an [`Err`] into an [`Ok`].
603    ///
604    /// See also [`basic::map_res`].
605    ///
606    /// # Example
607    /// ```
608    /// # use pars::prelude::*;
609    /// # use pars::bytes::{self, PResult, Error, ErrorKind};
610    /// fn my_parser(input: &[u8]) -> PResult<u8, &[u8]> {
611    ///     bytes::u8.map_res(|res| match res {
612    ///         Ok(byte) if byte >= 0x80 => {
613    ///             Err(ErrorKind::InvalidInput.into_error(b"".as_slice()))
614    ///         },
615    ///         Ok(byte) => Ok(byte),
616    ///         Err(err) if err.kind() == ErrorKind::NeedMoreInput => Ok(0),
617    ///         Err(err) => Err(err),
618    ///     }).parse(input)
619    /// }
620    ///
621    /// assert!(my_parser.parse(b"hello") == Ok(Success(b'h', b"ello")));
622    /// assert_eq!(
623    ///     my_parser.parse(b"\xffhello").unwrap_err().0.kind(),
624    ///     ErrorKind::InvalidInput,
625    /// );
626    /// assert!(my_parser.parse(b"") == Ok(Success(b'\x00', b"")));
627    /// ```
628    #[inline]
629    fn map_res<F, R, E>(self, map_res_fn: F) -> impl Parse<I, Parsed = R, Error = E>
630    where
631        Self: Sized,
632        F: Fn(Result<Self::Parsed, Self::Error>) -> Result<R, E>,
633        E: Error<I>,
634    {
635        basic::map_res(self, map_res_fn)
636    }
637
638    /// Creates a parser whose parsed result or parsing error are converted via [`Into`].
639    ///
640    /// If `self` parses successfully, its result value is converted to `R` using the
641    /// [`Into<R>`] trait. If `self` fails, the returned parsing error is converted to
642    /// `E` using the [`Into<E>`] trait.
643    ///
644    /// See also [`basic::res_into`].
645    ///
646    /// # Example
647    /// ```
648    /// # use pars::prelude::*;
649    /// # use pars::{PResult, Error};
650    /// # use pars::bytes;
651    /// #[derive(Debug, PartialEq)]
652    /// struct MyError<'a>(&'a [u8]);
653    ///
654    /// # /*
655    /// impl<'a> Error<&'a [u8]> for MyError<'a> { ... }
656    /// impl<'a> From<bytes::Error<&'a [u8]>> for MyError<'a> { ... }
657    /// # */
658    /// # impl<'a> Error<&'a [u8]> for MyError<'a> {
659    /// #     fn need_more_input(pos: &'a [u8]) -> Self { Self(pos) }
660    /// #     fn expected_eof(pos: &'a [u8]) -> Self { Self(pos) }
661    /// #     fn invalid_input(pos: &'a [u8]) -> Self { Self(pos) }
662    /// #     fn position(&self) -> &&'a [u8] { &self.0 }
663    /// # }
664    /// # impl<'a> From<bytes::Error<&'a [u8]>> for MyError<'a> {
665    /// #     fn from(err: bytes::Error<&'a [u8]>) -> Self {
666    /// #         Self(*err.position())
667    /// #     }
668    /// # }
669    ///
670    /// fn my_parser(input: &[u8]) -> PResult<Vec<u8>, &[u8], MyError<'_>> {
671    ///     bytes::u8.array::<5>().res_into().parse(input)
672    /// }
673    ///
674    /// assert!(my_parser.parse(b"hello world") == Ok(Success(Vec::from(b"hello"), b" world")));
675    /// assert!(my_parser.parse(b"hi") == Err(Failure(MyError(b""), b"hi")));
676    /// ```
677    #[inline]
678    fn res_into<R, E>(self) -> impl Parse<I, Parsed = R, Error = E>
679    where
680        Self: Sized,
681        Self::Parsed: Into<R>,
682        Self::Error: Into<E>,
683        E: Error<I>,
684    {
685        basic::res_into(self)
686    }
687
688    /// Creates a parser whose parsed result is converted via [`Into`].
689    ///
690    /// If `self` parses successfully, its result value is converted to `R` using the
691    /// [`Into<R>`] trait.
692    ///
693    /// See also [`basic::ok_into`].
694    ///
695    /// # Example
696    /// ```
697    /// # use pars::prelude::*;
698    /// # use pars::bytes::{self, PResult};
699    /// fn my_parser(input: &[u8]) -> PResult<Vec<u8>, &[u8]> {
700    ///     bytes::u8.array::<5>().ok_into().parse(input)
701    /// }
702    ///
703    /// assert!(my_parser.parse(b"hello world") == Ok(Success(Vec::from(b"hello"), b" world")));
704    /// ```
705    #[inline]
706    fn ok_into<R>(self) -> impl Parse<I, Parsed = R, Error = Self::Error>
707    where
708        Self: Sized,
709        Self::Parsed: Into<R>,
710    {
711        basic::ok_into(self)
712    }
713
714    /// Creates a parser whose parsing error is converted via [`Into`].
715    ///
716    /// If `slef` fails to parse, the returned parsing error is converted to
717    /// `E` using the [`Into<E>`] trait.
718    ///
719    /// See also [`basic::err_into`].
720    ///
721    /// # Example
722    /// ```
723    /// # use pars::prelude::*;
724    /// # use pars::{PResult, Error};
725    /// # use pars::bytes;
726    /// #[derive(Debug, PartialEq)]
727    /// struct MyError<'a>(&'a [u8]);
728    ///
729    /// # /*
730    /// impl<'a> Error<&'a [u8]> for MyError<'a> { ... }
731    /// impl<'a> From<bytes::Error<&'a [u8]>> for MyError<'a> { ... }
732    /// # */
733    /// # impl<'a> Error<&'a [u8]> for MyError<'a> {
734    /// #     fn need_more_input(pos: &'a [u8]) -> Self { Self(pos) }
735    /// #     fn expected_eof(pos: &'a [u8]) -> Self { Self(pos) }
736    /// #     fn invalid_input(pos: &'a [u8]) -> Self { Self(pos) }
737    /// #     fn position(&self) -> &&'a [u8] { &self.0 }
738    /// # }
739    /// # impl<'a> From<bytes::Error<&'a [u8]>> for MyError<'a> {
740    /// #     fn from(err: bytes::Error<&'a [u8]>) -> Self {
741    /// #         Self(*err.position())
742    /// #     }
743    /// # }
744    ///
745    /// fn my_parser(input: &[u8]) -> PResult<u8, &[u8], MyError<'_>> {
746    ///     bytes::u8.err_into().parse(input)
747    /// }
748    ///
749    /// assert!(my_parser.parse(b"hello") == Ok(Success(b'h', b"ello")));
750    /// assert!(my_parser.parse(b"") == Err(Failure(MyError(b""), b"")));
751    #[inline]
752    fn err_into<E>(self) -> impl Parse<I, Parsed = Self::Parsed, Error = E>
753    where
754        Self: Sized,
755        Self::Error: Into<E>,
756        E: Error<I>,
757    {
758        basic::err_into(self)
759    }
760
761    /// Creates a parser whose parsed result is converted via [`TryInto`].
762    ///
763    /// If `self` parses successfully, its result value is converted to `R` using the
764    /// [`TryInto<R>`] trait. If [`TryInto`] fails, a parsing error is returned by
765    /// calling [`E::invalid_input`](Error::invalid_input). If `self` returns a parsing
766    /// error, that error is converted to `E` using the [`Into<E>`] trait.
767    ///
768    /// Note that the parsing error is not converted using [`TryInto`], but rather the
769    /// infallible [`Into`] trait. Only the successfully parsed result converts via
770    /// [`TryInto`].
771    ///
772    /// See also [`basic::res_try_into`].
773    ///
774    /// # Example
775    /// ```
776    /// # use pars::prelude::*;
777    /// # use pars::{PResult, Error};
778    /// # use pars::bytes;
779    /// #[derive(Debug, PartialEq)]
780    /// struct MyError<'a>(&'a [u8]);
781    ///
782    /// # /*
783    /// impl<'a> Error<&'a [u8]> for MyError<'a> { ... }
784    /// impl<'a> From<bytes::Error<&'a [u8]>> for MyError<'a> { ... }
785    /// # */
786    /// # impl<'a> Error<&'a [u8]> for MyError<'a> {
787    /// #     fn need_more_input(pos: &'a [u8]) -> Self { Self(pos) }
788    /// #     fn expected_eof(pos: &'a [u8]) -> Self { Self(pos) }
789    /// #     fn invalid_input(pos: &'a [u8]) -> Self { Self(pos) }
790    /// #     fn position(&self) -> &&'a [u8] { &self.0 }
791    /// # }
792    /// # impl<'a> From<bytes::Error<&'a [u8]>> for MyError<'a> {
793    /// #     fn from(err: bytes::Error<&'a [u8]>) -> Self {
794    /// #         Self(*err.position())
795    /// #     }
796    /// # }
797    ///
798    /// fn my_parser(input: &[u8]) -> PResult<char, &[u8], MyError<'_>> {
799    ///     bytes::be::u32.res_try_into().parse(input)
800    /// }
801    ///
802    /// assert!(my_parser.parse(b"\x00\x00\x00hello") == Ok(Success('h', b"ello")));
803    /// assert!(my_parser.parse(b"\xff\xff\xffhello")
804    ///     == Err(Failure(MyError(b"\xff\xff\xffhello"), b"\xff\xff\xffhello")));
805    /// assert!(my_parser.parse(b"") == Err(Failure(MyError(b""), b"")));
806    /// ```
807    #[inline]
808    fn res_try_into<R, E>(self) -> impl Parse<I, Parsed = R, Error = E>
809    where
810        Self: Sized,
811        Self::Parsed: core::convert::TryInto<R>,
812        Self::Error: Into<E>,
813        E: Error<I>,
814    {
815        basic::res_try_into(self)
816    }
817
818    /// Creates a parser whose parsed result is converted via [`TryInto`].
819    ///
820    /// If `self` parses successfully, its result value is converted to `R` using the
821    /// [`TryInto<R>`] trait. If [`TryInto`] fails, a parsing error is returned by calling
822    /// [`Error::invalid_input`].
823    ///
824    /// See also [`basic::ok_try_into`].
825    ///
826    /// # Example
827    /// ```
828    /// # use pars::prelude::*;
829    /// # use pars::bytes::{self, PResult};
830    /// fn my_parser(input: &[u8]) -> PResult<char, &[u8]> {
831    ///     bytes::be::u32.ok_try_into().parse(input)
832    /// }
833    ///
834    /// assert!(my_parser.parse(b"\x00\x00\x00hello") == Ok(Success('h', b"ello")));
835    /// assert!(my_parser.parse(b"\xff\xff\xffhello").is_err());
836    /// ```
837    #[inline]
838    fn ok_try_into<R>(self) -> impl Parse<I, Parsed = R, Error = Self::Error>
839    where
840        Self: Sized,
841        Self::Parsed: core::convert::TryInto<R>,
842    {
843        basic::ok_try_into(self)
844    }
845
846    /// Converts a parser to produce a different return value.
847    ///
848    /// If `self` parses successfully, its returned value is immediately dropped.
849    /// `with_fn` is then called to produce the new value. If `self` fails,
850    /// `with_fn` is not called.
851    ///
852    /// See also [`basic::with`].
853    ///
854    /// # Example
855    /// ```
856    /// # use pars::prelude::*;
857    /// # use pars::unicode::PResult;
858    /// use pars::unicode::strict::verbatim;
859    ///
860    /// #[derive(Debug, PartialEq)]
861    /// enum Token {
862    ///     Begin,
863    ///     End,
864    /// }
865    ///
866    /// fn begin(input: &str) -> PResult<Token, &str> {
867    ///     verbatim("BEGIN").with(|| Token::Begin).parse(input)
868    /// }
869    ///
870    /// fn end(input: &str) -> PResult<Token, &str> {
871    ///     verbatim("END").with(|| Token::End).parse(input)
872    /// }
873    ///
874    /// assert_eq!(begin.parse("BEGIN hello"), Ok(Success(Token::Begin, " hello")));
875    /// assert_eq!(end.parse("END world"), Ok(Success(Token::End, " world")));
876    /// ```
877    #[inline]
878    fn with<F, T>(self, with_fn: F) -> impl Parse<I, Parsed = T, Error = Self::Error>
879    where
880        Self: Sized,
881        F: Fn() -> T,
882    {
883        basic::with(self, with_fn)
884    }
885
886    /// Converts a parser to produce a different return value, via [`Default`].
887    ///
888    /// If `self` parses successfully, its returned value is immediately dropped.
889    /// The old returned value is replaced with a value of type `T`, produced by
890    /// calling [`Default::default()`].
891    ///
892    /// `parser.with_default()` is equivalent to `parser.with(Default::default)`
893    /// (see [`Parse::with`]).
894    ///
895    /// See also [`Parse::with_default`].
896    ///
897    /// # Example
898    /// ```
899    /// # use pars::prelude::*;
900    /// # use pars::unicode::PResult;
901    /// use pars::unicode::strict::verbatim;
902    ///
903    /// #[derive(Debug, PartialEq, Default)]
904    /// struct Token;
905    ///
906    /// fn token(input: &str) -> PResult<Token, &str> {
907    ///     verbatim("TOKEN").with_default().parse(input)
908    /// }
909    ///
910    /// assert_eq!(token.parse("TOKEN"), Ok(Success(Token, "")));
911    /// ```
912    #[inline]
913    fn with_default<T>(self) -> impl Parse<I, Parsed = T, Error = Self::Error>
914    where
915        Self: Sized,
916        T: Default,
917    {
918        basic::with_default(self)
919    }
920
921    /// Converts a parser to produce a different return value, via [`Clone`].
922    ///
923    /// If `self` parses successfully, its returned value is immediately dropped.
924    /// `value` is is cloned to replace the returned value.
925    ///
926    /// See also [`basic::with_value`].
927    ///
928    /// # Example
929    /// ```
930    /// # use pars::prelude::*;
931    /// # use pars::unicode::PResult;
932    /// use pars::unicode::strict::verbatim;
933    ///
934    /// #[derive(Debug, PartialEq, Clone)]
935    /// struct Token;
936    ///
937    /// fn token(input: &str) -> PResult<Token, &str> {
938    ///     verbatim("TOKEN").with_value(Token).parse(input)
939    /// }
940    ///
941    /// assert_eq!(token.parse("TOKEN"), Ok(Success(Token, "")));
942    /// ```
943    #[inline]
944    fn with_value<T>(self, value: T) -> impl Parse<I, Parsed = T, Error = Self::Error>
945    where
946        Self: Sized,
947        T: Clone,
948    {
949        basic::with_value(self, value)
950    }
951
952    /// Maps the result of a parser onto a combinator to produce a new parser.
953    ///
954    /// If `self` parses successfully, the result value is passed into
955    /// `combinator`. The remaining input after `self` succeeds is then
956    /// parsed by the parser returned by `combinator`.
957    ///
958    /// Generally, this combinator is useful for building a parser that should
959    /// parse differently depending on information earlier in the input stream.
960    ///
961    /// See also [`basic::flat_map`].
962    ///
963    /// # Example
964    /// ```
965    /// # use pars::prelude::*;
966    /// # use pars::bytes::{self, PResult};
967    /// // First byte is the length of the string, then that many
968    /// // more bytes is the string data.
969    /// fn pascal_str(input: &[u8]) -> PResult<&[u8], &[u8]> {
970    ///     bytes::u8.ok_into().flat_map(take).ok_into().parse(input)
971    /// }
972    ///
973    /// assert!(pascal_str.parse(b"\x05hello") == Ok(Success(b"hello", b"")));
974    /// assert!(pascal_str.parse(b"\x05hi").is_err());
975    /// ```
976    #[inline]
977    fn flat_map<F, P>(self, combinator: F) -> impl Parse<I, Parsed = P::Parsed, Error = Self::Error>
978    where
979        Self: Sized,
980        F: Fn(Self::Parsed) -> P,
981        P: Parse<I, Error = Self::Error>,
982    {
983        basic::flat_map(self, combinator)
984    }
985
986    /// Maps the result of a parser onto a combinator to produce a new parser.
987    ///
988    /// If `self` parses successfully, the result value is passed into
989    /// `combinator`. `combinator` then returns a [`Result`] containing either
990    /// a new parser or an error implementing [`ErrorSeed`]. If a parser is
991    /// returned, the remaining input from `self` is passed to the new
992    /// parser. Otherwise, the error is converted to a parsing error and
993    /// returned.
994    ///
995    /// See also [`basic::try_flat_map`].
996    ///
997    /// # Example
998    /// ```
999    /// # use pars::prelude::*;
1000    /// # use pars::basic::try_flat_map;
1001    /// # use pars::bytes::{self, PResult, ErrorKind};
1002    /// fn my_parser(input: &[u8]) -> PResult<&[u8], &[u8]> {
1003    ///     bytes::u8.try_flat_map(|value| {
1004    ///         if value < 10 {
1005    ///             Ok(take(value.into()).ok_into())
1006    ///         } else {
1007    ///             Err(ErrorKind::InvalidInput)
1008    ///         }
1009    ///     }).parse(input)
1010    /// }
1011    ///
1012    /// assert!(my_parser.parse(b"\x05hello") == Ok(Success(b"hello", b"")));
1013    /// assert!(my_parser.parse(b"\x0bhello world").is_err());
1014    /// ```
1015    #[inline]
1016    fn try_flat_map<F, P, S>(
1017        self,
1018        combinator: F,
1019    ) -> impl Parse<I, Parsed = P::Parsed, Error = Self::Error>
1020    where
1021        Self: Sized,
1022        S: ErrorSeed<I, Self::Error>,
1023        F: Fn(Self::Parsed) -> Result<P, S>,
1024        P: Parse<I, Error = Self::Error>,
1025    {
1026        basic::try_flat_map(self, combinator)
1027    }
1028
1029    /// Creates a parser that returns a fallback value instead of an error.
1030    ///
1031    /// If `self` fails to parse, `value` is cloned and returned as a success
1032    /// value. The new parser will never return a parsing error.
1033    ///
1034    /// See also [`basic::or_value`]
1035    ///
1036    /// # Example
1037    /// ```
1038    /// # use pars::prelude::*;
1039    /// # use pars::unicode::{self, PResult};
1040    /// fn my_parser(input: &str) -> PResult<char, &str> {
1041    ///     unicode::strict::char.or_value('\0').parse(input)
1042    /// }
1043    ///
1044    /// assert_eq!(my_parser.parse("hello"), Ok(Success('h', "ello")));
1045    /// assert_eq!(my_parser.parse(""), Ok(Success('\0', "")));
1046    /// ```
1047    #[inline]
1048    fn or_value(
1049        self,
1050        value: Self::Parsed,
1051    ) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
1052    where
1053        Self: Sized,
1054        Self::Parsed: Clone,
1055    {
1056        basic::or_value(self, value)
1057    }
1058
1059    /// Creates a parser that returns a fallback value instead of an error.
1060    ///
1061    /// If `self` fails to parse, a default value is returned by calling
1062    /// [`Default::default`] instead of returning the error. The new parser
1063    /// will never return a parsing error.
1064    ///
1065    /// See also [`basic::or_default`].
1066    ///
1067    /// # Example
1068    /// ```
1069    /// # use pars::prelude::*;
1070    /// # use pars::unicode::{self, PResult};
1071    /// fn my_parser(input: &str) -> PResult<char, &str> {
1072    ///     unicode::strict::char.or_default().parse(input)
1073    /// }
1074    ///
1075    /// assert_eq!(my_parser.parse("hello"), Ok(Success('h', "ello")));
1076    /// assert_eq!(my_parser.parse(""), Ok(Success('\0', "")));
1077    /// ```
1078    #[inline]
1079    fn or_default(self) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
1080    where
1081        Self: Sized,
1082        Self::Parsed: Default,
1083    {
1084        basic::or_default(self)
1085    }
1086
1087    /// Creates a parser that returns a fallback value instead of an error.
1088    ///
1089    /// If `self` fails to parse, `or_else_fn` is called and its return value
1090    /// is returned as a success value. The new parser will never return a
1091    /// parsing error.
1092    ///
1093    /// See also [`basic::or_else`]
1094    ///
1095    /// # Example
1096    /// ```
1097    /// # use pars::prelude::*;
1098    /// # use pars::unicode::{self, PResult};
1099    /// fn my_parser(input: &str) -> PResult<char, &str> {
1100    ///     unicode::strict::char.or_else(|| '\0').parse(input)
1101    /// }
1102    ///
1103    /// assert_eq!(my_parser.parse("hello"), Ok(Success('h', "ello")));
1104    /// assert_eq!(my_parser.parse(""), Ok(Success('\0', "")));
1105    /// ```
1106    #[inline]
1107    fn or_else<F>(self, or_else_fn: F) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
1108    where
1109        Self: Sized,
1110        F: Fn() -> Self::Parsed,
1111    {
1112        basic::or_else(self, or_else_fn)
1113    }
1114
1115    /// Creates a parser that is repeated until a sentinel is parsed.
1116    ///
1117    /// [`Parse::repeated_until`] produces a parser that will apply `self` repeatedly
1118    /// until `sentinel_parser` succeeds. That is, `sentinel_parser` is applied; if it
1119    /// fails, `self` is applied before trying `sentinel_parser` again. If
1120    /// `sentinel_parser` succeeds, parsing is complete.
1121    ///
1122    /// The values produced by `self` are passed to `collect_fn` in the form of an
1123    /// iterator, [`RepeatedUntilIter`](basic::RepeatedUntilIter). The value returned
1124    /// by `collect_fn` and the parsed value from `sentinel_parser` together as a
1125    /// 2-tuple become the parsed result of the overall new parser. If `parser`
1126    /// returns a parsing error at any point, a parsing error is returned from the
1127    /// overall new parser.
1128    ///
1129    /// Note that the returned new parser does not allocate. Values produced by
1130    /// the iterator are obtained on demand by applying `self` each time
1131    /// [`Iterator::next`] is called. Allocation will only occur if the user
1132    /// provided function `collect_fn` allocates to produce its result.
1133    ///
1134    /// See also [`basic::repeated_until`].
1135    ///
1136    /// # Example
1137    /// ```
1138    /// # use pars::prelude::*;
1139    /// # use pars::bytes::{self, PResult};
1140    /// fn c_str_len(input: &[u8]) -> PResult<usize, &[u8]> {
1141    ///     bytes::u8.repeated_until(bytes::verbatim(b"\x00"), |iter| iter.count())
1142    ///         .map(|(len, _)| len) // we only want the length
1143    ///         .parse(input)
1144    /// }
1145    ///
1146    /// assert!(c_str_len.parse(b"hello\x00world") == Ok(Success(5, b"world")));
1147    /// assert!(c_str_len.parse(b"\x00") == Ok(Success(0, b"")));
1148    /// assert!(c_str_len.parse(b"hello").is_err());
1149    /// ```
1150    #[inline]
1151    fn repeated_until<P, F, R>(
1152        self,
1153        sentinel: P,
1154        collect_fn: F,
1155    ) -> impl Parse<I, Parsed = (R, P::Parsed), Error = Self::Error>
1156    where
1157        Self: Sized,
1158        P: Parse<I, Error = Self::Error>,
1159        F: for<'a> Fn(basic::RepeatedUntilIter<'a, Self, P, I>) -> R,
1160    {
1161        basic::repeated_until(self, sentinel, collect_fn)
1162    }
1163
1164    /// Creates a parser that is repeated until a sentinel is parsed.
1165    ///
1166    /// [`Parse::try_repeated_until`] produces a parser that will apply `self` repeatedly
1167    /// until `sentinel_parser` succeeds. That is, `sentinel_parser` is applied; if it fails,
1168    /// `self` is applied before trying `sentinel_parser` again. If `sentinel_parser`
1169    /// succeeds, parsing is complete.
1170    ///
1171    /// The values produced by `self` are passed to `collect_fn` in the form of an
1172    /// iterator, [`RepeatedUntilIter`](basic::RepeatedUntilIter). If `collect_fn` returns
1173    /// an [`Ok`] value, that and the value from `sentinel_parser` together as a 2-tuple
1174    /// become the parsed result of the overall new parser. If `collect_fn` returns an
1175    /// [`Err`], a parsing error is returned. If `self` returns a parsing error at any point,
1176    /// a parsing error is returned form the overall new parser.
1177    ///
1178    /// Note that the returned new parser does not allocate. Values produced by
1179    /// the iterator are obtained on demand by applying `self` each time
1180    /// [`Iterator::next`] is called. Allocation will only occur if the user
1181    /// provided function `collect_fn` allocates to produce its result.
1182    ///
1183    /// See also [`basic::try_repeated_until`].
1184    ///
1185    /// # Example
1186    /// ```
1187    /// # use pars::prelude::*;
1188    /// # use pars::ErrorKind;
1189    /// # use pars::bytes::{self, PResult};
1190    /// fn c_str_len(input: &[u8]) -> PResult<usize, &[u8]> {
1191    ///     bytes::u8.try_repeated_until(bytes::verbatim(b"\x00"), |iter| {
1192    ///         let count = iter.count();
1193    ///         // disallow empty strings
1194    ///         if count == 0 {
1195    ///             Err(ErrorKind::InvalidInput)
1196    ///         } else {
1197    ///             Ok(count)
1198    ///         }
1199    ///     }).map(|(len, _)| len) // we only want the length
1200    ///         .parse(input)
1201    /// }
1202    ///
1203    /// assert!(c_str_len.parse(b"hello\x00world") == Ok(Success(5, b"world")));
1204    /// assert!(c_str_len.parse(b"\x00").is_err());
1205    /// assert!(c_str_len.parse(b"hello").is_err());
1206    /// ```
1207    #[inline]
1208    fn try_repeated_until<P, F, R, S>(
1209        self,
1210        sentinel: P,
1211        collect_fn: F,
1212    ) -> impl Parse<I, Parsed = (R, P::Parsed), Error = Self::Error>
1213    where
1214        Self: Sized,
1215        S: ErrorSeed<I, Self::Error>,
1216        P: Parse<I, Error = Self::Error>,
1217        F: for<'a> Fn(basic::RepeatedUntilIter<'a, Self, P, I>) -> Result<R, S>,
1218    {
1219        basic::try_repeated_until(self, sentinel, collect_fn)
1220    }
1221
1222    /// Creates a parser that is repeated until a sentinel is parsed.
1223    ///
1224    /// [`Parse::collect_repeated_until`] produces a parser that will apply `self`
1225    /// repeatedly until `sentinel_parser` succeeds. That is, `sentinel_parser` is
1226    /// applied; if it fails, `self` is applied before trying `sentinel_parser` again.
1227    /// If `sentinel_parser` succeeds, parsing is complete.
1228    ///
1229    /// The values produced by `self` are collected using the [`FromIterator`] trait
1230    /// implementation for the new parser's parsed result type, which is usually
1231    /// deduced. This value together with the parsed value from `sentinel_parser` as a
1232    /// 2-tuple become the parsed value for the overall new parser. If `self` returns
1233    /// a parsing error at any point, a parsing error is returned from the overall new
1234    /// parser.
1235    ///
1236    /// Note that the returned new parser does not allocate unless the
1237    /// [`FromIterator`] implementation allocates.
1238    ///
1239    /// See also [`basic::collect_repeated_until`].
1240    ///
1241    /// # Example
1242    /// ```
1243    /// # use pars::prelude::*;
1244    /// # use pars::bytes::{self, PResult};
1245    /// fn c_str(input: &[u8]) -> PResult<Vec<u8>, &[u8]> {
1246    ///     bytes::u8.collect_repeated_until(bytes::verbatim(b"\x00"))
1247    ///         .map(|(s, _)| s)
1248    ///         .parse(input)
1249    /// }
1250    ///
1251    /// assert!(c_str.parse(b"hello\x00world") == Ok(Success(Vec::from(b"hello"), b"world")));
1252    /// assert!(c_str.parse(b"\x00") == Ok(Success(Vec::new(), b"")));
1253    /// assert!(c_str.parse(b"hello").is_err());
1254    /// ```
1255    #[inline]
1256    fn collect_repeated_until<P, R>(
1257        self,
1258        sentinel: P,
1259    ) -> impl Parse<I, Parsed = (R, P::Parsed), Error = Self::Error>
1260    where
1261        Self: Sized,
1262        P: Parse<I, Error = Self::Error>,
1263        R: FromIterator<Self::Parsed>,
1264    {
1265        basic::collect_repeated_until(self, sentinel)
1266    }
1267
1268    /// Creates a parser that is repeated a number of times within a range.
1269    ///
1270    /// [`Parse::repeated`] produces a parser that will apply `self` repeatedly. The
1271    /// number of times `self` will be applied is defined by `range`. For example, if
1272    /// `range` is `3..=5`, `self` will be applied at least 3 times, and will be
1273    /// applied up to 5 times if possible. If `self` fails before parsing 3 times
1274    /// (or whatever lower bound applies to `range`, if any), a parsing error is
1275    /// returned from the new parser. Once the upper bound is met (again, if any)
1276    /// parsing automatically ends successfully. `self` returning a parsing error
1277    /// after meeting the lower bound also ends parsing for the new parser.
1278    ///
1279    /// The values produced by `self` are passed to `collect_fn` in the form of
1280    /// an iterator, [`RepeatedIter`](basic::RepeatedIter). Whever `collect_fn`
1281    /// returns is the parsed value of the new parser.
1282    ///
1283    /// Note that the returned new parser does not allocate. Values produced by
1284    /// the iterator are obtained on demand by applying `self` each time
1285    /// [`Iterator::next`] is called. Allocation will only occur if the user
1286    /// provided function `collect_fn` allocates to produce its result.
1287    ///
1288    /// See also [`basic::repeated`].
1289    ///
1290    /// # Example
1291    /// ```
1292    /// # use pars::prelude::*;
1293    /// # use pars::bytes::{self, PResult};
1294    /// fn my_parser(input: &[u8]) -> PResult<u32, &[u8]> {
1295    ///     bytes::u8.repeated(2..=4, |iter| {
1296    ///         iter.map(|byte| byte as u32).sum()
1297    ///     }).parse(input)
1298    /// }
1299    ///
1300    /// assert!(my_parser.parse(b"\x01\x02\x03\x04\x05") == Ok(Success(10, b"\x05")));
1301    /// assert!(my_parser.parse(b"\x01\x02\x03") == Ok(Success(6, b"")));
1302    /// assert!(my_parser.parse(b"\x01\x02") == Ok(Success(3, b"")));
1303    /// assert!(my_parser.parse(b"\x01").is_err());
1304    /// assert!(my_parser.parse(b"").is_err());
1305    /// ```
1306    #[inline]
1307    fn repeated<N, F, R>(
1308        self,
1309        range: N,
1310        collect_fn: F,
1311    ) -> impl Parse<I, Parsed = R, Error = Self::Error>
1312    where
1313        Self: Sized,
1314        N: basic::Range,
1315        F: for<'a> Fn(basic::RepeatedIter<'a, Self, I>) -> R,
1316    {
1317        basic::repeated(self, range, collect_fn)
1318    }
1319
1320    /// Creates a parser that is repeated a number of times within a range.
1321    ///
1322    /// [`Parse::try_repeated`] produces a parser that will apply `self` repeatedly.
1323    /// The number of times `self` will be applied is defined by `range`. For example,
1324    /// if `range` is `3..=5`, `self` will be applied at least 3 times, and will
1325    /// be applied up to 5 times if possible. If `self` fails before parsing 3
1326    /// times (or whatever lower bound applies to `range`, if any), a parsing error
1327    /// is returned from the new parser. Once the upper bound is met (again, if any)
1328    /// parsing automatically ends successfully. `self` returning a parsing error
1329    /// after meeting the lower bound also ends parsing for the new parser.
1330    ///
1331    /// The values produced by `self` are passed to `collect_fn` in the form of
1332    /// an iterator, [`RepeatedIter`](basic::RepeatedIter). If `collect_fn` returns
1333    /// an [`Ok`] value, the contained value becomes the parsed value of the new
1334    /// parser. If `collect_fn` returns an [`Err`], that gets returned as a parsing
1335    /// error via [`ErrorSeed`].
1336    ///
1337    /// Note that the returned new parser does not allocate. Values produced by
1338    /// the iterator are obtained on demand by applying `self` each time
1339    /// [`Iterator::next`] is called. Allocation will only occur if the user
1340    /// provided function `collect_fn` allocates to produce its result.
1341    ///
1342    /// See also [`basic::try_repeated`].
1343    ///
1344    /// # Example
1345    /// ```
1346    /// # use pars::prelude::*;
1347    /// # use pars::bytes::{self, PResult};
1348    /// # use pars::ErrorKind;
1349    /// fn my_parser(input: &[u8]) -> PResult<u32, &[u8]> {
1350    ///     bytes::u8.try_repeated(2..=4, |iter| {
1351    ///         let sum = iter.map(|byte| byte as u32).sum();
1352    ///         // only allow even sums
1353    ///         if sum % 2 == 0 {
1354    ///             Ok(sum)
1355    ///         } else {
1356    ///             Err(ErrorKind::InvalidInput)
1357    ///         }
1358    ///     }).parse(input)
1359    /// }
1360    ///
1361    /// assert!(my_parser.parse(b"\x01\x02\x03\x04\x05") == Ok(Success(10, b"\x05")));
1362    /// assert!(my_parser.parse(b"\x01\x02\x03") == Ok(Success(6, b"")));
1363    /// assert!(my_parser.parse(b"\x01\x02").is_err());
1364    /// assert!(my_parser.parse(b"\x01").is_err());
1365    /// assert!(my_parser.parse(b"").is_err());
1366    /// ```
1367    #[inline]
1368    fn try_repeated<N, F, R, S>(
1369        self,
1370        range: N,
1371        collect_fn: F,
1372    ) -> impl Parse<I, Parsed = R, Error = Self::Error>
1373    where
1374        Self: Sized,
1375        N: basic::Range,
1376        S: ErrorSeed<I, Self::Error>,
1377        F: for<'a> Fn(basic::RepeatedIter<'a, Self, I>) -> Result<R, S>,
1378    {
1379        basic::try_repeated(self, range, collect_fn)
1380    }
1381
1382    /// Creates a parser that is repeated a number of times within a range.
1383    ///
1384    /// [`Parse::collect_repeated`] produces a parser that will apply `self`
1385    /// repeatedly. The number of times `self` will be applied is defined by `range`.
1386    /// For example, if `range` is `3..=5`, `self` will be applied at least 3 times,
1387    /// and will be applied up to 5 times if possible. If `self` fails before parsing
1388    /// 3 times (or whatever lower bound applies to `range`, if any), a parsing error
1389    /// is returned from the new parser. Once the upper bound is met (again, if any)
1390    /// parsing automatically ends successfully. `self` returning a parsing error
1391    /// after meeting the lower bound also ends parsing for the new parser.
1392    ///
1393    /// The values produced by `self` are collected by the [`FromIterator`] trait
1394    /// implementation on the parsed type of the parser, which is usually deduced.
1395    ///
1396    /// Note that the returned new parser does not allocate unless the
1397    /// [`FromIterator`] implementation allocates.
1398    ///
1399    /// See also [`basic::collect_repeated`].
1400    ///
1401    /// # Example
1402    /// ```
1403    /// # use pars::prelude::*;
1404    /// # use pars::unicode::PResult;
1405    /// use pars::unicode::{strict::char_with_prop, prop::Alphabetic};
1406    ///
1407    /// fn my_parser(input: &str) -> PResult<String, &str> {
1408    ///     char_with_prop(Alphabetic).collect_repeated(3..=5)
1409    ///         .parse(input)
1410    /// }
1411    ///
1412    /// assert_eq!(my_parser.parse("hello world"), Ok(Success(String::from("hello"), " world")));
1413    /// assert_eq!(my_parser.parse("hey there"), Ok(Success(String::from("hey"), " there")));
1414    /// assert!(my_parser.parse("hi there").is_err());
1415    /// assert_eq!(my_parser.parse("goodbye"), Ok(Success(String::from("goodb"), "ye")));
1416    /// ```
1417    #[inline]
1418    fn collect_repeated<C>(
1419        self,
1420        range: impl basic::Range,
1421    ) -> impl Parse<I, Parsed = C, Error = Self::Error>
1422    where
1423        Self: Sized,
1424        C: FromIterator<Self::Parsed>,
1425    {
1426        basic::collect_repeated(self, range)
1427    }
1428
1429    /// Creates a parser that is repeated a constant number of times.
1430    ///
1431    /// [`Parse::array`] produces a parser that applies `self` `LEN` times and
1432    /// places the parsed results in an array. If `self` returns a parsing error
1433    /// before parsing `LEN` times, a parsing error is returned from the new parser.
1434    ///
1435    /// [`Parse::array`] is similar to [`Parse::repeated`], except that the number of
1436    /// repetitions is known at compile time and the results are placed in array
1437    /// rather than being processed via an iterator. Most cases where the number of
1438    /// repetitions is known at compile time should prefer to use [`Parse::array`],
1439    /// but be aware of the tradeoffs of storing and moving the values on the stack
1440    /// when `LEN` is large.
1441    ///
1442    /// # Example
1443    /// ```
1444    /// # use pars::prelude::*;
1445    /// # use pars::bytes::{self, PResult};
1446    /// fn my_parser(input: &[u8]) -> PResult<[u8; 4], &[u8]> {
1447    ///     bytes::u8.array().parse(input)
1448    /// }
1449    ///
1450    /// assert!(my_parser.parse(b"\x01\x02\x03\x04\x05") == Ok(Success([1, 2, 3, 4], b"\x05")));
1451    /// assert!(my_parser.parse(b"\x01\x02\x03").is_err());
1452    /// ```
1453    #[inline]
1454    fn array<const LEN: usize>(
1455        self,
1456    ) -> impl Parse<I, Parsed = [Self::Parsed; LEN], Error = Self::Error>
1457    where
1458        Self: Sized,
1459    {
1460        basic::array(self)
1461    }
1462
1463    /// Creates a parser that is applied repeatedly while interspersed with a separator.
1464    ///
1465    /// [`Parse::separated`] produces a parser that will apply `self` and `separator`
1466    /// repeatedly, alternatiging between the two. The parsing pattern starts with
1467    /// `parser` and ends with `self`, such as `self separator self`. This is
1468    /// distinct from `parser.then(separator).repeated(...)` as [`Parse::separated`]
1469    /// will not parse a trailing separator, and a repeated pair must parse a trailing
1470    /// separator.
1471    ///
1472    /// The provided `range` determines the minimum and maximum number of repatitions
1473    /// of `self`. For example if `range` is `3..=5`, `self` must be able to parse
1474    /// at least 3 times, and up to 5 times. Similarly, `..` corresponds to 0 or more
1475    /// repetitions, and `1..` corresponds to 1 or more. An exact number of repetitions
1476    /// can be specified with a single integer value such as `3` (but `3..=3` is also
1477    /// valid and equivalent). See [`Range`](basic::Range) for more information on
1478    /// ranges.
1479    ///
1480    /// The values produced by `self` are passed to `collect_fn` in the form of an
1481    /// iterator, [`SeparatedIter`](basic::SeparatedIter). Whatever `collect_fn`
1482    /// returns is the parsed value of the new parser. The values produced by
1483    /// `separator` are discarded.
1484    ///
1485    /// Note that the returned new parser does not allocate. Values produced by the
1486    /// iterator are obtained on demand by applying `separator` and `self` each time
1487    /// [`Iterator::next`] is called. Allocation will only occur if the user provided
1488    /// `collect_fn` allocates when called.
1489    ///
1490    /// See also [`basic::separated`].
1491    ///
1492    /// # Example
1493    /// ```
1494    /// # use pars::prelude::*;
1495    /// # use pars::unicode::{self, PResult};
1496    /// fn my_parser(input: &str) -> PResult<Vec<char>, &str> {
1497    ///     unicode::strict::char.separated(
1498    ///         ..,
1499    ///         unicode::strict::verbatim(","),
1500    ///         |iter| {
1501    ///             let mut out = Vec::new();
1502    ///             for ch in iter {
1503    ///                 out.push(ch);
1504    ///             }
1505    ///             out
1506    ///         }
1507    ///     ).parse(input)
1508    /// }
1509    ///
1510    /// assert_eq!(
1511    ///     my_parser.parse("a,b,c"),
1512    ///     Ok(Success(vec!['a', 'b', 'c'], "")),
1513    /// );
1514    ///
1515    /// assert_eq!(
1516    ///     my_parser.parse("a,b,c,"),
1517    ///     Ok(Success(vec!['a', 'b', 'c'], ",")),
1518    /// );
1519    /// ```
1520    #[inline]
1521    fn separated<N, S, F, R>(
1522        self,
1523        range: N,
1524        separator: S,
1525        collect_fn: F,
1526    ) -> impl Parse<I, Parsed = R, Error = Self::Error>
1527    where
1528        Self: Sized,
1529        N: basic::Range,
1530        S: Parse<I, Error = Self::Error>,
1531        F: for<'a> Fn(basic::SeparatedIter<'a, Self, S, I>) -> R,
1532    {
1533        basic::separated(self, range, separator, collect_fn)
1534    }
1535
1536    /// Creates a parser that is applied repeatedly while interspersed with a separator.
1537    ///
1538    /// [`Parse::try_separated`] produces a parser that will apply `self` and
1539    /// `separator` repeatedly, alternatiging between the two. The parsing pattern
1540    /// starts with `self` and ends with `self`, such as `self separator self`. This is
1541    /// distinct from `parser.then(separator).try_repeated(...)` as
1542    /// [`Parse::try_separated`] will not parse a trailing separator, and a repeated
1543    /// pair must parse a trailing separator.
1544    ///
1545    /// The provided `range` determines the minimum and maximum number of repatitions
1546    /// of `self`. For example if `range` is `3..=5`, `self` must be able to parse
1547    /// at least 3 times, and up to 5 times. Similarly, `..` corresponds to 0 or more
1548    /// repetitions, and `1..` corresponds to 1 or more. An exact number of repetitions
1549    /// can be specified with a single integer value such as `3` (but `3..=3` is also
1550    /// valid and equivalent). See [`Range`](basic::Range) for more information on
1551    /// ranges.
1552    ///
1553    /// The values produced by `self` are passed to `collect_fn` in the form of an
1554    /// iterator, [`SeparatedIter`](basic::SeparatedIter). `collect_fn` then returns a
1555    /// [`Result`], where the [`Ok`] variant becomes the parsed value of the new parser,
1556    /// and the [`Err`] variant becomes an error returned by the new parser via the
1557    /// [`ErrorSeed`] trait.
1558    ///
1559    /// Note that the returned new parser does not allocate. Values produced by the
1560    /// iterator are obtained on demand by applying `separator` and `self` each time
1561    /// [`Iterator::next`] is called. Allocation will only occur if the user provided
1562    /// `collect_fn` allocates when called.
1563    ///
1564    /// See also [`basic::try_separated`].
1565    ///
1566    /// # Example
1567    /// ```
1568    /// # use pars::prelude::*;
1569    /// # use pars::ErrorKind;
1570    /// # use pars::unicode::{self, PResult};
1571    /// fn my_parser(input: &str) -> PResult<Vec<char>, &str> {
1572    ///     unicode::strict::char.try_separated(
1573    ///         ..,
1574    ///         unicode::strict::verbatim(","),
1575    ///         |iter| -> Result<Vec<char>, ErrorKind> {
1576    ///             let mut out = Vec::new();
1577    ///             for ch in iter {
1578    ///                 if !ch.is_ascii_alphabetic() {
1579    ///                     return Err(ErrorKind::InvalidInput);
1580    ///                 }
1581    ///                 out.push(ch);
1582    ///             }
1583    ///             Ok(out)
1584    ///         }
1585    ///     ).parse(input)
1586    /// }
1587    ///
1588    /// assert_eq!(
1589    ///     my_parser.parse("a,b,c"),
1590    ///     Ok(Success(vec!['a', 'b', 'c'], "")),
1591    /// );
1592    ///
1593    /// assert_eq!(
1594    ///     my_parser.parse("a,b,c,"),
1595    ///     Ok(Success(vec!['a', 'b', 'c'], ",")),
1596    /// );
1597    ///
1598    /// assert!(my_parser.parse("a,2,c").is_err());
1599    /// ```
1600    #[inline]
1601    fn try_separated<N, S, F, R, E>(
1602        self,
1603        range: N,
1604        separator: S,
1605        collect_fn: F,
1606    ) -> impl Parse<I, Parsed = R, Error = Self::Error>
1607    where
1608        Self: Sized,
1609        N: basic::Range,
1610        S: Parse<I, Error = Self::Error>,
1611        F: for<'a> Fn(basic::SeparatedIter<'a, Self, S, I>) -> Result<R, E>,
1612        E: ErrorSeed<I, Self::Error>,
1613    {
1614        basic::try_separated(self, range, separator, collect_fn)
1615    }
1616
1617    /// Creates a parser that is applied repeatedly while interspersed with a separator.
1618    ///
1619    /// [`Parse::collect_separated`] produces a parser that will apply `self` and
1620    /// `separator` repeatedly, alternatiging between the two. The parsing pattern
1621    /// starts with `self` and ends with `self`, such as `self separator self`. This
1622    /// is distinct from `parser.then(separator).collect_repeated(...)` as
1623    /// [`Parse::collect_separated`] will not parse a trailing separator, and a repeated
1624    /// pair must parse a trailing separator.
1625    ///
1626    /// The provided `range` determines the minimum and maximum number of repatitions
1627    /// of `self`. For example if `range` is `3..=5`, `self` must be able to parse
1628    /// at least 3 times, and up to 5 times. Similarly, `..` corresponds to 0 or more
1629    /// repetitions, and `1..` corresponds to 1 or more. An exact number of repetitions
1630    /// can be specified with a single integer value such as `3` (but `3..=3` is also
1631    /// valid and equivalent). See [`Range`](basic::Range) for more information on
1632    /// ranges.
1633    ///
1634    /// The values produced by `self` are collected via the [`FromIterator`] trait
1635    /// on the, typically inferred, parsed value type of the new parser. The values
1636    /// produced by `separator` are discarded.
1637    ///
1638    /// See also [`basic::collect_separated`].
1639    ///
1640    /// # Example
1641    /// ```
1642    /// # use pars::prelude::*;
1643    /// # use pars::unicode::{self, PResult};
1644    /// fn my_parser(input: &str) -> PResult<Vec<char>, &str> {
1645    ///     // Note that `Vec<char>` is inferred from the return type of `my_parser`
1646    ///     unicode::strict::char.collect_separated(
1647    ///         ..,
1648    ///         unicode::strict::verbatim(","),
1649    ///     ).parse(input)
1650    /// }
1651    ///
1652    /// assert_eq!(
1653    ///     my_parser.parse("a,b,c"),
1654    ///     Ok(Success(vec!['a', 'b', 'c'], "")),
1655    /// );
1656    ///
1657    /// assert_eq!(
1658    ///     my_parser.parse("a,b,c,"),
1659    ///     Ok(Success(vec!['a', 'b', 'c'], ",")),
1660    /// );
1661    /// ```
1662    #[inline]
1663    fn collect_separated<C>(
1664        self,
1665        range: impl basic::Range,
1666        separator: impl Parse<I, Error = Self::Error>,
1667    ) -> impl Parse<I, Parsed = C, Error = Self::Error>
1668    where
1669        Self: Sized,
1670        C: FromIterator<Self::Parsed>,
1671    {
1672        basic::collect_separated(self, range, separator)
1673    }
1674
1675    /// Creates a parser that fails if all input is not consumed.
1676    ///
1677    /// [`Parse::complete`] produces a parser that applies `self` and then verifies that
1678    /// all input was consumed. If there is still input remaining, an error is returned
1679    /// via [`Error::expected_eof`].
1680    ///
1681    /// See also [`basic::complete`].
1682    ///
1683    /// # Example
1684    /// ```
1685    /// # use pars::prelude::*;
1686    /// # use pars::bytes::{self, PResult};
1687    /// fn my_parser(input: &[u8]) -> PResult<u32, &[u8]> {
1688    ///     bytes::be::u32.complete().parse(input)
1689    /// }
1690    ///
1691    /// assert!(my_parser.parse(b"\x01\x02\x03\x04") == Ok(Success(0x01020304, &[][..])));
1692    /// assert!(my_parser.parse(b"\x01\x02\x03\x04\x05").is_err());
1693    /// ```
1694    #[inline]
1695    fn complete(self) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
1696    where
1697        Self: Sized,
1698    {
1699        basic::complete(self)
1700    }
1701
1702    /// Creates a parser that returns a [`Span`] of parsed input in addition to the parsed value.
1703    ///
1704    /// [`Parse::spanned`] produces a parser that applies `self`, and on success, it returns the
1705    /// parsed value from `parser` in a tuple with a [`Span`] of the input consumed to produce
1706    /// the parsed value.
1707    ///
1708    /// If only the [`Span`] is needed and the parsed value can be discarded,
1709    /// [`Parse::recognize`] can also be used instead.
1710    ///
1711    /// See also [`basic::spanned`].
1712    ///
1713    /// # Example
1714    /// ```
1715    /// # use pars::prelude::*;
1716    /// # use pars::bytes::{self, PResult};
1717    /// fn my_parser(input: &[u8]) -> PResult<(u32, Span<&[u8]>), &[u8]> {
1718    ///     bytes::be::u32.spanned().parse(input)
1719    /// }
1720    ///
1721    /// let res = my_parser.parse(b"\x01\x02\x03\x04\x05");
1722    /// assert!(res.is_ok());
1723    /// let Success((value, span), rem) = res.unwrap();
1724    /// let span: &[u8] = span.into();
1725    /// assert_eq!(value, 0x01020304);
1726    /// assert_eq!(span, &[1u8, 2, 3, 4][..]);
1727    /// assert_eq!(rem, &[5u8][..]);
1728    /// ```
1729    #[inline]
1730    fn spanned(self) -> impl Parse<I, Parsed = (Self::Parsed, Span<I>), Error = Self::Error>
1731    where
1732        Self: Sized,
1733    {
1734        basic::spanned(self)
1735    }
1736
1737    /// Creates a parser that also returns a span of the input that was parsed.
1738    ///
1739    /// [`Parse::spanned_into`] produces a parser that combines the parsed result
1740    /// of `self` with a [`Span`] of the input that `self` parsed converted to
1741    /// `S` via [`Into`] in a 2-tuple. This allows a parser to provide information
1742    /// about where in an input stream a parsed value originated from.
1743    ///
1744    /// [`Parse::spanned_into`] differs from [`Parse::spanned`] in that the
1745    /// produced [`Span`] is converted into another type, `S` by way of [`Into`].
1746    /// This is a common enough pattern that [`Parse::spanned_into`] exists to
1747    /// simplify usage. For example, a `Span<&str>` can be converted to a `&str`
1748    /// that contains just the content of the span.
1749    ///
1750    /// If the parsed value returned by `self` is not needed, such as a parser
1751    /// that returns the unit type, consider using [`Parse::recognize`] instead.
1752    /// Note that [`Parse::recognize`] doesn't provide a `recognize_into` variant,
1753    /// since the same effect can be accomplished with
1754    /// `parser.recognize().ok_into()` in this case.
1755    ///
1756    /// See also [`basic::spanned_into`].
1757    ///
1758    /// # Example
1759    /// ```
1760    /// # use pars::prelude::*;
1761    /// # use pars::bytes::{self, PResult};
1762    /// fn my_parser(input: &[u8]) -> PResult<(u32, &[u8]), &[u8]> {
1763    ///     bytes::be::u32.spanned_into().parse(input)
1764    /// }
1765    ///
1766    /// assert!(my_parser.parse(b"\x01\x02\x03\x04\x05") ==
1767    ///     Ok(Success((0x01020304, b"\x01\x02\x03\x04"), b"\x05")));
1768    /// assert!(my_parser.parse(b"").is_err());
1769    /// ```
1770    #[inline]
1771    fn spanned_into<S>(self) -> impl Parse<I, Parsed = (Self::Parsed, S), Error = Self::Error>
1772    where
1773        Self: Sized,
1774        Span<I>: Into<S>,
1775    {
1776        basic::spanned_into(self)
1777    }
1778
1779    /// Creates a parser that returns a [`Span`] of parsed input.
1780    ///
1781    /// [`Parse::recognize`] produces a parser that applies `self` and on success, it
1782    /// returns a [`Span`] of the input consumed by `self`.
1783    ///
1784    /// [`Parse::recognize`] is similar to [`Parse::spanned`], except that the parsed
1785    /// value returned by `self` is discarded.
1786    ///
1787    /// See also [`basic::recognize`].
1788    ///
1789    /// # Example
1790    /// ```
1791    /// # use pars::prelude::*;
1792    /// # use pars::bytes::{self, PResult};
1793    /// fn my_parser(input: &[u8]) -> PResult<Span<&[u8]>, &[u8]> {
1794    ///     bytes::be::u32.recognize().parse(input)
1795    /// }
1796    ///
1797    /// let res = my_parser.parse(b"\x01\x02\x03\x04\x05");
1798    /// assert!(res.is_ok());
1799    /// let Success(span, rem) = res.unwrap();
1800    /// let span: &[u8] = span.into();
1801    /// assert_eq!(span, b"\x01\x02\x03\x04");
1802    /// assert_eq!(rem, b"\x05");
1803    /// ```
1804    #[inline]
1805    fn recognize(self) -> impl Parse<I, Parsed = Span<I>, Error = Self::Error>
1806    where
1807        Self: Sized,
1808    {
1809        basic::recognize(self)
1810    }
1811
1812    /// Creates a parser that produces a parsed value only if `condition` is `true`.
1813    ///
1814    /// [`Parse::cond`] produces a parser that applies `self` only if `condition` is
1815    /// `true`. On success, the parsed value returned by `self` returned wrapped in
1816    /// [`Some`]. If `condition` is `false`, the parser succeeds, but returns [`None`]
1817    /// as the parsed value.
1818    ///
1819    /// See also [`basic::cond`].
1820    ///
1821    /// # Example
1822    /// ```
1823    /// # use pars::prelude::*;
1824    /// # use pars::bytes::{self, PResult};
1825    /// fn my_parser(input: &[u8]) -> PResult<Option<u32>, &[u8]> {
1826    ///     bytes::u8.flat_map(|b| {
1827    ///         bytes::be::u32.cond(b != 0)
1828    ///     }).parse(input)
1829    /// }
1830    ///
1831    /// assert!(my_parser.parse(b"\x00\x01\x02\x03\x04") == Ok(Success(None, b"\x01\x02\x03\x04")));
1832    /// assert!(my_parser.parse(b"\x01\x01\x02\x03\x04") == Ok(Success(Some(0x01020304), b"")));
1833    /// ```
1834    #[inline]
1835    fn cond(
1836        self,
1837        condition: bool,
1838    ) -> impl Parse<I, Parsed = Option<Self::Parsed>, Error = Self::Error>
1839    where
1840        Self: Sized,
1841    {
1842        basic::cond(self, condition)
1843    }
1844
1845    /// Creates a parser that succeeds only if the parsed value passes the given predicate.
1846    ///
1847    /// [`Parse::verify`] produces a parser that applies `self`, and on success, passes the parsed
1848    /// value to `verify_fn`. If `verify_fn` returns `true`, the parser returns the parsed value.
1849    /// If `verify_fn` returns `false`, the parser returns an error via [`Error::invalid_input`].
1850    ///
1851    /// See also [`basic::verify`].
1852    ///
1853    /// # Example
1854    /// ```
1855    /// # use pars::prelude::*;
1856    /// # use pars::bytes::{self, PResult};
1857    /// fn non_zero(input: &[u8]) -> PResult<u32, &[u8]> {
1858    ///     bytes::be::u32.verify(|v| *v != 0).parse(input)
1859    /// }
1860    ///
1861    /// assert!(non_zero.parse(b"\x00\x00\x00\x00").is_err());
1862    /// assert!(non_zero.parse(b"\x00\x00\x00\x01") == Ok(Success(1, b"")));
1863    /// ```
1864    #[inline]
1865    fn verify<F>(self, verify_fn: F) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
1866    where
1867        Self: Sized,
1868        F: Fn(&Self::Parsed) -> bool,
1869    {
1870        basic::verify(self, verify_fn)
1871    }
1872
1873    /// Creates a parser that succeeds only if `self` fails.
1874    ///
1875    /// [`Parse::not`] produces a parser that applies `self`, an on success, it returns
1876    /// an error via [`Error::invalid_input`]. If `self` fails, the new parser returns
1877    /// a success.
1878    ///
1879    /// See also [`basic::not`].
1880    ///
1881    /// # Example
1882    /// ```
1883    /// # use pars::prelude::*;
1884    /// # use pars::ascii::{self, PResult};
1885    /// fn my_parser(input: &str) -> PResult<(), &str> {
1886    ///     ascii::strict::char_with_prop(ascii::prop::Printable).not().parse(input)
1887    /// }
1888    ///
1889    /// assert!(my_parser.parse("hello").is_err());
1890    /// assert!(my_parser.parse("\0").is_ok());
1891    /// ```
1892    #[inline]
1893    fn not(self) -> impl Parse<I, Parsed = (), Error = Self::Error>
1894    where
1895        Self: Sized,
1896    {
1897        basic::not(self)
1898    }
1899
1900    /// Creates a parser that attempts to parse with `self` if possible.
1901    ///
1902    /// [`Parse::opt`] produces a parser that applies `self`, and on success, the new
1903    /// parser returns the parsed value wrapped in [`Some`]. On failure, the new parser
1904    /// still succeeds but returns [`None`] without consuming any input.
1905    ///
1906    /// See also [`basic::opt`].
1907    ///
1908    /// # Example
1909    /// ```
1910    /// # use pars::prelude::*;
1911    /// # use pars::bytes::{self, PResult};
1912    /// fn my_parser(input: &[u8]) -> PResult<Option<&[u8]>, &[u8]> {
1913    ///     bytes::verbatim("hello").ok_into().opt().parse(input)
1914    /// }
1915    ///
1916    /// assert!(my_parser.parse(b"hello world") == Ok(Success(Some(&b"hello"[..]), b" world")));
1917    /// assert!(my_parser.parse(b"goodbye world") == Ok(Success(None, b"goodbye world")));
1918    /// ```
1919    #[inline]
1920    fn opt(self) -> impl Parse<I, Parsed = Option<Self::Parsed>, Error = Self::Error>
1921    where
1922        Self: Sized,
1923    {
1924        basic::opt(self)
1925    }
1926
1927    /// Creates a parser that parses without consuming input.
1928    ///
1929    /// [`Parse::peek`] produces a parser that applies `self`, and on success, the new parser
1930    /// returns the parsed value returned by `self` and the entire input that was passed to
1931    /// the parser. On failure, the new parser forwards the error unmodified.
1932    ///
1933    /// See also [`basic::peek`].
1934    ///
1935    /// # Example
1936    /// ```
1937    /// # use pars::prelude::*;
1938    /// # use pars::bytes::{self, PResult};
1939    /// fn my_parser(input: &[u8]) -> PResult<u32, &[u8]> {
1940    ///     bytes::be::u32.peek().parse(input)
1941    /// }
1942    ///
1943    /// assert!(my_parser.parse(b"\x01\x02\x03\x04") == Ok(Success(0x01020304, b"\x01\x02\x03\x04")));
1944    /// ```
1945    #[inline]
1946    fn peek(self) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
1947    where
1948        Self: Sized,
1949    {
1950        basic::peek(self)
1951    }
1952
1953    /// Creates a parser out of a reference to another parser.
1954    ///
1955    /// [`Parse::by_ref`] produces a parser that directly applies `self`. The
1956    /// produced parser behaves identically to `self` and internally contains a
1957    /// reference to `self`.
1958    ///
1959    /// [`Parse`] is not automatically implemented for references to types that
1960    /// implement [`Parse`], so [`Parse::by_ref`] can by used to convert a
1961    /// reference to a parser into a parser.
1962    ///
1963    /// See also [`basic::by_ref`].
1964    ///
1965    /// # Example
1966    /// ```
1967    /// # use pars::prelude::*;
1968    /// # use pars::bytes::{self, PResult};
1969    /// fn my_parser(input: &[u8]) -> PResult<u8, &[u8]> {
1970    ///     bytes::u8.by_ref().parse(input)
1971    /// }
1972    ///
1973    /// assert!(my_parser.parse(b"\x01\x02\x03\x04") == Ok(Success(1u8, b"\x02\x03\x04")));
1974    /// assert!(my_parser.parse(b"").is_err());
1975    /// ```
1976    #[inline]
1977    fn by_ref<'a>(&'a self) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error> + 'a
1978    where
1979        I: 'a,
1980    {
1981        basic::by_ref(self)
1982    }
1983
1984    /// Creates a parser that applies two parsers in sequence.
1985    ///
1986    /// [`Parse::then`] produces a parser that applies `self` and `other` in that
1987    /// order. On success, the new parser returns the parsed values returned
1988    /// by `self` and `other` as a tuple. If either parser fails, the
1989    /// corresponding error is returned from the new parser. When `self` fails,
1990    /// `other` is not applied.
1991    ///
1992    /// See also [`basic::pair`] and [`basic::seq`].
1993    ///
1994    /// # Example
1995    /// ```
1996    /// # use pars::prelude::*;
1997    /// # use pars::unicode::{self, PResult};
1998    /// fn my_parser(input: &str) -> PResult<(char, char), &str> {
1999    ///     unicode::strict::char.then(unicode::strict::char)
2000    ///         .parse(input)
2001    /// }
2002    ///
2003    /// assert_eq!(my_parser.parse("abc"), Ok(Success(('a', 'b'), "c")));
2004    /// assert!(my_parser.parse("a").is_err());
2005    /// assert!(my_parser.parse("").is_err());
2006    /// ```
2007    #[inline]
2008    fn then<P>(
2009        self,
2010        other: P,
2011    ) -> impl Parse<I, Parsed = (Self::Parsed, P::Parsed), Error = Self::Error>
2012    where
2013        Self: Sized,
2014        P: Parse<I, Error = Self::Error>,
2015    {
2016        basic::pair(self, other)
2017    }
2018
2019    /// Creates a parser that attempts to parse with each of two parsers.
2020    ///
2021    /// [`Parse::or`] produces a parser that first attempts to apply `self` and
2022    /// then applies `other` if `self` fails. If `self` succeeds, the
2023    /// parsed value `self` returns is returned by the new parser. If `self`
2024    /// fails and then `other` succeeds, the parsed value returned by `other`
2025    /// is returned by the new parser. If both fail, the error returned by
2026    /// `other` is returned.
2027    ///
2028    /// See also [`basic::either`] and [`basic::alt`].
2029    ///
2030    /// # Example
2031    /// ```
2032    /// # use pars::prelude::*;
2033    /// # use pars::unicode::{self, PResult};
2034    /// # use pars::basic::either;
2035    /// fn my_parser(input: &str) -> PResult<&str, &str> {
2036    ///     either(
2037    ///         unicode::strict::verbatim("foo").ok_into(),
2038    ///         unicode::strict::verbatim("bar").ok_into(),
2039    ///     ).parse(input)
2040    /// }
2041    ///
2042    /// assert_eq!(my_parser.parse("foobaz"), Ok(Success("foo", "baz")));
2043    /// assert_eq!(my_parser.parse("barbaz"), Ok(Success("bar", "baz")));
2044    /// assert!(my_parser.parse("baz").is_err());
2045    /// ```
2046    #[inline]
2047    fn or<P>(self, other: P) -> impl Parse<I, Parsed = Self::Parsed, Error = Self::Error>
2048    where
2049        Self: Sized,
2050        P: Parse<I, Parsed = Self::Parsed, Error = Self::Error>,
2051    {
2052        basic::either(self, other)
2053    }
2054}
2055
2056mod sealed {
2057    use super::{Error, Input, PResult};
2058
2059    pub trait Sealed {}
2060
2061    impl<T, I: Input, E: Error<I>> Sealed for PResult<T, I, E> {}
2062}
2063
2064/// Additional convenience methods for [`PResult`].
2065pub trait PResultExt: sealed::Sealed {
2066    type Parsed;
2067    type Error: Error<Self::Input>;
2068    type Input: Input;
2069
2070    fn success(parsed: Self::Parsed, rem: Self::Input) -> Self;
2071
2072    fn failure(error: Self::Error, rem: Self::Input) -> Self;
2073
2074    fn remaining(&self) -> &Self::Input;
2075
2076    fn remaining_mut(&mut self) -> &mut Self::Input;
2077
2078    fn parsed(&self) -> Option<&Self::Parsed>;
2079
2080    fn parsed_mut(&mut self) -> Option<&mut Self::Parsed>;
2081
2082    fn extract(self) -> (Result<Self::Parsed, Self::Error>, Self::Input);
2083
2084    fn map_parsed<F, R>(self, map_fn: F) -> PResult<R, Self::Input, Self::Error>
2085    where
2086        F: FnOnce(Self::Parsed) -> R;
2087}
2088
2089impl<T, I: Input, E: Error<I>> PResultExt for PResult<T, I, E> {
2090    type Parsed = T;
2091    type Error = E;
2092    type Input = I;
2093
2094    fn success(parsed: T, rem: I) -> Self {
2095        Ok(Success(parsed, rem))
2096    }
2097
2098    fn failure(error: E, rem: I) -> Self {
2099        Err(Failure(error, rem))
2100    }
2101
2102    fn remaining(&self) -> &I {
2103        match self {
2104            Ok(Success(_, rem)) => rem,
2105            Err(Failure(_, rem)) => rem,
2106        }
2107    }
2108
2109    fn remaining_mut(&mut self) -> &mut I {
2110        match self {
2111            Ok(succ) => &mut succ.1,
2112            Err(fail) => &mut fail.1,
2113        }
2114    }
2115
2116    fn parsed(&self) -> Option<&Self::Parsed> {
2117        if let Ok(Success(val, _)) = self {
2118            Some(val)
2119        } else {
2120            None
2121        }
2122    }
2123
2124    fn parsed_mut(&mut self) -> Option<&mut Self::Parsed> {
2125        if let Ok(succ) = self {
2126            Some(&mut succ.0)
2127        } else {
2128            None
2129        }
2130    }
2131
2132    fn extract(self) -> (Result<T, E>, I) {
2133        match self {
2134            Ok(Success(val, rem)) => (Ok(val), rem),
2135            Err(Failure(err, rem)) => (Err(err), rem),
2136        }
2137    }
2138
2139    fn map_parsed<F, R>(self, map_fn: F) -> PResult<R, I, E>
2140    where
2141        F: FnOnce(Self::Parsed) -> R,
2142    {
2143        self.map(move |succ| succ.map(map_fn))
2144    }
2145}
2146
2147impl<F, T, I, E> Parse<I> for F
2148where
2149    F: Fn(I) -> PResult<T, I, E>,
2150    I: Input,
2151    E: Error<I>,
2152{
2153    type Parsed = T;
2154    type Error = E;
2155
2156    fn parse<N>(&self, input: N) -> PResult<T, I, E>
2157    where
2158        N: IntoInput<Input = I>,
2159    {
2160        (*self)(input.into_input())
2161    }
2162}
2163
2164impl<T, I: Input> Success<T, I> {
2165    pub fn map<F, R>(self, map_fn: F) -> Success<R, I>
2166    where
2167        F: FnOnce(T) -> R,
2168    {
2169        let Success(val, rem) = self;
2170        Success(map_fn(val), rem)
2171    }
2172}
2173
2174impl<T, I> From<Success<T, I>> for (T, I) {
2175    fn from(Success(t, i): Success<T, I>) -> (T, I) {
2176        (t, i)
2177    }
2178}
2179
2180impl<T, I> From<(T, I)> for Success<T, I> {
2181    fn from((t, i): (T, I)) -> Self {
2182        Self(t, i)
2183    }
2184}
2185
2186impl<T, U: From<T>, I: Input, E: Error<I>> From<Success<T, I>> for PResult<U, I, E> {
2187    fn from(Success(val, rem): Success<T, I>) -> Self {
2188        Ok(Success(U::from(val), rem))
2189    }
2190}
2191
2192impl<T, I> From<Failure<T, I>> for (T, I) {
2193    fn from(Failure(t, i): Failure<T, I>) -> (T, I) {
2194        (t, i)
2195    }
2196}
2197
2198impl<T, I> From<(T, I)> for Failure<T, I> {
2199    fn from((t, i): (T, I)) -> Self {
2200        Self(t, i)
2201    }
2202}
2203
2204impl<T, I: Input, E: Error<I>, U: Error<I> + From<E>> From<Failure<E, I>> for PResult<T, I, U> {
2205    fn from(Failure(err, rem): Failure<E, I>) -> Self {
2206        Err(Failure(U::from(err), rem))
2207    }
2208}
2209
2210impl<I: Input, E: Error<I>> ErrorSeed<I, E> for core::convert::Infallible {
2211    fn into_error(self, pos: I) -> E {
2212        let _ = pos;
2213        unreachable!()
2214    }
2215}
2216
2217impl<I: Input, E: Error<I>> ErrorSeed<I, E> for ErrorKind {
2218    fn into_error(self, pos: I) -> E {
2219        match self {
2220            Self::NeedMoreInput => E::need_more_input(pos),
2221            Self::ExpectedEof => E::expected_eof(pos),
2222            Self::InvalidInput => E::invalid_input(pos),
2223        }
2224    }
2225}
2226
2227impl<LT, LI, RT, RI> PartialEq<Success<RT, RI>> for Success<LT, LI>
2228where
2229    LT: PartialEq<RT>,
2230    LI: PartialEq<RI>,
2231{
2232    fn eq(&self, other: &Success<RT, RI>) -> bool {
2233        PartialEq::eq(&self.0, &other.0) && PartialEq::eq(&self.1, &other.1)
2234    }
2235}
2236
2237impl<T, I> Eq for Success<T, I>
2238where
2239    T: Eq,
2240    I: Eq,
2241{
2242}
2243
2244impl<LE, LI, RE, RI> PartialEq<Failure<RE, RI>> for Failure<LE, LI>
2245where
2246    LE: PartialEq<RE>,
2247    LI: PartialEq<RI>,
2248{
2249    fn eq(&self, other: &Failure<RE, RI>) -> bool {
2250        PartialEq::eq(&self.0, &other.0) && PartialEq::eq(&self.1, &other.1)
2251    }
2252}
2253
2254impl<T, I> Eq for Failure<T, I>
2255where
2256    T: Eq,
2257    I: Eq,
2258{
2259}