use std::error::Error;
use std::fmt;
use snafu::{ErrorCompat, IntoError, ResultExt};
pub use alioth_macros::{DebugTrace, trace_error};
pub trait DebugTrace: Error {
fn debug_trace(&self, f: &mut fmt::Formatter) -> Result<u32, fmt::Error>;
}
impl Error for Box<dyn DebugTrace + Send + Sync + 'static> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Error::source(Box::as_ref(self))
}
}
pub trait BoxTrace<'a, T> {
fn box_trace<C, E>(self, context: C) -> Result<T, E>
where
C: IntoError<E, Source = Box<dyn DebugTrace + Send + Sync + 'a>>,
E: Error + ErrorCompat;
}
impl<'a, T, E1> BoxTrace<'a, T> for Result<T, E1>
where
E1: DebugTrace + Send + Sync + 'a,
{
fn box_trace<C, E>(self, context: C) -> Result<T, E>
where
C: IntoError<E, Source = Box<dyn DebugTrace + Send + Sync + 'a>>,
E: Error + ErrorCompat,
{
self.map_err(|e| Box::new(e) as _).context(context)
}
}