#[derive(Debug, PartialEq)]
pub enum Outcome<P, O> {
Next(P),
Parsed(O),
}
use Outcome::*;
pub trait OutcomeExt<P, O> {
type MappedOutcome<P2, O2>;
fn map_parser<F, P2>(self, f: F) -> Self::MappedOutcome<P2, O>
where
F: FnOnce(P) -> P2;
fn map_output<F, O2>(self, f: F) -> Self::MappedOutcome<P, O2>
where
F: FnOnce(O) -> O2;
}
impl<P, O> OutcomeExt<P, O> for Outcome<P, O> {
type MappedOutcome<P2, O2> = Outcome<P2, O2>;
fn map_parser<F, P2>(self, f: F) -> Outcome<P2, O>
where
F: FnOnce(P) -> P2,
{
match self {
Next(p) => Next(f(p)),
Parsed(x) => Parsed(x),
}
}
fn map_output<F, O2>(self, f: F) -> Outcome<P, O2>
where
F: FnOnce(O) -> O2,
{
match self {
Next(p) => Next(p),
Parsed(x) => Parsed(f(x)),
}
}
}
impl<P, O, E> Outcome<P, Result<O, E>> {
pub fn transpose_output(self) -> Result<Outcome<P, O>, E> {
match self {
Next(s) => Ok(Next(s)),
Parsed(Ok(x)) => Ok(Parsed(x)),
Parsed(Err(e)) => Err(e),
}
}
}