1use std::fmt::Display;
4use std::{any, fmt};
5
6pub type Result<T, E = DsntkError> = std::result::Result<T, E>;
8
9pub trait ToErrorMessage {
11 fn message(self) -> String;
13}
14
15#[derive(Debug, PartialEq, Eq)]
17pub struct DsntkError(String);
18
19impl Display for DsntkError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 write!(f, "{}", self.0)
23 }
24}
25
26impl DsntkError {
27 pub fn new(source: &str, message: &str) -> Self {
29 Self(format!("<{source}> {message}"))
30 }
31}
32
33impl<T> From<T> for DsntkError
34where
35 T: ToErrorMessage,
36{
37 fn from(value: T) -> Self {
39 let error_type_name = any::type_name::<T>().split("::").last().unwrap_or("UnknownError");
40 DsntkError::new(error_type_name, &value.message())
41 }
42}