Skip to main content

age_vault/
error.rs

1//! Error types for the vault.
2//!
3//! This module defines the [`Error`] enum covering all possible failures
4//! (I/O, database, crypto, serialization, etc.) and a [`Result`] type alias.
5
6use thiserror::Error;
7
8/// Main error type for the age-vault crate.
9///
10/// All fallible operations return a `Result<T, Error>`. Variants wrap errors
11/// from underlying dependencies or provide specific vault-related failures.
12///
13/// # Examples
14///
15/// ```
16/// use age_vault::Error;
17///
18/// fn might_fail() -> Result<(), Error> {
19///     Err(Error::AccountNotFound("alice".into()))
20/// }
21/// ```
22#[derive(Debug, Error)]
23pub enum Error {
24    /// I/O error (e.g., file not found, permission denied).
25    #[error("IO error: {0}")]
26    Io(#[from] std::io::Error),
27
28    /// Database error from neuxdb.
29    #[error("Database error: {0}")]
30    Db(#[from] neuxdb::Error),
31
32    /// Configuration error from neuxcfg.
33    #[error("Configuration error: {0}")]
34    Config(#[from] neuxcfg::NeuxcfgError),
35
36    /// Cryptographic error from age_crypto.
37    #[error("Crypto error: {0}")]
38    Crypto(#[from] age_crypto::Error),
39
40    /// Key generation error from age_setup.
41    #[error("Key generation error: {0}")]
42    KeyGen(#[from] age_setup::Error),
43
44    /// Key derivation function error (Argon2).
45    #[error("KDF error: {0}")]
46    Kdf(String),
47
48    /// Serialization/deserialization error.
49    #[error("Serialization error: {0}")]
50    Serde(#[from] serde_json::Error),
51
52    /// Requested account does not exist.
53    #[error("Account not found: {0}")]
54    AccountNotFound(String),
55
56    /// Account with the same name already exists.
57    #[error("Account already exists: {0}")]
58    AccountExists(String),
59
60    /// The provided master password is incorrect.
61    #[error("Invalid master password")]
62    InvalidMasterPassword,
63
64    /// Decryption operation failed.
65    #[error("Decryption failed: {0}")]
66    DecryptionFailed(String),
67
68    /// Encryption operation failed.
69    #[error("Encryption failed: {0}")]
70    EncryptionFailed(String),
71}
72
73/// Convenient result type alias using [`Error`].
74///
75/// This is the standard result type used throughout the crate.
76pub type Result<T> = std::result::Result<T, Error>;
77
78impl From<argon2::password_hash::Error> for Error {
79    fn from(err: argon2::password_hash::Error) -> Self {
80        Error::Kdf(err.to_string())
81    }
82}