1use std::fmt;
2
3use crate::{ReportHandler, protocol::Diagnostic};
4
5#[derive(Debug, Clone)]
11pub struct DebugReportHandler;
12
13impl DebugReportHandler {
14 pub const fn new() -> Self {
17 Self
18 }
19}
20
21impl Default for DebugReportHandler {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl DebugReportHandler {
28 pub fn render_report(
32 &self,
33 f: &mut fmt::Formatter<'_>,
34 diagnostic: &dyn Diagnostic,
35 ) -> fmt::Result {
36 let mut diag = f.debug_struct("Diagnostic");
37 diag.field("message", &format!("{diagnostic}"));
38 if let Some(code) = diagnostic.code() {
39 diag.field("code", &code.to_string());
40 }
41 if let Some(severity) = diagnostic.severity() {
42 diag.field("severity", &format!("{severity:?}"));
43 }
44 if let Some(url) = diagnostic.url() {
45 diag.field("url", &url.to_string());
46 }
47 if let Some(help) = diagnostic.help() {
48 diag.field("help", &help.to_string());
49 }
50 if let Some(note) = diagnostic.note() {
51 diag.field("note", ¬e.to_string());
52 }
53 if let Some(labels) = diagnostic.labels() {
54 let labels: Vec<_> = labels.collect();
55 diag.field("labels", &format!("{labels:?}"));
56 }
57 if let Some(cause) = diagnostic.diagnostic_source() {
58 diag.field("caused by", &format!("{cause:?}"));
59 }
60 diag.finish()?;
61 writeln!(f)?;
62 writeln!(
63 f,
64 "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()."
65 )
66 }
67}
68
69impl ReportHandler for DebugReportHandler {
70 fn debug(&self, diagnostic: &dyn Diagnostic, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 if f.alternate() {
72 return fmt::Debug::fmt(diagnostic, f);
73 }
74
75 self.render_report(f, diagnostic)
76 }
77}