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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use anyhow::anyhow;
use std::cmp::Ordering;
use std::str::FromStr;
use super::report_code::ReportCode;
use super::file_definition::{FileID, FileLibrary, FileLocation};
use atty;
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::term;
pub type ReportCollection = Vec<Report>;
pub type DiagnosticCode = String;
pub type ReportLabel = Label<FileID>;
type ReportNote = String;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MessageCategory {
Error,
Warning,
Info,
}
impl PartialOrd for MessageCategory {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
use MessageCategory::*;
match (self, other) {
(Info, Info) => Some(Ordering::Equal),
(Info, Warning) | (Info, Error) => Some(Ordering::Less),
(Warning, Warning) => Some(Ordering::Equal),
(Warning, Error) => Some(Ordering::Less),
(Error, Error) => Some(Ordering::Equal),
_ => Some(Ordering::Greater),
}
}
}
impl Ord for MessageCategory {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl ToString for MessageCategory {
fn to_string(&self) -> String {
use MessageCategory::*;
match self {
Error => "error",
Warning => "warning",
Info => "info",
}
.to_string()
}
}
impl FromStr for MessageCategory {
type Err = anyhow::Error;
fn from_str(category: &str) -> Result<MessageCategory, Self::Err> {
match category.to_lowercase().as_str() {
"warning" => Ok(MessageCategory::Warning),
"info" => Ok(MessageCategory::Info),
"error" => Ok(MessageCategory::Error),
_ => Err(anyhow!("unknown level '{category}'")),
}
}
}
#[derive(Clone)]
pub struct Report {
category: MessageCategory,
message: String,
primary: Vec<ReportLabel>,
secondary: Vec<ReportLabel>,
notes: Vec<ReportNote>,
code: ReportCode,
}
impl Report {
fn new(category: MessageCategory, message: String, code: ReportCode) -> Report {
Report {
category,
message,
primary: Vec::new(),
secondary: Vec::new(),
notes: Vec::new(),
code,
}
}
pub fn print_reports(reports: &[Report], file_library: &FileLibrary, verbose: bool) {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
let writer = if atty::is(atty::Stream::Stdout) {
StandardStream::stdout(ColorChoice::Always)
} else {
StandardStream::stdout(ColorChoice::Never)
};
let mut config = term::Config::default();
let mut diagnostics = Vec::new();
let files = file_library.to_storage();
for report in reports.iter() {
diagnostics.push(report.to_diagnostic(verbose));
}
config.styles.header_help.set_intense(false);
config.styles.header_error.set_intense(false);
config.styles.header_warning.set_intense(false);
for diagnostic in diagnostics.iter() {
let print_result = term::emit(&mut writer.lock(), &config, files, diagnostic);
if print_result.is_err() {
panic!("error printing reports")
}
}
}
pub fn error(message: String, code: ReportCode) -> Report {
Report::new(MessageCategory::Error, message, code)
}
pub fn warning(message: String, code: ReportCode) -> Report {
Report::new(MessageCategory::Warning, message, code)
}
pub fn info(message: String, code: ReportCode) -> Report {
Report::new(MessageCategory::Info, message, code)
}
pub fn add_primary(
&mut self,
location: FileLocation,
file_id: FileID,
message: String,
) -> &mut Self {
let label = ReportLabel::primary(file_id, location).with_message(message);
self.primary_mut().push(label);
self
}
pub fn add_secondary(
&mut self,
location: FileLocation,
file_id: FileID,
possible_message: Option<String>,
) -> &mut Self {
let mut label = ReportLabel::secondary(file_id, location);
if let Option::Some(message) = possible_message {
label = label.with_message(message);
}
self.secondary_mut().push(label);
self
}
pub fn add_note(&mut self, note: String) -> &mut Self {
self.notes_mut().push(note);
self
}
fn to_diagnostic(&self, verbose: bool) -> Diagnostic<FileID> {
let mut labels = self.primary().clone();
let mut secondary = self.secondary().clone();
labels.append(&mut secondary);
let diagnostic = match self.category() {
MessageCategory::Error => Diagnostic::error(),
MessageCategory::Warning => Diagnostic::warning(),
MessageCategory::Info => Diagnostic::note(),
}
.with_message(self.message())
.with_labels(labels);
let mut notes = self.notes().clone();
if verbose {
notes.push(format!("To ignore this type of result, use `--allow {}`.", self.id()));
diagnostic.with_code(self.id()).with_notes(notes)
} else {
diagnostic.with_notes(notes)
}
}
pub fn category(&self) -> &MessageCategory {
&self.category
}
pub fn message(&self) -> &String {
&self.message
}
pub fn primary(&self) -> &Vec<ReportLabel> {
&self.primary
}
fn primary_mut(&mut self) -> &mut Vec<ReportLabel> {
&mut self.primary
}
pub fn secondary(&self) -> &Vec<ReportLabel> {
&self.secondary
}
fn secondary_mut(&mut self) -> &mut Vec<ReportLabel> {
&mut self.secondary
}
pub fn notes(&self) -> &Vec<ReportNote> {
&self.notes
}
fn notes_mut(&mut self) -> &mut Vec<ReportNote> {
&mut self.notes
}
pub fn code(&self) -> &ReportCode {
&self.code
}
pub fn id(&self) -> String {
self.code.id()
}
}