how.rs/src/explain.rs

42 lines
1.1 KiB
Rust

use crate::{How, IntoContext, IntoResultHow};
crate::seal!(pub(crate) private::Sealed);
pub trait Explain: Sealed {
type Output;
#[must_use]
fn context(self, context: impl IntoContext) -> Self::Output;
}
impl<T, E> Sealed for Result<T, E> where Result<T, E>: IntoResultHow {}
impl<T> Sealed for Option<T> where Option<T>: IntoResultHow {}
impl<T, E> Explain for Result<T, E>
where
Result<T, E>: IntoResultHow,
{
type Output = Result<<Self as IntoResultHow>::T, How>;
#[inline(always)]
#[track_caller]
fn context(self, context: impl IntoContext) -> Self::Output {
self.into_result_how().map_err(#[inline(never)] move |e| e.context(context))
}
}
impl<T> Explain for Option<T> {
type Output = Result<T, How>;
#[inline(always)]
#[track_caller]
fn context(self, context: impl IntoContext) -> Self::Output {
// TODO: maybe add a feature for the extra "Option::None" context
match self {
Some(t) => Ok(t),
None => Err(How::new(context))
}
//self.into_result_how().map_err(#[inline(never)] move |e| e.context(context))
}
}