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