Skip to content

Commit

Permalink
feat(graphical): Allow miette users to opt-out of the rendering of th…
Browse files Browse the repository at this point in the history
…e cause chain (#192)

Fixes: #191
  • Loading branch information
LukeMathWalker authored Aug 10, 2022
1 parent c3e6c98 commit b9ea587
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct MietteHandlerOpts {
pub(crate) footer: Option<String>,
pub(crate) context_lines: Option<usize>,
pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: Option<bool>,
}

impl MietteHandlerOpts {
Expand Down Expand Up @@ -87,6 +88,18 @@ impl MietteHandlerOpts {
self
}

/// Include the cause chain of the top-level error in the report.
pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(true);
self
}

/// Do not include the cause chain of the top-level error in the report.
pub fn without_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(false);
self
}

/// If true, colors will be used during graphical rendering, regardless
/// of whether or not the terminal supports them.
///
Expand Down Expand Up @@ -165,6 +178,13 @@ impl MietteHandlerOpts {
if let Some(context_lines) = self.context_lines {
handler = handler.with_context_lines(context_lines);
}
if let Some(with_cause_chain) = self.with_cause_chain {
if with_cause_chain {
handler = handler.with_cause_chain();
} else {
handler = handler.without_cause_chain();
}
}
MietteHandler {
inner: Box::new(handler),
}
Expand Down Expand Up @@ -197,6 +217,13 @@ impl MietteHandlerOpts {
.with_width(width)
.with_links(linkify)
.with_theme(theme);
if let Some(with_cause_chain) = self.with_cause_chain {
if with_cause_chain {
handler = handler.with_cause_chain();
} else {
handler = handler.without_cause_chain();
}
}
if let Some(footer) = self.footer {
handler = handler.with_footer(footer);
}
Expand Down
21 changes: 21 additions & 0 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct GraphicalReportHandler {
pub(crate) footer: Option<String>,
pub(crate) context_lines: usize,
pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -48,6 +49,7 @@ impl GraphicalReportHandler {
footer: None,
context_lines: 1,
tab_width: None,
with_cause_chain: true,
}
}

Expand All @@ -60,6 +62,7 @@ impl GraphicalReportHandler {
footer: None,
context_lines: 1,
tab_width: None,
with_cause_chain: true,
}
}

Expand All @@ -79,6 +82,20 @@ impl GraphicalReportHandler {
self
}

/// Include the cause chain of the top-level error in the graphical output,
/// if available.
pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = true;
self
}

/// Do not include the cause chain of the top-level error in the graphical
/// output.
pub fn without_cause_chain(mut self) -> Self {
self.with_cause_chain = false;
self
}

/// Whether to include [`Diagnostic::url()`] in the output.
///
/// Disabling this is not recommended, but can be useful for more easily
Expand Down Expand Up @@ -199,6 +216,10 @@ impl GraphicalReportHandler {

writeln!(f, "{}", textwrap::fill(&diagnostic.to_string(), opts))?;

if !self.with_cause_chain {
return Ok(());
}

if let Some(mut cause_iter) = diagnostic
.diagnostic_source()
.map(DiagnosticChain::from_diagnostic)
Expand Down
19 changes: 18 additions & 1 deletion src/handlers/narratable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ non-graphical environments, such as non-TTY output.
#[derive(Debug, Clone)]
pub struct NarratableReportHandler {
context_lines: usize,
with_cause_chain: bool,
footer: Option<String>,
}

Expand All @@ -24,9 +25,23 @@ impl NarratableReportHandler {
Self {
footer: None,
context_lines: 1,
with_cause_chain: true,
}
}

/// Include the cause chain of the top-level error in the report, if
/// available.
pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = true;
self
}

/// Do not include the cause chain of the top-level error in the report.
pub fn without_cause_chain(mut self) -> Self {
self.with_cause_chain = false;
self
}

/// Set the footer to be displayed at the end of the report.
pub fn with_footer(mut self, footer: String) -> Self {
self.footer = Some(footer);
Expand Down Expand Up @@ -57,7 +72,9 @@ impl NarratableReportHandler {
diagnostic: &(dyn Diagnostic),
) -> fmt::Result {
self.render_header(f, diagnostic)?;
self.render_causes(f, diagnostic)?;
if self.with_cause_chain {
self.render_causes(f, diagnostic)?;
}
let src = diagnostic.source_code();
self.render_snippets(f, diagnostic, src)?;
self.render_footer(f, diagnostic)?;
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,14 @@
//! `miette` was not developed in a void. It owes enormous credit to various
//! other projects and their authors:
//!
//! - [`anyhow`](http://crates.io/crates/anyhow) and
//! [`color-eyre`](https://crates.io/crates/color-eyre): these two
//! enormously influential error handling libraries have pushed forward the
//! experience of application-level error handling and error reporting.
//! `miette`'s `Report` type is an attempt at a very very rough version of
//! their `Report` types.
//! - [`thiserror`](https://crates.io/crates/thiserror) for setting the
//! standard for library-level error definitions, and for being the
//! inspiration behind `miette`'s derive macro.
//! - [`anyhow`](http://crates.io/crates/anyhow) and [`color-eyre`](https://crates.io/crates/color-eyre):
//! these two enormously influential error handling libraries have pushed
//! forward the experience of application-level error handling and error
//! reporting. `miette`'s `Report` type is an attempt at a very very rough
//! version of their `Report` types.
//! - [`thiserror`](https://crates.io/crates/thiserror) for setting the standard
//! for library-level error definitions, and for being the inspiration behind
//! `miette`'s derive macro.
//! - `rustc` and [@estebank](https://github.com/estebank) for their
//! state-of-the-art work in compiler diagnostics.
//! - [`ariadne`](https://crates.io/crates/ariadne) for pushing forward how
Expand Down

0 comments on commit b9ea587

Please sign in to comment.