Add compression level option to serde.compress (#224)

This commit is contained in:
Nick Winans 2024-07-06 13:38:35 -07:00 committed by GitHub
parent cb552af660
commit 8aefe88104
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 6 deletions

View file

@ -13,6 +13,7 @@ use async_compression::{
BrotliDecoder, BrotliEncoder, GzipDecoder, GzipEncoder, ZlibDecoder, ZlibEncoder, BrotliDecoder, BrotliEncoder, GzipDecoder, GzipEncoder, ZlibDecoder, ZlibEncoder,
}, },
Level::Best as CompressionQuality, Level::Best as CompressionQuality,
Level::Precise as PreciseCompressionQuality,
}; };
/** /**
@ -119,6 +120,7 @@ impl<'lua> FromLua<'lua> for CompressDecompressFormat {
pub async fn compress<'lua>( pub async fn compress<'lua>(
source: impl AsRef<[u8]>, source: impl AsRef<[u8]>,
format: CompressDecompressFormat, format: CompressDecompressFormat,
level: Option<i32>,
) -> LuaResult<Vec<u8>> { ) -> LuaResult<Vec<u8>> {
if let CompressDecompressFormat::LZ4 = format { if let CompressDecompressFormat::LZ4 = format {
let source = source.as_ref().to_vec(); let source = source.as_ref().to_vec();
@ -130,18 +132,22 @@ pub async fn compress<'lua>(
let mut bytes = Vec::new(); let mut bytes = Vec::new();
let reader = BufReader::new(source.as_ref()); let reader = BufReader::new(source.as_ref());
let compression_quality = match level {
Some(l) => PreciseCompressionQuality(l),
None => CompressionQuality,
};
match format { match format {
CompressDecompressFormat::Brotli => { CompressDecompressFormat::Brotli => {
let mut encoder = BrotliEncoder::with_quality(reader, CompressionQuality); let mut encoder = BrotliEncoder::with_quality(reader, compression_quality);
copy(&mut encoder, &mut bytes).await?; copy(&mut encoder, &mut bytes).await?;
} }
CompressDecompressFormat::GZip => { CompressDecompressFormat::GZip => {
let mut encoder = GzipEncoder::with_quality(reader, CompressionQuality); let mut encoder = GzipEncoder::with_quality(reader, compression_quality);
copy(&mut encoder, &mut bytes).await?; copy(&mut encoder, &mut bytes).await?;
} }
CompressDecompressFormat::ZLib => { CompressDecompressFormat::ZLib => {
let mut encoder = ZlibEncoder::with_quality(reader, CompressionQuality); let mut encoder = ZlibEncoder::with_quality(reader, compression_quality);
copy(&mut encoder, &mut bytes).await?; copy(&mut encoder, &mut bytes).await?;
} }
CompressDecompressFormat::LZ4 => unreachable!(), CompressDecompressFormat::LZ4 => unreachable!(),

View file

@ -46,9 +46,9 @@ fn serde_decode(lua: &Lua, (format, bs): (EncodeDecodeFormat, BString)) -> LuaRe
async fn serde_compress( async fn serde_compress(
lua: &Lua, lua: &Lua,
(format, bs): (CompressDecompressFormat, BString), (format, bs, level): (CompressDecompressFormat, BString, Option<i32>),
) -> LuaResult<LuaString> { ) -> LuaResult<LuaString> {
let bytes = compress(bs, format).await?; let bytes = compress(bs, format, level).await?;
lua.create_string(bytes) lua.create_string(bytes)
} }

View file

@ -136,9 +136,10 @@ end
@param format The format to use @param format The format to use
@param s The string to compress @param s The string to compress
@param level The compression level to use, clamped to the format's limits. The best compression level is used by default
@return The compressed string @return The compressed string
]=] ]=]
function serde.compress(format: CompressDecompressFormat, s: buffer | string): string function serde.compress(format: CompressDecompressFormat, s: buffer | string, level: number?): string
return nil :: any return nil :: any
end end