simplified compression

This commit is contained in:
Dennis Schwerdel 2017-03-17 11:42:17 +01:00
parent 69eaf4085e
commit c3fa38e0b8
1 changed files with 53 additions and 53 deletions

View File

@ -34,32 +34,41 @@ quick_error!{
} }
} }
#[derive(Clone, Debug, Copy)]
pub enum CompressionAlgo {
Deflate, // Standardized
Brotli, // Good speed and ratio
Lzma2, // Very good ratio, slow
Lz4 // Very fast, low ratio
}
serde_impl!(CompressionAlgo(u8) {
Deflate => 0,
Brotli => 1,
Lzma2 => 2,
Lz4 => 3
});
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Compression { pub struct Compression {
Snappy(()), algo: CompressionAlgo,
Deflate(u8), level: u8
Brotli(u8), }
Lzma2(u8), impl Default for Compression {
ZStd(u8) fn default() -> Self {
Compression { algo: CompressionAlgo::Brotli, level: 3 }
}
} }
serde_impl!(Compression(u64) { serde_impl!(Compression(u64) {
Snappy(()) => 0, algo: CompressionAlgo => 0,
Deflate(u8) => 1, level: u8 => 1
Brotli(u8) => 2,
Lzma2(u8) => 3,
ZStd(u8) => 4
}); });
impl Compression { impl Compression {
#[inline] #[inline]
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
if let Some(level) = self.level() { format!("{}/{}", self.name(), self.level)
format!("{}/{}", self.name(), level)
} else {
self.name().to_string()
}
} }
#[inline] #[inline]
@ -71,24 +80,23 @@ impl Compression {
} else { } else {
(name, 5) (name, 5)
}; };
match name { let algo = match name {
"snappy" => Ok(Compression::Snappy(())), "deflate" | "zlib" | "gzip" => CompressionAlgo::Deflate,
"zstd" => Ok(Compression::ZStd(level)), "brotli" => CompressionAlgo::Brotli,
"deflate" | "zlib" | "gzip" => Ok(Compression::Deflate(level)), "lzma2" => CompressionAlgo::Lzma2,
"brotli" => Ok(Compression::Brotli(level)), "lz4" => CompressionAlgo::Lz4,
"lzma2" => Ok(Compression::Lzma2(level)), _ => return Err(CompressionError::UnsupportedCodec(name.to_string()))
_ => Err(CompressionError::UnsupportedCodec(name.to_string())) };
} Ok(Compression { algo: algo, level: level })
} }
#[inline] #[inline]
pub fn name(&self) -> &'static str { pub fn name(&self) -> &'static str {
match *self { match self.algo {
Compression::Snappy(_) => "snappy", CompressionAlgo::Deflate => "deflate",
Compression::Deflate(_) => "deflate", CompressionAlgo::Brotli => "brotli",
Compression::Brotli(_) => "brotli", CompressionAlgo::Lzma2 => "lzma2",
Compression::Lzma2(_) => "lzma2", CompressionAlgo::Lz4 => "lz4",
Compression::ZStd(_) => "zstd",
} }
} }
@ -103,34 +111,26 @@ impl Compression {
} }
#[inline] #[inline]
pub fn level(&self) -> Option<u8> { pub fn level(&self) -> u8 {
match *self { self.level
Compression::Snappy(_) => None,
Compression::Deflate(lvl) |
Compression::Brotli(lvl) |
Compression::ZStd(lvl) |
Compression::Lzma2(lvl) => Some(lvl),
}
} }
fn options(&self) -> Result<*mut SquashOptions, CompressionError> { fn options(&self) -> Result<*mut SquashOptions, CompressionError> {
let codec = try!(self.codec()); let codec = try!(self.codec());
let options = unsafe { squash_options_new(codec, ptr::null::<()>()) }; let options = unsafe { squash_options_new(codec, ptr::null::<()>()) };
if let Some(level) = self.level() { if options.is_null() {
if options.is_null() { return Err(CompressionError::InitializeOptions)
return Err(CompressionError::InitializeOptions) }
} let option = CString::new("level");
let option = CString::new("level"); let value = CString::new(format!("{}", self.level));
let value = CString::new(format!("{}", level)); let res = unsafe { squash_options_parse_option(
let res = unsafe { squash_options_parse_option( options,
options, option.unwrap().as_ptr(),
option.unwrap().as_ptr(), value.unwrap().as_ptr()
value.unwrap().as_ptr() )};
)}; if res != SQUASH_OK {
if res != SQUASH_OK { //panic!(unsafe { CStr::from_ptr(squash_status_to_string(res)).to_str().unwrap() });
//panic!(unsafe { CStr::from_ptr(squash_status_to_string(res)).to_str().unwrap() }); return Err(CompressionError::InitializeOptions)
return Err(CompressionError::InitializeOptions)
}
} }
Ok(options) Ok(options)
} }