use std::borrow::Cow;
use std::error::Error as StdError;
use std::panic::Location;
use crate::{Error, Frame, WithContext};
pub trait Context<T, E> {
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Into<Cow<'static, str>>;
fn with_context<C, F>(self, f: F) -> Result<T, Error>
where
C: Into<Cow<'static, str>>,
F: FnOnce() -> C;
}
impl<T, E> Context<T, E> for Result<T, E>
where
E: StdError + Send + Sync + 'static,
{
#[track_caller]
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Into<Cow<'static, str>>,
{
let location = Location::caller();
self.map_err(|e| {
let source = e.into();
let frame = Frame { source, location };
Error::new(context).with_context(frame)
})
}
#[track_caller]
fn with_context<C, F>(self, f: F) -> Result<T, Error>
where
C: Into<Cow<'static, str>>,
F: FnOnce() -> C,
{
let location = Location::caller();
self.map_err(|e| {
let source = e.into();
let frame = Frame { source, location };
Error::new(f()).with_context(frame)
})
}
}