Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Add HelpInfo::Report and Section::{report, with_report} functions #123

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,12 @@ impl eyre::EyreHandler for Handler {
)?;
}

for section in self
.sections
.iter()
.filter(|s| matches!(s, HelpInfo::Error(_, _)))
{
write!(separated.ready(), "{}", section)?;
}

for section in self
.sections
.iter()
.filter(|s| matches!(s, HelpInfo::Custom(_)))
{
for section in self.sections.iter().filter(|s| {
matches!(
s,
HelpInfo::Error(_, _) | HelpInfo::Report(_, _) | HelpInfo::Custom(_)
)
}) {
write!(separated.ready(), "{}", section)?;
}

Expand Down Expand Up @@ -130,11 +123,12 @@ impl eyre::EyreHandler for Handler {
let mut h = f.header("\n");
let mut f = h.in_progress();

for section in self
.sections
.iter()
.filter(|s| !matches!(s, HelpInfo::Custom(_) | HelpInfo::Error(_, _)))
{
for section in self.sections.iter().filter(|s| {
!matches!(
s,
HelpInfo::Custom(_) | HelpInfo::Error(_, _) | HelpInfo::Report(_, _)
)
}) {
write!(&mut f, "{}", section)?;
f = h.ready();
}
Expand Down
49 changes: 49 additions & 0 deletions src/section/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ impl Section for Report {
self
}

fn report(mut self, report: Report) -> Self::Return {
if let Some(handler) = self.handler_mut().downcast_mut::<crate::Handler>() {
handler
.sections
.push(HelpInfo::Report(report, handler.theme));
}

self
}

fn with_report<F>(mut self, report: F) -> Self::Return
where
F: FnOnce() -> Report,
{
if let Some(handler) = self.handler_mut().downcast_mut::<crate::Handler>() {
handler
.sections
.push(HelpInfo::Report(report(), handler.theme));
}

self
}

fn suppress_backtrace(mut self, suppress: bool) -> Self::Return {
if let Some(handler) = self.handler_mut().downcast_mut::<crate::Handler>() {
handler.suppress_backtrace = suppress;
Expand Down Expand Up @@ -243,6 +266,19 @@ where
.map_err(|report| report.error(error()))
}

fn report(self, report: Report) -> Self::Return {
self.map_err(|error| error.into())
.map_err(|full_report| full_report.report(report))
}

fn with_report<F>(self, report: F) -> Self::Return
where
F: FnOnce() -> Report,
{
self.map_err(|error| error.into())
.map_err(|full_report| full_report.report(report()))
}

fn suppress_backtrace(self, suppress: bool) -> Self::Return {
self.map_err(|error| error.into())
.map_err(|report| report.suppress_backtrace(suppress))
Expand All @@ -251,6 +287,7 @@ where

pub(crate) enum HelpInfo {
Error(Box<dyn std::error::Error + Send + Sync + 'static>, Theme),
Report(Report, Theme),
Custom(Box<dyn Display + Send + Sync + 'static>),
Note(Box<dyn Display + Send + Sync + 'static>, Theme),
Warning(Box<dyn Display + Send + Sync + 'static>, Theme),
Expand Down Expand Up @@ -289,6 +326,17 @@ impl Display for HelpInfo {
write!(indented(f).ind(n), "{}", error.style(theme.help_info_error))?;
}

Ok(())
}
HelpInfo::Report(report, theme) => {
let errors = report.chain();

write!(f, "Error:")?;
for (n, error) in errors.enumerate() {
writeln!(f)?;
write!(indented(f).ind(n), "{}", error.style(theme.help_info_error))?;
}

Ok(())
}
}
Expand All @@ -315,6 +363,7 @@ impl fmt::Debug for HelpInfo {
.field(&format_args!("{}", custom))
.finish(),
HelpInfo::Error(error, ..) => f.debug_tuple("Error").field(error).finish(),
HelpInfo::Report(report, ..) => f.debug_tuple("Report").field(report).finish(),
}
}
}
7 changes: 7 additions & 0 deletions src/section/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Helpers for adding custom sections to error reports
use crate::eyre::Report;
use crate::writers::WriterExt;
use std::fmt::{self, Display};

Expand Down Expand Up @@ -230,6 +231,12 @@ pub trait Section: crate::private::Sealed {
F: FnOnce() -> E,
E: std::error::Error + Send + Sync + 'static;

fn report(self, report: Report) -> Self::Return;

fn with_report<F>(self, report: F) -> Self::Return
where
F: FnOnce() -> Report;

/// Add a Note to an error report, to be displayed after the chain of errors.
///
/// # Examples
Expand Down