use crate::Error;
pub trait Context<Ok, Kind> {
fn context<Func, Str>(self, context: Func) -> Result<Ok, Error<Kind>>
where
Self: Sized,
Func: FnOnce() -> Str,
Str: Into<String>;
}
impl<Ok, Err, Kind> Context<Ok, Kind> for Result<Ok, Err>
where
Err: std::error::Error + Send + Sync + 'static,
{
fn context<Func, Str>(self, context: Func) -> Result<Ok, Error<Kind>>
where
Self: Sized,
Func: FnOnce() -> Str,
Str: Into<String>,
{
match self {
Ok(ok) => Ok(ok),
Err(source) => Err(Error::wrap(source, context())),
}
}
}
impl<Ok, Kind> Context<Ok, Kind> for Option<Ok> {
fn context<Func, Str>(self, context: Func) -> Result<Ok, Error<Kind>>
where
Self: Sized,
Func: FnOnce() -> Str,
Str: Into<String>,
{
match self {
Some(ok) => Ok(ok),
None => Err(Error::root(context())),
}
}
}