oni_comb_parser/text/
char.rs1use crate::error::{ExpectError, Expected};
2use crate::fail::{Fail, PResult};
3use crate::input::Input;
4use crate::parser::Parser;
5use crate::str_input::StrInput;
6
7pub struct Char(char);
8
9pub fn char(c: char) -> Char {
10 Char(c)
11}
12
13impl<'a> Parser<StrInput<'a>> for Char {
14 type Error = <StrInput<'a> as Input>::Error;
15 type Output = char;
16
17 #[inline]
18 fn parse_next(&mut self, input: &mut StrInput<'a>) -> PResult<Self::Output, Self::Error> {
19 let pos = input.offset();
20 let remaining = input.remaining();
21 match remaining.chars().next() {
22 Some(c) if c == self.0 => {
23 input.advance(c.len_utf8());
24 Ok(c)
25 }
26 _ => Err(Fail::Backtrack(Self::Error::from_expected(pos, Expected::Char(self.0)))),
27 }
28 }
29}