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
use fog_crypto::{hash::Hash, CryptoError};
use std::fmt;
use serde::{de, ser};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
OldVersion(String),
SchemaMismatch {
actual: Option<Hash>,
expected: Option<Hash>,
},
SerdeFail(String),
BadHeader(String),
FailDecompress(String),
LengthTooLong { max: usize, actual: usize },
LengthTooShort {
step: &'static str,
actual: usize,
expected: usize,
},
BadSignature,
BadEncode(String),
FailValidate(String),
CryptoError(CryptoError),
ParseLimit(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::OldVersion(ref err) => write!(f, "Old version: {}", err),
Error::SchemaMismatch {
ref actual,
ref expected,
} => match (actual, expected) {
(Some(actual), Some(expected)) => write!(
f,
"Expected schema {}, but document used schema {}",
expected, actual
),
(Some(actual), None) => {
write!(f, "Expected no schema, but document used schema {}", actual)
}
(None, Some(expected)) => write!(
f,
"Expected schema {}, but document used no schema",
expected
),
(None, None) => write!(
f,
"Expected and got no schema (this should not have been an error)"
),
},
Error::SerdeFail(ref msg) => f.write_str(msg),
Error::BadHeader(ref err) => write!(f, "Data has bad header format: {}", err),
Error::FailDecompress(ref err) => write!(f, "Failed decompression step: {}", err),
Error::LengthTooLong { max, actual } => write!(
f,
"Data too long: was {} bytes, maximum allowed is {}",
actual, max
),
Error::LengthTooShort {
step,
actual,
expected,
} => write!(
f,
"Expected data length {}, but got {} on step [{}]",
expected, actual, step
),
Error::BadSignature => write!(f, "A signature failed to verify"),
Error::BadEncode(ref err) => write!(f, "Basic data encoding failure: {}", err),
Error::FailValidate(ref err) => write!(f, "Failed validation: {}", err),
Error::CryptoError(_) => write!(f, "Cryptographic Error"),
Error::ParseLimit(ref err) => write!(f, "Hit parsing limit: {}", err),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Error::CryptoError(ref err) => Some(err),
_ => None,
}
}
}
impl std::convert::From<CryptoError> for Error {
fn from(e: CryptoError) -> Self {
Self::CryptoError(e)
}
}
impl ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::SerdeFail(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::SerdeFail(msg.to_string())
}
}