use std::fmt;
#[cfg(feature = "instrument")]
use tracing::error;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeError {
InvalidSuffix(InvalidSuffixReason),
InvalidUuid(InvalidUuidReason),
InvalidNamespace(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidSuffixReason {
InvalidLength,
NonAsciiCharacter,
InvalidFirstCharacter,
InvalidCharacter,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidUuidReason {
InvalidVersion,
InvalidVariant,
InvalidBytes,
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self {
Self::InvalidSuffix(reason) => format!("Invalid `TypeID` suffix: {reason}"),
Self::InvalidUuid(reason) => format!("Invalid UUID: {reason}"),
Self::InvalidNamespace(s) => format!(
"invalid namespace UUID '{s}': expected format like '6ba7b810-9dad-11d1-80b4-00c04fd430c8'"
),
};
#[cfg(feature = "instrument")]
error!("{msg}");
write!(f, "{msg}")
}
}
impl fmt::Display for InvalidSuffixReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self {
Self::InvalidLength => "Suffix must be exactly 26 characters long",
Self::NonAsciiCharacter => "Suffix contains non-ASCII characters",
Self::InvalidFirstCharacter => "First character of suffix must be '7' or less",
Self::InvalidCharacter => "Suffix contains characters not in the base32 alphabet",
};
#[cfg(feature = "instrument")]
error!("{}", msg);
write!(f, "{msg}")
}
}
impl fmt::Display for InvalidUuidReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self {
Self::InvalidVersion => "UUID version is not valid for this TypeID",
Self::InvalidVariant => "UUID variant is not RFC4122",
Self::InvalidBytes => "UUID bytes are invalid",
};
#[cfg(feature = "instrument")]
error!("{msg}");
write!(f, "{msg}")
}
}
impl std::error::Error for DecodeError {}