how.rs/src/explain.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2022-07-21 20:40:44 -04:00
use crate::{How, IntoContext, IntoResultHow};
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
crate::seal!(pub(crate) private::Sealed);
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
pub trait Explain: Sealed {
type Output;
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
#[must_use]
fn context(self, context: impl IntoContext) -> Self::Output;
2022-07-21 20:39:47 -04:00
}
2022-07-21 20:40:44 -04:00
impl<T, E> Sealed for Result<T, E> where Result<T, E>: IntoResultHow {}
impl<T> Sealed for Option<T> where Option<T>: IntoResultHow {}
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
impl<T, E> Explain for Result<T, E>
where
Result<T, E>: IntoResultHow,
{
type Output = Result<<Self as IntoResultHow>::T, How>;
2022-07-21 20:39:47 -04:00
#[inline(always)]
2023-02-06 09:06:19 -05:00
#[track_caller]
2022-07-21 20:40:44 -04:00
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)]
2023-02-06 09:06:19 -05:00
#[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))
2023-01-17 07:29:58 -05:00
}
}