1use 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) => {
87 match err {
88 SymmetricCipherError::InvalidLength => {
89 write!(f, "Crypto error: invalid length.")
90 }
91
92 SymmetricCipherError::InvalidPadding => {
93 write!(f, "Crypto error: invalid padding.")
94 }
95 }
96 }
97
98 Error::InvalidBlockHash => write!(f, "Invalid block hash"),
99 Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
100 Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
101 Error::InvalidFinalBlockHash(val) => write!(f, "Invalid final block hash: {:?}", val),
102 Error::InvalidHeaderSize {
103 id,
104 expected,
105 actual,
106 } => {
107 write!(
108 f,
109 "Invalid header size: id: {}, expected: {}, actual: {}",
110 id,
111 expected,
112 actual
113 )
114 }
115 Error::InvalidHeaderHash => write!(f, "Invalid header hash"),
116 Error::InvalidKey => write!(f, "Invalid key"),
117 Error::InvalidKeyFile => write!(f, "Invalid key file"),
118 Error::Io(ref err) => write!(f, "IO error: {}", err),
119 Error::MissingHeader(val) => write!(f, "Missing header: {}", val),
120 Error::UnhandledCompression(val) => write!(f, "Unhandled compression: {}", val),
121 Error::UnhandledDbType(val) => write!(f, "Unhandled database type: {:?}", val),
122 Error::UnhandledHeader(val) => write!(f, "Unhandled header: {}", val),
123 Error::UnhandledMasterCipher(val) => write!(f, "Unhandled master cipher: {:?}", val),
124 Error::UnhandledStreamCipher(val) => write!(f, "Unhandled stream cipher: {}", val),
125 Error::Unimplemented(ref val) => write!(f, "Unimplemented: {}", val),
126 Error::XmlError(ref val) => write!(f, "XML error: {}", val),
127 }
128 }
129}
130
131impl error::Error for Error {
132 fn description(&self) -> &str {
133 match *self {
134 Error::CryptoError(_) => "crypto error",
135 Error::InvalidBlockHash => "invalid block hash",
136 Error::InvalidBlockId(_) => "invalid block id",
137 Error::InvalidDbSignature(_) => "invalid database signature",
138 Error::InvalidFinalBlockHash(_) => "invalid final block hash",
139 Error::InvalidHeaderSize { .. } => "invalid header size",
140 Error::InvalidHeaderHash => "invalid header hash",
141 Error::InvalidKey => "invalid key",
142 Error::InvalidKeyFile => "invalid key file",
143 Error::Io(ref err) => err.description(),
144 Error::MissingHeader(_) => "missing header",
145 Error::UnhandledCompression(_) => "unhandled compression",
146 Error::UnhandledDbType(_) => "unhandled database type",
147 Error::UnhandledHeader(_) => "unhandled header",
148 Error::UnhandledMasterCipher(_) => "unhandled master cipher",
149 Error::UnhandledStreamCipher(_) => "unhandled stream cipher",
150 Error::Unimplemented(_) => "unimplemented",
151 Error::XmlError(_) => "xml error",
152 }
153 }
154
155 fn cause(&self) -> Option<&error::Error> {
156 match *self {
157 Error::Io(ref err) => Some(err),
158 _ => None,
159 }
160 }
161}
162
163impl From<io::Error> for Error {
164 fn from(err: io::Error) -> Error {
165 Error::Io(err)
166 }
167}
168
169impl From<xmlreader::Error> for Error {
170 fn from(err: xmlreader::Error) -> Error {
171 Error::XmlError(format!("{}", err))
172 }
173}
174
175impl From<xmlwriter::Error> for Error {
176 fn from(err: xmlwriter::Error) -> Error {
177 Error::XmlError(format!("{}", err))
178 }
179}
180
181impl From<SymmetricCipherError> for Error {
182 fn from(err: SymmetricCipherError) -> Error {
183 Error::CryptoError(err)
184 }
185}