use crate::{Error, InputToken, Mismatch, Parser};
pub fn end_of_input<IT>() -> impl Parser<IT, ()>
where
IT: InputToken,
{
|input| match input.peek() {
None => Ok(()),
Some(token) => {
let position = token.position();
let mismatch = Mismatch::new("end of input", token.token().clone().to_string());
Err(Error::UnexpectedToken(
input.source_name(),
position,
Some(mismatch),
))
}
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn success() {
let mut input = Input::new_from_chars("".chars(), None);
assert!(end_of_input()(&mut input).is_ok());
}
#[test]
fn fail() {
let mut input = Input::new_from_chars("hello".chars(), None);
assert!(end_of_input()(&mut input).is_err());
}
}