1use std::{cmp::Ordering, fmt};
4
5#[derive(Clone, Copy, Eq, PartialEq, Default)]
10pub struct Span {
11 pub start: Position,
13 pub end: Position,
15}
16
17impl fmt::Debug for Span {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 write!(f, "Span({:?}, {:?})", self.start, self.end)
20 }
21}
22
23impl Ord for Span {
24 fn cmp(&self, other: &Self) -> Ordering {
25 (&self.start, &self.end).cmp(&(&other.start, &other.end))
26 }
27}
28
29impl PartialOrd for Span {
30 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31 Some(self.cmp(other))
32 }
33}
34
35#[derive(Clone, Copy, Eq, PartialEq)]
40pub struct Position {
41 pub offset: usize,
47 pub line: usize,
49 pub column: usize,
51}
52
53impl Default for Position {
54 fn default() -> Self {
55 Self { offset: usize::default(), line: 1, column: 1 }
56 }
57}
58
59impl fmt::Debug for Position {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 write!(
62 f,
63 "Position(o: {:?}, l: {:?}, c: {:?})",
64 self.offset, self.line, self.column
65 )
66 }
67}
68
69impl Ord for Position {
70 fn cmp(&self, other: &Self) -> Ordering {
71 self.offset.cmp(&other.offset)
72 }
73}
74
75impl PartialOrd for Position {
76 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
77 Some(self.cmp(other))
78 }
79}
80
81impl Span {
82 pub const fn new(start: Position, end: Position) -> Self {
84 Self { start, end }
85 }
86
87 pub const fn splat(pos: Position) -> Self {
89 Self::new(pos, pos)
90 }
91
92 pub const fn with_start(self, pos: Position) -> Self {
95 Self { start: pos, ..self }
96 }
97
98 pub const fn with_end(self, pos: Position) -> Self {
101 Self { end: pos, ..self }
102 }
103}
104
105impl Position {
106 pub const fn new(offset: usize, line: usize, column: usize) -> Self {
115 Self { offset, line, column }
116 }
117}