Skip to main content

assay_registry/canonicalize/
errors.rs

1//! Canonicalization error types and limits.
2
3use crate::error::RegistryError;
4
5/// Maximum nesting depth for YAML structures.
6pub const MAX_DEPTH: usize = 50;
7
8/// Maximum number of keys in a single mapping.
9pub const MAX_KEYS_PER_MAPPING: usize = 10_000;
10
11/// Maximum string length (1MB).
12pub const MAX_STRING_LENGTH: usize = 1_024 * 1_024;
13
14/// Maximum total YAML size (10MB).
15pub const MAX_TOTAL_SIZE: usize = 10 * 1_024 * 1_024;
16
17/// Maximum safe integer value (2^53 for JSON compatibility).
18pub const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_992; // 2^53
19
20/// Minimum safe integer value (-2^53 for JSON compatibility).
21pub const MIN_SAFE_INTEGER: i64 = -9_007_199_254_740_992; // -2^53
22
23/// Errors specific to canonicalization.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub enum CanonicalizeError {
26    /// YAML contains anchors (forbidden).
27    AnchorFound { position: String },
28
29    /// YAML contains aliases (forbidden).
30    AliasFound { position: String },
31
32    /// YAML contains tags (forbidden).
33    TagFound { tag: String },
34
35    /// YAML contains multiple documents (forbidden).
36    MultiDocumentFound,
37
38    /// YAML contains duplicate keys (forbidden).
39    DuplicateKey { key: String },
40
41    /// YAML contains float values (forbidden).
42    FloatNotAllowed { value: String },
43
44    /// Integer outside safe range.
45    IntegerOutOfRange { value: i64 },
46
47    /// Nesting too deep.
48    MaxDepthExceeded { depth: usize },
49
50    /// Too many keys in mapping.
51    MaxKeysExceeded { count: usize },
52
53    /// String too long.
54    StringTooLong { length: usize },
55
56    /// Input too large.
57    InputTooLarge { size: usize },
58
59    /// YAML parse error.
60    ParseError { message: String },
61
62    /// JSON serialization error.
63    SerializeError { message: String },
64}
65
66impl std::fmt::Display for CanonicalizeError {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        match self {
69            Self::AnchorFound { position } => write!(f, "YAML anchor found at {}", position),
70            Self::AliasFound { position } => write!(f, "YAML alias found at {}", position),
71            Self::TagFound { tag } => write!(f, "YAML tag not allowed: {}", tag),
72            Self::MultiDocumentFound => write!(f, "multi-document YAML not allowed"),
73            Self::DuplicateKey { key } => write!(f, "duplicate key: {}", key),
74            Self::FloatNotAllowed { value } => write!(f, "float values not allowed: {}", value),
75            Self::IntegerOutOfRange { value } => {
76                write!(f, "integer {} out of safe range (±2^53)", value)
77            }
78            Self::MaxDepthExceeded { depth } => {
79                write!(f, "nesting depth {} exceeds limit {}", depth, MAX_DEPTH)
80            }
81            Self::MaxKeysExceeded { count } => write!(
82                f,
83                "mapping has {} keys, exceeds limit {}",
84                count, MAX_KEYS_PER_MAPPING
85            ),
86            Self::StringTooLong { length } => write!(
87                f,
88                "string length {} exceeds limit {}",
89                length, MAX_STRING_LENGTH
90            ),
91            Self::InputTooLarge { size } => {
92                write!(f, "input size {} exceeds limit {}", size, MAX_TOTAL_SIZE)
93            }
94            Self::ParseError { message } => write!(f, "YAML parse error: {}", message),
95            Self::SerializeError { message } => write!(f, "JSON serialize error: {}", message),
96        }
97    }
98}
99
100impl std::error::Error for CanonicalizeError {}
101
102/// Result type for canonicalization operations.
103pub type CanonicalizeResult<T> = Result<T, CanonicalizeError>;
104
105impl From<CanonicalizeError> for RegistryError {
106    fn from(err: CanonicalizeError) -> Self {
107        RegistryError::InvalidResponse {
108            message: format!(
109                "canonicalization failed (pack invalid/unsupported): {}",
110                err
111            ),
112        }
113    }
114}