pub trait Parser<T>: Sized {
type Err;
// Required method
fn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>;
// Provided methods
fn try_parse(&self, s: &mut ParserString) -> Result<T, Self::Err> { ... }
fn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> Chain<T, U, Self, P2> { ... }
fn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> Or<T, E, Self, P2> { ... }
fn many(self) -> Many<T, Self> { ... }
fn many1(self) -> Many1<T, Self> { ... }
fn map<U: 'static>(
self,
f: impl Fn(T) -> U + 'static,
) -> impl Parser<U, Err = Self::Err> { ... }
fn map_err<E: 'static>(
self,
f: impl Fn(Self::Err) -> E + 'static,
) -> impl Parser<T, Err = E> { ... }
fn and_then<U: 'static, E: Into<Self::Err>>(
self,
f: impl Fn(T) -> Result<U, E> + 'static,
) -> impl Parser<U, Err = Self::Err> { ... }
fn after<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> impl Parser<T, Err = Self::Err> { ... }
fn replace<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> impl Parser<U, Err = Self::Err> { ... }
fn convert_err<E: From<Self::Err> + 'static>(
self,
) -> impl Parser<T, Err = E> { ... }
}
Expand description
All parsers implement this trait. Any function or closure with the signature
Fn(&mut ParserString) -> Result<T, E>
implements Parser.
Required Associated Types§
Required Methods§
Sourcefn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
fn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
Run this parser, using a ParserString
.
Provided Methods§
Sourcefn try_parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
fn try_parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
Run this parser without affecting the string on failure. In other words, the string will be “rewinded” on failure.
Sourcefn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> Chain<T, U, Self, P2>
fn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2, ) -> Chain<T, U, Self, P2>
Constructs a Chain
combinator.
Sourcefn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> Or<T, E, Self, P2>
fn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>( self, other: P2, ) -> Or<T, E, Self, P2>
Constructs a Or
combinator.
Sourcefn map<U: 'static>(
self,
f: impl Fn(T) -> U + 'static,
) -> impl Parser<U, Err = Self::Err>
fn map<U: 'static>( self, f: impl Fn(T) -> U + 'static, ) -> impl Parser<U, Err = Self::Err>
Apply a function to the output of this parser on success.
Sourcefn map_err<E: 'static>(
self,
f: impl Fn(Self::Err) -> E + 'static,
) -> impl Parser<T, Err = E>
fn map_err<E: 'static>( self, f: impl Fn(Self::Err) -> E + 'static, ) -> impl Parser<T, Err = E>
Apply a function to the Err
output of this parser on failure.
Sourcefn and_then<U: 'static, E: Into<Self::Err>>(
self,
f: impl Fn(T) -> Result<U, E> + 'static,
) -> impl Parser<U, Err = Self::Err>
fn and_then<U: 'static, E: Into<Self::Err>>( self, f: impl Fn(T) -> Result<U, E> + 'static, ) -> impl Parser<U, Err = Self::Err>
Applies a function to the output of this parser on success, using error coercion rules.
Sourcefn after<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> impl Parser<T, Err = Self::Err>
fn after<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2, ) -> impl Parser<T, Err = Self::Err>
Similar to Chain
, but only keeps the output of the first parser.
Sourcefn replace<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2,
) -> impl Parser<U, Err = Self::Err>
fn replace<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2, ) -> impl Parser<U, Err = Self::Err>
Similar to Chain
, but only keeps the output of the second parser.
Sourcefn convert_err<E: From<Self::Err> + 'static>(self) -> impl Parser<T, Err = E>
fn convert_err<E: From<Self::Err> + 'static>(self) -> impl Parser<T, Err = E>
Explicitly sets the target error type of this parser. Can help with type inference.
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.