use std::error::Error;
use std::fmt;
use crate::deflate::CompressionLevel;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Strategy {
#[default]
Default,
Filtered,
HuffmanOnly,
Rle,
Fixed,
}
impl Strategy {
pub(crate) fn is_fastest_kind(self) -> bool {
matches!(self, Self::HuffmanOnly | Self::Rle | Self::Fixed)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MemoryLevel(u8);
impl MemoryLevel {
pub const DEFAULT: Self = Self(8);
pub const MAX: Self = Self(9);
pub const fn new(level: u8) -> Result<Self, InvalidMemoryLevel> {
if matches!(level, 1..=9) {
Ok(Self(level))
} else {
Err(InvalidMemoryLevel(level))
}
}
pub const fn get(self) -> u8 {
self.0
}
pub(crate) const fn symbol_buffer(self) -> usize {
1 << (self.0 + 6)
}
}
impl Default for MemoryLevel {
fn default() -> Self {
Self::DEFAULT
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InvalidMemoryLevel(pub u8);
impl fmt::Display for InvalidMemoryLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid memory level {}; expected 1 to 9", self.0)
}
}
impl Error for InvalidMemoryLevel {}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Options {
pub(crate) level: CompressionLevel,
pub(crate) strategy: Strategy,
pub(crate) memory_level: MemoryLevel,
}
impl Options {
pub const fn new(level: CompressionLevel) -> Self {
Self {
level,
strategy: Strategy::Default,
memory_level: MemoryLevel::DEFAULT,
}
}
#[must_use]
pub const fn strategy(mut self, strategy: Strategy) -> Self {
self.strategy = strategy;
self
}
#[must_use]
pub const fn memory_level(mut self, memory_level: MemoryLevel) -> Self {
self.memory_level = memory_level;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Method {
Stored,
Quick,
Fast,
Medium,
Slow,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Config {
pub(super) good_length: u32,
pub(super) max_lazy: u32,
pub(super) nice_length: u32,
pub(super) max_chain: u32,
pub(super) method: Method,
}
impl Config {
pub(super) const fn of(level: CompressionLevel) -> Self {
let (good_length, max_lazy, nice_length, max_chain, method) = match level.get() {
0 => (0, 0, 0, 0, Method::Stored),
1 => (0, 0, 0, 0, Method::Quick),
2 => (4, 4, 8, 4, Method::Fast),
3 => (4, 6, 16, 6, Method::Medium),
4 => (4, 12, 32, 24, Method::Medium),
5 => (8, 16, 32, 32, Method::Medium),
6 => (8, 16, 128, 128, Method::Medium),
7 => (8, 32, 128, 256, Method::Slow),
8 => (32, 128, 258, 1024, Method::Slow),
_ => (32, 258, 258, 4096, Method::Slow),
};
Self {
good_length,
max_lazy,
nice_length,
max_chain,
method,
}
}
pub(super) const fn rolls(self) -> bool {
self.max_chain > 1024
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn memory_levels_validate_range() {
for level in 1..=9 {
assert_eq!(MemoryLevel::new(level).map(MemoryLevel::get), Ok(level));
}
assert_eq!(MemoryLevel::new(10), Err(InvalidMemoryLevel(10)));
assert_eq!(
InvalidMemoryLevel(0).to_string(),
"invalid memory level 0; expected 1 to 9"
);
assert_eq!(MemoryLevel::DEFAULT.symbol_buffer(), 16_384);
assert_eq!(MemoryLevel::MAX.symbol_buffer(), 32_768);
assert_eq!(MemoryLevel::default(), MemoryLevel::DEFAULT);
}
#[test]
fn levels_select_zlib_ng_methods() {
let method = |level| Config::of(CompressionLevel::new(level).unwrap()).method;
assert_eq!(method(0), Method::Stored);
assert_eq!(method(1), Method::Quick);
assert_eq!(method(2), Method::Fast);
assert!((3..=6).all(|level| method(level) == Method::Medium));
assert!((7..=9).all(|level| method(level) == Method::Slow));
assert!(Config::of(CompressionLevel::BEST).rolls());
assert!(!Config::of(CompressionLevel::new(8).unwrap()).rolls());
}
#[test]
fn options_keep_what_they_are_given() {
let options = Options::new(CompressionLevel::FASTEST)
.strategy(Strategy::Rle)
.memory_level(MemoryLevel::MAX);
assert_eq!(options.level, CompressionLevel::FASTEST);
assert_eq!(options.strategy, Strategy::Rle);
assert_eq!(options.memory_level, MemoryLevel::MAX);
assert_eq!(Options::default(), Options::new(CompressionLevel::DEFAULT));
assert!(Strategy::Fixed.is_fastest_kind() && !Strategy::Filtered.is_fastest_kind());
}
}