meta: initial commit (archival)

This commit is contained in:
2023-12-27 15:38:47 -05:00
commit 0dd0715c29
5 changed files with 184 additions and 0 deletions

50
src/lib.rs Normal file
View File

@@ -0,0 +1,50 @@
use std::{
error::Error,
fmt::{Debug, Display, Formatter},
};
#[derive(Debug)]
pub struct ErrorWithSuggestion<E, S> {
error: E,
suggestion: S,
}
impl<E: Display, S: Display> Display for ErrorWithSuggestion<E, S> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.error.fmt(f)?;
writeln!(f)?;
write!(f, "💡 ")?;
self.suggestion.fmt(f)?;
Ok(())
}
}
impl<E: Error, S: Debug + Display> Error for ErrorWithSuggestion<E, S> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.error.source()
}
}
pub trait ResultExt {
type O;
type E;
fn with_suggestion<Suggestion: Display>(
self,
suggestion: Suggestion,
) -> Result<Self::O, ErrorWithSuggestion<Self::E, Suggestion>>;
}
impl<Ok, Err> ResultExt for Result<Ok, Err> {
type O = Ok;
type E = Err;
fn with_suggestion<Suggestion: Display>(
self,
suggestion: Suggestion,
) -> Result<Self::O, ErrorWithSuggestion<Self::E, Suggestion>> {
self.map_err(|error| ErrorWithSuggestion { error, suggestion })
}
}