crate::cfg_feature_alloc! {
#[macro_export]
macro_rules! impl_error {
($ErrorName:ident, $ErrorType:ident) => {
extern crate alloc;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct $ErrorName {
error_type: $ErrorType,
msg: alloc::string::String,
}
impl core::fmt::Display for $ErrorName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("{:?}: {}", self.error_type, self.msg))
}
}
#[cfg(feature = "std")]
impl std::error::Error for $ErrorName {}
};
}
}
#[macro_export]
macro_rules! impl_from_error {
($ErrorName:ident,$source:ty,$ty:expr) => {
impl From<$source> for $ErrorName {
fn from(value: $source) -> Self {
Self {
error_type: $ty,
msg: value.to_string(),
}
}
}
};
}
#[macro_export]
macro_rules! impl_err_fn {
($ErrorName:ident, $ty:expr, $name:ident, $name_err:ident) => {
impl $ErrorName {
pub fn $name(msg: String) -> Self {
Self {
error_type: $ty,
msg,
}
}
pub fn $name_err<T>(msg: String) -> Result<T, Self> {
Err(Self::$name(msg))
}
}
};
}