use std::fmt;
use typeid_prefix::prelude::*;
use typeid_suffix::prelude::*;
#[cfg(feature = "instrument")]
use tracing::{error, instrument};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MagicTypeIdError {
Prefix(ValidationError),
Suffix(DecodeError),
}
impl fmt::Display for MagicTypeIdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Prefix(err) => write!(f, "Prefix error: {err}"),
Self::Suffix(err) => write!(f, "Suffix error: {err}"),
}
}
}
impl std::error::Error for MagicTypeIdError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Prefix(err) => Some(err),
Self::Suffix(err) => Some(err),
}
}
}
impl From<ValidationError> for MagicTypeIdError {
#[cfg_attr(feature = "instrument", instrument(level = "error", fields(error = %err)))]
fn from(err: ValidationError) -> Self {
#[cfg(feature = "instrument")]
error!("Converting ValidationError to MagicTypeIdError: {}", err);
Self::Prefix(err)
}
}
impl From<DecodeError> for MagicTypeIdError {
#[cfg_attr(feature = "instrument", instrument(level = "error", fields(error = %err)))]
fn from(err: DecodeError) -> Self {
#[cfg(feature = "instrument")]
error!("Converting DecodeError to MagicTypeIdError: {}", err);
Self::Suffix(err)
}
}