nessie_parse/
position.rs

1pub type Row = u16;
2pub type Col = u16;
3
4/// A position in some source code string.
5/// Positions save the offset, the row and the column. That means that a
6/// position is only valid for a specific source code string.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct Pos {
9    /// Starts at 0.
10    pub offset: usize,
11    /// Starts at 1.
12    pub row: Row,
13    /// Starts at 1.
14    pub col: Col,
15}
16
17impl Pos {
18    /// Creates a new position at the start of source code.
19    pub const fn start() -> Self {
20        Pos {
21            offset: 0,
22            row: 1,
23            col: 1,
24        }
25    }
26
27    /// Returns this position, one character to the right.
28    pub const fn right(self) -> Self {
29        Pos {
30            offset: self.offset + 1,
31            row: self.row,
32            col: self.col + 1,
33        }
34    }
35
36    /// Returns this position, one line down, *and with the column reset to 1*.
37    pub const fn down(self) -> Self {
38        Pos {
39            offset: self.offset + 1,
40            row: self.row + 1,
41            col: 1,
42        }
43    }
44}
45
46impl Default for Pos {
47    fn default() -> Self {
48        Pos::start()
49    }
50}
51
52impl std::fmt::Display for Pos {
53    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
54        let Pos { offset, row, col } = *self;
55        write!(f, "Pos(offset {offset} column {col} row {row})")
56    }
57}