Skip to main content

rewriter/
span.rs

1use crate::interface;
2use std::cmp::Ordering;
3
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub struct LineColumn {
6    /// 1-based line
7    pub line: usize,
8    /// 0-based column
9    pub column: usize,
10}
11
12impl Default for LineColumn {
13    fn default() -> Self {
14        Self { line: 1, column: 0 }
15    }
16}
17
18impl Ord for LineColumn {
19    fn cmp(&self, other: &Self) -> Ordering {
20        let ordering = self.line.cmp(&other.line);
21        if ordering == Ordering::Equal {
22            self.column.cmp(&other.column)
23        } else {
24            ordering
25        }
26    }
27}
28
29impl PartialOrd for LineColumn {
30    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31        Some(self.cmp(other))
32    }
33}
34
35impl interface::LineColumn for LineColumn {
36    fn line(&self) -> usize {
37        self.line
38    }
39
40    fn line_mut(&mut self) -> &mut usize {
41        &mut self.line
42    }
43
44    fn column(&self) -> usize {
45        self.column
46    }
47
48    fn column_mut(&mut self) -> &mut usize {
49        &mut self.column
50    }
51}
52
53#[derive(Clone, Copy, Debug, Default)]
54pub struct Span {
55    start: LineColumn,
56    end: LineColumn,
57}
58
59impl Span {
60    #[must_use]
61    pub fn new(start: LineColumn, end: LineColumn) -> Self {
62        Self { start, end }
63    }
64}
65
66impl interface::Span for Span {
67    type LineColumn = LineColumn;
68
69    fn line_column(line: usize, column: usize) -> Self::LineColumn {
70        LineColumn { line, column }
71    }
72
73    fn start(&self) -> LineColumn {
74        self.start
75    }
76
77    fn end(&self) -> LineColumn {
78        self.end
79    }
80}