use std::fmt;
#[derive(Debug, Clone)]
pub struct CallbackError {
message: String,
callback_type: Option<&'static str>,
}
impl CallbackError {
#[inline]
pub fn from_display<T: fmt::Display>(error: T) -> Self {
Self {
message: error.to_string(),
callback_type: None,
}
}
#[inline]
pub fn with_type<T: fmt::Display>(source_type: &'static str, error: T) -> Self {
Self {
message: error.to_string(),
callback_type: Some(source_type),
}
}
#[inline]
pub fn message(&self) -> &str {
&self.message
}
#[inline]
pub fn callback_type(&self) -> Option<&'static str> {
self.callback_type
}
#[inline]
pub fn is_typed(&self) -> bool {
self.callback_type.is_some()
}
}
impl fmt::Display for CallbackError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.callback_type {
Some(callback_type) => write!(f, "{}: {}", callback_type, self.message),
None => write!(f, "{}", self.message),
}
}
}