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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Module containing a few common error wrappers which allows more information to be saved for
//! later display to the user

use std::any::Any;
use std::error::Error as StdError;
use std::fmt;
use std::iter::{Extend, FromIterator};
use std::ops::{Index, IndexMut};
use std::slice;
use std::str;
use std::vec;

use codespan_reporting::diagnostic::{Diagnostic, Label};

use crate::{
    pos::{BytePos, Spanned},
    source::FileId,
};

/// An error type which can represent multiple errors.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Errors<T> {
    errors: Vec<T>,
}

impl<T> Default for Errors<T> {
    fn default() -> Self {
        Errors::new()
    }
}

impl<T> Errors<T> {
    /// Creates a new, empty `Errors` instance.
    pub fn new() -> Errors<T> {
        Errors::from(Vec::new())
    }

    /// Returns true if `self` contains any errors
    pub fn has_errors(&self) -> bool {
        !self.is_empty()
    }

    /// The number of errors in the error list
    pub fn len(&self) -> usize {
        self.errors.len()
    }

    pub fn is_empty(&self) -> bool {
        self.errors.is_empty()
    }

    /// Adds an error to `self`
    pub fn push(&mut self, t: T) {
        self.errors.push(t);
    }

    /// Pops and error off the error list
    pub fn pop(&mut self) -> Option<T> {
        self.errors.pop()
    }

    pub fn iter(&self) -> slice::Iter<T> {
        self.errors.iter()
    }

    pub fn drain(
        &mut self,
        range: impl std::ops::RangeBounds<usize>,
    ) -> impl Iterator<Item = T> + '_ {
        self.errors.drain(range)
    }
}

impl<T> Index<usize> for Errors<T> {
    type Output = T;
    fn index(&self, index: usize) -> &T {
        &self.errors[index]
    }
}

impl<T> IndexMut<usize> for Errors<T> {
    fn index_mut(&mut self, index: usize) -> &mut T {
        &mut self.errors[index]
    }
}

impl<T> Extend<T> for Errors<T> {
    fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
        self.errors.extend(iter);
    }
}

impl<T> From<Vec<T>> for Errors<T> {
    fn from(errors: Vec<T>) -> Errors<T> {
        Errors { errors }
    }
}

impl<T> FromIterator<T> for Errors<T> {
    fn from_iter<Iter: IntoIterator<Item = T>>(iter: Iter) -> Errors<T> {
        Errors {
            errors: iter.into_iter().collect(),
        }
    }
}

impl<T> Into<Vec<T>> for Errors<T> {
    fn into(self) -> Vec<T> {
        self.errors
    }
}

impl<T> IntoIterator for Errors<T> {
    type Item = T;

    type IntoIter = vec::IntoIter<T>;

    fn into_iter(self) -> vec::IntoIter<T> {
        self.errors.into_iter()
    }
}

impl<'a, T> IntoIterator for &'a Errors<T> {
    type Item = &'a T;

    type IntoIter = slice::Iter<'a, T>;

    fn into_iter(self) -> slice::Iter<'a, T> {
        self.errors.iter()
    }
}

impl<'a, T> IntoIterator for &'a mut Errors<T> {
    type Item = &'a mut T;

    type IntoIter = slice::IterMut<'a, T>;

    fn into_iter(self) -> slice::IterMut<'a, T> {
        self.errors.iter_mut()
    }
}

impl<T: fmt::Display> fmt::Display for Errors<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (i, error) in self.errors.iter().enumerate() {
            write!(f, "{}", error)?;
            // Errors are assumed to not have a newline at the end so we add one to keep errors on
            // separate lines and one to space them out
            if i + 1 != self.errors.len() {
                writeln!(f)?;
                writeln!(f)?;
            }
        }
        Ok(())
    }
}

impl<T: fmt::Display + fmt::Debug + Any> StdError for Errors<T> {
    fn description(&self) -> &str {
        "Errors"
    }
}

/// Error type which contains information of which file and where in the file the error occurred
#[derive(Clone, Debug)]
pub struct InFile<E> {
    source: crate::source::CodeMap,
    error: Errors<Spanned<E, BytePos>>,
}

impl<E> Eq for InFile<E> where E: Eq {}

impl<E> PartialEq for InFile<E>
where
    E: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.error == other.error
    }
}

impl<E> std::hash::Hash for InFile<E>
where
    E: std::hash::Hash,
{
    #[inline(always)]
    fn hash<H>(&self, state: &mut H)
    where
        H: std::hash::Hasher,
    {
        self.error.hash(state)
    }
}

impl<E: fmt::Display> InFile<E> {
    /// Creates a new `InFile` error which states that the error occurred in `file` using the file
    /// contents in `source` to provide a context to the span.
    pub fn new(source: crate::source::CodeMap, error: Errors<Spanned<E, BytePos>>) -> InFile<E> {
        let err = InFile { source, error };
        // Verify that the source name can be accessed
        debug_assert!({
            err.source_name();
            true
        });
        err
    }

    pub fn source_name(&self) -> &str {
        self.source
            .get(self.error[0].span.start())
            .unwrap_or_else(|| {
                panic!(
                    "Source file does not exist in associated code map. Error: {}",
                    self.error
                )
            })
            .name()
    }

