how.rs/src/explain.rs

34 lines
704 B
Rust

use crate::{How, IntoContext};
pub trait Explain: Sized {
type T;
fn explained(self) -> Result<Self::T, How>;
fn context(self, context: impl IntoContext) -> Result<Self::T, How> {
self.explained()
.context(context)
}
}
impl<T, E> Explain for Result<T, E> where E: std::error::Error {
type T = T;
fn explained(self) -> Result<Self::T, How> {
self.map_err(Into::into)
}
}
impl<T> Explain for Result<T, How> {
type T = T;
fn explained(self) -> Result<Self::T, How> {
self
}
#[inline(always)]
fn context(self, context: impl IntoContext) -> Result<Self::T, How> {
self.map_err(|e| e.context(context))
}
}