use alloc::boxed::Box;
#[derive(Debug, thiserror::Error, Default)]
#[non_exhaustive]
pub enum ErrorKind {
#[default]
#[error("UnitError")]
UnitError,
#[error("TimeoutError")]
TimeoutError,
#[error("ProbablyNotRootCauseError")]
ProbablyNotRootCauseError,
#[error("{0}")]
StrError(&'static str),
#[error("{0}")]
StringError(alloc::string::String),
#[error("{0}")]
CowStrError(alloc::borrow::Cow<'static, str>),
#[error("{0}")]
BoxedError(Box<dyn std::error::Error + Send + Sync>),
#[error("{0}")]
TryFromIntError(core::num::TryFromIntError),
#[error("{0}")]
StdIoError(std::io::Error),
#[error("{0}")]
FromUtf8Error(alloc::string::FromUtf8Error),
#[error("{0}")]
FromUtf16Error(alloc::string::FromUtf16Error),
#[error("{0}")]
ParseIntError(core::num::ParseIntError),
#[error("{0}")]
ParseFloatError(core::num::ParseFloatError),
}
use ErrorKind::*;
impl ErrorKind {
pub fn from_err<E: std::error::Error + Send + Sync + 'static>(e: E) -> Self {
ErrorKind::BoxedError(Box::new(e))
}
pub fn from_box(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
ErrorKind::BoxedError(e)
}
pub fn downcast<E: std::error::Error + 'static>(self) -> Result<E, Self> {
if let BoxedError(boxed_err) = self {
match boxed_err.downcast() {
Ok(err) => Ok(*err),
Err(boxed_err) => Err(Self::BoxedError(boxed_err)),
}
} else {
Err(self)
}
}
}