#[cfg(test)]
mod tests;
mod arrayimpl;
mod charimpl;
mod sliceimpl;
mod strimpl;
use derive_new::new;
use crate::Parser;
use crate::state::{Buffer, ParserState, Update};
pub trait Literal<I>: Sized + Copy + Parser<I, Output = Self>
where
I: ?Sized + Buffer,
{
fn literal_len(self) -> usize;
fn literal_eq(self, candidate: &I) -> bool;
}
#[derive(Copy, Clone, Debug, new)]
pub struct LiteralParser<L>(L);
impl<I, L> ParserState<I> for LiteralParser<L>
where
I: ?Sized + Buffer,
L: Literal<I>,
{
type Output = L::Output;
type Error = L::Error;
fn feed(self, input: &I) -> Result<Update<Self, L>, Self::Error> {
use crate::BaseParserError::UnexpectedInput;
use crate::state::Outcome::{Next, Parsed};
let n = self.0.literal_len();
let prefix = input.prefix_up_to(n);
if prefix.len() < n {
Ok(Update::new(0, Next(self)))
} else if self.0.literal_eq(prefix) {
Ok(Update::new(n, Parsed(self.0)))
} else {
Err(Self::Error::from(UnexpectedInput))
}
}
}