Skip to main content

oni_comb_parser/text/
tag.rs

1use crate::error::ParseError;
2use crate::fail::{Fail, PResult};
3use crate::input::Input;
4use crate::parser::Parser;
5use crate::str_input::StrInput;
6
7pub struct Tag(&'static str);
8
9pub fn tag(s: &'static str) -> Tag {
10  Tag(s)
11}
12
13impl Parser<StrInput<'_>> for Tag {
14  type Error = ParseError;
15  type Output = &'static str;
16
17  #[inline]
18  fn parse_next(&mut self, input: &mut StrInput<'_>) -> PResult<Self::Output, Self::Error> {
19    let pos = input.offset();
20    let remaining = input.remaining();
21    if remaining.starts_with(self.0) {
22      input.advance(self.0.len());
23      Ok(self.0)
24    } else {
25      Err(Fail::Backtrack(ParseError::expected_tag(pos, self.0)))
26    }
27  }
28}