1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::core::span::Span;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::ops::Sub;

#[derive(Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Pos(usize);

impl Pos {
    pub fn start() -> Self {
        Self(0)
    }

    pub fn end(input: &str) -> Self {
        Self(input.len())
    }

    pub fn span_to(self, other: Self) -> Span {
        Span::new(self, other)
    }

    pub fn next(self, input: &str) -> (Self, Option<(Span, char)>) {
        match input[self.0..].chars().next() {
            None => (self, None),
            Some(c) => (
                Self(self.0 + c.len_utf8()),
                Some((Span::new(self, Self(self.0 + c.len_utf8())), c)),
            ),
        }
    }

    pub fn invalid() -> Self {
        Self(usize::MAX)
    }
}

impl Display for Pos {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Sub<Pos> for Pos {
    type Output = usize;

    fn sub(self, rhs: Pos) -> Self::Output {
        self.0 - rhs.0
    }
}

impl From<Pos> for usize {
    fn from(val: Pos) -> Self {
        val.0
    }
}