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
use std::fmt;
use crate::{protocol::Diagnostic, ReportHandler};
#[derive(Debug, Clone)]
pub struct DebugReportHandler;
impl DebugReportHandler {
pub fn new() -> Self {
Self
}
}
impl Default for DebugReportHandler {
fn default() -> Self {
Self::new()
}
}
impl DebugReportHandler {
pub fn render_report(
&self,
f: &mut fmt::Formatter<'_>,
diagnostic: &(dyn Diagnostic),
) -> fmt::Result {
let mut diag = f.debug_struct("Diagnostic");
diag.field("message", &format!("{}", diagnostic));
if let Some(code) = diagnostic.code() {
diag.field("code", &code.to_string());
}
if let Some(severity) = diagnostic.severity() {
diag.field("severity", &format!("{:?}", severity));
}
if let Some(url) = diagnostic.url() {
diag.field("url", &url.to_string());
}
if let Some(help) = diagnostic.help() {
diag.field("help", &help.to_string());
}
if let Some(labels) = diagnostic.labels() {
let labels: Vec<_> = labels.collect();
diag.field("labels", &format!("{:?}", labels));
}
if let Some(cause) = diagnostic.diagnostic_source() {
diag.field("caused by", &format!("{:?}", cause));
}
diag.finish()?;
writeln!(f)?;
writeln!(f, "NOTE: If you're looking for the fancy error reports, install miette with the `fancy` feature, or write your own and hook it up with miette::set_hook().")
}
}
impl ReportHandler for DebugReportHandler {
fn debug(&self, diagnostic: &(dyn Diagnostic), f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
return fmt::Debug::fmt(diagnostic, f);
}
self.render_report(f, diagnostic)
}
}