#![expect(deprecated, reason = "We use `Context` to maintain compatibility")]
use core::{error::Error, fmt};
use crate::{Context, IntoReport, Report};
#[deprecated(
note = "Use `core::result::Result<T, Report<C>>` or `anystack::ResultStack` instead",
since = "0.6.0"
)]
pub type Result<T, C = dyn Error> = core::result::Result<T, Report<C>>;
pub type ResultStack<T, C = dyn Error> = core::result::Result<T, Report<C>>;
pub trait ResultExt {
type Context: ?Sized;
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, E> ResultExt for core::result::Result<T, E>
where
E: IntoReport,
{
type Context = E::Context;
type Ok = T;
#[track_caller]
fn attach<A>(self, attachment: A) -> core::result::Result<T, Report<E::Context>>
where
A: Send + Sync + 'static,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach(attachment)),
}
}
#[track_caller]
fn attach_lazy<A, F>(self, attachment: F) -> core::result::Result<T, Report<E::Context>>
where
A: Send + Sync + 'static,
F: FnOnce() -> A,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach(attachment())),
}
}
#[track_caller]
fn attach_printable<A>(self, attachment: A) -> core::result::Result<T, Report<E::Context>>
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach_printable(attachment)),
}
}
#[track_caller]
fn attach_printable_lazy<A, F>(
self,
attachment: F,
) -> core::result::Result<T, Report<E::Context>>
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
F: FnOnce() -> A,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach_printable(attachment())),
}
}
#[track_caller]
fn change_context<C>(self, context: C) -> core::result::Result<T, Report<C>>
where
C: Context,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().change_context(context)),
}
}
#[track_caller]
fn change_context_lazy<C, F>(self, context: F) -> core::result::Result<T, Report<C>>
where
C: Context,
F: FnOnce() -> C,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().change_context(context())),
}
}
}