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