use std::fmt;
use super::ChunkType;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Parse(Box<dyn std::error::Error>),
Encrypted,
InvalidInput(String),
InvalidPartition(usize),
Decompress {
partition_num: usize,
chunk_num: usize,
chunk_type: ChunkType,
},
UnsupportedEncryption(String),
InvalidPassword,
#[cfg(feature = "crypto")]
Decryption(openssl::error::ErrorStack),
}
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
Error::Io(ref err) => Some(err),
#[cfg(feature = "crypto")]
Error::Decryption(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Encrypted => write!(f, "DMG seems to encrypted"),
Error::InvalidInput(ref str) => write!(f, "{}", str),
Error::InvalidPartition(num_partition) => {
write!(f, "partition {} does not exist", num_partition)
}
Error::UnsupportedEncryption(ref str) => {
write!(f, "unsupported encryption parameters: {}", str)
}
Error::InvalidPassword => write!(f, "invalid password given"),
Error::Decompress {
partition_num,
chunk_num,
ref chunk_type,
} => write!(
f,
"there was an error during decompression (partition={} chunk={} type={:?})",
partition_num, chunk_num, chunk_type
),
Error::Io(ref e) => e.fmt(f),
Error::Parse(ref e) => write!(f, "parse error ({})", e),
#[cfg(feature = "crypto")]
Error::Decryption(_) => write!(f, "error during decryption"),
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}
impl From<Box<bincode::ErrorKind>> for Error {
fn from(err: Box<bincode::ErrorKind>) -> Error {
match *err {
bincode::ErrorKind::Io(err) => Error::Io(err),
err => Error::Parse(Box::new(err)),
}
}
}
impl From<plist::Error> for Error {
fn from(err: plist::Error) -> Error {
match err.into_io() {
Ok(io_err) => Error::Io(io_err),
Err(err) => Error::Parse(Box::new(err)),
}
}
}
#[cfg(feature = "crypto")]
impl From<openssl::error::ErrorStack> for Error {
fn from(err: openssl::error::ErrorStack) -> Error {
Error::Decryption(err)
}
}