Skip to main content

cortexai_encryption/
error.rs

1//! Error types for encryption operations.
2
3use thiserror::Error;
4
5/// Errors that can occur during encryption operations.
6#[derive(Debug, Error)]
7pub enum CryptoError {
8    /// Encryption failed
9    #[error("encryption failed: {0}")]
10    EncryptionFailed(String),
11
12    /// Decryption failed
13    #[error("decryption failed: {0}")]
14    DecryptionFailed(String),
15
16    /// Invalid key length
17    #[error("invalid key length: expected {expected}, got {got}")]
18    InvalidKeyLength { expected: usize, got: usize },
19
20    /// Invalid nonce length
21    #[error("invalid nonce length: expected {expected}, got {got}")]
22    InvalidNonceLength { expected: usize, got: usize },
23
24    /// Key derivation failed
25    #[error("key derivation failed: {0}")]
26    KeyDerivationFailed(String),
27
28    /// Invalid ciphertext format
29    #[error("invalid ciphertext format: {0}")]
30    InvalidCiphertext(String),
31
32    /// Key not found for version
33    #[error("key not found for version {0}")]
34    KeyNotFound(u32),
35
36    /// Serialization error
37    #[error("serialization error: {0}")]
38    SerializationError(String),
39
40    /// Base64 decoding error
41    #[error("base64 decode error: {0}")]
42    Base64Error(String),
43}
44
45impl From<serde_json::Error> for CryptoError {
46    fn from(err: serde_json::Error) -> Self {
47        CryptoError::SerializationError(err.to_string())
48    }
49}
50
51impl From<base64::DecodeError> for CryptoError {
52    fn from(err: base64::DecodeError) -> Self {
53        CryptoError::Base64Error(err.to_string())
54    }
55}
56
57/// Result type for encryption operations.
58pub type CryptoResult<T> = Result<T, CryptoError>;