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
225
226
227
228
229
230
231
232
233
234
235
236
237
use miette::{GraphicalReportHandler, GraphicalTheme};
use {
miette::{Diagnostic, ReportHandler},
std::{borrow::Cow, error::Error, fmt},
};
pub struct Diagnostics<'a> {
pub(crate) error: Option<Box<dyn Diagnostic + Send + Sync + 'static>>,
pub(crate) ignored: Vec<Box<dyn Diagnostic + Send + Sync + 'static>>,
pub(crate) disabled: Option<Box<dyn Diagnostic + Send + Sync + 'static>>,
pub(crate) source: Cow<'a, str>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DiagnosticsTheme {
Ascii,
AsciiNocolor,
Unicode,
UnicodeNocolor,
Guess,
}
impl Default for DiagnosticsTheme {
fn default() -> Self {
DiagnosticsTheme::Guess
}
}
impl DiagnosticsTheme {
fn report_handler(self) -> GraphicalReportHandler {
match self {
Self::Ascii => GraphicalReportHandler::new_themed(GraphicalTheme::ascii()),
Self::AsciiNocolor => GraphicalReportHandler::new_themed(GraphicalTheme::none()),
Self::Unicode => GraphicalReportHandler::new_themed(GraphicalTheme::unicode()),
Self::UnicodeNocolor => {
GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor())
},
Self::Guess => GraphicalReportHandler::new(),
}
}
}
impl Diagnostics<'_> {
pub fn is_error(&self) -> bool {
self.error.is_some()
}
pub fn is_warning(&self) -> bool {
!self.ignored.is_empty() || self.disabled.is_some()
}
pub fn is_empty(&self) -> bool {
!self.is_error() && !self.is_warning()
}
pub fn into_owned(self) -> Diagnostics<'static> {
Diagnostics {
error: self.error,
ignored: self.ignored,
disabled: self.disabled,
source: Cow::Owned(self.source.into()),
}
}
pub fn error(&self, theme: DiagnosticsTheme) -> Option<impl fmt::Display + '_> {
struct ErrorDiagnostics<'a>(&'a Diagnostics<'a>, DiagnosticsTheme);
impl fmt::Display for ErrorDiagnostics<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let report_handler = self.1.report_handler();
if let Some(error) = &self.0.error {
report_handler.debug(&DiagnosticWithStr::new(&**error, &self.0.source), f)?;
}
Ok(())
}
}
if self.is_error() {
Some(ErrorDiagnostics(self, theme))
} else {
None
}
}
pub fn warn(&self, theme: DiagnosticsTheme) -> Option<impl fmt::Display + '_> {
struct WarnDiagnostics<'a>(&'a Diagnostics<'a>, DiagnosticsTheme);
impl fmt::Display for WarnDiagnostics<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let report_handler = self.1.report_handler();
if !self.0.ignored.is_empty() {
writeln!(
f,
"{} directives were ignored as invalid",
self.0.ignored.len()
)?;
}
for ignored in &self.0.ignored {
report_handler.debug(&DiagnosticWithStr::new(&**ignored, &self.0.source), f)?;
}
if let Some(disabled) = &self.0.disabled {
report_handler
.debug(&DiagnosticWithStr::new(&**disabled, &self.0.source), f)?;
}
Ok(())
}
}
if self.is_warning() {
Some(WarnDiagnostics(self, theme))
} else {
None
}
}
}
impl fmt::Display for Diagnostics<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
f.debug_struct(stringify!(Diagnostics))
.field(stringify!(error), &self.error)
.field(stringify!(ignored), &self.ignored)
.field(stringify!(disabled), &self.disabled)
.finish()
} else {
if let Some(error) = &self.error {
writeln!(f, "{}", error)?;
}
if !self.ignored.is_empty() {
writeln!(
f,
"{} directives were ignored as invalid",
self.ignored.len()
)?;
for ignored in &self.ignored {
writeln!(f, "{}", ignored)?;
}
}
if let Some(disabled) = &self.disabled {
writeln!(f, "{}", disabled)?;
}
Ok(())
}
}
}
impl fmt::Debug for Diagnostics<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
f.debug_struct(stringify!(Diagnostics))
.field(stringify!(error), &self.error)
.field(stringify!(ignored), &self.ignored)
.field(stringify!(disabled), &self.disabled)
.finish()
} else {
if let Some(error) = self.error(DiagnosticsTheme::Guess) {
writeln!(f, "{}", error)?;
}
if let Some(warn) = self.warn(DiagnosticsTheme::Guess) {
writeln!(f, "{}", warn)?;
}
Ok(())
}
}
}
#[derive(Debug)]
struct DiagnosticWithStr<'a> {
diagnostic: &'a dyn Diagnostic,
source: &'a str,
}
impl<'a> DiagnosticWithStr<'a> {
fn new(diagnostic: &'a dyn Diagnostic, source: &'a str) -> Self {
Self { diagnostic, source }
}
}
impl fmt::Display for DiagnosticWithStr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.diagnostic, f)
}
}
impl Error for DiagnosticWithStr<'_> {}
impl Diagnostic for DiagnosticWithStr<'_> {
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.diagnostic.code()
}
fn severity(&self) -> Option<miette::Severity> {
self.diagnostic.severity()
}
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.diagnostic.help()
}
fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.diagnostic.url()
}
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(&self.source)
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
self.diagnostic.labels()
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
self.diagnostic.related()
}
}