use core::{fmt, result::Result};
use crate::{Context, Report};
pub type BigResult<T, C> = Result<T, Report<C>>;
pub trait ResultExt {
type Context: Context;
type Ok;
fn attach<A>(self, attachment: A) -> Result<Self::Ok, Report<Self::Context>>
where
A: Send + Sync + 'static;
fn attach_lazy<A, F>(self, attachment: F) -> Result<Self::Ok, Report<Self::Context>>
where
A: Send + Sync + 'static,
F: FnOnce() -> A;
fn attach_printable<A>(self, attachment: A) -> Result<Self::Ok, Report<Self::Context>>
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static;
fn attach_printable_lazy<A, F>(self, attachment: F) -> 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) -> Result<Self::Ok, Report<C>>
where
C: Context;
fn change_context_lazy<C, F>(self, context: F) -> Result<Self::Ok, Report<C>>
where
C: Context,
F: FnOnce() -> C;
}
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) -> BigResult<T, C>
where
A: Send + Sync + 'static,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::new(error).attach(attachment)),
}
}
#[track_caller]
fn attach_lazy<A, F>(self, attachment: F) -> BigResult<T, C>
where
A: Send + Sync + 'static,
F: FnOnce() -> A,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::new(error).attach(attachment())),
}
}
#[track_caller]
fn attach_printable<A>(self, attachment: A) -> BigResult<T, C>
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::new(error).attach_printable(attachment)),
}
}
#[track_caller]
fn attach_printable_lazy<A, F>(self, attachment: F) -> BigResult<T, C>
where
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
F: FnOnce() -> A,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::new(error).attach_printable(attachment())),
}
}
#[track_caller]
fn change_context<C2>(self, context: C2) -> BigResult<T, C2>
where
C2: Context,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::new(error).change_context(context)),
}
}
#[track_caller]
fn change_context_lazy<C2, F>(self, context: F) -> BigResult<T, C2>
where
C2: Context,
F: FnOnce() -> C2,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(Report::new(error).change_context(context())),
}
}
}
impl<T, C> ResultExt for BigResult<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) -> BigResult<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) -> BigResult<T, C2>
where
C2: Context,
F: FnOnce() -> C2,
{
match self {
Ok(ok) => Ok(ok),
Err(report) => Err(report.change_context(context())),
}
}
}