Skip to main content

oni_comb_parser/
byte_input.rs

1use crate::input::Input;
2
3pub struct ByteInput<'a> {
4  src: &'a [u8],
5  offset: usize,
6}
7
8impl<'a> ByteInput<'a> {
9  pub fn new(src: &'a [u8]) -> Self {
10    Self { src, offset: 0 }
11  }
12
13  #[inline]
14  pub fn peek_byte(&self) -> Option<u8> {
15    self.src.get(self.offset).copied()
16  }
17}
18
19impl<'a> Input for ByteInput<'a> {
20  type Checkpoint = usize;
21  #[cfg(feature = "alloc")]
22  type Error = crate::error::ParseError;
23  #[cfg(not(feature = "alloc"))]
24  type Error = crate::error::MinimalError;
25  type Slice = &'a [u8];
26  type Token = u8;
27
28  #[inline]
29  fn next_token(&mut self) -> Option<u8> {
30    let b = self.src.get(self.offset).copied()?;
31    self.offset += 1;
32    Some(b)
33  }
34
35  #[inline]
36  fn peek_token(&self) -> Option<u8> {
37    self.src.get(self.offset).copied()
38  }
39
40  #[inline]
41  fn slice_since(&self, cp: usize) -> &'a [u8] {
42    &self.src[cp..self.offset]
43  }
44
45  fn checkpoint(&self) -> usize {
46    self.offset
47  }
48
49  fn reset(&mut self, cp: usize) {
50    self.offset = cp;
51  }
52
53  fn offset(&self) -> usize {
54    self.offset
55  }
56
57  fn remaining(&self) -> &'a [u8] {
58    &self.src[self.offset..]
59  }
60
61  fn is_eof(&self) -> bool {
62    self.offset >= self.src.len()
63  }
64}