pub trait AlpmParser: Sized {
// Required method
fn parser<'a>(input: &mut Input<'a>) -> PResult<'a, Self>;
// Provided method
fn delimiter_error_context<'a, O, P>(
parser: P,
) -> impl Parser<Input<'a>, O, ErrMode<ParseStack<'a>>>
where P: Parser<Input<'a>, O, ErrMode<ParseStack<'a>>> { ... }
}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::prelude::*;
use winnow::{Parser, token::take_while};
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
struct Alphanumeric(String);
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.parse_peek(Input::new("abc123\nnext"))?;
assert_eq!(*remaining, "\nnext");
assert_eq!(parsed, Alphanumeric("abc123".to_string()));
Required Methods§
Provided Methods§
Sourcefn delimiter_error_context<'a, O, P>(
parser: P,
) -> impl Parser<Input<'a>, O, ErrMode<ParseStack<'a>>>
fn delimiter_error_context<'a, O, P>( parser: P, ) -> impl Parser<Input<'a>, O, ErrMode<ParseStack<'a>>>
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".