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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
macro_rules! algos {
    (@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
        #[cfg(feature = $algo_s)]
        decoder! {
            /// A
            #[doc = $algo_s]
            /// decoder, or decompressor.
            #[cfg_attr(docsrs, doc(cfg(feature = $algo_s)))]
            $decoder
        }

        #[cfg(feature = $algo_s)]
        encoder! {
            /// A
            #[doc = $algo_s]
            /// encoder, or compressor.
            #[cfg_attr(docsrs, doc(cfg(feature = $algo_s)))]
            $encoder<$inner> $({ $($constructor)* })*
        }
    };

    ($($mod:ident)::+<$inner:ident>) => {
        algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder<$inner> {
            /// The `level` argument here is typically 0-11.
            pub fn new(reader: $inner, level: u32) -> Self {
                let mut params = brotli2::CompressParams::new();
                params.quality(level);
                Self::from_params(reader, &params)
            }
        } {
            pub fn from_params(inner: $inner, params: &brotli2::CompressParams) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::BrotliEncoder::new(params),
                    ),
                }
            }
        });

        algos!(@algo bzip2 ["bzip2"] BzDecoder BzEncoder<$inner> {
            pub fn new(inner: $inner, level: bzip2::Compression) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::BzEncoder::new(level, 30),
                    ),
                }
            }
        });

        algos!(@algo deflate ["deflate"] DeflateDecoder DeflateEncoder<$inner> {
            pub fn new(inner: $inner, level: flate2::Compression) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::DeflateEncoder::new(level),
                    ),
                }
            }
        });

        algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder<$inner> {
            pub fn new(inner: $inner, level: flate2::Compression) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::GzipEncoder::new(level),
                    ),
                }
            }
        });

        algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder<$inner> {
            pub fn new(inner: $inner, level: flate2::Compression) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::ZlibEncoder::new(level),
                    ),
                }
            }
        });

        algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder<$inner> {
            /// The `level` argument here can range from 1-21. A level of `0` will use zstd's default, which is `3`.
            pub fn new(inner: $inner, level: i32) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::ZstdEncoder::new(level),
                    ),
                }
            }
        });
    }
}