use crate::error_impl::ErrorInfoImpl;
use core::any::TypeId;
use core::fmt::{Debug, Formatter};
pub struct ErrorCodeInfo {
pub tid: TypeId,
pub value: u32,
pub type_name: &'static str,
pub variant_name: &'static str,
pub message: Option<&'static str>,
}
impl ErrorCodeInfo {
pub fn is_value<T: ErrorCodePrivate>(&self, val: T) -> bool {
self.tid == TypeId::of::<T>() && val.is_value(self.value)
}
pub fn decode_value<T: ErrorCodePrivate>(&self) -> Option<T> {
if self.tid == TypeId::of::<T>() {
Some(T::from_value(self.value))
} else {
None
}
}
}
impl Debug for ErrorCodeInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ErrorCodeInfo")
.field("variant", &format_args!("{}::{}", self.type_name, self.value))
.field("message", &self.message)
.finish()
}
}
pub trait ErrorCode: 'static + ErrorCodePrivate {}
pub trait ErrorCodePrivate: 'static {
type ConstHelper;
const CONST_HELPER_INSTANCE: Self::ConstHelper;
fn error_source(self) -> &'static ErrorInfoImpl;
fn is_value(self, value: u32) -> bool;
fn from_value(value: u32) -> Self;
}