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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
macro_rules! algos {
    (@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
        { @enc $($encoder_methods:tt)* }
        { @dec $($decoder_methods:tt)* }
    ) => {
        #[cfg(feature = $algo_s)]
        decoder! {
            #[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
            #[cfg(feature = $algo_s)]
            $decoder<$inner>

            { $($decoder_methods)* }
        }

        #[cfg(feature = $algo_s)]
        encoder! {
            #[doc = concat!("A ", $algo_s, " encoder, or compressor.")]
            #[cfg(feature = $algo_s)]
            $encoder<$inner> {
                pub fn new(inner: $inner) -> Self {
                    Self::with_quality(inner, crate::Level::Default)
                }
            }

            { $($encoder_methods)* }
        }
    };

    (@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
        { @dec $($decoder_methods:tt)* }
    ) => {
        #[cfg(feature = $algo_s)]
        decoder! {
            #[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
            #[cfg(feature = $algo_s)]
            $decoder<$inner>

            { $($decoder_methods)* }
        }
    };

    ($($mod:ident)::+ <$inner:ident>) => {
        algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder <$inner>
        { @enc
            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
                let params = brotli::enc::backward_references::BrotliEncoderParams::default();

                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::BrotliEncoder::new(level.into_brotli(params)),
                    ),
                }
            }

            /// Creates a new encoder, using the specified compression level and parameters, which
            /// will read uncompressed data from the given stream and emit a compressed stream.
            pub fn with_quality_and_params(
                inner: $inner,
                level: crate::Level,
                params: crate::brotli::EncoderParams,
            ) -> Self {
                let params = level.into_brotli(params.as_brotli());

                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::BrotliEncoder::new(params),
                    ),
                }
            }
        }
        { @dec }
        );

        algos!(@algo bzip2 ["bzip2"] BzDecoder BzEncoder <$inner>
        { @enc

            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::BzEncoder::new(level.into_bzip2(), 0),
                    ),
                }
            }
        }
        { @dec }
        );

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

        algos!(@algo deflate ["deflate64"] Deflate64Decoder Deflate64Encoder <$inner>
        { @dec }
        );

        algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder <$inner>
        { @enc

            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::GzipEncoder::new(level.into_flate2()),
                    ),
                }
            }
        }
        { @dec }
        );

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

            /// Returns the total number of input bytes which have been processed by this compression object.
            pub fn total_in(&self) -> u64 {
                self.inner.get_encoder_ref().get_ref().get_ref().total_in()
            }

            /// Returns the total number of output bytes which have been produced by this compression object.
            pub fn total_out(&self) -> u64 {
                self.inner.get_encoder_ref().get_ref().get_ref().total_out()
            }
        }
        { @dec }
        );

        algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder <$inner>
        { @enc

            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::ZstdEncoder::new(level.into_zstd()),
                    ),
                }
            }

            /// Creates a new encoder, using the specified compression level and parameters, which
            /// will read uncompressed data from the given stream and emit a compressed stream.
            ///
            /// # Panics
            ///
            /// Panics if this function is called with a [`CParameter::nb_workers()`] parameter and
            /// the `zstdmt` crate feature is _not_ enabled.
            ///
            /// [`CParameter::nb_workers()`]: crate::zstd::CParameter
            //
            // TODO: remove panic note on next breaking release, along with `CParameter::nb_workers`
            // change
            pub fn with_quality_and_params(inner: $inner, level: crate::Level, params: &[crate::zstd::CParameter]) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::ZstdEncoder::new_with_params(level.into_zstd(), params),
                    ),
                }
            }

            /// Creates a new encoder, using the specified compression level and pre-trained
            /// dictionary, which will read uncompressed data from the given stream and emit a
            /// compressed stream.
            ///
            /// Dictionaries provide better compression ratios for small files, but are required to
            /// be present during decompression.
            ///
            /// # Errors
            ///
            /// Returns error when `dictionary` is not valid.
            pub fn with_dict(inner: $inner, level: crate::Level, dictionary: &[u8]) -> ::std::io::Result<Self> {
                Ok(Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::ZstdEncoder::new_with_dict(level.into_zstd(), dictionary)?,
                    ),
                })
            }
        }
        { @dec
            /// Creates a new decoder, using the specified parameters, which will read compressed
            /// data from the given stream and emit a decompressed stream.
            pub fn with_params(inner: $inner, params: &[crate::zstd::DParameter]) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Decoder::new(
                        inner,
                        crate::codec::ZstdDecoder::new_with_params(params),
                    ),
                }
            }

            /// Creates a new decoder, using the specified compression level and pre-trained
            /// dictionary, which will read compressed data from the given stream and emit an
            /// uncompressed stream.
            ///
            /// Dictionaries provide better compression ratios for small files, but are required to
            /// be present during decompression. The dictionary used must be the same as the one
            /// used for compression.
            ///
            /// # Errors
            ///
            /// Returns error when `dictionary` is not valid.
            pub fn with_dict(inner: $inner, dictionary: &[u8]) -> ::std::io::Result<Self> {
                Ok(Self {
                    inner: crate::$($mod::)+generic::Decoder::new(
                        inner,
                        crate::codec::ZstdDecoder::new_with_dict(dictionary)?,
                    ),
                })
            }
        }
        );

        algos!(@algo xz ["xz"] XzDecoder XzEncoder <$inner>
        { @enc

            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::XzEncoder::new(level.into_xz2()),
                    ),
                }
            }
        }
        { @dec
            /// Creates a new decoder with the specified limit of memory.
            ///
            /// # Errors
            ///
            /// An IO error may be returned during decoding if the specified limit is too small.
            pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Decoder::new(
                        read,
                        crate::codec::XzDecoder::with_memlimit(memlimit),
                    ),
                }
            }
        }
        );

        algos!(@algo lzma ["lzma"] LzmaDecoder LzmaEncoder <$inner>
        { @enc

            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Encoder::new(
                        inner,
                        crate::codec::LzmaEncoder::new(level.into_xz2()),
                    ),
                }
            }
        }
        { @dec
            /// Creates a new decoder with the specified limit of memory.
            ///
            /// # Errors
            ///
            /// An IO error may be returned during decoding if the specified limit is too small.
            pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
                Self {
                    inner: crate::$($mod::)+generic::Decoder::new(
                        read,
                        crate::codec::LzmaDecoder::with_memlimit(memlimit),
                    ),
                }
            }

        }
        );
    }
}