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
use brotli2::CompressParams;
use futures::io::AsyncBufRead;

decoder! {
    /// A brotli decoder, or decompressor.
    #[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
    BrotliDecoder
}

encoder! {
    /// A brotli encoder, or compressor.
    #[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
    BrotliEncoder
}

impl<R: AsyncBufRead> BrotliEncoder<R> {
    /// Creates a new encoder which will read uncompressed data from the given stream and emit a
    /// compressed stream.
    ///
    /// The `level` argument here is typically 0-11.
    pub fn new(reader: R, level: u32) -> Self {
        let mut params = CompressParams::new();
        params.quality(level);
        Self::from_params(reader, &params)
    }

    /// Creates a new encoder with a custom [`CompressParams`].
    pub fn from_params(reader: R, params: &CompressParams) -> Self {
        Self {
            inner: crate::bufread::generic::Encoder::new(
                reader,
                crate::codec::BrotliEncoder::new(params),
            ),
        }
    }
}