feat(report): add debug report as default, instead of narrated one

This commit is contained in:
Kat Marchán 2021-09-17 10:29:00 -07:00
parent cd69a8ae92
commit 9841d6fd77
3 changed files with 74 additions and 2 deletions

View File

@ -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<dyn ReportHandl
#[cfg(feature = "fancy")]
return Box::new(MietteHandler::new());
#[cfg(not(feature = "fancy"))]
return Box::new(NarratableReportHandler::new());
return Box::new(DebugReportHandler::new());
}
impl dyn ReportHandler {

69
src/handlers/debug.rs Normal file
View File

@ -0,0 +1,69 @@
use std::fmt;
use crate::{protocol::Diagnostic, ReportHandler};
/**
[ReportHandler] that renders plain text and avoids extraneous graphics.
It's optimized for screen readers and braille users, but is also used in any
non-graphical environments, such as non-TTY output.
*/
#[derive(Debug, Clone)]
pub struct DebugReportHandler;
impl DebugReportHandler {
/// Create a new [NarratableReportHandler]. There are no customization
/// options.
pub fn new() -> 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)
}
}

View File

@ -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;