use anyhow::Result;
pub trait Parsing<P> {
fn run(&self, input: &str) -> Result<P>;
fn name(&self) -> &'static str;
}
pub struct Parser<F, P>
where
F: Fn(&str) -> P,
{
pub run: F,
pub name: &'static str,
}
impl<F, P> Parsing<P> for Parser<F, P>
where
F: Fn(&str) -> P,
{
fn run(&self, input: &str) -> Result<P> {
Ok((self.run)(input))
}
fn name(&self) -> &'static str {
self.name
}
}
pub struct FailableParser<F, P>
where
F: Fn(&str) -> Result<P>,
{
pub run: F,
pub name: &'static str,
}
impl<F, P> Parsing<P> for FailableParser<F, P>
where
F: Fn(&str) -> Result<P>,
{
fn run(&self, input: &str) -> Result<P> {
(self.run)(input)
}
fn name(&self) -> &'static str {
self.name
}
}