Coverage Report

Created: 2026-03-12 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/codespan-reporting-0.12.0/src/diagnostic.rs
Line
Count
Source
1
//! Diagnostic data structures.
2
3
use alloc::{
4
    string::{String, ToString},
5
    vec::Vec,
6
};
7
use core::ops::Range;
8
9
#[cfg(feature = "serialization")]
10
use serde::{Deserialize, Serialize};
11
12
/// A severity level for diagnostic messages.
13
///
14
/// These are ordered in the following way:
15
///
16
/// ```rust
17
/// use codespan_reporting::diagnostic::Severity;
18
///
19
/// assert!(Severity::Bug > Severity::Error);
20
/// assert!(Severity::Error > Severity::Warning);
21
/// assert!(Severity::Warning > Severity::Note);
22
/// assert!(Severity::Note > Severity::Help);
23
/// ```
24
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
25
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
26
pub enum Severity {
27
    /// A help message.
28
    Help,
29
    /// A note.
30
    Note,
31
    /// A warning.
32
    Warning,
33
    /// An error.
34
    Error,
35
    /// An unexpected bug.
36
    Bug,
37
}
38
39
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd)]
40
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
41
pub enum LabelStyle {
42
    /// Labels that describe the primary cause of a diagnostic.
43
    Primary,
44
    /// Labels that provide additional context for a diagnostic.
45
    Secondary,
46
}
47
48
/// A label describing an underlined region of code associated with a diagnostic.
49
#[derive(Clone, Debug, PartialEq, Eq)]
50
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
51
pub struct Label<FileId> {
52
    /// The style of the label.
53
    pub style: LabelStyle,
54
    /// The file that we are labelling.
55
    pub file_id: FileId,
56
    /// The range in bytes we are going to include in the final snippet.
57
    pub range: Range<usize>,
58
    /// An optional message to provide some additional information for the
59
    /// underlined code. These should not include line breaks.
60
    pub message: String,
61
}
62
63
impl<FileId> Label<FileId> {
64
    /// Create a new label.
65
0
    pub fn new(
66
0
        style: LabelStyle,
67
0
        file_id: FileId,
68
0
        range: impl Into<Range<usize>>,
69
0
    ) -> Label<FileId> {
70
0
        Label {
71
0
            style,
72
0
            file_id,
73
0
            range: range.into(),
74
0
            message: String::new(),
75
0
        }
76
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Label<()>>::new::<core::ops::range::Range<usize>>
Unexecuted instantiation: <codespan_reporting::diagnostic::Label<_>>::new::<_>
77
78
    /// Create a new label with a style of [`LabelStyle::Primary`].
79
    ///
80
    /// [`LabelStyle::Primary`]: LabelStyle::Primary
81
0
    pub fn primary(file_id: FileId, range: impl Into<Range<usize>>) -> Label<FileId> {
82
0
        Label::new(LabelStyle::Primary, file_id, range)
83
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Label<()>>::primary::<core::ops::range::Range<usize>>
Unexecuted instantiation: <codespan_reporting::diagnostic::Label<_>>::primary::<_>
84
85
    /// Create a new label with a style of [`LabelStyle::Secondary`].
86
    ///
87
    /// [`LabelStyle::Secondary`]: LabelStyle::Secondary
88
0
    pub fn secondary(file_id: FileId, range: impl Into<Range<usize>>) -> Label<FileId> {
89
0
        Label::new(LabelStyle::Secondary, file_id, range)
90
0
    }
91
92
    /// Add a message to the diagnostic.
93
0
    pub fn with_message(mut self, message: impl ToString) -> Label<FileId> {
94
0
        self.message = message.to_string();
95
0
        self
96
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Label<()>>::with_message::<alloc::string::String>
Unexecuted instantiation: <codespan_reporting::diagnostic::Label<_>>::with_message::<_>
97
}
98
99
/// Represents a diagnostic message that can provide information like errors and
100
/// warnings to the user.
101
///
102
/// The position of a Diagnostic is considered to be the position of the [`Label`] that has the earliest starting position and has the highest style which appears in all the labels of the diagnostic.
103
#[derive(Clone, Debug, PartialEq, Eq)]
104
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
105
pub struct Diagnostic<FileId> {
106
    /// The overall severity of the diagnostic
107
    pub severity: Severity,
108
    /// An optional code that identifies this diagnostic.
109
    pub code: Option<String>,
110
    /// The main message associated with this diagnostic.
111
    ///
112
    /// These should not include line breaks, and in order support the 'short'
113
    /// diagnostic display mod, the message should be specific enough to make
114
    /// sense on its own, without additional context provided by labels and notes.
115
    pub message: String,
116
    /// Source labels that describe the cause of the diagnostic.
117
    /// The order of the labels inside the vector does not have any meaning.
118
    /// The labels are always arranged in the order they appear in the source code.
119
    pub labels: Vec<Label<FileId>>,
120
    /// Notes that are associated with the primary cause of the diagnostic.
121
    /// These can include line breaks for improved formatting.
122
    pub notes: Vec<String>,
123
}
124
125
impl<FileId> Diagnostic<FileId> {
126
    /// Create a new diagnostic.
127
0
    pub fn new(severity: Severity) -> Diagnostic<FileId> {
128
0
        Diagnostic {
129
0
            severity,
130
0
            code: None,
131
0
            message: String::new(),
132
0
            labels: Vec::new(),
133
0
            notes: Vec::new(),
134
0
        }
135
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<()>>::new
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<_>>::new
136
137
    /// Create a new diagnostic with a severity of [`Severity::Bug`].
138
    ///
139
    /// [`Severity::Bug`]: Severity::Bug
140
0
    pub fn bug() -> Diagnostic<FileId> {
141
0
        Diagnostic::new(Severity::Bug)
142
0
    }
143
144
    /// Create a new diagnostic with a severity of [`Severity::Error`].
145
    ///
146
    /// [`Severity::Error`]: Severity::Error
147
0
    pub fn error() -> Diagnostic<FileId> {
148
0
        Diagnostic::new(Severity::Error)
149
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<()>>::error
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<_>>::error
150
151
    /// Create a new diagnostic with a severity of [`Severity::Warning`].
152
    ///
153
    /// [`Severity::Warning`]: Severity::Warning
154
0
    pub fn warning() -> Diagnostic<FileId> {
155
0
        Diagnostic::new(Severity::Warning)
156
0
    }
157
158
    /// Create a new diagnostic with a severity of [`Severity::Note`].
159
    ///
160
    /// [`Severity::Note`]: Severity::Note
161
0
    pub fn note() -> Diagnostic<FileId> {
162
0
        Diagnostic::new(Severity::Note)
163
0
    }
164
165
    /// Create a new diagnostic with a severity of [`Severity::Help`].
166
    ///
167
    /// [`Severity::Help`]: Severity::Help
168
0
    pub fn help() -> Diagnostic<FileId> {
169
0
        Diagnostic::new(Severity::Help)
170
0
    }
171
172
    /// Set the error code of the diagnostic.
173
0
    pub fn with_code(mut self, code: impl ToString) -> Diagnostic<FileId> {
174
0
        self.code = Some(code.to_string());
175
0
        self
176
0
    }
177
178
    /// Set the message of the diagnostic.
179
0
    pub fn with_message(mut self, message: impl ToString) -> Diagnostic<FileId> {
180
0
        self.message = message.to_string();
181
0
        self
182
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<()>>::with_message::<alloc::string::String>
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<_>>::with_message::<_>
183
184
    /// Add a label to the diagnostic.
185
0
    pub fn with_label(mut self, label: Label<FileId>) -> Diagnostic<FileId> {
186
0
        self.labels.push(label);
187
0
        self
188
0
    }
189
190
    /// Add some labels to the diagnostic.
191
0
    pub fn with_labels(mut self, mut labels: Vec<Label<FileId>>) -> Diagnostic<FileId> {
192
0
        self.labels.append(&mut labels);
193
0
        self
194
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<()>>::with_labels
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<_>>::with_labels
195
196
    /// Add some labels to the diagnostic.
197
0
    pub fn with_labels_iter(
198
0
        mut self,
199
0
        labels: impl IntoIterator<Item = Label<FileId>>,
200
0
    ) -> Diagnostic<FileId> {
201
0
        self.labels.extend(labels);
202
0
        self
203
0
    }
204
205
    /// Add a note to the diagnostic.
206
0
    pub fn with_note(mut self, note: impl ToString) -> Diagnostic<FileId> {
207
0
        self.notes.push(note.to_string());
208
0
        self
209
0
    }
210
211
    /// Add some notes to the diagnostic.
212
0
    pub fn with_notes(mut self, mut notes: Vec<String>) -> Diagnostic<FileId> {
213
0
        self.notes.append(&mut notes);
214
0
        self
215
0
    }
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<()>>::with_notes
Unexecuted instantiation: <codespan_reporting::diagnostic::Diagnostic<_>>::with_notes
216
217
    /// Add some notes to the diagnostic.
218
0
    pub fn with_notes_iter(
219
0
        mut self,
220
0
        notes: impl IntoIterator<Item = String>,
221
0
    ) -> Diagnostic<FileId> {
222
0
        self.notes.extend(notes);
223
0
        self
224
0
    }
225
}