hermes_support/
location.rs1use std::num::NonZeroU32;
4
5#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
8pub struct SourceId(NonZeroU32);
9
10impl SourceId {
11 pub fn from_index(index: u32) -> SourceId {
12 SourceId(NonZeroU32::new(index + 1).expect("index + 1 is nonzero"))
13 }
14 pub fn index(self) -> u32 {
15 self.0.get() - 1
16 }
17}
18
19#[derive(Copy, Clone, PartialEq, Eq, Debug)]
23pub struct SMLoc {
24 pub source: SourceId,
25 pub offset: u32,
26}
27
28#[derive(Copy, Clone, PartialEq, Eq, Debug)]
30pub struct SMRange {
31 pub start: SMLoc,
32 pub end: SMLoc,
33}
34
35impl SMRange {
36 pub fn combine(a: SMLoc, b: SMLoc) -> SMRange {
40 debug_assert_eq!(a.source, b.source);
41 let (lo, hi) = if a.offset <= b.offset { (a, b) } else { (b, a) };
42 SMRange {
43 start: lo,
44 end: SMLoc {
45 source: hi.source,
46 offset: hi.offset + 1,
47 },
48 }
49 }
50}
51
52#[derive(Copy, Clone, PartialEq, Eq, Debug)]
55pub struct SourceCoords {
56 pub buf: SourceId,
58 pub line: u32,
60 pub col: u32,
62}
63
64impl SourceCoords {
65 pub fn is_same_source_line_as(&self, o: &SourceCoords) -> bool {
66 self.buf == o.buf && self.line == o.line
67 }
68 pub fn less(&self, o: &SourceCoords) -> bool {
69 (self.buf.index(), self.line, self.col) < (o.buf.index(), o.line, o.col)
70 }
71}
72
73#[allow(dead_code)]
76#[derive(Copy, Clone, Debug)]
77pub struct LineCoord<'a> {
78 pub buf: SourceId,
79 pub line: u32,
80 pub line_ref: &'a [u8],
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn source_id_niche() {
89 assert_eq!(
91 std::mem::size_of::<Option<SourceId>>(),
92 std::mem::size_of::<SourceId>()
93 );
94 assert_eq!(std::mem::size_of::<SMLoc>(), 8);
95 }
96
97 #[test]
98 fn coords_ordering_and_same_line() {
99 let s = SourceId::from_index(0);
100 let a = SourceCoords {
101 buf: s,
102 line: 2,
103 col: 3,
104 };
105 let b = SourceCoords {
106 buf: s,
107 line: 2,
108 col: 5,
109 };
110 assert!(a.less(&b));
111 assert!(a.is_same_source_line_as(&b));
112 }
113
114 #[test]
115 fn combine_into_range_orders_endpoints() {
116 let s = SourceId::from_index(0);
117 let lo = SMLoc {
118 source: s,
119 offset: 4,
120 };
121 let hi = SMLoc {
122 source: s,
123 offset: 9,
124 };
125 let r = SMRange::combine(hi, lo);
126 assert_eq!(r.start.offset, 4);
127 assert_eq!(r.end.offset, 10); }
129}