rust_bottle/
errors.rs

1use thiserror::Error;
2
3/// Result type for Bottle operations.
4///
5/// This is a type alias for `std::result::Result<T, BottleError>`, providing
6/// a convenient way to return results from Bottle operations.
7pub type Result<T> = std::result::Result<T, BottleError>;
8
9/// Errors that can occur in Bottle operations.
10///
11/// This enum covers all error conditions that can arise when using the rust-bottle
12/// library, including cryptographic failures, key management issues, and
13/// serialization problems.
14///
15/// # Example
16///
17/// ```rust
18/// use rust_bottle::errors::{BottleError, Result};
19///
20/// fn example() -> Result<()> {
21///     // Operations that might fail
22///     Err(BottleError::KeyNotFound)
23/// }
24/// ```
25#[derive(Error, Debug, Clone, PartialEq)]
26pub enum BottleError {
27    #[error("No appropriate key available to decrypt")]
28    NoAppropriateKey,
29
30    #[error("Signature verification failed")]
31    VerifyFailed,
32
33    #[error("Key not found in keychain/IDCard")]
34    KeyNotFound,
35
36    #[error("Group not found in IDCard")]
37    GroupNotFound,
38
39    #[error("Key not authorized for the operation")]
40    KeyUnfit,
41
42    #[error("No valid recipient for encryption")]
43    EncryptNoRecipient,
44
45    #[error("Invalid key type")]
46    InvalidKeyType,
47
48    #[error("Serialization error: {0}")]
49    Serialization(String),
50
51    #[error("Deserialization error: {0}")]
52    Deserialization(String),
53
54    #[error("Encryption error: {0}")]
55    Encryption(String),
56
57    #[error("Decryption error: {0}")]
58    Decryption(String),
59
60    #[error("IO error: {0}")]
61    Io(String),
62
63    #[error("Invalid format")]
64    InvalidFormat,
65
66    #[error("Unsupported algorithm")]
67    UnsupportedAlgorithm,
68}
69
70impl From<std::io::Error> for BottleError {
71    fn from(err: std::io::Error) -> Self {
72        BottleError::Io(err.to_string())
73    }
74}