1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
5pub struct Location {
6 pub line: u64,
8 pub column: u64,
10}
11
12impl Location {
13 pub const fn empty() -> Self {
15 Self { line: 0, column: 0 }
16 }
17
18 pub const fn new(line: u64, column: u64) -> Self {
20 Self { line, column }
21 }
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
26pub struct Span {
27 pub start: Location,
28 pub end: Location,
29}
30
31impl Span {
32 pub const fn empty() -> Self {
34 Self {
35 start: Location::empty(),
36 end: Location::empty(),
37 }
38 }
39
40 pub const fn new(start: Location, end: Location) -> Self {
42 Self { start, end }
43 }
44
45 pub fn union(&self, other: &Span) -> Span {
47 if self.start.line == 0 {
48 return *other;
49 }
50 if other.start.line == 0 {
51 return *self;
52 }
53 Span {
54 start: core::cmp::min(self.start, other.start),
55 end: core::cmp::max(self.end, other.end),
56 }
57 }
58}
59
60pub trait Spanned {
62 fn span(&self) -> Span;
63}