#[cfg(not(feature = "derive"))]
use core::fmt;
use crate::utils::urn::builders::spec::Pattern;
#[cfg(feature = "derive")]
use crate::Errorizable;
#[cfg_attr(feature = "derive", derive(Errorizable))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UrnValidationError {
#[cfg_attr(feature = "derive", error("Required field missing: {0}"))]
RequiredFieldMissing(&'static str),
#[cfg_attr(
feature = "derive",
error("Invalid format for field '{field}': expected pattern {pattern:?}")
)]
InvalidFormat { field: &'static str, pattern: Option<Pattern> },
#[cfg_attr(feature = "derive", error("Forbidden field present: {0}"))]
ForbiddenFieldPresent(&'static str),
#[cfg_attr(feature = "derive", error("NID does not match spec"))]
NidMismatch,
#[cfg_attr(feature = "derive", error("Invalid NID length: must be 2-32 characters"))]
InvalidNidLength,
#[cfg_attr(feature = "derive", error("Invalid NID: must start with a letter"))]
InvalidNidStart,
#[cfg_attr(
feature = "derive",
error("Invalid NID characters: must be alphanumeric and hyphens only")
)]
InvalidNidCharacters,
}
#[cfg(not(feature = "derive"))]
impl fmt::Display for UrnValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RequiredFieldMissing(field) => write!(f, "Required field missing: {field}"),
Self::InvalidFormat { field, pattern } => {
if let Some(p) = pattern {
write!(f, "Invalid format for field '{field}': expected pattern {}", p.pattern_str())
} else {
write!(f, "Invalid format for field '{field}'")
}
}
Self::ForbiddenFieldPresent(field) => write!(f, "Forbidden field present: {field}"),
Self::NidMismatch => write!(f, "NID does not match spec"),
Self::InvalidNidLength => write!(f, "Invalid NID length: must be 2-32 characters"),
Self::InvalidNidStart => write!(f, "Invalid NID: must start with a letter"),
Self::InvalidNidCharacters => {
write!(f, "Invalid NID characters: must be alphanumeric and hyphens only")
}
}
}
}
#[cfg(all(feature = "std", not(feature = "derive")))]
impl std::error::Error for UrnValidationError {}