1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! This module contains Brotli-specific types for async-compression.

use brotli::enc::backward_references::{BrotliEncoderMode, BrotliEncoderParams};

/// A compression parameter for Brotli. This is a stable wrapper around Brotli's own encoder params
/// type, to abstract over different versions of the Brotli library.
///
/// See the [Brotli documentation](https://www.brotli.org/encode.html#a9a8) for more information on
/// these parameters.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EncoderParams {
    window_size: i32,
    block_size: i32,
    size_hint: usize,
    mode: BrotliEncoderMode,
}

impl EncoderParams {
    /// Sets window size in bytes (as a power of two).
    ///
    /// Used as Brotli's `lgwin` parameter.
    ///
    /// `window_size` is clamped to `0 <= window_size <= 24`.
    pub fn window_size(mut self, window_size: i32) -> Self {
        self.window_size = window_size.clamp(0, 24);
        self
    }

    /// Sets input block size in bytes (as a power of two).
    ///
    /// Used as Brotli's `lgblock` parameter.
    ///
    /// `block_size` is clamped to `16 <= block_size <= 24`.
    pub fn block_size(mut self, block_size: i32) -> Self {
        self.block_size = block_size.clamp(16, 24);
        self
    }

    /// Sets hint for size of data to be compressed.
    pub fn size_hint(mut self, size_hint: usize) -> Self {
        self.size_hint = size_hint;
        self
    }

    /// Sets encoder to text mode.
    ///
    /// If input data is known to be UTF-8 text, this allows the compressor to make assumptions and
    /// optimizations.
    ///
    /// Used as Brotli's `mode` parameter.
    pub fn text_mode(mut self) -> Self {
        self.mode = BrotliEncoderMode::BROTLI_MODE_TEXT;
        self
    }

    pub(crate) fn as_brotli(&self) -> BrotliEncoderParams {
        BrotliEncoderParams {
            lgwin: self.window_size,
            lgblock: self.block_size,
            size_hint: self.size_hint,
            mode: self.mode,
            ..Default::default()
        }
    }
}