Skip to main content

wecanencrypt/
error.rs

1//! Error types for the wecanencrypt library.
2//!
3//! This module provides a comprehensive error type that covers all possible
4//! failure modes in OpenPGP operations.
5
6use thiserror::Error;
7
8/// The main error type for wecanencrypt operations.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Cryptographic operation failed
12    #[error("Cryptographic operation failed: {0}")]
13    Crypto(String),
14
15    /// Certificate parsing failed
16    #[error("Certificate parsing failed: {0}")]
17    Parse(String),
18
19    /// Invalid password or unable to decrypt secret key
20    #[error("Invalid password or key")]
21    InvalidPassword,
22
23    /// Requested key was not found
24    #[error("Key not found: {0}")]
25    KeyNotFound(String),
26
27    /// Signature verification failed
28    #[error("Signature verification failed")]
29    VerificationFailed,
30
31    /// Attempted to merge identical keys
32    #[error("Cannot merge identical keys")]
33    SameKeyError,
34
35    /// File I/O error
36    #[error("I/O error: {0}")]
37    Io(#[from] std::io::Error),
38
39    /// Invalid input provided
40    #[error("Invalid input: {0}")]
41    InvalidInput(String),
42
43    /// Algorithm not supported
44    #[error("Unsupported algorithm: {0}")]
45    UnsupportedAlgorithm(String),
46
47    /// Key has expired
48    #[error("Key has expired")]
49    KeyExpired,
50
51    /// Key has been revoked
52    #[error("Key has been revoked")]
53    KeyRevoked,
54
55    /// No suitable encryption subkey found
56    #[error("No suitable encryption subkey found")]
57    NoEncryptionSubkey,
58
59    /// No suitable signing subkey found
60    #[error("No suitable signing subkey found")]
61    NoSigningSubkey,
62
63    /// No suitable authentication subkey found
64    #[error("No suitable authentication subkey found")]
65    NoAuthenticationSubkey,
66
67    /// Certificate does not contain secret key material
68    #[error("Certificate does not contain secret key material")]
69    NoSecretKey,
70
71    /// Armored data is malformed
72    #[error("Malformed armored data: {0}")]
73    MalformedArmor(String),
74
75    /// User ID not found in certificate
76    #[error("User ID not found: {0}")]
77    UidNotFound(String),
78
79    /// Database error (keystore feature)
80    #[cfg(feature = "keystore")]
81    #[error("Database error: {0}")]
82    Database(#[from] rusqlite::Error),
83
84    /// Network error (network feature)
85    #[error("Network error: {0}")]
86    Network(String),
87
88    /// rpgp OpenPGP error
89    #[error("OpenPGP error: {0}")]
90    OpenPgp(#[from] pgp::errors::Error),
91
92    /// Generic error from anyhow
93    #[error("Error: {0}")]
94    Generic(#[from] anyhow::Error),
95
96    /// Smart card error (card feature)
97    #[cfg(feature = "card")]
98    #[error("Smart card error: {0}")]
99    Card(#[from] crate::card::CardError),
100}
101
102/// A specialized Result type for wecanencrypt operations.
103pub type Result<T> = std::result::Result<T, Error>;
104
105impl From<String> for Error {
106    fn from(s: String) -> Self {
107        Error::Crypto(s)
108    }
109}
110
111impl From<&str> for Error {
112    fn from(s: &str) -> Self {
113        Error::Crypto(s.to_string())
114    }
115}