use crate::error::RegistryError;
pub const MAX_DEPTH: usize = 50;
pub const MAX_KEYS_PER_MAPPING: usize = 10_000;
pub const MAX_STRING_LENGTH: usize = 1_024 * 1_024;
pub const MAX_TOTAL_SIZE: usize = 10 * 1_024 * 1_024;
pub const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_992;
pub const MIN_SAFE_INTEGER: i64 = -9_007_199_254_740_992;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CanonicalizeError {
AnchorFound { position: String },
AliasFound { position: String },
TagFound { tag: String },
MultiDocumentFound,
DuplicateKey { key: String },
FloatNotAllowed { value: String },
IntegerOutOfRange { value: i64 },
MaxDepthExceeded { depth: usize },
MaxKeysExceeded { count: usize },
StringTooLong { length: usize },
InputTooLarge { size: usize },
ParseError { message: String },
SerializeError { message: String },
}
impl std::fmt::Display for CanonicalizeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AnchorFound { position } => write!(f, "YAML anchor found at {}", position),
Self::AliasFound { position } => write!(f, "YAML alias found at {}", position),
Self::TagFound { tag } => write!(f, "YAML tag not allowed: {}", tag),
Self::MultiDocumentFound => write!(f, "multi-document YAML not allowed"),
Self::DuplicateKey { key } => write!(f, "duplicate key: {}", key),
Self::FloatNotAllowed { value } => write!(f, "float values not allowed: {}", value),
Self::IntegerOutOfRange { value } => {
write!(f, "integer {} out of safe range (±2^53)", value)
}
Self::MaxDepthExceeded { depth } => {
write!(f, "nesting depth {} exceeds limit {}", depth, MAX_DEPTH)
}
Self::MaxKeysExceeded { count } => write!(
f,
"mapping has {} keys, exceeds limit {}",
count, MAX_KEYS_PER_MAPPING
),
Self::StringTooLong { length } => write!(
f,
"string length {} exceeds limit {}",
length, MAX_STRING_LENGTH
),
Self::InputTooLarge { size } => {
write!(f, "input size {} exceeds limit {}", size, MAX_TOTAL_SIZE)
}
Self::ParseError { message } => write!(f, "YAML parse error: {}", message),
Self::SerializeError { message } => write!(f, "JSON serialize error: {}", message),
}
}
}
impl std::error::Error for CanonicalizeError {}
pub type CanonicalizeResult<T> = Result<T, CanonicalizeError>;
impl From<CanonicalizeError> for RegistryError {
fn from(err: CanonicalizeError) -> Self {
RegistryError::InvalidResponse {
message: format!(
"canonicalization failed (pack invalid/unsupported): {}",
err
),
}
}
}