Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add level option to serde.compress #224

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/lune-std-serde/src/compress_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use async_compression::{
BrotliDecoder, BrotliEncoder, GzipDecoder, GzipEncoder, ZlibDecoder, ZlibEncoder,
},
Level::Best as CompressionQuality,
Level::Precise as PreciseCompressionQuality,
};

/**
Expand Down Expand Up @@ -119,6 +120,7 @@ impl<'lua> FromLua<'lua> for CompressDecompressFormat {
pub async fn compress<'lua>(
source: impl AsRef<[u8]>,
format: CompressDecompressFormat,
level: Option<i32>,
) -> LuaResult<Vec<u8>> {
if let CompressDecompressFormat::LZ4 = format {
let source = source.as_ref().to_vec();
Expand All @@ -130,18 +132,22 @@ pub async fn compress<'lua>(

let mut bytes = Vec::new();
let reader = BufReader::new(source.as_ref());
let compression_quality = match level {
Some(l) => PreciseCompressionQuality(l),
None => CompressionQuality,
};

match format {
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?;
}
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?;
}
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?;
}
CompressDecompressFormat::LZ4 => unreachable!(),
Expand Down
4 changes: 2 additions & 2 deletions crates/lune-std-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fn serde_decode(lua: &Lua, (format, bs): (EncodeDecodeFormat, BString)) -> LuaRe

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

Expand Down
3 changes: 2 additions & 1 deletion types/serde.luau
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ end

@param format The format to use
@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
]=]
function serde.compress(format: CompressDecompressFormat, s: buffer | string): string
function serde.compress(format: CompressDecompressFormat, s: buffer | string, level: number?): string
return nil :: any
end

Expand Down