-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
location.rs
205 lines (178 loc) · 5.1 KB
/
location.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
use core::cmp::Ordering;
use std::fmt;
use std::path::PathBuf;
use intern::string_key::Intern;
use intern::string_key::StringKey;
use intern::Lookup;
use crate::span::Span;
/// The location of a source. Could be a standalone file (e.g. test.graphql),
/// an embedded source (GraphQL tag in a JS file) or generated code without a
/// location.
#[derive(
Copy,
Clone,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
serde::Serialize,
serde::Deserialize
)]
#[serde(tag = "type")]
pub enum SourceLocationKey {
/// A source embedded within a file. The 0-based index is an index into the
/// embedded sources. E.g. the second graphql tag has index 1.
Embedded {
path: StringKey,
index: u16,
},
Standalone {
path: StringKey,
},
Generated,
}
impl SourceLocationKey {
/// Returns a `SourceLocationKey` that's not backed by a real file. In most
/// cases it's preferred to use a related real file.
pub fn generated() -> Self {
SourceLocationKey::Generated
}
pub fn standalone(path: &str) -> Self {
SourceLocationKey::Standalone {
path: path.intern(),
}
}
pub fn embedded(path: &str, index: usize) -> Self {
SourceLocationKey::Embedded {
path: path.intern(),
index: index.try_into().unwrap(),
}
}
pub fn path(self) -> &'static str {
match self {
SourceLocationKey::Embedded { path, .. } => path.lookup(),
SourceLocationKey::Standalone { path } => path.lookup(),
SourceLocationKey::Generated => "<generated>",
}
}
pub fn get_dir(self) -> PathBuf {
let mut path = PathBuf::from(self.path());
path.pop();
path
}
pub fn is_generated(&self) -> bool {
matches!(self, SourceLocationKey::Generated)
}
}
/// An absolute source location describing both the file and position (span)
/// with that file.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Serialize)]
pub struct Location {
/// The source containing this location (e.g. embedded or standalone file).
source_location: SourceLocationKey,
/// Relative position with the file
span: Span,
}
impl fmt::Debug for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{:?}", self.source_location.path(), self.span)
}
}
impl Location {
/// Returns a location that's not backed by a real file. In most cases it's
/// preferred to use a related real location.
pub fn generated() -> Self {
Location::new(SourceLocationKey::generated(), Span::new(0, 0))
}
pub fn new(source_location: SourceLocationKey, span: Span) -> Self {
Self {
source_location,
span,
}
}
pub fn source_location(&self) -> SourceLocationKey {
self.source_location
}
pub fn span(&self) -> Span {
self.span
}
pub fn with_span(&self, span: Span) -> Self {
Self {
source_location: self.source_location,
span,
}
}
pub fn contains(&self, subspan: Span) -> bool {
self.span.contains(subspan)
}
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct WithLocation<T> {
pub location: Location,
pub item: T,
}
impl<T: Ord> Ord for WithLocation<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.item.cmp(&other.item)
}
}
impl<T: PartialOrd> PartialOrd for WithLocation<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.item.partial_cmp(&other.item)
}
}
impl<T> WithLocation<T> {
pub fn from_span(source_location: SourceLocationKey, span: Span, item: T) -> Self {
Self {
location: Location::new(source_location, span),
item,
}
}
pub fn new(location: Location, item: T) -> Self {
Self { location, item }
}
/// Wraps the given item in a `WithLocation`, without associating it with
/// any real location. In most cases it's preferred to use a related real
/// location the item was derived from.
pub fn generated(item: T) -> Self {
Self {
location: Location::generated(),
item,
}
}
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> WithLocation<U> {
WithLocation {
location: self.location,
item: f(self.item),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn with_location_memory() {
assert_eq!(20, std::mem::size_of::<WithLocation<StringKey>>());
}
#[test]
fn location_debug_printing() {
assert_eq!(
format!(
"{:?}",
Location::new(
SourceLocationKey::embedded("example/file.js", 2),
Span::new(10, 30)
)
),
"example/file.js:10:30".to_string()
);
}
}