voided_core/
error.rs

1//! Error types for voided-core
2
3use thiserror::Error;
4
5/// Result type alias for voided-core operations
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Errors that can occur during cryptographic operations
9#[derive(Error, Debug, Clone, PartialEq, Eq)]
10pub enum Error {
11    /// Invalid format - magic bytes mismatch
12    #[error("Invalid format: expected magic bytes")]
13    InvalidFormat,
14
15    /// Unsupported version number
16    #[error("Unsupported version: {0}")]
17    UnsupportedVersion(u8),
18
19    /// Unsupported algorithm identifier
20    #[error("Unsupported algorithm: {0}")]
21    UnsupportedAlgorithm(u8),
22
23    /// Data shorter than expected
24    #[error("Truncated payload: expected at least {expected} bytes, got {actual}")]
25    TruncatedPayload {
26        /// Expected minimum size in bytes
27        expected: usize,
28        /// Actual size received
29        actual: usize,
30    },
31
32    /// Authentication tag verification failed
33    #[error("Authentication failed: invalid tag")]
34    AuthenticationFailed,
35
36    /// Encryption operation failed
37    #[error("Encryption failed: {0}")]
38    EncryptionFailed(String),
39
40    /// Decryption operation failed
41    #[error("Decryption failed: {0}")]
42    DecryptionFailed(String),
43
44    /// Decompression failed
45    #[error("Decompression failed: {0}")]
46    DecompressionFailed(String),
47
48    /// Compression failed
49    #[error("Compression failed: {0}")]
50    CompressionFailed(String),
51
52    /// Invalid UTF-8 sequence
53    #[error("Invalid UTF-8: {0}")]
54    InvalidUtf8(String),
55
56    /// Invalid Base64 encoding
57    #[error("Invalid Base64: {0}")]
58    InvalidBase64(String),
59
60    /// Invalid hex encoding
61    #[error("Invalid hex: {0}")]
62    InvalidHex(String),
63
64    /// Size mismatch after decompression
65    #[error("Size mismatch: expected {expected}, got {actual}")]
66    SizeMismatch {
67        /// Expected size in bytes
68        expected: usize,
69        /// Actual size in bytes
70        actual: usize,
71    },
72
73    /// Required field missing in JSON
74    #[error("Missing field: {0}")]
75    MissingField(String),
76
77    /// Invalid key format
78    #[error("Invalid key format: {0}")]
79    InvalidKeyFormat(String),
80
81    /// Invalid key length
82    #[error("Invalid key length: expected {expected}, got {actual}")]
83    InvalidKeyLength {
84        /// Expected key length in bytes
85        expected: usize,
86        /// Actual key length in bytes
87        actual: usize,
88    },
89
90    /// Invalid nonce length
91    #[error("Invalid nonce length: expected {expected}, got {actual}")]
92    InvalidNonceLength {
93        /// Expected nonce length in bytes
94        expected: usize,
95        /// Actual nonce length in bytes
96        actual: usize,
97    },
98
99    /// Signing operation failed
100    #[error("Signing failed: {0}")]
101    SigningFailed(String),
102
103    /// Signature verification failed
104    #[error("Signature verification failed")]
105    SignatureVerificationFailed,
106
107    /// Key generation failed
108    #[error("Key generation failed: {0}")]
109    KeyGenerationFailed(String),
110
111    /// Key derivation failed
112    #[error("Key derivation failed: {0}")]
113    KeyDerivationFailed(String),
114
115    /// Hash operation failed
116    #[error("Hash operation failed: {0}")]
117    HashFailed(String),
118
119    /// Obfuscation operation failed
120    #[error("Obfuscation failed: {0}")]
121    ObfuscationFailed(String),
122
123    /// Deobfuscation operation failed
124    #[error("Deobfuscation failed: {0}")]
125    DeobfuscationFailed(String),
126
127    /// Map generation failed
128    #[error("Map generation failed: {0}")]
129    MapGenerationFailed(String),
130
131    /// Payload too large
132    #[error("Payload too large: {size} bytes exceeds limit of {limit} bytes")]
133    PayloadTooLarge {
134        /// Actual payload size in bytes
135        size: usize,
136        /// Maximum allowed size in bytes
137        limit: usize,
138    },
139
140    /// Random number generation failed
141    #[error("Random generation failed: {0}")]
142    RandomFailed(String),
143
144    /// Serialization error
145    #[error("Serialization error: {0}")]
146    SerializationError(String),
147
148    /// Deserialization error
149    #[error("Deserialization error: {0}")]
150    DeserializationError(String),
151}
152
153impl From<base64::DecodeError> for Error {
154    fn from(e: base64::DecodeError) -> Self {
155        Error::InvalidBase64(e.to_string())
156    }
157}
158
159impl From<core::str::Utf8Error> for Error {
160    fn from(e: core::str::Utf8Error) -> Self {
161        Error::InvalidUtf8(e.to_string())
162    }
163}
164
165impl From<serde_json::Error> for Error {
166    fn from(e: serde_json::Error) -> Self {
167        Error::DeserializationError(e.to_string())
168    }
169}
170