Skip to main content

compression_codecs/zrip/
params.rs

1//! This module contains zrip-specific types for async-compression.
2//!
3//! The parameter types mirror `zstd::params` where the two backends overlap, so switching between
4//! them only needs a type change.
5
6use compression_core::Level;
7use zrip::{MAX_LEVEL, MIN_LEVEL};
8
9/// zstd's default level, which it also selects for level 0. zrip's default is 1.
10const DEFAULT_LEVEL: i32 = 3;
11
12/// A compression parameter for the `zrip` backend.
13///
14/// As with the `zstd` backend, the last value given for a parameter wins.
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16pub struct CParameter(CParameterInner);
17
18#[derive(Copy, Clone, Debug, PartialEq, Eq)]
19enum CParameterInner {
20    WindowLog(u32),
21    EnableLongDistanceMatching(bool),
22}
23
24impl CParameter {
25    pub fn quality(level: Level) -> i32 {
26        // Same numbers as the `zstd` backend, clamped to the levels zrip encodes.
27        match level {
28            Level::Fastest => 1,
29            Level::Best => MAX_LEVEL,
30            Level::Precise(0) => DEFAULT_LEVEL,
31            Level::Precise(quality) => quality.clamp(MIN_LEVEL, MAX_LEVEL),
32            _ => DEFAULT_LEVEL,
33        }
34    }
35
36    /// Window size in bytes (as a power of two)
37    ///
38    /// zrip clamps it to 10..=27, 1 KiB to 128 MiB, where zstd rejects values out of its range.
39    /// `0` selects the level's default, as with the `zstd` backend.
40    pub fn window_log(value: u32) -> Self {
41        Self(CParameterInner::WindowLog(value))
42    }
43
44    /// Enable long distance matching mode to look for and emit long-distance references.
45    ///
46    /// This increases the default window size.
47    pub fn enable_long_distance_matching(value: bool) -> Self {
48        Self(CParameterInner::EnableLongDistanceMatching(value))
49    }
50}
51
52pub(crate) fn options(params: &[CParameter]) -> zrip::Options {
53    params
54        .iter()
55        .fold(zrip::Options::default(), |opts, param| match param.0 {
56            CParameterInner::WindowLog(value) => opts.window_log(value),
57            CParameterInner::EnableLongDistanceMatching(value) => opts.ldm(value),
58        })
59}
60
61/// A decompression parameter for the `zrip` backend.
62#[derive(Copy, Clone, Debug, PartialEq, Eq)]
63pub struct DParameter(DParameterInner);
64
65#[derive(Copy, Clone, Debug, PartialEq, Eq)]
66enum DParameterInner {
67    WindowLogMax(u32),
68}
69
70impl DParameter {
71    /// Maximum window size in bytes (as a power of two). The default is 128 MiB.
72    pub fn window_log_max(value: u32) -> Self {
73        Self(DParameterInner::WindowLogMax(value))
74    }
75}
76
77/// The largest window the parameters allow, if they set one. As with the `zstd` backend, `0`
78/// selects the default.
79pub(crate) fn max_window(params: &[DParameter]) -> Option<u64> {
80    params.iter().fold(None, |_, param| match param.0 {
81        DParameterInner::WindowLogMax(0) => None,
82        DParameterInner::WindowLogMax(value) => Some(1u64 << value.min(63)),
83    })
84}