use crate::ColorExt;
use crate::{Report, Result};
use ansi_term::Color::*;
use indenter::indented;
use std::fmt::Write;
use std::fmt::{self, Display};
pub trait Help<T>: private::Sealed {
fn section<D>(self, section: D) -> Result<T>
where
D: Display + Send + Sync + 'static;
fn with_section<D, F>(self, section: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
fn error<E>(self, error: E) -> Result<T>
where
E: std::error::Error + Send + Sync + 'static;
fn with_error<E, F>(self, error: F) -> Result<T>
where
F: FnOnce() -> E,
E: std::error::Error + Send + Sync + 'static;
fn note<D>(self, note: D) -> Result<T>
where
D: Display + Send + Sync + 'static;
fn with_note<D, F>(self, f: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
fn warning<D>(self, warning: D) -> Result<T>
where
D: Display + Send + Sync + 'static;
fn with_warning<D, F>(self, f: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
fn suggestion<D>(self, suggestion: D) -> Result<T>
where
D: Display + Send + Sync + 'static;
fn with_suggestion<D, F>(self, f: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
}
impl<T, E> Help<T> for std::result::Result<T, E>
where
E: Into<Report>,
{
fn note<D>(self, note: D) -> Result<T>
where
D: Display + Send + Sync + 'static,
{
self.map_err(|e| {
let mut e = e.into();
e.handler_mut()
.sections
.push(HelpInfo::Note(Box::new(note)));
e
})
}
fn with_note<D, F>(self, note: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.map_err(|e| {
let mut e = e.into();
e.handler_mut()
.sections
.push(HelpInfo::Note(Box::new(note())));
e
})
}
fn warning<D>(self, warning: D) -> Result<T>
where
D: Display + Send + Sync + 'static,
{
self.map_err(|e| {
let mut e = e.into();
e.handler_mut()
.sections
.push(HelpInfo::Warning(Box::new(warning)));
e
})
}
fn with_warning<D, F>(self, warning: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.map_err(|e| {
let mut e = e.into();
e.handler_mut()
.sections
.push(HelpInfo::Warning(Box::new(warning())));
e
})
}
fn suggestion<D>(self, suggestion: D) -> Result<T>
where
D: Display + Send + Sync + 'static,
{
self.map_err(|e| {
let mut e = e.into();
e.handler_mut()
.sections
.push(HelpInfo::Suggestion(Box::new(suggestion)));
e
})
}
fn with_suggestion<D, F>(self, suggestion: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.map_err(|e| {
let mut e = e.into();
e.handler_mut()
.sections
.push(HelpInfo::Suggestion(Box::new(suggestion())));
e
})
}
fn with_section<D, F>(self, section: F) -> Result<T>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.map_err(|e| {
let mut e = e.into();
let section = Box::new(section());
e.handler_mut().sections.push(HelpInfo::Custom(section));
e
})
}
fn section<D>(self, section: D) -> Result<T>
where
D: Display + Send + Sync + 'static,
{
self.map_err(|e| {
let mut e = e.into();
let section = Box::new(section);
e.handler_mut().sections.push(HelpInfo::Custom(section));
e
})
}
fn error<E2>(self, error: E2) -> Result<T>
where
E2: std::error::Error + Send + Sync + 'static,
{
self.map_err(|e| {
let mut e = e.into();
let error = error.into();
e.handler_mut().sections.push(HelpInfo::Error(error));
e
})
}
fn with_error<E2, F>(self, error: F) -> Result<T>
where
F: FnOnce() -> E2,
E2: std::error::Error + Send + Sync + 'static,
{
self.map_err(|e| {
let mut e = e.into();
let error = error().into();
e.handler_mut().sections.push(HelpInfo::Error(error));
e
})
}
}
pub(crate) enum HelpInfo {
Error(Box<dyn std::error::Error + Send + Sync + 'static>),
Custom(Box<dyn Display + Send + Sync + 'static>),
Note(Box<dyn Display + Send + Sync + 'static>),
Warning(Box<dyn Display + Send + Sync + 'static>),
Suggestion(Box<dyn Display + Send + Sync + 'static>),
}
impl Display for HelpInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HelpInfo::Note(note) => write!(f, "{}: {}", Cyan.make_intense().paint("Note"), note),
HelpInfo::Warning(warning) => {
write!(f, "{}: {}", Yellow.make_intense().paint("Warning"), warning)
}
HelpInfo::Suggestion(suggestion) => write!(
f,
"{}: {}",
Cyan.make_intense().paint("Suggestion"),
suggestion
),
HelpInfo::Custom(section) => write!(f, "{}", section),
HelpInfo::Error(error) => {
let errors = std::iter::successors(
Some(error.as_ref() as &(dyn std::error::Error + 'static)),
|e| e.source(),
);
write!(f, "Error:")?;
let mut buf = String::new();
for (n, error) in errors.enumerate() {
writeln!(f)?;
buf.clear();
write!(&mut buf, "{}", error).unwrap();
write!(indented(f).ind(n), "{}", Red.paint(&buf))?;
}
Ok(())
}
}
}
}
impl fmt::Debug for HelpInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HelpInfo::Note(note) => f
.debug_tuple("Note")
.field(&format_args!("{}", note))
.finish(),
HelpInfo::Warning(warning) => f
.debug_tuple("Warning")
.field(&format_args!("{}", warning))
.finish(),
HelpInfo::Suggestion(suggestion) => f
.debug_tuple("Suggestion")
.field(&format_args!("{}", suggestion))
.finish(),
HelpInfo::Custom(custom) => f
.debug_tuple("CustomSection")
.field(&format_args!("{}", custom))
.finish(),
HelpInfo::Error(error) => f.debug_tuple("Error").field(error).finish(),
}
}
}
pub(crate) mod private {
use crate::Report;
pub trait Sealed {}
impl<T, E> Sealed for std::result::Result<T, E> where E: Into<Report> {}
}