use crate::BaseParserError;
use crate::combinators::{MapError, MapOutput, Or, Then};
use crate::state::{Buffer, ParserState};
pub trait Parser<I>: Sized
where
I: ?Sized,
{
type Output;
type Error: From<BaseParserError>;
type State: ParserState<I, Output = Self::Output, Error = Self::Error>;
fn into_parser(self) -> Self::State;
fn parse_all(self, input: &I) -> Result<Self::Output, Self::Error>
where
I: Buffer,
{
use crate::state::Outcome::{Next, Parsed};
use crate::state::Update;
let Update { consumed, outcome } = self.into_parser().feed(input)?;
match outcome {
Next(p) => p.end_input(input.drop_prefix(consumed)),
Parsed(output) => Ok(output),
}
}
fn map<F, O>(self, f: F) -> MapOutput<Self, F, O>
where
F: FnOnce(Self::Output) -> O,
{
MapOutput::new(self, f)
}
fn map_error<F, E>(self, f: F) -> MapError<Self, F, Self::Error>
where
F: FnOnce(Self::Error) -> E,
{
MapError::new(self, f)
}
fn then<Q>(self, other: Q) -> Then<Self, Q> {
Then::new(self, other)
}
fn or<Q>(self, other: Q) -> Or<Self, Q> {
Or::new(self, other)
}
}