kcl_error/
source_range.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq, Clone, Copy, Hash, Deserialize, Serialize, ts_rs::TS)]
7#[ts(export)]
8pub struct ModuleId(u32);
9
10impl ModuleId {
11 pub fn from_usize(id: usize) -> Self {
12 Self(u32::try_from(id).expect("module ID should fit in a u32"))
13 }
14
15 pub fn as_usize(&self) -> usize {
16 usize::try_from(self.0).expect("module ID should fit in a usize")
17 }
18
19 pub fn is_top_level(&self) -> bool {
22 *self == Self::default()
23 }
24}
25
26impl std::fmt::Display for ModuleId {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 write!(f, "{}", self.0)
29 }
30}
31
32#[derive(Debug, Default, Deserialize, Serialize, PartialEq, Copy, Clone, ts_rs::TS, Hash, Eq)]
40#[ts(export, type = "[number, number, number]")]
41pub struct SourceRange([usize; 3]);
42
43impl From<[usize; 3]> for SourceRange {
44 fn from(value: [usize; 3]) -> Self {
45 Self(value)
46 }
47}
48
49impl Ord for SourceRange {
50 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
51 let module_id_cmp = self.module_id().cmp(&other.module_id());
53 if module_id_cmp != std::cmp::Ordering::Equal {
54 return module_id_cmp;
55 }
56 let start_cmp = self.start().cmp(&other.start());
57 if start_cmp != std::cmp::Ordering::Equal {
58 return start_cmp;
59 }
60 self.end().cmp(&other.end())
61 }
62}
63
64impl PartialOrd for SourceRange {
65 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
66 Some(self.cmp(other))
67 }
68}
69
70impl From<&SourceRange> for miette::SourceSpan {
71 fn from(source_range: &SourceRange) -> Self {
72 let length = source_range.end() - source_range.start();
73 let start = miette::SourceOffset::from(source_range.start());
74 Self::new(start, length)
75 }
76}
77
78impl From<SourceRange> for miette::SourceSpan {
79 fn from(source_range: SourceRange) -> Self {
80 Self::from(&source_range)
81 }
82}
83
84impl SourceRange {
85 pub fn new(start: usize, end: usize, module_id: ModuleId) -> Self {
87 Self([start, end, module_id.as_usize()])
88 }
89
90 pub fn synthetic() -> Self {
92 Self::default()
93 }
94
95 pub fn merge(mut ranges: impl Iterator<Item = SourceRange>) -> Self {
96 let mut result = ranges.next().unwrap_or_default();
97
98 for r in ranges {
99 debug_assert!(r.0[2] == result.0[2], "Merging source ranges from different files");
100 if r.0[0] < result.0[0] {
101 result.0[0] = r.0[0]
102 }
103 if r.0[1] > result.0[1] {
104 result.0[1] = r.0[1];
105 }
106 }
107
108 result
109 }
110
111 pub fn is_synthetic(&self) -> bool {
114 self.start() == 0 && self.end() == 0
115 }
116
117 pub fn start(&self) -> usize {
119 self.0[0]
120 }
121
122 pub fn start_as_range(&self) -> Self {
125 Self([self.0[0], self.0[0], self.0[2]])
126 }
127
128 pub fn end(&self) -> usize {
130 self.0[1]
131 }
132
133 pub fn module_id(&self) -> ModuleId {
135 ModuleId::from_usize(self.0[2])
136 }
137
138 pub fn is_top_level_module(&self) -> bool {
140 self.module_id().is_top_level()
141 }
142
143 pub fn contains(&self, pos: usize) -> bool {
145 pos >= self.start() && pos <= self.end()
146 }
147
148 pub fn contains_range(&self, other: &Self) -> bool {
150 self.module_id() == other.module_id() && self.start() <= other.start() && self.end() >= other.end()
151 }
152}