From 4ecf46ebc2d24762c1c38b659bf139d33567912b Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 13 Mar 2020 16:35:00 +0100 Subject: [PATCH] Add function to BDFWriter to set the compression level --- Cargo.toml | 2 +- src/chunks.rs | 4 ++-- src/io.rs | 9 ++++++++- src/lib.rs | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9b7128..5f10bd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bdflib" -version = "0.1.1" +version = "0.1.2" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/chunks.rs b/src/chunks.rs index 473e1df..7596ef2 100644 --- a/src/chunks.rs +++ b/src/chunks.rs @@ -144,9 +144,9 @@ impl GenericChunk { } /// Compresses the data of the chunk using lzma with a level of 6 - pub fn compress(&mut self) -> Result<(), Error> { + pub fn compress(&mut self, level: u32) -> Result<(), Error> { let data = self.data.as_slice(); - let mut compressor = XzEncoder::new(data, 1); + let mut compressor = XzEncoder::new(data, level); let mut compressed: Vec = Vec::new(); compressor.read_to_end(&mut compressed)?; self.length = compressed.len() as u32; diff --git a/src/io.rs b/src/io.rs index d3d78c3..ce53cb0 100644 --- a/src/io.rs +++ b/src/io.rs @@ -24,6 +24,7 @@ pub struct BDFWriter { data_entries: Vec, head_written: bool, compressed: bool, + compression_level: u32, } impl BDFWriter { @@ -36,6 +37,7 @@ impl BDFWriter { writer, head_written: false, compressed: compress, + compression_level: 1, } } @@ -81,7 +83,7 @@ impl BDFWriter { let mut data_chunk = GenericChunk::from_data_entries(&self.data_entries, &self.lookup_table); if self.compressed { - data_chunk.compress()?; + data_chunk.compress(self.compression_level)?; } let data = data_chunk.serialize(); self.writer.write(data.as_slice())?; @@ -95,6 +97,11 @@ impl BDFWriter { pub fn flush_writer(&mut self) -> Result<(), Error> { self.writer.flush() } + + /// Sets the compression level for lzma compression + pub fn set_compression_level(&mut self, level: u32) { + self.compression_level = level; + } } impl BDFReader { diff --git a/src/lib.rs b/src/lib.rs index cecff83..1250cef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,7 @@ mod tests { #[test] fn it_writes_compressed() -> Result<(), Error> { let mut writer = new_writer("tmp2.bdf", 2, true)?; + writer.set_compression_level(3); writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?; writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?;