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