1#![cfg_attr(not(feature = "std"), no_std)]
4
5#[cfg(feature = "std")]
6use std::string::String;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Error {
11 InvalidKey {
13 context: &'static str,
14 #[cfg(feature = "std")]
15 message: String,
16 },
17
18 InvalidSignature {
20 context: &'static str,
21 #[cfg(feature = "std")]
22 message: String,
23 },
24
25 DecryptionFailed {
27 context: &'static str,
28 #[cfg(feature = "std")]
29 message: String,
30 },
31
32 InvalidCiphertext {
34 context: &'static str,
35 #[cfg(feature = "std")]
36 message: String,
37 },
38
39 InvalidLength {
41 context: &'static str,
42 expected: usize,
43 actual: usize,
44 },
45
46 InvalidParameter {
48 context: &'static str,
49 #[cfg(feature = "std")]
50 message: String,
51 },
52
53 SerializationError {
55 context: &'static str,
56 #[cfg(feature = "std")]
57 message: String,
58 },
59
60 RandomGenerationError {
62 context: &'static str,
63 #[cfg(feature = "std")]
64 message: String,
65 },
66
67 NotImplemented { feature: &'static str },
69
70 AuthenticationFailed {
72 context: &'static str,
73 #[cfg(feature = "std")]
74 message: String,
75 },
76
77 Other {
79 context: &'static str,
80 #[cfg(feature = "std")]
81 message: String,
82 },
83}
84
85pub type Result<T> = core::result::Result<T, Error>;
87
88impl Error {
89 pub fn with_context(self, context: &'static str) -> Self {
91 match self {
92 Self::InvalidKey { .. } => Self::InvalidKey {
93 context,
94 #[cfg(feature = "std")]
95 message: String::new(),
96 },
97 Self::InvalidSignature { .. } => Self::InvalidSignature {
98 context,
99 #[cfg(feature = "std")]
100 message: String::new(),
101 },
102 Self::DecryptionFailed { .. } => Self::DecryptionFailed {
103 context,
104 #[cfg(feature = "std")]
105 message: String::new(),
106 },
107 Self::InvalidCiphertext { .. } => Self::InvalidCiphertext {
108 context,
109 #[cfg(feature = "std")]
110 message: String::new(),
111 },
112 Self::InvalidLength {
113 expected, actual, ..
114 } => Self::InvalidLength {
115 context,
116 expected,
117 actual,
118 },
119 Self::InvalidParameter { .. } => Self::InvalidParameter {
120 context,
121 #[cfg(feature = "std")]
122 message: String::new(),
123 },
124 Self::SerializationError { .. } => Self::SerializationError {
125 context,
126 #[cfg(feature = "std")]
127 message: String::new(),
128 },
129 Self::RandomGenerationError { .. } => Self::RandomGenerationError {
130 context,
131 #[cfg(feature = "std")]
132 message: String::new(),
133 },
134 Self::NotImplemented { feature } => Self::NotImplemented { feature },
135 Self::AuthenticationFailed { .. } => Self::AuthenticationFailed {
136 context,
137 #[cfg(feature = "std")]
138 message: String::new(),
139 },
140 Self::Other { .. } => Self::Other {
141 context,
142 #[cfg(feature = "std")]
143 message: String::new(),
144 },
145 }
146 }
147
148 #[cfg(feature = "std")]
150 pub fn with_message(self, message: impl Into<String>) -> Self {
151 let message = message.into();
152 match self {
153 Self::InvalidKey { context, .. } => Self::InvalidKey { context, message },
154 Self::InvalidSignature { context, .. } => Self::InvalidSignature { context, message },
155 Self::DecryptionFailed { context, .. } => Self::DecryptionFailed { context, message },
156 Self::InvalidCiphertext { context, .. } => Self::InvalidCiphertext { context, message },
157 Self::InvalidLength {
158 context,
159 expected,
160 actual,
161 } => Self::InvalidLength {
162 context,
163 expected,
164 actual,
165 },
166 Self::InvalidParameter { context, .. } => Self::InvalidParameter { context, message },
167 Self::SerializationError { context, .. } => {
168 Self::SerializationError { context, message }
169 }
170 Self::RandomGenerationError { context, .. } => {
171 Self::RandomGenerationError { context, message }
172 }
173 Self::NotImplemented { feature } => Self::NotImplemented { feature },
174 Self::AuthenticationFailed { context, .. } => {
175 Self::AuthenticationFailed { context, message }
176 }
177 Self::Other { context, .. } => Self::Other { context, message },
178 }
179 }
180}
181
182impl core::fmt::Display for Error {
183 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
184 match self {
185 Self::InvalidKey { context, .. } => {
186 write!(f, "Invalid key: {}", context)
187 }
188 Self::InvalidSignature { context, .. } => {
189 write!(f, "Invalid signature: {}", context)
190 }
191 Self::DecryptionFailed { context, .. } => {
192 write!(f, "Decryption failed: {}", context)
193 }
194 Self::InvalidCiphertext { context, .. } => {
195 write!(f, "Invalid ciphertext: {}", context)
196 }
197 Self::InvalidLength {
198 context,
199 expected,
200 actual,
201 } => {
202 write!(
203 f,
204 "{}: invalid length (expected {}, got {})",
205 context, expected, actual
206 )
207 }
208 #[cfg(feature = "std")]
209 Self::InvalidParameter { context, message } => {
210 write!(f, "{}: {}", context, message)
211 }
212 #[cfg(not(feature = "std"))]
213 Self::InvalidParameter { context } => {
214 write!(f, "Invalid parameter: {}", context)
215 }
216 #[cfg(feature = "std")]
217 Self::SerializationError { context, message } => {
218 write!(f, "Serialization error: {}: {}", context, message)
219 }
220 #[cfg(not(feature = "std"))]
221 Self::SerializationError { context } => {
222 write!(f, "Serialization error: {}", context)
223 }
224 #[cfg(feature = "std")]
225 Self::RandomGenerationError { context, message } => {
226 write!(f, "Random generation error: {}: {}", context, message)
227 }
228 #[cfg(not(feature = "std"))]
229 Self::RandomGenerationError { context } => {
230 write!(f, "Random generation error: {}", context)
231 }
232 Self::NotImplemented { feature } => {
233 write!(f, "{} is not implemented", feature)
234 }
235 #[cfg(feature = "std")]
236 Self::AuthenticationFailed { context, message } => {
237 write!(f, "Authentication failed: {}: {}", context, message)
238 }
239 #[cfg(not(feature = "std"))]
240 Self::AuthenticationFailed { context } => {
241 write!(f, "Authentication failed: {}", context)
242 }
243 #[cfg(feature = "std")]
244 Self::Other { context, message } => {
245 write!(f, "{}: {}", context, message)
246 }
247 #[cfg(not(feature = "std"))]
248 Self::Other { context } => {
249 write!(f, "Error: {}", context)
250 }
251 }
252 }
253}