use lzma_sys::*;
use std::convert::From;
use std::result::Result;
use std::error;
use std::fmt;
use std::error::Error as StdError;
use std::io::Error as IoError;
#[derive(Debug)]
pub enum LzmaError {
Mem,
MemLimit,
Format,
Options,
Data,
Buf,
Io(IoError),
Other,
}
impl fmt::Display for LzmaError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LzmaError::Mem => write!(f, "Memory allocation failed"),
LzmaError::MemLimit => write!(f, "Memory limit would be violated"),
LzmaError::Format => write!(f, "XZ magic bytes were not found"),
LzmaError::Options => write!(f, "Unsupported compression options"),
LzmaError::Data => write!(f, "Corrupt data"),
LzmaError::Buf => write!(f, "Data look like it was truncated or possibly corrupt"),
LzmaError::Io(ref err) => write!(f, "{}", err),
LzmaError::Other => write!(f, "Unknown error"),
}
}
}
impl StdError for LzmaError {
fn description(&self) -> &str {
match *self {
LzmaError::Mem => "Memory allocation failed",
LzmaError::MemLimit => "Memory limit would be violated",
LzmaError::Format => "XZ magic bytes were not found",
LzmaError::Options => "Unsupported compression options",
LzmaError::Data => "Corrupt data",
LzmaError::Buf => "Data look like it was truncated or possibly corrupt",
LzmaError::Io(..) => "IO error",
LzmaError::Other => "Unknown error",
}
}
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
LzmaError::Io(ref err) => Some(err),
_ => None,
}
}
}
impl From<IoError> for LzmaError {
fn from(err: IoError) -> LzmaError {
LzmaError::Io(err)
}
}
pub type LzmaLibResult = Result<lzma_ret, LzmaError>;
impl From<lzma_ret> for LzmaLibResult {
fn from(ret: lzma_ret) -> LzmaLibResult {
match ret {
lzma_ret::LzmaOk => Ok(ret),
lzma_ret::LzmaStreamEnd => Ok(ret),
lzma_ret::LzmaNoCheck => Ok(ret),
lzma_ret::LzmaUnsupportedCheck => Ok(ret), lzma_ret::LzmaGetCheck => Ok(ret),
lzma_ret::LzmaMemError => Err(LzmaError::Mem),
lzma_ret::LzmaMemlimitError => Err(LzmaError::MemLimit),
lzma_ret::LzmaFormatError => Err(LzmaError::Format),
lzma_ret::LzmaOptionsError => Err(LzmaError::Options),
lzma_ret::LzmaDataError => Err(LzmaError::Data),
lzma_ret::LzmaBufError => Err(LzmaError::Buf),
_ => Err(LzmaError::Other),
}
}
}