oom/source/
position.rs

1use crate::Span;
2
3#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
4pub struct Position {
5    pub line: usize,
6    pub column: usize,
7}
8
9impl Position {
10    pub fn new(line: usize, column: usize) -> Position {
11        Position { line, column }
12    }
13
14    pub fn from_tuple(line_col: (usize, usize)) -> Position {
15        let (line, column) = line_col;
16        Position { line, column }
17    }
18
19    pub fn to_tuple(&self) -> (usize, usize) {
20        (self.line, self.column)
21    }
22
23    pub fn span_to(&self, input: &str) -> Span {
24        let lines = input.lines().map(String::from).collect::<Vec<String>>();
25        let end = Position::new(
26            self.line + lines.len(),
27            self.column + lines.iter().map(|line| line.len()).sum::<usize>(),
28        );
29        Span::new(input.to_string(), self.to_tuple(), end.to_tuple())
30    }
31}
32
33impl Into<(usize, usize)> for Position {
34    fn into(self) -> (usize, usize) {
35        self.to_tuple()
36    }
37}