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

allow the full range of compression levels for zstd #479

Merged
merged 1 commit into from
Jan 12, 2024
Merged
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
13 changes: 7 additions & 6 deletions crates/rattler_package_streaming/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ pub enum CompressionLevel {
#[default]
Default,
/// Use a numeric compression level (zstd: 1-22, bzip2: 1-9)
Numeric(u32),
Numeric(i32),
}

impl CompressionLevel {
fn to_zstd_level(self) -> Result<i32, std::io::Error> {
match self {
CompressionLevel::Lowest => Ok(1),
CompressionLevel::Lowest => Ok(-7),
CompressionLevel::Highest => Ok(22),
CompressionLevel::Default => Ok(15),
CompressionLevel::Numeric(n) => {
if (1..=22).contains(&n) {
Ok(n as i32)
if (-7..=22).contains(&n) {
Ok(n)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"zstd compression level must be between 1 and 22",
"zstd compression level must be between -7 and 22",
))
}
}
Expand All @@ -67,7 +67,8 @@ impl CompressionLevel {
CompressionLevel::Default | CompressionLevel::Highest => Ok(bzip2::Compression::new(9)),
CompressionLevel::Numeric(n) => {
if (1..=9).contains(&n) {
Ok(bzip2::Compression::new(n))
// this conversion from i32 to u32 cannot panic because of the check above
Ok(bzip2::Compression::new(n.try_into().unwrap()))
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
Expand Down