Allow miette users to opt-out of the rendering of the cause chain

This commit is contained in:
Luca Palmieri 2022-08-07 15:01:40 +01:00
parent c3e6c98336
commit f99a7b786d
3 changed files with 62 additions and 2 deletions

View File

@ -56,6 +56,7 @@ pub struct MietteHandlerOpts {
pub(crate) footer: Option<String>, pub(crate) footer: Option<String>,
pub(crate) context_lines: Option<usize>, pub(crate) context_lines: Option<usize>,
pub(crate) tab_width: Option<usize>, pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: Option<bool>
} }
impl MietteHandlerOpts { impl MietteHandlerOpts {
@ -86,6 +87,18 @@ impl MietteHandlerOpts {
self.width = Some(width); self.width = Some(width);
self 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 /// If true, colors will be used during graphical rendering, regardless
/// of whether or not the terminal supports them. /// of whether or not the terminal supports them.
@ -165,6 +178,13 @@ impl MietteHandlerOpts {
if let Some(context_lines) = self.context_lines { if let Some(context_lines) = self.context_lines {
handler = handler.with_context_lines(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 { MietteHandler {
inner: Box::new(handler), inner: Box::new(handler),
} }
@ -197,6 +217,13 @@ impl MietteHandlerOpts {
.with_width(width) .with_width(width)
.with_links(linkify) .with_links(linkify)
.with_theme(theme); .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 { if let Some(footer) = self.footer {
handler = handler.with_footer(footer); handler = handler.with_footer(footer);
} }

View File

@ -28,6 +28,7 @@ pub struct GraphicalReportHandler {
pub(crate) footer: Option<String>, pub(crate) footer: Option<String>,
pub(crate) context_lines: usize, pub(crate) context_lines: usize,
pub(crate) tab_width: Option<usize>, pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: bool
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -48,6 +49,7 @@ impl GraphicalReportHandler {
footer: None, footer: None,
context_lines: 1, context_lines: 1,
tab_width: None, tab_width: None,
with_cause_chain: true
} }
} }
@ -60,6 +62,7 @@ impl GraphicalReportHandler {
footer: None, footer: None,
context_lines: 1, context_lines: 1,
tab_width: None, tab_width: None,
with_cause_chain: true
} }
} }
@ -78,6 +81,18 @@ impl GraphicalReportHandler {
}; };
self 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. /// Whether to include [`Diagnostic::url()`] in the output.
/// ///
@ -134,7 +149,9 @@ impl GraphicalReportHandler {
) -> fmt::Result { ) -> fmt::Result {
self.render_header(f, diagnostic)?; self.render_header(f, diagnostic)?;
writeln!(f)?; writeln!(f)?;
self.render_causes(f, diagnostic)?; if self.with_cause_chain {
self.render_causes(f, diagnostic)?;
}
let src = diagnostic.source_code(); let src = diagnostic.source_code();
self.render_snippets(f, diagnostic, src)?; self.render_snippets(f, diagnostic, src)?;
self.render_footer(f, diagnostic)?; self.render_footer(f, diagnostic)?;

View File

@ -14,6 +14,7 @@ non-graphical environments, such as non-TTY output.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct NarratableReportHandler { pub struct NarratableReportHandler {
context_lines: usize, context_lines: usize,
with_cause_chain: bool,
footer: Option<String>, footer: Option<String>,
} }
@ -24,8 +25,21 @@ impl NarratableReportHandler {
Self { Self {
footer: None, footer: None,
context_lines: 1, 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. /// Set the footer to be displayed at the end of the report.
pub fn with_footer(mut self, footer: String) -> Self { pub fn with_footer(mut self, footer: String) -> Self {
@ -57,7 +71,9 @@ impl NarratableReportHandler {
diagnostic: &(dyn Diagnostic), diagnostic: &(dyn Diagnostic),
) -> fmt::Result { ) -> fmt::Result {
self.render_header(f, diagnostic)?; 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(); let src = diagnostic.source_code();
self.render_snippets(f, diagnostic, src)?; self.render_snippets(f, diagnostic, src)?;
self.render_footer(f, diagnostic)?; self.render_footer(f, diagnostic)?;