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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use crate::{Location, Span};
use std::borrow::{Borrow, BorrowMut};
use std::ops::{Deref, DerefMut};

/// Located data.
///
/// This is a simple wrapper around data that can be located in a source file.
/// It is useful to wrap abstract syntax tree nodes.
///
/// It is a tuple struct so it can be easily deconstructed using pattern matching.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct Loc<T, F, S = Span>(pub T, pub Location<F, S>);

impl<T, F, S> Loc<T, F, S> {
	/// Creates a new located value.
	#[inline(always)]
	pub fn new(t: T, location: Location<F, S>) -> Self {
		Self(t, location)
	}

	/// Unwraps the value and discard its location.
	#[inline(always)]
	pub fn into_value(self) -> T {
		self.0
	}

	/// Discards the value and returns its location.
	#[inline(always)]
	pub fn into_location(self) -> Location<F, S> {
		self.1
	}

	/// Discards the value and returns its file.
	#[inline(always)]
	pub fn into_file(self) -> F {
		self.1.into_file()
	}

	/// Discards the value and returns its span.
	#[inline(always)]
	pub fn into_span(self) -> S {
		self.1.into_span()
	}

	/// Returns a reference to the wrapped value.
	#[inline(always)]
	pub fn value(&self) -> &T {
		&self.0
	}

	/// Returns a mutable reference to the wrapped value.
	#[inline(always)]
	pub fn value_mut(&mut self) -> &mut T {
		&mut self.0
	}

	/// Returns a reference to the value's location.
	#[inline(always)]
	pub fn location(&self) -> &Location<F, S> {
		&self.1
	}

	/// Returns a mutable reference to the value's location.
	#[inline(always)]
	pub fn location_mut(&mut self) -> &mut Location<F, S> {
		&mut self.1
	}

	/// Returns the value's span.
	#[inline(always)]
	pub fn span(&self) -> S
	where
		S: Clone,
	{
		self.1.span()
	}

	/// Returns a mutable reference the value's span.
	#[inline(always)]
	pub fn span_mut(&mut self) -> &mut S {
		self.1.span_mut()
	}

	/// Sets the value's span and returns the previous one.
	#[inline(always)]
	pub fn set_span(&mut self, span: S) -> S {
		self.1.set_span(span)
	}

	/// Returns a reference to the value's source file.
	#[inline(always)]
	pub fn file(&self) -> &F {
		self.1.file()
	}

	/// Returns a mutable reference to the value's source file.
	#[inline(always)]
	pub fn file_mut(&mut self) -> &mut F {
		self.1.file_mut()
	}

	/// Sets the value's file and returns the previous one.
	#[inline(always)]
	pub fn set_file(&mut self, file: F) -> F {
		self.1.set_file(file)
	}

	/// Maps the inner value.
	#[inline(always)]
	pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Loc<U, F, S> {
		Loc(f(self.0), self.1)
	}

	/// Converts the inner value.
	#[inline(always)]
	pub fn cast<U>(self) -> Loc<U, F, S>
	where
		U: From<T>,
	{
		Loc(self.0.into(), self.1)
	}

	/// Tries to map the inner value.
	#[inline(always)]
	pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<Loc<U, F, S>, E> {
		Ok(Loc(f(self.0)?, self.1))
	}

	/// Tries to convert the inner value.
	#[inline(always)]
	pub fn try_cast<U>(self) -> Result<Loc<U, F, S>, U::Error>
	where
		U: TryFrom<T>,
	{
		Ok(Loc(self.0.try_into()?, self.1))
	}

	/// Maps the value's location.
	#[inline(always)]
	pub fn map_location<G, U>(
		self,
		f: impl FnOnce(Location<F, S>) -> Location<G, U>,
	) -> Loc<T, G, U> {
		Loc(self.0, f(self.1))
	}

	/// Maps the value's location's file.
	#[inline(always)]
	pub fn map_file<G>(self, f: impl FnOnce(F) -> G) -> Loc<T, G, S> {
		Loc(self.0, self.1.map_file(f))
	}

	/// Borrows the value and file.
	#[inline(always)]
	pub fn borrow(&self) -> Loc<&T, &F, S>
	where
		S: Clone,
	{
		Loc(&self.0, self.1.borrow())
	}

	/// Borrows the value and clones the file.
	#[inline(always)]
	pub fn borrow_value(&self) -> Loc<&T, F, S>
	where
		F: Clone,
		S: Clone,
	{
		Loc(&self.0, self.1.clone())
	}

	/// Borrows the file and clones the value.
	#[inline(always)]
	pub fn borrow_file(&self) -> Loc<T, &F, S>
	where
		T: Clone,
		S: Clone,
	{
		Loc(self.0.clone(), self.1.borrow())
	}
}

impl<'t, T: Clone, F, S> Loc<&'t T, F, S> {
	/// Clones the borrowed value and the file to return a new `Loc<T, F>`.
	#[inline(always)]
	pub fn cloned_value(&self) -> Loc<T, F, S>
	where
		F: Clone,
		S: Clone,
	{
		Loc(self.0.clone(), self.1.clone())
	}

	/// Clones the borrowed value and consume the file to return a new `Loc<T, F>`.
	#[inline(always)]
	pub fn into_cloned_value(self) -> Loc<T, F, S> {
		Loc(self.0.clone(), self.1)
	}
}

impl<'f, T, F: Clone, S: Clone> Loc<T, &'f F, S> {
	/// Clones the value and the borrowed file to return a new `Loc<T, F>`.
	#[inline(always)]
	pub fn cloned_file(&self) -> Loc<T, F, S>
	where
		T: Clone,
	{
		Loc(self.0.clone(), self.1.cloned())
	}

