1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::StorageError;
use base64::DecodeError;
use std::{
error::Error,
fmt::{self, Display, Formatter},
io,
};
#[derive(Debug)]
pub enum CryptoError {
FsIoError { source: io::Error },
FileNotFound { path: String },
CiphertextFailedVerification,
InvalidKeyLength { expected: usize, actual: usize },
StorageError { source: StorageError },
NotDowncastable,
FilePathHasNoFileStem { path: String },
FilePathIsInvalidUTF8,
NotDeserializableToBaseDataType,
Base64Decode { source: DecodeError },
WrongNonceType,
}
impl Error for CryptoError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
CryptoError::FsIoError { ref source } => Some(source),
CryptoError::FileNotFound { .. } => None,
CryptoError::CiphertextFailedVerification => None,
CryptoError::InvalidKeyLength { .. } => None,
CryptoError::StorageError { ref source } => Some(source),
CryptoError::NotDowncastable => None,
CryptoError::FilePathHasNoFileStem { .. } => None,
CryptoError::FilePathIsInvalidUTF8 => None,
CryptoError::NotDeserializableToBaseDataType => None,
CryptoError::Base64Decode { ref source } => Some(source),
CryptoError::WrongNonceType => None,
}
}
}
impl Display for CryptoError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
CryptoError::FsIoError { .. } => {
write!(f, "Error occured during file system IO")
}
CryptoError::FileNotFound { ref path } => {
write!(f, "Path \"{}\" not found", path)
}
CryptoError::CiphertextFailedVerification => {
write!(f, "Ciphertext failed verification before decryption")
}
CryptoError::InvalidKeyLength {
ref expected,
ref actual,
} => {
write!(
f,
"Provided key was not the correct length, expected: {}, actual: {}",
expected, actual,
)
}
CryptoError::StorageError { .. } => {
write!(f, "Error occured while interacting with key storage")
}
CryptoError::NotDowncastable => {
write!(
f,
"Could not downcast the Types-value into the requested variant"
)
}
CryptoError::FilePathHasNoFileStem { ref path } => {
write!(
f,
"File path \"{}\" was invalid as the file name has no stem",
path
)
}
CryptoError::FilePathIsInvalidUTF8 => {
write!(f, "Given file path was not valid UTF-8")
}
CryptoError::NotDeserializableToBaseDataType => {
write!(f, "Given bytes could not be deserialized to one of: bool, u64, i64, f64, or string")
}
CryptoError::Base64Decode { .. } => {
write!(f, "Error occurred while decoding string from base64")
}
CryptoError::WrongNonceType => {
write!(f, "Invalid type of nonce was provided for the operation")
}
}
}
}
#[cfg(test)]
mod tests {
use crate::StorageError;
#[test]
fn test_to_string_internal_error() {
let s = StorageError::InternalError {
source: Box::new(StorageError::NotFound),
}
.to_string();
assert_eq!(s, "Internal error occurred");
}
#[test]
fn test_to_string_not_found() {
let s = StorageError::NotFound.to_string();
assert_eq!(s, "Key not found");
}
}