1use crate::rust_crypto::symmetriccipher::SymmetricCipherError;
10use std::error;
11use std::fmt;
12use std::io;
13use xml::reader as xmlreader;
14use xml::writer as xmlwriter;
15
16#[derive(Debug)]
18pub enum Error {
19 CryptoError(SymmetricCipherError),
21
22 InvalidBlockHash,
24
25 InvalidBlockId(u32),
27
28 InvalidDbSignature([u8; 4]),
30
31 InvalidFinalBlockHash([u8; 32]),
33
34 InvalidHeaderHash,
36
37 InvalidHeaderSize {
39 id: u8,
41
42 expected: u16,
44
45 actual: u16,
47 },
48
49 InvalidKey,
51
52 InvalidKeyFile,
54
55 Io(io::Error),
57
58 MissingHeader(u8),
60
61 UnhandledCompression(u32),
63
64 UnhandledDbType([u8; 4]),
66
67 UnhandledHeader(u8),
69
70 UnhandledMasterCipher([u8; 16]),
72
73 UnhandledStreamCipher(u32),
75
76 Unimplemented(String),
78
79 XmlError(String),
81}
82
83impl fmt::Display for Error {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 match *self {
86 Error::CryptoError(err) => match err {
87 SymmetricCipherError::InvalidLength => {
88 write!(f, "Crypto error: invalid length.")
89 }
90
91 SymmetricCipherError::InvalidPadding => {
92 write!(f, "Crypto error: invalid padding.")
93 }
94 },
95
96 Error::InvalidBlockHash => write!(f, "Invalid block hash"),
97 Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
98 Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
99 Error::InvalidFinalBlockHash(val) => write!(f, "Invalid final block hash: {:?}", val),
100 Error::InvalidHeaderSize {
101 id,
102 expected,
103 actual,
104 } => {
105 write!(
106 f,
107 "Invalid header size: id: {}, expected: {}, actual: {}",
108 id, expected, actual
109 )
110 }
111 Error::InvalidHeaderHash => write!(f, "Invalid header hash"),
112 Error::InvalidKey => write!(f, "Invalid key"),
113 Error::InvalidKeyFile => write!(f, "Invalid key file"),
114 Error::Io(ref err) => write!(f, "IO error: {}", err),
115 Error::MissingHeader(val) => write!(f, "Missing header: {}", val),
116 Error::UnhandledCompression(val) => write!(f, "Unhandled compression: {}", val),
117 Error::UnhandledDbType(val) => write!(f, "Unhandled database type: {:?}", val),
118 Error::UnhandledHeader(val) => write!(f, "Unhandled header: {}", val),
119 Error::UnhandledMasterCipher(val) => write!(f, "Unhandled master cipher: {:?}", val),
120 Error::UnhandledStreamCipher(val) => write!(f, "Unhandled stream cipher: {}", val),
121 Error::Unimplemented(ref val) => write!(f, "Unimplemented: {}", val),
122 Error::XmlError(ref val) => write!(f, "XML error: {}", val),
123 }
124 }
125}
126
127impl error::Error for Error {
128 fn cause(&self) -> Option<&dyn error::Error> {
129 match *self {
130 Error::Io(ref err) => Some(err),
131 _ => None,
132 }
133 }
134}
135
136impl From<io::Error> for Error {
137 fn from(err: io::Error) -> Error {
138 Error::Io(err)
139 }
140}
141
142impl From<xmlreader::Error> for Error {
143 fn from(err: xmlreader::Error) -> Error {
144 Error::XmlError(format!("{}", err))
145 }
146}
147
148impl From<xmlwriter::Error> for Error {
149 fn from(err: xmlwriter::Error) -> Error {
150 Error::XmlError(format!("{}", err))
151 }
152}
153
154impl From<SymmetricCipherError> for Error {
155 fn from(err: SymmetricCipherError) -> Error {
156 Error::CryptoError(err)
157 }
158}