Skip to main content

bias_miette/handlers/
debug.rs

1use std::fmt;
2
3use crate::protocol::Diagnostic;
4
5/**
6[`ReportHandler`] that renders plain text and avoids extraneous graphics.
7It's optimized for screen readers and braille users, but is also used in any
8non-graphical environments, such as non-TTY output.
9*/
10#[derive(Debug, Clone)]
11pub struct DebugReportHandler;
12
13impl DebugReportHandler {
14    /// Create a new [`NarratableReportHandler`](crate::NarratableReportHandler)
15    /// There are no customization options.
16    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    /// Render a [`Diagnostic`]. This function is mostly internal and meant to
29    /// be called by the toplevel [`ReportHandler`] handler, but is made public
30    /// to make it easier (possible) to test in isolation from global state.
31    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        diag.finish()?;
55        writeln!(f)
56    }
57}