    pub fn source(&self) -> &crate::source::CodeMap {
        &self.source
    }

    pub fn errors(&self) -> &Errors<Spanned<E, BytePos>> {
        &self.error
    }

    pub fn into_errors(self) -> Errors<Spanned<E, BytePos>> {
        self.error
    }

    pub fn emit_string(&self) -> crate::source::Result<String>
    where
        E: AsDiagnostic,
    {
        let mut output = Vec::new();
        self.emit(&mut ::codespan_reporting::term::termcolor::NoColor::new(
            &mut output,
        ))?;
        Ok(String::from_utf8(output).unwrap())
    }

    pub fn emit(
        &self,
        writer: &mut dyn ::codespan_reporting::term::termcolor::WriteColor,
    ) -> crate::source::Result<()>
    where
        E: AsDiagnostic,
    {
        let iter = self
            .error
            .iter()
            .map(|error| error.as_diagnostic(&self.source))
            .enumerate();
        for (i, diagnostic) in iter {
            if i != 0 {
                writeln!(writer)?;
            }
            ::codespan_reporting::term::emit(
                &mut *writer,
                &Default::default(),
                &self.source,
                &diagnostic,
            )?;
        }
        Ok(())
    }
}

impl<E: fmt::Display + AsDiagnostic> fmt::Display for InFile<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buffer = Vec::new();
        {
            let mut writer = ::codespan_reporting::term::termcolor::NoColor::new(&mut buffer);

            self.emit(&mut writer).map_err(|_| fmt::Error)?;
        }

        write!(f, "{}", str::from_utf8(&buffer).unwrap())
    }
}

impl<E: fmt::Display + fmt::Debug + Any + AsDiagnostic> StdError for InFile<E> {
    fn description(&self) -> &str {
        "Error in file"
    }
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct Help<E, H> {
    pub error: E,
    pub help: Option<H>,
}

impl<E, H> fmt::Display for Help<E, H>
where
    E: fmt::Display,
    H: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error)?;
        if let Some(ref help) = self.help {
            writeln!(f)?;
            write!(f, "help: {}", help)?;
        }
        Ok(())
    }
}

impl<E, H> From<E> for Help<E, H> {
    fn from(error: E) -> Help<E, H> {
        Help { error, help: None }
    }
}

pub trait AsDiagnostic {
    fn as_diagnostic(&self, map: &crate::source::CodeMap) -> Diagnostic<FileId>;
}

impl<E> AsDiagnostic for Spanned<E, BytePos>
where
    E: AsDiagnostic,
{
    fn as_diagnostic(&self, map: &crate::source::CodeMap) -> Diagnostic<FileId> {
        let mut diagnostic = self.value.as_diagnostic(map);
        for label in &mut diagnostic.labels {
            if label.file_id == FileId::default() {
                label.file_id = self.span.start();
            }
            if label.range == (0..0) {
                if let Some(range) = self.span.to_range(map) {
                    label.range = range;
                }
            }
        }
        if diagnostic.labels.is_empty() {
            if let Some(range) = self.span.to_range(map) {
                diagnostic
                    .labels
                    .push(Label::primary(self.span.start(), range));
            }
        }
        diagnostic
    }
}

impl<E, H> AsDiagnostic for Help<E, H>
where
    E: AsDiagnostic,
    H: fmt::Display,
{
    fn as_diagnostic(&self, map: &crate::source::CodeMap) -> Diagnostic<FileId> {
        let mut diagnostic = self.error.as_diagnostic(map);
        if let Some(ref help) = self.help {
            diagnostic.labels.push(
                Label::secondary(
                    diagnostic
                        .labels
                        .last()
                        .map(|label| label.file_id)
                        .unwrap_or_default(),
                    0..0,
                )
                .with_message(help.to_string()),
            );
        }
        diagnostic
    }
}

impl AsDiagnostic for Box<dyn ::std::error::Error + Send + Sync> {
    fn as_diagnostic(&self, _map: &crate::source::CodeMap) -> Diagnostic<FileId> {
        Diagnostic::error().with_message(self.to_string())
    }
}

pub type SalvageResult<T, E> = std::result::Result<T, Salvage<T, E>>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Salvage<T, E> {
    pub value: Option<T>,
    pub error: E,
}

impl<T, E> fmt::Display for Salvage<T, E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error)
    }
}

impl<T, E> Salvage<T, E> {
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Salvage<U, E> {
        Salvage {
            value: self.value.map(f),
            error: self.error,
        }
    }

    pub fn map_err<U>(self, f: impl FnOnce(E) -> U) -> Salvage<T, U> {
        Salvage {
            value: self.value,
            error: f(self.error),
        }
    }

    pub fn get_value(self) -> std::result::Result<T, E> {
        self.value.ok_or(self.error)
    }

    pub fn err_into<F>(self) -> Salvage<T, F>
    where
        F: From<E>,
    {
        let Salvage { value, error } = self;
        Salvage {
            value,
            error: error.into(),
        }
    }
}

impl<T, E> From<E> for Salvage<T, E> {
    fn from(error: E) -> Self {
        Salvage { value: None, error }
    }
}

impl<T, E> From<Salvage<T, InFile<E>>> for InFile<E> {
    fn from(s: Salvage<T, InFile<E>>) -> Self {
        s.error
    }
}