1use crate::position::Pos;
2
3#[derive(Debug, Clone, Copy)]
4pub struct State<'input> {
5 pub text: &'input str,
6 pub pos: Pos,
7}
8
9impl<'input> State<'input> {
10 pub const fn new(text: &'input str) -> Self {
11 let pos = Pos::start();
12 State { text, pos }
13 }
14
15 pub const fn at(text: &'input str, pos: Pos) -> Self {
16 State { text, pos }
17 }
18
19 pub const fn with_pos(self, pos: Pos) -> Self {
20 State { pos, ..self }
21 }
22
23 pub fn rest(&self) -> &'input str {
24 &self.text[self.pos.offset..]
25 }
26
27 pub const fn eof(&self) -> bool {
28 self.pos.offset >= self.text.len()
29 }
30}
31
32impl<'input, T: Into<&'input str>> From<T> for State<'input> {
33 fn from(text: T) -> Self {
34 State::new(text.into())
35 }
36}