1use core::fmt;
4use dcrypt_algorithms::error::Error as PrimitiveError;
5use dcrypt_api::error::Error as CoreError;
6
7#[derive(Debug)]
9pub enum Error {
10 Primitive(PrimitiveError),
12
13 KeyGeneration {
15 algorithm: &'static str,
16 details: &'static str,
17 },
18
19 Encapsulation {
20 algorithm: &'static str,
21 details: &'static str,
22 },
23
24 Decapsulation {
25 algorithm: &'static str,
26 details: &'static str,
27 },
28
29 InvalidKey {
31 key_type: &'static str,
32 reason: &'static str,
33 },
34
35 InvalidCiphertext {
37 algorithm: &'static str,
38 reason: &'static str,
39 },
40
41 Serialization {
43 context: &'static str,
44 details: &'static str,
45 },
46
47 #[cfg(feature = "std")]
49 Io(std::io::Error),
50}
51
52impl Clone for Error {
54 fn clone(&self) -> Self {
55 match self {
56 Error::Primitive(e) => Error::Primitive(e.clone()),
57 Error::KeyGeneration { algorithm, details } => {
58 Error::KeyGeneration { algorithm, details }
59 }
60 Error::Encapsulation { algorithm, details } => {
61 Error::Encapsulation { algorithm, details }
62 }
63 Error::Decapsulation { algorithm, details } => {
64 Error::Decapsulation { algorithm, details }
65 }
66 Error::InvalidKey { key_type, reason } => Error::InvalidKey { key_type, reason },
67 Error::InvalidCiphertext { algorithm, reason } => {
68 Error::InvalidCiphertext { algorithm, reason }
69 }
70 Error::Serialization { context, details } => Error::Serialization { context, details },
71 #[cfg(feature = "std")]
72 Error::Io(e) => Error::Io(std::io::Error::new(e.kind(), e.to_string())),
73 }
74 }
75}
76
77pub type Result<T> = core::result::Result<T, Error>;
79
80impl fmt::Display for Error {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 match self {
83 Error::Primitive(e) => write!(f, "Primitive error: {}", e),
84 Error::KeyGeneration { algorithm, details } => {
85 write!(f, "Key generation error for {}: {}", algorithm, details)
86 }
87 Error::Encapsulation { algorithm, details } => {
88 write!(f, "Encapsulation error for {}: {}", algorithm, details)
89 }
90 Error::Decapsulation { algorithm, details } => {
91 write!(f, "Decapsulation error for {}: {}", algorithm, details)
92 }
93 Error::InvalidKey { key_type, reason } => {
94 write!(f, "Invalid {} key: {}", key_type, reason)
95 }
96 Error::InvalidCiphertext { algorithm, reason } => {
97 write!(f, "Invalid {} ciphertext: {}", algorithm, reason)
98 }
99 Error::Serialization { context, details } => {
100 write!(f, "Serialization error in {}: {}", context, details)
101 }
102 #[cfg(feature = "std")]
103 Error::Io(e) => write!(f, "I/O error: {}", e),
104 }
105 }
106}
107
108#[cfg(feature = "std")]
110impl std::error::Error for Error {
111 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
112 match self {
113 Error::Primitive(e) => Some(e),
114 Error::Io(e) => Some(e),
115 _ => None,
116 }
117 }
118}
119
120impl From<PrimitiveError> for Error {
122 fn from(err: PrimitiveError) -> Self {
123 Error::Primitive(err)
124 }
125}
126
127#[cfg(feature = "std")]
129impl From<std::io::Error> for Error {
130 fn from(err: std::io::Error) -> Self {
131 Error::Io(err)
132 }
133}
134
135impl From<Error> for CoreError {
137 fn from(err: Error) -> Self {
138 match err {
139 Error::Primitive(e) => e.into(),
140 Error::KeyGeneration { algorithm, details } => {
141 #[cfg(not(feature = "std"))]
142 let _ = details;
143 CoreError::Other {
144 context: algorithm,
145 #[cfg(feature = "std")]
146 message: format!("key generation failed: {}", details),
147 }
148 }
149 Error::Encapsulation { algorithm, details } => {
150 #[cfg(not(feature = "std"))]
151 let _ = details;
152 CoreError::Other {
153 context: algorithm,
154 #[cfg(feature = "std")]
155 message: format!("encapsulation failed: {}", details),
156 }
157 }
158 Error::Decapsulation { algorithm, details } => {
159 #[cfg(not(feature = "std"))]
160 let _ = details;
161 CoreError::DecryptionFailed {
162 context: algorithm,
163 #[cfg(feature = "std")]
164 message: format!("decapsulation failed: {}", details),
165 }
166 }
167 Error::InvalidKey { key_type, reason } => {
168 #[cfg(not(feature = "std"))]
169 let _ = reason;
170 CoreError::InvalidKey {
171 context: key_type,
172 #[cfg(feature = "std")]
173 message: reason.to_string(),
174 }
175 }
176 Error::InvalidCiphertext { algorithm, reason } => {
177 #[cfg(not(feature = "std"))]
178 let _ = reason;
179 CoreError::InvalidCiphertext {
180 context: algorithm,
181 #[cfg(feature = "std")]
182 message: reason.to_string(),
183 }
184 }
185 Error::Serialization { context, details } => {
186 #[cfg(not(feature = "std"))]
187 let _ = details;
188 CoreError::SerializationError {
189 context,
190 #[cfg(feature = "std")]
191 message: details.to_string(),
192 }
193 }
194 #[cfg(feature = "std")]
195 Error::Io(e) => CoreError::Other {
196 context: "I/O operation",
197 message: e.to_string(),
198 },
199 }
200 }
201}
202
203pub mod validate;