serdevault/
errors.rs

1use std::error::Error as StdError;
2use std::fmt;
3use std::fmt::Debug;
4use std::io;
5
6/// Custom error type for safe_serde operations
7#[derive(Debug)]
8pub enum SerdeVaultError {
9    IoError(io::Error),
10    SerializationError(String),
11    DeserializationError(String),
12    EncryptionError(String),
13    DecryptionError(String),
14    PasswordError(String),
15    RandomError(String), // Added for OsRng errors
16}
17
18// From implementations for error conversions
19impl From<io::Error> for SerdeVaultError {
20    fn from(error: io::Error) -> Self {
21        SerdeVaultError::IoError(error)
22    }
23}
24
25// Implement Display trait for SerdeVaultError
26impl fmt::Display for SerdeVaultError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            SerdeVaultError::IoError(e) => write!(f, "I/O error: {}", e),
30            SerdeVaultError::SerializationError(e) => write!(f, "Serialization error: {}", e),
31            SerdeVaultError::DeserializationError(e) => write!(f, "Deserialization error: {}", e),
32            SerdeVaultError::EncryptionError(e) => write!(f, "Encryption error: {}", e),
33            SerdeVaultError::DecryptionError(e) => write!(f, "Decryption error: {}", e),
34            SerdeVaultError::PasswordError(e) => write!(f, "Password error: {}", e),
35            SerdeVaultError::RandomError(_) => todo!(),
36        }
37    }
38}
39
40// Implement Error trait for SerdeVaultError
41impl StdError for SerdeVaultError {
42    fn source(&self) -> Option<&(dyn StdError + 'static)> {
43        match self {
44            SerdeVaultError::IoError(e) => Some(e),
45            _ => None,
46        }
47    }
48}