pub type Result<T, E = DsntkError> = std::result::Result<T, E>;
pub trait ToErrorMessage {
fn message(self) -> String;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DsntkError(String);
impl std::fmt::Display for DsntkError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl DsntkError {
pub fn new(source: &str, message: &str) -> Self {
Self(format!("<{source}> {message}"))
}
}
impl<T> From<T> for DsntkError
where
T: ToErrorMessage,
{
fn from(value: T) -> Self {
let error_type_name = std::any::type_name::<T>().split("::").last().unwrap_or("UnknownError");
DsntkError::new(error_type_name, &value.message())
}
}