	/// Clones the borrowed file and consumes the value to return a new `Loc<T, F>`.
	#[inline(always)]
	pub fn into_cloned_file(self) -> Loc<T, F, S> {
		Loc(self.0, self.1.cloned())
	}
}

impl<'t, 'f, T: Clone, F: Clone, S: Clone> Loc<&'t T, &'f F, S> {
	/// Clones the borrowed value and file to return a new `Loc<T, F>`.
	pub fn cloned(&self) -> Loc<T, F, S> {
		Loc(self.0.clone(), self.1.cloned())
	}
}

impl<T, F, S> Loc<Option<T>, F, S> {
	/// Unwraps the inner `Option`.
	#[inline(always)]
	pub fn unwrap(self) -> Loc<T, F, S> {
		self.map(Option::unwrap)
	}

	#[inline(always)]
	pub fn transpose(self) -> Option<Loc<T, F, S>> {
		match self.0 {
			Some(t) => Some(Loc(t, self.1)),
			None => None,
		}
	}
}

impl<T, F, S> Deref for Loc<T, F, S> {
	type Target = T;

	#[inline(always)]
	fn deref(&self) -> &T {
		self.value()
	}
}

impl<T, F, S> DerefMut for Loc<T, F, S> {
	#[inline(always)]
	fn deref_mut(&mut self) -> &mut T {
		self.value_mut()
	}
}

impl<T, F, S> AsRef<T> for Loc<T, F, S> {
	#[inline(always)]
	fn as_ref(&self) -> &T {
		self.value()
	}
}

impl<T, F, S> AsMut<T> for Loc<T, F, S> {
	#[inline(always)]
	fn as_mut(&mut self) -> &mut T {
		self.value_mut()
	}
}

impl<T, F, S> Borrow<T> for Loc<T, F, S> {
	#[inline(always)]
	fn borrow(&self) -> &T {
		self.value()
	}
}

impl<T, F, S> BorrowMut<T> for Loc<T, F, S> {
	#[inline(always)]
	fn borrow_mut(&mut self) -> &mut T {
		self.value_mut()
	}
}

/// Provides the `at` function to locate any value.
///
/// This trait is implemented for all types.
pub trait At: Sized {
	/// Wraps `self` inside a `Loc<Self, F>` using the given `location`.
	///
	/// Equivalent to `Loc(self, location)`.
	fn at<F, S>(self, location: Location<F, S>) -> Loc<Self, F, S>;
}

impl<T> At for T {
	fn at<F, S>(self, location: Location<F, S>) -> Loc<Self, F, S> {
		Loc(self, location)
	}
}

/// Provides a transposition function from `Option<Loc<T, F>>` to `Loc<Option<T>, F>`.
pub trait TransposeLoc {
	/// Located value type.
	type Value;

	/// File id type.
	type FileId;

	/// Transposes a `Option<Loc<Self::Value, Self::FileId>>` into a `Loc<Option<Self::Value>, Self::FileId>`.
	fn transpose_loc(
		self,
		none_location: impl FnOnce() -> Location<Self::FileId>,
	) -> Loc<Option<Self::Value>, Self::FileId>;
}

impl<T, F> TransposeLoc for Option<Loc<T, F>> {
	type Value = T;
	type FileId = F;

	#[inline(always)]
	fn transpose_loc(self, none_location: impl FnOnce() -> Location<F>) -> Loc<Option<T>, F> {
		match self {
			Some(Loc(t, loc)) => Loc(Some(t), loc),
			None => Loc(None, none_location()),
		}
	}
}

/// Locates the error of a `Result<T, E>`.
pub trait ErrAt {
	/// Success type.
	type Value;

	/// Error type.
	type Error;

	/// Changes a `Result<Self::Value, Self::Error>` into a `Result<Self::Value, Loc<Self::Error, F>>` by wrapping
	/// any eventual error using the result of the `location` function.
	fn err_at<F>(
		self,
		location: impl FnOnce() -> Location<F>,
	) -> Result<Self::Value, Loc<Self::Error, F>>;
}

impl<T, E> ErrAt for Result<T, E> {
	type Value = T;
	type Error = E;

	#[inline(always)]
	fn err_at<F>(
		self,
		location: impl FnOnce() -> Location<F>,
	) -> Result<Self::Value, Loc<Self::Error, F>> {
		match self {
			Ok(t) => Ok(t),
			Err(e) => Err(Loc(e, location())),
		}
	}
}

/// Maps the located error of a `Result<T, Loc<E, F>>`.
pub trait MapLocErr {
	/// Success type.
	type Value;

	/// Error type.
	type Error;

	/// File id type.
	type FileId;

	/// Changes a `Result<Self::Value, Loc<Self::Error, Self::FileId>>` into a `Result<Self::Value, Loc<G, Self::FileId>>`
	/// by mapping the error value using `f`.
	fn map_loc_err<G>(
		self,
		f: impl FnOnce(Self::Error) -> G,
	) -> Result<Self::Value, Loc<G, Self::FileId>>;
}

impl<T, E, F> MapLocErr for Result<T, Loc<E, F>> {
	type Value = T;
	type Error = E;
	type FileId = F;

	#[inline(always)]
	fn map_loc_err<G>(
		self,
		f: impl FnOnce(Self::Error) -> G,
	) -> Result<Self::Value, Loc<G, Self::FileId>> {
		match self {
			Ok(t) => Ok(t),
			Err(Loc(e, loc)) => Err(Loc(f(e), loc)),
		}
	}
}