rune/workspace/
diagnostics.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
use crate::alloc::{self, Vec};
use crate::workspace::WorkspaceError;
use crate::SourceId;

/// A fatal diagnostic in a workspace.
#[derive(Debug)]
pub struct FatalDiagnostic {
    source_id: SourceId,
    error: WorkspaceError,
}

impl FatalDiagnostic {
    /// Get source id of the diagnostic.
    pub fn source_id(&self) -> SourceId {
        self.source_id
    }

    /// Access the underlying workspace error.
    pub fn error(&self) -> &WorkspaceError {
        &self.error
    }
}

/// A single workspace diagnostic.
#[derive(Debug)]
#[non_exhaustive]
pub enum Diagnostic {
    /// An error in a workspace.
    Fatal(FatalDiagnostic),
}

/// Diagnostics emitted about a workspace.
#[derive(Default)]
pub struct Diagnostics {
    pub(crate) diagnostics: Vec<Diagnostic>,
}

impl Diagnostics {
    /// Access underlying diagnostics.
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Test if diagnostics has errors.
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|e| matches!(e, Diagnostic::Fatal(..)))
    }

    /// Test if diagnostics is empty.
    pub fn is_empty(&self) -> bool {
        self.diagnostics.is_empty()
    }

    /// Report a single workspace error.
    pub(crate) fn fatal(
        &mut self,
        source_id: SourceId,
        error: WorkspaceError,
    ) -> alloc::Result<()> {
        self.diagnostics
            .try_push(Diagnostic::Fatal(FatalDiagnostic { source_id, error }))
    }
}

impl Diagnostics {
    /// Construct an empty diagnostics container.
    pub fn new() -> Self {
        Self::default()
    }
}