use std::error::Error as StdError;
use error_stack::{IntoReport, ResultExt};
use crate::{Result, kind::ErrorKind, result::TypedResult};
pub trait IntoErrorExt<T, E>: Sized {
fn into_error(self, kind: ErrorKind) -> Result<T>
where
E: IntoReport;
fn into_error_lazy<F>(self, f: F) -> Result<T>
where
E: IntoReport,
F: FnOnce() -> ErrorKind;
}
impl<T, E> IntoErrorExt<T, E> for core::result::Result<T, E> {
fn into_error(self, kind: ErrorKind) -> Result<T>
where
E: IntoReport,
{
self.change_context(kind)
}
fn into_error_lazy<F>(self, f: F) -> Result<T>
where
E: IntoReport,
F: FnOnce() -> ErrorKind,
{
self.change_context_lazy(f)
}
}
pub trait IntoTypedContextExt<T, E>: Sized {
fn into_typed<C>(self, context: C) -> TypedResult<T, C>
where
C: StdError + Send + Sync + 'static,
E: IntoReport;
fn into_typed_lazy<C, F>(self, f: F) -> TypedResult<T, C>
where
C: StdError + Send + Sync + 'static,
E: IntoReport,
F: FnOnce() -> C;
}
impl<T, E> IntoTypedContextExt<T, E> for core::result::Result<T, E> {
fn into_typed<C>(self, context: C) -> TypedResult<T, C>
where
C: StdError + Send + Sync + 'static,
E: IntoReport,
{
self.change_context(context)
}
fn into_typed_lazy<C, F>(self, f: F) -> TypedResult<T, C>
where
C: StdError + Send + Sync + 'static,
E: IntoReport,
F: FnOnce() -> C,
{
self.change_context_lazy(f)
}
}
#[cfg(test)]
mod tests {
use super::{IntoErrorExt, IntoTypedContextExt};
use crate::{ErrorKind, RawResult, TypedResult};
use error_stack::Report;
use std::sync::atomic::{AtomicBool, Ordering};
use thiserror::Error;
#[derive(Debug, Error)]
#[error("raw leaf error")]
struct RawLeafError;
#[derive(Debug, Error, PartialEq, Eq)]
enum LocalContext {
#[error("decode failed")]
Decode,
#[error("load failed")]
Load,
}
#[test]
fn test_into_error_converts_raw_result_to_public_error() {
let result: RawResult<(), RawLeafError> = Err(RawLeafError);
let error = result
.into_error(ErrorKind::InvalidInput)
.expect_err("expected public report");
assert_eq!(error.current_context(), &ErrorKind::InvalidInput);
assert!(error.contains::<RawLeafError>());
}
#[test]
fn test_into_typed_converts_raw_result_to_typed_report() {
let result: RawResult<(), RawLeafError> = Err(RawLeafError);
let error = result
.into_typed(LocalContext::Decode)
.expect_err("expected typed report");
assert_eq!(error.current_context(), &LocalContext::Decode);
assert!(error.contains::<RawLeafError>());
}
#[test]
fn test_into_error_preserves_existing_typed_context() {
let result: TypedResult<(), LocalContext> =
Err(Report::new(LocalContext::Load).attach("load_config"));
let error = result
.into_error(ErrorKind::Dependency)
.expect_err("expected public report");
assert_eq!(error.current_context(), &ErrorKind::Dependency);
assert!(error.contains::<LocalContext>());
assert!(error.contains::<ErrorKind>());
assert!(error.contains::<&'static str>());
}
#[test]
fn test_into_error_lazy_is_not_evaluated_on_ok_path() {
static CALLED: AtomicBool = AtomicBool::new(false);
let result: RawResult<u32, RawLeafError> = Ok(7);
let value = result
.into_error_lazy(|| {
CALLED.store(true, Ordering::SeqCst);
ErrorKind::Internal
})
.expect("ok path should pass through");
assert_eq!(value, 7);
assert!(!CALLED.load(Ordering::SeqCst));
}
#[test]
fn test_into_typed_lazy_is_not_evaluated_on_ok_path() {
static CALLED: AtomicBool = AtomicBool::new(false);
let result: RawResult<&'static str, RawLeafError> = Ok("ok");
let value = result
.into_typed_lazy(|| {
CALLED.store(true, Ordering::SeqCst);
LocalContext::Decode
})
.expect("ok path should pass through");
assert_eq!(value, "ok");
assert!(!CALLED.load(Ordering::SeqCst));
}
#[test]
fn test_into_error_lazy_is_evaluated_on_err_path() {
static CALLED: AtomicBool = AtomicBool::new(false);
let result: RawResult<(), RawLeafError> = Err(RawLeafError);
let error = result
.into_error_lazy(|| {
CALLED.store(true, Ordering::SeqCst);
ErrorKind::Internal
})
.expect_err("err path should convert");
assert_eq!(error.current_context(), &ErrorKind::Internal);
assert!(CALLED.load(Ordering::SeqCst));
}
}