use alloc::string::ToString;
use core::fmt;
use crate::{Diagnostic, Report};
pub trait WrapErr<T, E>: _private::Sealed {
#[track_caller]
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static;
#[track_caller]
fn wrap_err_with<D, F>(self, f: F) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> D;
#[track_caller]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static;
#[track_caller]
fn with_context<D, F>(self, f: F) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> D;
}
mod _private {
use crate::Report;
pub trait Sealed {}
impl<T, E> Sealed for Result<T, E> where Report: From<E> {}
impl<T> Sealed for Option<T> {}
}
impl<T> WrapErr<T, core::convert::Infallible> for Option<T> {
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
{
match self {
Some(t) => Ok(t),
None => Err(Report::new(DisplayError(msg))),
}
}
fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
match self {
Some(t) => Ok(t),
None => Err(Report::new(DisplayError(msg()))),
}
}
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
{
self.wrap_err(msg)
}
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.wrap_err_with(msg)
}
}
impl<T, E> WrapErr<T, E> for Result<T, E>
where
Report: From<E>,
{
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
{
match self {
Ok(t) => Ok(t),
Err(e) => Err(Report::from(e).context(msg.to_string())),
}
}
fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
match self {
Ok(t) => Ok(t),
Err(e) => Err(Report::from(e).context(msg().to_string())),
}
}
#[inline]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
{
self.wrap_err(msg)
}
#[inline]
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: fmt::Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.wrap_err_with(msg)
}
}
#[repr(transparent)]
struct DisplayError<M>(M);
impl<M> fmt::Debug for DisplayError<M>
where
M: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<M> fmt::Display for DisplayError<M>
where
M: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<M> core::error::Error for DisplayError<M> where M: fmt::Display + 'static {}
impl<M> Diagnostic for DisplayError<M>
where
M: fmt::Display + 'static,
{
fn message(&self, out: &mut dyn fmt::Write) -> fmt::Result {
out.write_fmt(format_args!("{}", self.0))
}
}