async_compression/
macros.rs

1macro_rules! algos {
2    (@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
3        { @enc $($encoder_methods:tt)* }
4        { @dec $($decoder_methods:tt)* }
5    ) => {
6        #[cfg(feature = $algo_s)]
7        decoder! {
8            #[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
9            #[cfg(feature = $algo_s)]
10            $decoder<$inner>
11
12            { $($decoder_methods)* }
13        }
14
15        #[cfg(feature = $algo_s)]
16        encoder! {
17            #[doc = concat!("A ", $algo_s, " encoder, or compressor.")]
18            #[cfg(feature = $algo_s)]
19            $encoder<$inner> {
20                pub fn new(inner: $inner) -> Self {
21                    Self::with_quality(inner, crate::Level::Default)
22                }
23            }
24
25            { $($encoder_methods)* }
26        }
27    };
28
29    (@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
30        { @dec $($decoder_methods:tt)* }
31    ) => {
32        #[cfg(feature = $algo_s)]
33        decoder! {
34            #[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
35            #[cfg(feature = $algo_s)]
36            $decoder<$inner>
37
38            { $($decoder_methods)* }
39        }
40    };
41
42    ($($mod:ident)::+ <$inner:ident>) => {
43        algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder <$inner>
44        { @enc
45            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
46                let params = brotli::enc::backward_references::BrotliEncoderParams::default();
47
48                Self {
49                    inner: crate::$($mod::)+generic::Encoder::new(
50                        inner,
51                        crate::codec::BrotliEncoder::new(level.into_brotli(params)),
52                    ),
53                }
54            }
55
56            /// Creates a new encoder, using the specified compression level and parameters, which
57            /// will read uncompressed data from the given stream and emit a compressed stream.
58            pub fn with_quality_and_params(
59                inner: $inner,
60                level: crate::Level,
61                params: crate::brotli::EncoderParams,
62            ) -> Self {
63                let params = level.into_brotli(params.as_brotli());
64
65                Self {
66                    inner: crate::$($mod::)+generic::Encoder::new(
67                        inner,
68                        crate::codec::BrotliEncoder::new(params),
69                    ),
70                }
71            }
72        }
73        { @dec }
74        );
75
76        algos!(@algo bzip2 ["bzip2"] BzDecoder BzEncoder <$inner>
77        { @enc
78
79            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
80                Self {
81                    inner: crate::$($mod::)+generic::Encoder::new(
82                        inner,
83                        crate::codec::BzEncoder::new(level.into_bzip2(), 0),
84                    ),
85                }
86            }
87        }
88        { @dec }
89        );
90
91        algos!(@algo deflate ["deflate"] DeflateDecoder DeflateEncoder <$inner>
92        { @enc
93            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
94                Self {
95                    inner: crate::$($mod::)+generic::Encoder::new(
96                        inner,
97                        crate::codec::DeflateEncoder::new(level.into_flate2()),
98                    ),
99                }
100            }
101
102            /// Returns the total number of input bytes which have been processed by this compression object.
103            pub fn total_in(&self) -> u64 {
104                self.inner.get_encoder_ref().get_ref().get_ref().total_in()
105            }
106
107            /// Returns the total number of output bytes which have been produced by this compression object.
108            pub fn total_out(&self) -> u64 {
109                self.inner.get_encoder_ref().get_ref().get_ref().total_out()
110            }
111        }
112        { @dec }
113        );
114
115        algos!(@algo deflate ["deflate64"] Deflate64Decoder Deflate64Encoder <$inner>
116        { @dec }
117        );
118
119        algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder <$inner>
120        { @enc
121
122            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
123                Self {
124                    inner: crate::$($mod::)+generic::Encoder::new(
125                        inner,
126                        crate::codec::GzipEncoder::new(level.into_flate2()),
127                    ),
128                }
129            }
130        }
131        { @dec }
132        );
133
134        algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder <$inner>
135        { @enc
136            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
137                Self {
138                    inner: crate::$($mod::)+generic::Encoder::new(
139                        inner,
140                        crate::codec::ZlibEncoder::new(level.into_flate2()),
141                    ),
142                }
143            }
144
145            /// Returns the total number of input bytes which have been processed by this compression object.
146            pub fn total_in(&self) -> u64 {
147                self.inner.get_encoder_ref().get_ref().get_ref().total_in()
148            }
149
150            /// Returns the total number of output bytes which have been produced by this compression object.
151            pub fn total_out(&self) -> u64 {
152                self.inner.get_encoder_ref().get_ref().get_ref().total_out()
153            }
154        }
155        { @dec }
156        );
157
158        algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder <$inner>
159        { @enc
160
161            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
162                Self {
163                    inner: crate::$($mod::)+generic::Encoder::new(
164                        inner,
165                        crate::codec::ZstdEncoder::new(level.into_zstd()),
166                    ),
167                }
168            }
169
170            /// Creates a new encoder, using the specified compression level and parameters, which
171            /// will read uncompressed data from the given stream and emit a compressed stream.
172            ///
173            /// # Panics
174            ///
175            /// Panics if this function is called with a [`CParameter::nb_workers()`] parameter and
176            /// the `zstdmt` crate feature is _not_ enabled.
177            ///
178            /// [`CParameter::nb_workers()`]: crate::zstd::CParameter
179            //
180            // TODO: remove panic note on next breaking release, along with `CParameter::nb_workers`
181            // change
182            pub fn with_quality_and_params(inner: $inner, level: crate::Level, params: &[crate::zstd::CParameter]) -> Self {
183                Self {
184                    inner: crate::$($mod::)+generic::Encoder::new(
185                        inner,
186                        crate::codec::ZstdEncoder::new_with_params(level.into_zstd(), params),
187                    ),
188                }
189            }
190
191            /// Creates a new encoder, using the specified compression level and pre-trained
192            /// dictionary, which will read uncompressed data from the given stream and emit a
193            /// compressed stream.
194            ///
195            /// Dictionaries provide better compression ratios for small files, but are required to
196            /// be present during decompression.
197            ///
198            /// # Errors
199            ///
200            /// Returns error when `dictionary` is not valid.
201            pub fn with_dict(inner: $inner, level: crate::Level, dictionary: &[u8]) -> ::std::io::Result<Self> {
202                Ok(Self {
203                    inner: crate::$($mod::)+generic::Encoder::new(
204                        inner,
205                        crate::codec::ZstdEncoder::new_with_dict(level.into_zstd(), dictionary)?,
206                    ),
207                })
208            }
209        }
210        { @dec
211            /// Creates a new decoder, using the specified parameters, which will read compressed
212            /// data from the given stream and emit a decompressed stream.
213            pub fn with_params(inner: $inner, params: &[crate::zstd::DParameter]) -> Self {
214                Self {
215                    inner: crate::$($mod::)+generic::Decoder::new(
216                        inner,
217                        crate::codec::ZstdDecoder::new_with_params(params),
218                    ),
219                }
220            }
221
222            /// Creates a new decoder, using the specified compression level and pre-trained
223            /// dictionary, which will read compressed data from the given stream and emit an
224            /// uncompressed stream.
225            ///
226            /// Dictionaries provide better compression ratios for small files, but are required to
227            /// be present during decompression. The dictionary used must be the same as the one
228            /// used for compression.
229            ///
230            /// # Errors
231            ///
232            /// Returns error when `dictionary` is not valid.
233            pub fn with_dict(inner: $inner, dictionary: &[u8]) -> ::std::io::Result<Self> {
234                Ok(Self {
235                    inner: crate::$($mod::)+generic::Decoder::new(
236                        inner,
237                        crate::codec::ZstdDecoder::new_with_dict(dictionary)?,
238                    ),
239                })
240            }
241        }
242        );
243
244        algos!(@algo xz ["xz"] XzDecoder XzEncoder <$inner>
245        { @enc
246
247            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
248                Self {
249                    inner: crate::$($mod::)+generic::Encoder::new(
250                        inner,
251                        crate::codec::XzEncoder::new(level.into_xz2()),
252                    ),
253                }
254            }
255        }
256        { @dec
257            /// Creates a new decoder with the specified limit of memory.
258            ///
259            /// # Errors
260            ///
261            /// An IO error may be returned during decoding if the specified limit is too small.
262            pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
263                Self {
264                    inner: crate::$($mod::)+generic::Decoder::new(
265                        read,
266                        crate::codec::XzDecoder::with_memlimit(memlimit),
267                    ),
268                }
269            }
270        }
271        );
272
273        algos!(@algo lzma ["lzma"] LzmaDecoder LzmaEncoder <$inner>
274        { @enc
275
276            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
277                Self {
278                    inner: crate::$($mod::)+generic::Encoder::new(
279                        inner,
280                        crate::codec::LzmaEncoder::new(level.into_xz2()),
281                    ),
282                }
283            }
284        }
285        { @dec
286            /// Creates a new decoder with the specified limit of memory.
287            ///
288            /// # Errors
289            ///
290            /// An IO error may be returned during decoding if the specified limit is too small.
291            pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
292                Self {
293                    inner: crate::$($mod::)+generic::Decoder::new(
294                        read,
295                        crate::codec::LzmaDecoder::with_memlimit(memlimit),
296                    ),
297                }
298            }
299
300        }
301        );
302
303        algos!(@algo lz4 ["lz4"] Lz4Decoder Lz4Encoder <$inner>
304        { @enc
305
306            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
307                Self::with_quality_and_params(inner, level, crate::lz4::EncoderParams::default())
308            }
309
310            /// Creates a new encoder, using the specified compression level and parameters, which
311            /// will read uncompressed data from the given stream and emit a compressed stream.
312            pub fn with_quality_and_params(
313                inner: $inner,
314                level: crate::Level,
315                params: crate::lz4::EncoderParams,
316            ) -> Self {
317                let encoder = crate::codec::Lz4Encoder::new(level.into_lz4(params.as_lz4()));
318                let cap = encoder.buffer_size();
319                Self {
320                    inner: crate::$($mod::)+generic::Encoder::with_capacity(
321                        inner,
322                        encoder,
323                        cap,
324                    ),
325                }
326            }
327        }
328        { @dec }
329        );
330
331    }
332}