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
//! The global reporting context for diagnostics.

use crate::diagnostic::EnsembleDiagnostic;

/// The global reporting context for diagnostics.
// TODO(@ThePuzzlemaker: frame|diag):
//   rewrite nonfatals as a better "lint" system
pub struct GlobalReportingCtxt {
    errors: Vec<EnsembleDiagnostic>,
    nonfatals: Vec<EnsembleDiagnostic>,
    fatal: Option<EnsembleDiagnostic>,
}

impl Default for GlobalReportingCtxt {
    fn default() -> Self {
        Self::new()
    }
}

impl GlobalReportingCtxt {
    /// Create a new `GlobalReportingCtxt`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            errors: Vec::new(),
            nonfatals: Vec::new(),
            fatal: None,
        }
    }

    /// Clear the list of synchronized errors.
    pub fn clear_syncd(&mut self) {
        self.errors.clear();
    }

    /// Clear the list of nonfatals.
    pub fn clear_nonfatals(&mut self) {
        self.nonfatals.clear();
    }

    /// Clear the current fatal error.
    pub fn clear_fatal(&mut self) {
        self.fatal = None;
    }

    /// Clear the entire reporting context
    pub fn clear(&mut self) {
        self.clear_fatal();
        self.clear_nonfatals();
        self.clear_syncd();
    }

    /// Report an error that was synchronizable.
    pub fn report_syncd(&mut self, value: EnsembleDiagnostic) {
        self.errors.push(value);
    }

    /// Report a non-fatal error.
    pub fn report_non_fatal(&mut self, value: EnsembleDiagnostic) {
        self.nonfatals.push(value);
    }

    /// Report a fatal error. If there is already a fatal error reported, it
    /// will not be replaced.
    pub fn report_fatal(&mut self, value: EnsembleDiagnostic) {
        if self.fatal.is_none() {
            self.fatal = Some(value);
        }
    }

    /// Get the list of nonfatal errors.
    #[must_use]
    pub fn nonfatals(&self) -> &[EnsembleDiagnostic] {
        &self.nonfatals
    }

    /// Get the current fatal error, if any.
    #[must_use]
    pub fn fatal(&self) -> Option<&EnsembleDiagnostic> {
        self.fatal.as_ref()
    }

    /// Get the list of synchronizable errors.
    #[must_use]
    pub fn errors(&self) -> &[EnsembleDiagnostic] {
        &self.errors
    }
}