use cbc::cipher::block_padding::UnpadError;
use std::error;
use std::fmt;
use std::io;
use xml::reader as xmlreader;
use xml::writer as xmlwriter;
#[derive(Debug)]
pub enum Error {
CryptoError(UnpadError),
InvalidBlockHash,
InvalidBlockId(u32),
InvalidDbSignature([u8; 4]),
InvalidFinalBlockHash([u8; 32]),
InvalidHeaderHash,
InvalidHeaderSize {
id: u8,
expected: u16,
actual: u16,
},
InvalidKey,
InvalidKeyFile,
Io(io::Error),
MissingHeader(u8),
UnhandledCompression(u32),
UnhandledDbType([u8; 4]),
UnhandledHeader(u8),
UnhandledMasterCipher([u8; 16]),
UnhandledStreamCipher(u32),
Unimplemented(String),
XmlError(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::CryptoError(_) => write!(f, "Crypto error: invalid padding."),
Error::InvalidBlockHash => write!(f, "Invalid block hash"),
Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
Error::InvalidFinalBlockHash(val) => write!(f, "Invalid final block hash: {:?}", val),
Error::InvalidHeaderSize {
id,
expected,
actual,
} => {
write!(
f,
"Invalid header size: id: {}, expected: {}, actual: {}",
id, expected, actual
)
}
Error::InvalidHeaderHash => write!(f, "Invalid header hash"),
Error::InvalidKey => write!(f, "Invalid key"),
Error::InvalidKeyFile => write!(f, "Invalid key file"),
Error::Io(ref err) => write!(f, "IO error: {}", err),
Error::MissingHeader(val) => write!(f, "Missing header: {}", val),
Error::UnhandledCompression(val) => write!(f, "Unhandled compression: {}", val),
Error::UnhandledDbType(val) => write!(f, "Unhandled database type: {:?}", val),
Error::UnhandledHeader(val) => write!(f, "Unhandled header: {}", val),
Error::UnhandledMasterCipher(val) => write!(f, "Unhandled master cipher: {:?}", val),
Error::UnhandledStreamCipher(val) => write!(f, "Unhandled stream cipher: {}", val),
Error::Unimplemented(ref val) => write!(f, "Unimplemented: {}", val),
Error::XmlError(ref val) => write!(f, "XML error: {}", val),
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Io(ref err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}
impl From<xmlreader::Error> for Error {
fn from(err: xmlreader::Error) -> Error {
Error::XmlError(format!("{}", err))
}
}
impl From<xmlwriter::Error> for Error {
fn from(err: xmlwriter::Error) -> Error {
Error::XmlError(format!("{}", err))
}
}
impl From<UnpadError> for Error {
fn from(err: UnpadError) -> Error {
Error::CryptoError(err)
}
}