Skip to main content

AlpmParser

Trait AlpmParser 

Source
pub trait AlpmParser: Sized {
    // Required method
    fn parser(input: &mut &str) -> ModalResult<Self>;

    // Provided method
    fn delimiter_error_context<'a, O, P>(
        parser: P,
    ) -> impl Parser<&'a str, O, ErrMode<ContextError>>
       where P: Parser<&'a str, O, ErrMode<ContextError>> { ... }
}
Expand description

A trait for types that implement a parser.

The AlpmParser::parser function is expected to only consume the characters that are part of its own structure/format.

For checks, such as “parse till EOF” or “parse until delimiter”, check the ParserUntil and ParserUntilInclusive traits, which provide auto implementations for AlpmParser.

§Examples

use alpm_parsers::traits::AlpmParser;
use winnow::{
    ModalResult,
    Parser,
    error::{ContextError, ErrMode, StrContext},
    token::take_while,
};

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

impl AlpmParser for Alphanumeric {
    fn parser(input: &mut &str) -> ModalResult<Self> {
        take_while(1.., |c: char| c.is_alphanumeric())
            .map(|s: &str| Alphanumeric(s.to_string()))
            .parse_next(input)
    }
}

assert_eq!(
    Alphanumeric::parser.parse_peek("abc123\nnext"),
    Ok(("\nnext", Alphanumeric("abc123".to_string())))
);

Required Methods§

Source

fn parser(input: &mut &str) -> ModalResult<Self>

Returns a [Parser] that parses Self from the given input stream.

This parser is expected to not consume anything but the tokens needed to parse and build Self.

Provided Methods§

Source

fn delimiter_error_context<'a, O, P>( parser: P, ) -> impl Parser<&'a str, O, ErrMode<ContextError>>
where P: Parser<&'a str, O, ErrMode<ContextError>>,

Attaches a winnow error context to the parser when a parse error is encountered.

§Note

This is useful to, for example, show consistent error messages whenever a character is encountered that should not be in the character set of the value to parse (see ParserUntil::parser_until).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§