Skip to main content

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    /// Unsupported fused preset identifier
24    #[error("Unsupported fused preset: {0}")]
25    UnsupportedPreset(u8),
26
27    /// Data shorter than expected
28    #[error("Truncated payload: expected at least {expected} bytes, got {actual}")]
29    TruncatedPayload {
30        /// Expected minimum size in bytes
31        expected: usize,
32        /// Actual size received
33        actual: usize,
34    },
35
36    /// Authentication tag verification failed
37    #[error("Authentication failed: invalid tag")]
38    AuthenticationFailed,
39
40    /// Encryption operation failed
41    #[error("Encryption failed: {0}")]
42    EncryptionFailed(String),
43
44    /// Decryption operation failed
45    #[error("Decryption failed: {0}")]
46    DecryptionFailed(String),
47
48    /// Decompression failed
49    #[error("Decompression failed: {0}")]
50    DecompressionFailed(String),
51
52    /// Compression failed
53    #[error("Compression failed: {0}")]
54    CompressionFailed(String),
55
56    /// Invalid UTF-8 sequence
57    #[error("Invalid UTF-8: {0}")]
58    InvalidUtf8(String),
59
60    /// Invalid Base64 encoding
61    #[error("Invalid Base64: {0}")]
62    InvalidBase64(String),
63
64    /// Invalid hex encoding
65    #[error("Invalid hex: {0}")]
66    InvalidHex(String),
67
68    /// Size mismatch after decompression
69    #[error("Size mismatch: expected {expected}, got {actual}")]
70    SizeMismatch {
71        /// Expected size in bytes
72        expected: usize,
73        /// Actual size in bytes
74        actual: usize,
75    },
76
77    /// Required field missing in JSON
78    #[error("Missing field: {0}")]
79    MissingField(String),
80
81    /// Invalid key format
82    #[error("Invalid key format: {0}")]
83    InvalidKeyFormat(String),
84
85    /// Invalid key length
86    #[error("Invalid key length: expected {expected}, got {actual}")]
87    InvalidKeyLength {
88        /// Expected key length in bytes
89        expected: usize,
90        /// Actual key length in bytes
91        actual: usize,
92    },
93
94    /// Invalid nonce length
95    #[error("Invalid nonce length: expected {expected}, got {actual}")]
96    InvalidNonceLength {
97        /// Expected nonce length in bytes
98        expected: usize,
99        /// Actual nonce length in bytes
100        actual: usize,
101    },
102
103    /// Invalid runtime configuration
104    #[error("Invalid configuration: {0}")]
105    InvalidConfiguration(String),
106
107    /// Signing operation failed
108    #[error("Signing failed: {0}")]
109    SigningFailed(String),
110
111    /// Signature verification failed
112    #[error("Signature verification failed")]
113    SignatureVerificationFailed,
114
115    /// Key generation failed
116    #[error("Key generation failed: {0}")]
117    KeyGenerationFailed(String),
118
119    /// Key derivation failed
120    #[error("Key derivation failed: {0}")]
121    KeyDerivationFailed(String),
122
123    /// Hash operation failed
124    #[error("Hash operation failed: {0}")]
125    HashFailed(String),
126
127    /// Payload too large
128    #[error("Payload too large: {size} bytes exceeds limit of {limit} bytes")]
129    PayloadTooLarge {
130        /// Actual payload size in bytes
131        size: usize,
132        /// Maximum allowed size in bytes
133        limit: usize,
134    },
135
136    /// Random number generation failed
137    #[error("Random generation failed: {0}")]
138    RandomFailed(String),
139
140    /// Serialization error
141    #[error("Serialization error: {0}")]
142    SerializationError(String),
143
144    /// Deserialization error
145    #[error("Deserialization error: {0}")]
146    DeserializationError(String),
147}
148
149impl From<base64::DecodeError> for Error {
150    fn from(e: base64::DecodeError) -> Self {
151        Error::InvalidBase64(e.to_string())
152    }
153}
154
155impl From<core::str::Utf8Error> for Error {
156    fn from(e: core::str::Utf8Error) -> Self {
157        Error::InvalidUtf8(e.to_string())
158    }
159}
160
161impl From<serde_json::Error> for Error {
162    fn from(e: serde_json::Error) -> Self {
163        Error::DeserializationError(e.to_string())
164    }
165}