use crate::handled::IntoValue;
use crate::Result;
pub trait HandleExt<T> {
fn then<U, F>(self, f: F) -> Result<U>
where
F: FnOnce(T) -> Result<U>;
fn then_with<U, F>(self, ctx: impl Into<String>, f: F) -> Result<U>
where
F: FnOnce(T) -> Result<U>;
fn context(self, ctx: impl Into<String>) -> Self;
fn attach(self, key: &'static str, val: impl IntoValue) -> Self;
}
impl<T> HandleExt<T> for Result<T> {
#[track_caller]
fn then<U, F>(self, f: F) -> Result<U>
where
F: FnOnce(T) -> Result<U>,
{
let loc = core::panic::Location::caller();
match self {
Ok(v) => f(v).map_err(|e| e.frame(loc.file(), loc.line(), loc.column())),
Err(e) => Err(e),
}
}
#[track_caller]
fn then_with<U, F>(self, ctx: impl Into<String>, f: F) -> Result<U>
where
F: FnOnce(T) -> Result<U>,
{
let loc = core::panic::Location::caller();
let ctx = ctx.into();
match self {
Ok(v) => f(v).map_err(|e| e.frame(loc.file(), loc.line(), loc.column()).ctx(ctx)),
Err(e) => Err(e),
}
}
#[track_caller]
fn context(self, ctx: impl Into<String>) -> Self {
let loc = core::panic::Location::caller();
self.map_err(|e| e.frame(loc.file(), loc.line(), loc.column()).ctx(ctx))
}
#[track_caller]
fn attach(self, key: &'static str, val: impl IntoValue) -> Self {
let loc = core::panic::Location::caller();
self.map_err(|e| e.frame(loc.file(), loc.line(), loc.column()).kv(key, val))
}
}