use std::error::Error;
use std::fmt;
pub type AnyErr = Box<dyn Error + Send + Sync + 'static>;
pub struct StringError(String);
impl fmt::Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl fmt::Debug for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for StringError {}
pub trait IntoAnyErr<T> {
fn any(self) -> Result<T, AnyErr>;
}
impl<T, E> IntoAnyErr<T> for Result<T, E>
where
E: std::fmt::Display,
{
#[inline]
fn any(self) -> Result<T, AnyErr> {
self.map_err(|e| {
let string_error = e.to_string();
let generic_err = StringError(string_error);
Box::new(generic_err) as AnyErr
})
}
}