diff --git a/src/_cookbook.rs b/src/_cookbook.rs index cc975f4f..efec2b9d 100644 --- a/src/_cookbook.rs +++ b/src/_cookbook.rs @@ -70,7 +70,7 @@ //! pub fn peol_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, (), E> //! { //! pair('%', take_till1("\n\r")) -//! .value(()) // Output is thrown away. +//! .void() // Output is thrown away. //! .parse_next(i) //! } //! ``` @@ -93,7 +93,7 @@ //! take_until("*)"), //! "*)" //! ) -//! .value(()) // Output is thrown away. +//! .void() // Output is thrown away. //! .parse_next(i) //! } //! ``` diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index 1af98e9e..ddbf11f5 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -947,6 +947,28 @@ impl, F: Parser> Parser } } +/// Implementation of [`Parser::void`] +#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] +pub struct Void { + parser: F, + phantom: core::marker::PhantomData, +} + +impl Void { + pub(crate) fn new(parser: F) -> Self { + Self { + parser, + phantom: Default::default(), + } + } +} + +impl, F: Parser> Parser for Void { + fn parse_next(&mut self, input: I) -> IResult { + (self.parser).parse_next(input).map(|(i, _)| (i, ())) + } +} + /// Succeeds if the child parser returns an error. /// /// ```rust diff --git a/src/parser.rs b/src/parser.rs index a373fc16..dae8b191 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -392,6 +392,7 @@ pub trait Parser { { ByRef::new(self) } + /// Returns the provided value if the child parser succeeds. /// /// # Example @@ -415,6 +416,28 @@ pub trait Parser { Value::new(self, val) } + /// Discards the output of the `Parser` + /// + /// # Example + /// + /// ```rust + /// # use winnow::{Err,error::ErrorKind, error::Error, IResult, Parser}; + /// use winnow::character::alpha1; + /// # fn main() { + /// + /// let mut parser = alpha1.void(); + /// + /// assert_eq!(parser.parse_next("abcd"), Ok(("", ()))); + /// assert_eq!(parser.parse_next("123abcd;"), Err(Err::Error(Error::new("123abcd;", ErrorKind::Alpha)))); + /// # } + /// ``` + fn void(self) -> Void + where + Self: core::marker::Sized, + { + Void::new(self) + } + /// Convert the parser's output to another type using [`std::convert::From`] /// /// # Example