language_reporting/
diagnostic.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
use crate::{ReportingSpan, Severity};
use serde_derive::{Serialize, Deserialize};

/// A style for the label
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
pub enum LabelStyle {
    /// The main focus of the diagnostic
    Primary,
    /// Supporting labels that may help to isolate the cause of the diagnostic
    Secondary,
}

/// A label describing an underlined region of code associated with a diagnostic
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Label<Span: ReportingSpan> {
    /// The span we are going to include in the final snippet.
    pub span: Span,
    /// A message to provide some additional information for the underlined code.
    pub message: Option<String>,
    /// The style to use for the label.
    pub style: LabelStyle,
}

impl<Span: ReportingSpan> Label<Span> {
    pub fn new(span: Span, style: LabelStyle) -> Label<Span> {
        Label {
            span,
            message: None,
            style,
        }
    }

    pub fn new_primary(span: Span) -> Label<Span> {
        Label::new(span, LabelStyle::Primary)
    }

    pub fn new_secondary(span: Span) -> Label<Span> {
        Label::new(span, LabelStyle::Secondary)
    }

    pub fn with_message<S: Into<String>>(mut self, message: S) -> Label<Span> {
        self.message = Some(message.into());
        self
    }

    pub fn message(&self) -> &Option<String> {
        &self.message
    }
}

/// Represents a diagnostic message and associated child messages.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Diagnostic<Span: ReportingSpan> {
    /// The overall severity of the diagnostic
    pub severity: Severity,
    /// An optional code that identifies this diagnostic.
    pub code: Option<String>,
    /// The main message associated with this diagnostic
    pub message: String,
    /// The labelled spans marking the regions of code that cause this
    /// diagnostic to be raised
    pub labels: Vec<Label<Span>>,
}

impl<Span: ReportingSpan> Diagnostic<Span> {
    pub fn new<S: Into<String>>(severity: Severity, message: S) -> Diagnostic<Span> {
        Diagnostic {
            severity,
            code: None,
            message: message.into(),
            labels: Vec::new(),
        }
    }

    pub fn new_bug<S: Into<String>>(message: S) -> Diagnostic<Span> {
        Diagnostic::new(Severity::Bug, message)
    }

    pub fn new_error<S: Into<String>>(message: S) -> Diagnostic<Span> {
        Diagnostic::new(Severity::Error, message)
    }

    pub fn new_warning<S: Into<String>>(message: S) -> Diagnostic<Span> {
        Diagnostic::new(Severity::Warning, message)
    }

    pub fn new_note<S: Into<String>>(message: S) -> Diagnostic<Span> {
        Diagnostic::new(Severity::Note, message)
    }

    pub fn new_help<S: Into<String>>(message: S) -> Diagnostic<Span> {
        Diagnostic::new(Severity::Help, message)
    }

    pub fn with_code<S: Into<String>>(mut self, code: S) -> Diagnostic<Span> {
        self.code = Some(code.into());
        self
    }

    pub fn with_label(mut self, label: Label<Span>) -> Diagnostic<Span> {
        self.labels.push(label);
        self
    }

    pub fn with_labels<Labels: IntoIterator<Item = Label<Span>>>(
        mut self,
        labels: Labels,
    ) -> Diagnostic<Span> {
        self.labels.extend(labels);
        self
    }
}