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
use codespan::{FileId, Span};
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub enum Severity {
Bug,
Error,
Warning,
Note,
Help,
}
impl Severity {
fn to_cmp_int(self) -> u8 {
match self {
Severity::Bug => 5,
Severity::Error => 4,
Severity::Warning => 3,
Severity::Note => 2,
Severity::Help => 1,
}
}
}
impl PartialOrd for Severity {
fn partial_cmp(&self, other: &Severity) -> Option<Ordering> {
u8::partial_cmp(&self.to_cmp_int(), &other.to_cmp_int())
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct Label {
pub file_id: FileId,
pub span: Span,
pub message: String,
}
impl Label {
pub fn new(file_id: FileId, span: impl Into<Span>, message: impl Into<String>) -> Label {
Label {
file_id,
span: span.into(),
message: message.into(),
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct Diagnostic {
pub severity: Severity,
pub code: Option<String>,
pub message: String,
pub primary_label: Label,
pub notes: Vec<String>,
pub secondary_labels: Vec<Label>,
}
impl Diagnostic {
pub fn new(severity: Severity, message: impl Into<String>, primary_label: Label) -> Diagnostic {
Diagnostic {
severity,
code: None,
message: message.into(),
primary_label,
notes: Vec::new(),
secondary_labels: Vec::new(),
}
}
pub fn new_bug(message: impl Into<String>, primary_label: Label) -> Diagnostic {
Diagnostic::new(Severity::Bug, message, primary_label)
}
pub fn new_error(message: impl Into<String>, primary_label: Label) -> Diagnostic {
Diagnostic::new(Severity::Error, message, primary_label)
}
pub fn new_warning(message: impl Into<String>, primary_label: Label) -> Diagnostic {
Diagnostic::new(Severity::Warning, message, primary_label)
}
pub fn new_note(message: impl Into<String>, primary_label: Label) -> Diagnostic {
Diagnostic::new(Severity::Note, message, primary_label)
}
pub fn new_help(message: impl Into<String>, primary_label: Label) -> Diagnostic {
Diagnostic::new(Severity::Help, message, primary_label)
}
pub fn with_code(mut self, code: impl Into<String>) -> Diagnostic {
self.code = Some(code.into());
self
}
pub fn with_notes(mut self, notes: Vec<String>) -> Diagnostic {
self.notes = notes;
self
}
pub fn with_secondary_labels(mut self, labels: impl IntoIterator<Item = Label>) -> Diagnostic {
self.secondary_labels.extend(labels);
self
}
}