Skip to main content

sal_vault/kvs/
error.rs

1//! Error types for the key-value store.
2
3use thiserror::Error;
4
5/// Errors that can occur when using the key-value store.
6#[derive(Debug, Error)]
7pub enum KvsError {
8    /// I/O error
9    #[error("I/O error: {0}")]
10    Io(#[from] std::io::Error),
11
12    /// Key not found
13    #[error("Key not found: {0}")]
14    KeyNotFound(String),
15
16    /// Store not found
17    #[error("Store not found: {0}")]
18    StoreNotFound(String),
19
20    /// Serialization error
21    #[error("Serialization error: {0}")]
22    Serialization(String),
23
24    /// Deserialization error
25    #[error("Deserialization error: {0}")]
26    Deserialization(String),
27
28    /// Encryption error
29    #[error("Encryption error: {0}")]
30    Encryption(String),
31
32    /// Decryption error
33    #[error("Decryption error: {0}")]
34    Decryption(String),
35
36    /// Other error
37    #[error("Error: {0}")]
38    Other(String),
39}
40
41impl From<serde_json::Error> for KvsError {
42    fn from(err: serde_json::Error) -> Self {
43        KvsError::Serialization(err.to_string())
44    }
45}
46
47impl From<KvsError> for crate::error::CryptoError {
48    fn from(err: KvsError) -> Self {
49        crate::error::CryptoError::SerializationError(err.to_string())
50    }
51}
52
53impl From<crate::error::CryptoError> for KvsError {
54    fn from(err: crate::error::CryptoError) -> Self {
55        match err {
56            crate::error::CryptoError::EncryptionFailed(msg) => KvsError::Encryption(msg),
57            crate::error::CryptoError::DecryptionFailed(msg) => KvsError::Decryption(msg),
58            crate::error::CryptoError::SerializationError(msg) => KvsError::Serialization(msg),
59            _ => KvsError::Other(err.to_string()),
60        }
61    }
62}
63
64/// Result type for key-value store operations.
65pub type Result<T> = std::result::Result<T, KvsError>;