From 9841d6fd77ce665acb40f7459f410e83cdc131c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 17 Sep 2021 10:29:00 -0700 Subject: [PATCH] feat(report): add debug report as default, instead of narrated one --- src/eyreish/mod.rs | 4 +-- src/handlers/debug.rs | 69 +++++++++++++++++++++++++++++++++++++++++++ src/handlers/mod.rs | 3 ++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/handlers/debug.rs diff --git a/src/eyreish/mod.rs b/src/eyreish/mod.rs index 77b8659..06be76e 100644 --- a/src/eyreish/mod.rs +++ b/src/eyreish/mod.rs @@ -30,7 +30,7 @@ use crate::Diagnostic; #[cfg(feature = "fancy")] use crate::MietteHandler; #[cfg(not(feature = "fancy"))] -use crate::NarratableReportHandler; +use crate::DebugReportHandler; use error::ErrorImpl; @@ -95,7 +95,7 @@ fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box Self { + Self + } +} + +impl Default for DebugReportHandler { + fn default() -> Self { + Self::new() + } +} + +impl DebugReportHandler { + /// Render a [Diagnostic]. This function is mostly internal and meant to + /// be called by the toplevel [ReportHandler] handler, but is + /// made public to make it easier (possible) to test in isolation from + /// global state. + 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(snippets) = diagnostic.snippets() { + let snippets: Vec<_> = snippets.collect(); + diag.field("snippets", &format!("{:?}", snippets)); + } + 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) + } +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 5606268..7823937 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -2,6 +2,8 @@ Reporters included with `miette`. */ +#[allow(unreachable_pub)] +pub use debug::*; #[allow(unreachable_pub)] #[cfg(feature = "fancy")] pub use graphical::*; @@ -11,6 +13,7 @@ pub use narratable::*; #[cfg(feature = "fancy")] pub use theme::*; +mod debug; #[cfg(feature = "fancy")] mod graphical; mod narratable;