Skip to main content

ParserUntilInclusive

Trait ParserUntilInclusive 

Source
pub trait ParserUntilInclusive: Sized {
    // Required method
    fn parser_until_inclusive<'a, P>(
        delimiter: P,
    ) -> impl Parser<Input<'a>, Self, ErrMode<ParseStack<'a>>>
       where P: Parser<Input<'a>, &'a str, ErrMode<ParseStack<'a>>>;

    // Provided method
    fn parser_until_line_ending_inclusive<'a>(
        input: &mut Input<'a>,
    ) -> PResult<'a, Self> { ... }
}
Expand description

A trait for types that can be parsed until a given delimiter parser matches, while consuming that delimiter.

Allows creating non-delimiter-aware type parsers, that can be used inline in file parsers.

Automatically implemented for all types implementing ParserUntil and thereby transitively AlpmParser.

§Examples

use alpm_parsers::prelude::*;
use winnow::{Parser, token::take_while};

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
struct Alphanumeric(String);

// Implementing `AlpmParser` automatically implements `ParserUntilInclusive`:
// `ParserUntil` is auto-implemented over `AlpmParser` and `ParserUntilInclusive` is auto-implemented over `ParserUntil`.
impl AlpmParser for Alphanumeric {
    fn parser<'a>(input: &mut Input<'a>) -> PResult<'a, Self> {
        take_while(1.., |c: char| c.is_alphanumeric())
            .map(|s: &str| Alphanumeric(s.to_string()))
            .parse_next(input)
    }
}

let (remaining, parsed) = Alphanumeric::parser_until_line_ending_inclusive
    .parse_peek(Input::new("abc123\nnext"))?;
assert_eq!(*remaining, "next");
assert_eq!(parsed, Alphanumeric("abc123".to_string()));

Required Methods§

Source

fn parser_until_inclusive<'a, P>( delimiter: P, ) -> impl Parser<Input<'a>, Self, ErrMode<ParseStack<'a>>>
where P: Parser<Input<'a>, &'a str, ErrMode<ParseStack<'a>>>,

Returns a Parser that parses the whole input and consumes the given delimiter parser.

Provided Methods§

Source

fn parser_until_line_ending_inclusive<'a>( input: &mut Input<'a>, ) -> PResult<'a, Self>

Returns a Parser that parses until the end of line and fully consumes it.

Delegates to Self::parser_until_inclusive with the [line_ending] and [eof] delimiter.

A line ending is \n, \r\n or [eof] and consumed as well.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§