use std::error::Error;
use std::fmt;
use anyerror::AnyError;
use crate::errors::ErrorSource;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BoxedErrorSource {
inner: Box<AnyError>,
}
impl fmt::Display for BoxedErrorSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
impl Error for BoxedErrorSource {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.inner.source()
}
}
impl ErrorSource for BoxedErrorSource {
fn from_error<E: Error + 'static>(error: &E) -> Self {
Self {
inner: Box::new(AnyError::new(error)),
}
}
fn from_string(msg: impl ToString) -> Self {
Self {
inner: Box::new(AnyError::error(msg)),
}
}
fn has_backtrace(&self) -> bool {
anyerror::backtrace_str().is_some()
}
fn fmt_backtrace(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(bt) = anyerror::backtrace_str() {
write!(f, "{}", bt)
} else {
Ok(())
}
}
}