1use std::fmt;
2
3use crate::{protocol::Diagnostic, ReportHandler};
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(labels) = diagnostic.labels() {
51 let labels: Vec<_> = labels.collect();
52 diag.field("labels", &format!("{:?}", labels));
53 }
54 if let Some(cause) = diagnostic.diagnostic_source() {
55 diag.field("caused by", &format!("{:?}", cause));
56 }
57 diag.finish()?;
58 writeln!(f)?;
59 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().")
60 }
61}
62
63impl ReportHandler for DebugReportHandler {
64 fn debug(&self, diagnostic: &(dyn Diagnostic), f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 if f.alternate() {
66 return fmt::Debug::fmt(diagnostic, f);
67 }
68
69 self.render_report(f, diagnostic)
70 }
71}