use core::fmt;
use crate::{Context, Report};
pub type Result<T, C> = core::result::Result<T, Report<C>>;
pub trait ResultExt {
type Ok;
#[must_use]
fn attach<A>(self, attachment: A) -> Self
where
A: Send + Sync + 'static;
#[must_use]
fn attach_lazy<A, F>(self, attachment: F) -> Self
where
A: Send + Sync + 'static,
F: FnOnce() -> A;
#[must_use]
fn attach_printable<A>(self, attachment: A) -> Self
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static;
#[must_use]
fn attach_printable_lazy<A, F>(self, attachment: F) -> Self
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
F: FnOnce() -> A;
fn change_context<C>(self, context: C) -> Result<Self::Ok, C>
where
C: Context;
fn change_context_lazy<C, F>(self, context: F) -> Result<Self::Ok, C>
where
C: Context,
F: FnOnce() -> C;
}
impl<T, C> ResultExt for Result<T, C> {
type Ok = T;
#[track_caller]
fn attach<A>(self, attachment: A) -> Self
where
A: Send + Sync + 'static,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.attach(attachment)),
}
}
#[track_caller]
fn attach_lazy<A, F>(self, attachment: F) -> Self
where
A: Send + Sync + 'static,
F: FnOnce() -> A,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.attach(attachment())),
}
}
#[track_caller]
fn attach_printable<A>(self, attachment: A) -> Self
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.attach_printable(attachment)),
}
}
#[track_caller]
fn attach_printable_lazy<A, F>(self, attachment: F) -> Self
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
F: FnOnce() -> A,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.attach_printable(attachment())),
}
}
#[track_caller]
fn change_context<C2>(self, context: C2) -> Result<T, C2>
where
C2: Context,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.change_context(context)),
}
}
#[track_caller]
fn change_context_lazy<C2, F>(self, context: F) -> Result<T, C2>
where
C2: Context,
F: FnOnce() -> C2,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.change_context(context())),
}
}
}
pub trait IntoReport: Sized {
type Ok;
type Err;
#[deprecated(since = "0.2.0", note = "Use `IntoReport::into_report` instead")]
#[inline]
#[allow(missing_docs)]
fn report(self) -> Result<Self::Ok, Self::Err> {
self.into_report()
}
fn into_report(self) -> Result<Self::Ok, Self::Err>;
}
impl<T, E> IntoReport for core::result::Result<T, E>
where
Report<E>: From<E>,
{
type Err = E;
type Ok = T;
#[track_caller]
fn into_report(self) -> Result<T, E> {
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::from(error)),
}
}
}