1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug)]
10pub enum Error {
11 InvalidKey(String),
13
14 EncryptionFailed(String),
16
17 DecryptionFailed(String),
19
20 SigningFailed(String),
22
23 VerificationFailed(String),
25
26 KeyGenerationFailed(String),
28
29 SerializationError(String),
31
32 InvalidCiphertext(String),
34
35 InvalidSignature(String),
37
38 IoError(String),
40
41 UnsupportedVersion(u32),
43
44 Other(String),
46}
47
48impl fmt::Display for Error {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 Error::InvalidKey(msg) => write!(f, "Invalid key: {}", msg),
52 Error::EncryptionFailed(msg) => write!(f, "Encryption failed: {}", msg),
53 Error::DecryptionFailed(msg) => write!(f, "Decryption failed: {}", msg),
54 Error::SigningFailed(msg) => write!(f, "Signing failed: {}", msg),
55 Error::VerificationFailed(msg) => write!(f, "Verification failed: {}", msg),
56 Error::KeyGenerationFailed(msg) => write!(f, "Key generation failed: {}", msg),
57 Error::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
58 Error::InvalidCiphertext(msg) => write!(f, "Invalid ciphertext: {}", msg),
59 Error::InvalidSignature(msg) => write!(f, "Invalid signature: {}", msg),
60 Error::IoError(msg) => write!(f, "IO error: {}", msg),
61 Error::UnsupportedVersion(v) => write!(f, "Unsupported version: {}", v),
62 Error::Other(msg) => write!(f, "{}", msg),
63 }
64 }
65}
66
67impl std::error::Error for Error {}
68
69impl From<std::io::Error> for Error {
70 fn from(err: std::io::Error) -> Self {
71 Error::IoError(err.to_string())
72 }
73}
74
75impl From<serde_json::Error> for Error {
76 fn from(err: serde_json::Error) -> Self {
77 Error::SerializationError(err.to_string())
78 }
79}
80
81impl From<base64::DecodeError> for Error {
82 fn from(err: base64::DecodeError) -> Self {
83 Error::SerializationError(format!("Base64 decode error: {}", err))
84 }
85}
86
87impl From<rsa::Error> for Error {
88 fn from(err: rsa::Error) -> Self {
89 Error::Other(format!("RSA error: {}", err))
90 }
91}
92
93impl From<rsa::pkcs8::Error> for Error {
94 fn from(err: rsa::pkcs8::Error) -> Self {
95 Error::InvalidKey(format!("PKCS8 error: {}", err))
96 }
97}
98
99impl From<aes_gcm::Error> for Error {
100 fn from(err: aes_gcm::Error) -> Self {
101 Error::EncryptionFailed(format!("AES-GCM error: {:?}", err))
102 }
103}
104