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