compression_codecs/zrip/
params.rs1use compression_core::Level;
7use zrip::{MAX_LEVEL, MIN_LEVEL};
8
9const DEFAULT_LEVEL: i32 = 3;
11
12#[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 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 pub fn window_log(value: u32) -> Self {
41 Self(CParameterInner::WindowLog(value))
42 }
43
44 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#[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 pub fn window_log_max(value: u32) -> Self {
73 Self(DParameterInner::WindowLogMax(value))
74 }
75}
76
77pub(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}