Skip to main content

beaconcrypt/
error.rs

1// SPDX-License-Identifier: 0BSD
2
3use core::{error::Error, fmt};
4
5#[derive(Debug, Clone)]
6pub struct KeyGenError;
7
8impl fmt::Display for KeyGenError {
9	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10		write!(f, "Key generation failure")
11	}
12}
13
14impl Error for KeyGenError {}
15
16#[derive(Debug, Clone)]
17pub struct EncodingError;
18
19impl fmt::Display for EncodingError {
20	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21		write!(f, "Encoding failure")
22	}
23}
24
25impl Error for EncodingError {}
26
27#[derive(Debug, Clone)]
28pub struct DecodingError;
29
30impl fmt::Display for DecodingError {
31	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32		write!(f, "Decoding failure")
33	}
34}
35
36impl Error for DecodingError {}
37
38#[derive(Debug, Clone)]
39pub struct SignatureError;
40
41impl fmt::Display for SignatureError {
42	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43		write!(f, "Signature failure")
44	}
45}
46
47impl Error for SignatureError {}
48
49#[derive(Debug, Clone)]
50pub struct CipherTextError;
51
52impl fmt::Display for CipherTextError {
53	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54		write!(f, "Ciphertext failure")
55	}
56}
57
58impl Error for CipherTextError {}
59
60#[derive(Debug, Clone)]
61pub struct DecryptionError;
62
63impl fmt::Display for DecryptionError {
64	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65		write!(f, "Decryption failure")
66	}
67}
68
69impl Error for DecryptionError {}
70
71#[cfg(test)]
72mod tests {
73	use super::*;
74
75	#[test]
76	fn errors_implement_std_error() {
77		fn assert_error<T: Error>() {}
78
79		assert_error::<KeyGenError>();
80		assert_error::<EncodingError>();
81		assert_error::<DecodingError>();
82		assert_error::<SignatureError>();
83		assert_error::<CipherTextError>();
84		assert_error::<DecryptionError>();
85	}
86
87	#[test]
88	fn errors_have_stable_human_readable_messages() {
89		assert_eq!(KeyGenError.to_string(), "Key generation failure");
90		assert_eq!(EncodingError.to_string(), "Encoding failure");
91		assert_eq!(DecodingError.to_string(), "Decoding failure");
92		assert_eq!(SignatureError.to_string(), "Signature failure");
93		assert_eq!(CipherTextError.to_string(), "Ciphertext failure");
94		assert_eq!(DecryptionError.to_string(), "Decryption failure");
95	}
96}