use thiserror::Error;
#[derive(Debug, Error)]
pub enum EncryptionError {
#[error("Encryption failed: {0}")]
EncryptFailed(String),
#[error("Decryption failed: {0}")]
DecryptFailed(String),
#[error("Nonce generation failed: {0}")]
NonceError(String),
#[error("Invalid ciphertext: expected at least {expected} bytes, got {actual}")]
InvalidCiphertext {
expected: usize,
actual: usize,
},
#[error("Invalid WAL entry: expected at least {expected} bytes, got {actual}")]
InvalidWalEntry {
expected: usize,
actual: usize,
},
}
#[derive(Debug, Error)]
pub enum KeyProviderError {
#[error("Key not found")]
KeyNotFound,
#[error("Access denied: {0}")]
AccessDenied(String),
#[error("Provider unavailable: {0}")]
Unavailable(String),
#[error("Invalid key format: {0}")]
InvalidKeyFormat(String),
#[error("Key rotation not supported by this provider")]
RotationNotSupported,
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Provider error: {0}")]
Provider(#[source] Box<dyn std::error::Error + Send + Sync>),
}
#[derive(Debug, Error)]
pub enum KeyDerivationError {
#[error("Key derivation failed: {0}")]
DerivationFailed(String),
#[error("Invalid master key length: expected 32 bytes, got {0}")]
InvalidKeyLength(usize),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encryption_error_display() {
let err = EncryptionError::EncryptFailed("bad data".into());
assert!(err.to_string().contains("bad data"));
}
#[test]
fn encryption_error_invalid_ciphertext() {
let err = EncryptionError::InvalidCiphertext {
expected: 28,
actual: 10,
};
assert!(err.to_string().contains("28"));
assert!(err.to_string().contains("10"));
}
#[test]
fn key_provider_error_display() {
let err = KeyProviderError::KeyNotFound;
assert_eq!(err.to_string(), "Key not found");
}
#[test]
fn key_provider_error_io() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file gone");
let err = KeyProviderError::from(io_err);
assert!(err.to_string().contains("file gone"));
}
#[test]
fn key_derivation_error_display() {
let err = KeyDerivationError::InvalidKeyLength(16);
assert!(err.to_string().contains("16"));
}
}