diff --git a/src/eyreish/context.rs b/src/eyreish/context.rs index 3d9238b..ee1fa08 100644 --- a/src/eyreish/context.rs +++ b/src/eyreish/context.rs @@ -38,6 +38,44 @@ mod ext { } } +impl WrapErr for Option { + fn wrap_err(self, msg: D) -> Result + where + D: Display + Send + Sync + 'static, + { + match self { + Some(t) => Ok(t), + None => Err(Report::from(crate::eyreish::wrapper::DisplayError(msg))), + } + } + + fn wrap_err_with(self, msg: F) -> Result + where + D: Display + Send + Sync + 'static, + F: FnOnce() -> D, + { + match self { + Some(t) => Ok(t), + None => Err(Report::from(crate::eyreish::wrapper::DisplayError(msg()))), + } + } + + fn context(self, msg: D) -> Result + where + D: Display + Send + Sync + 'static, + { + self.wrap_err(msg) + } + + fn with_context(self, msg: F) -> Result + where + D: Display + Send + Sync + 'static, + F: FnOnce() -> D, + { + self.wrap_err_with(msg) + } +} + impl WrapErr for Result where E: ext::Diag + Send + Sync + 'static, diff --git a/src/eyreish/wrapper.rs b/src/eyreish/wrapper.rs index 62cf7a4..bab7320 100644 --- a/src/eyreish/wrapper.rs +++ b/src/eyreish/wrapper.rs @@ -6,6 +6,30 @@ use crate::{Diagnostic, LabeledSpan, Report, SourceCode}; use crate as miette; +#[repr(transparent)] +pub(crate) struct DisplayError(pub(crate) M); + +impl Debug for DisplayError +where + M: Display, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Display::fmt(&self.0, f) + } +} + +impl Display for DisplayError +where + M: Display, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Display::fmt(&self.0, f) + } +} + +impl StdError for DisplayError where M: Display + 'static {} +impl Diagnostic for DisplayError where M: Display + 'static {} + #[repr(transparent)] pub(crate) struct MessageError(pub(crate) M);