#[derive(Debug)]
pub(crate) struct Inner(pub(crate) Box<dyn std::error::Error + Send + Sync>);
pub struct Error(pub(crate) Inner);
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &*self.0 .0)
}
}
impl<E: std::error::Error + Send + Sync + 'static> From<E> for Error {
fn from(e: E) -> Self {
Self(Inner(Box::new(e)))
}
}
impl Error {
pub fn new<E: std::error::Error + Send + Sync + 'static>(error: E) -> Self {
Self::from(error)
}
pub fn source(&self) -> &(dyn std::error::Error + 'static) {
&*self.0 .0
}
pub fn msg(message: impl Into<String>) -> Self {
Self::new(MessageError(message.into()))
}
pub fn downcast<E: std::error::Error + 'static>(&self) -> Option<&E> {
self.0 .0.downcast_ref::<E>()
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &*self.0 .0)
}
}
#[derive(Debug)]
struct MessageError(String);
impl std::fmt::Display for MessageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for MessageError {}