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        { @dec }
103        );
104
105        algos!(@algo deflate ["deflate64"] Deflate64Decoder Deflate64Encoder <$inner>
106        { @dec }
107        );
108
109        algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder <$inner>
110        { @enc
111
112            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
113                Self {
114                    inner: crate::$($mod::)+generic::Encoder::new(
115                        inner,
116                        crate::codec::GzipEncoder::new(level.into_flate2()),
117                    ),
118                }
119            }
120        }
121        { @dec }
122        );
123
124        algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder <$inner>
125        { @enc
126            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
127                Self {
128                    inner: crate::$($mod::)+generic::Encoder::new(
129                        inner,
130                        crate::codec::ZlibEncoder::new(level.into_flate2()),
131                    ),
132                }
133            }
134
135            /// Returns the total number of input bytes which have been processed by this compression object.
136            pub fn total_in(&self) -> u64 {
137                self.inner.get_encoder_ref().get_ref().get_ref().total_in()
138            }
139
140            /// Returns the total number of output bytes which have been produced by this compression object.
141            pub fn total_out(&self) -> u64 {
142                self.inner.get_encoder_ref().get_ref().get_ref().total_out()
143            }
144        }
145        { @dec }
146        );
147
148        algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder <$inner>
149        { @enc
150
151            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
152                Self {
153                    inner: crate::$($mod::)+generic::Encoder::new(
154                        inner,
155                        crate::codec::ZstdEncoder::new(level.into_zstd()),
156                    ),
157                }
158            }
159
160            /// Creates a new encoder, using the specified compression level and parameters, which
161            /// will read uncompressed data from the given stream and emit a compressed stream.
162            ///
163            /// # Panics
164            ///
165            /// Panics if this function is called with a [`CParameter::nb_workers()`] parameter and
166            /// the `zstdmt` crate feature is _not_ enabled.
167            ///
168            /// [`CParameter::nb_workers()`]: crate::zstd::CParameter
169            //
170            // TODO: remove panic note on next breaking release, along with `CParameter::nb_workers`
171            // change
172            pub fn with_quality_and_params(inner: $inner, level: crate::Level, params: &[crate::zstd::CParameter]) -> Self {
173                Self {
174                    inner: crate::$($mod::)+generic::Encoder::new(
175                        inner,
176                        crate::codec::ZstdEncoder::new_with_params(level.into_zstd(), params),
177                    ),
178                }
179            }
180
181            /// Creates a new encoder, using the specified compression level and pre-trained
182            /// dictionary, which will read uncompressed data from the given stream and emit a
183            /// compressed stream.
184            ///
185            /// Dictionaries provide better compression ratios for small files, but are required to
186            /// be present during decompression.
187            ///
188            /// # Errors
189            ///
190            /// Returns error when `dictionary` is not valid.
191            pub fn with_dict(inner: $inner, level: crate::Level, dictionary: &[u8]) -> ::std::io::Result<Self> {
192                Ok(Self {
193                    inner: crate::$($mod::)+generic::Encoder::new(
194                        inner,
195                        crate::codec::ZstdEncoder::new_with_dict(level.into_zstd(), dictionary)?,
196                    ),
197                })
198            }
199        }
200        { @dec
201            /// Creates a new decoder, using the specified compression level and pre-trained
202            /// dictionary, which will read compressed data from the given stream and emit an
203            /// uncompressed stream.
204            ///
205            /// Dictionaries provide better compression ratios for small files, but are required to
206            /// be present during decompression. The dictionary used must be the same as the one
207            /// used for compression.
208            ///
209            /// # Errors
210            ///
211            /// Returns error when `dictionary` is not valid.
212            pub fn with_dict(inner: $inner, dictionary: &[u8]) -> ::std::io::Result<Self> {
213                Ok(Self {
214                    inner: crate::$($mod::)+generic::Decoder::new(
215                        inner,
216                        crate::codec::ZstdDecoder::new_with_dict(dictionary)?,
217                    ),
218                })
219            }
220        }
221        );
222
223        algos!(@algo xz ["xz"] XzDecoder XzEncoder <$inner>
224        { @enc
225
226            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
227                Self {
228                    inner: crate::$($mod::)+generic::Encoder::new(
229                        inner,
230                        crate::codec::XzEncoder::new(level.into_xz2()),
231                    ),
232                }
233            }
234        }
235        { @dec
236            /// Creates a new decoder with the specified limit of memory.
237            ///
238            /// # Errors
239            ///
240            /// An IO error may be returned during decoding if the specified limit is too small.
241            pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
242                Self {
243                    inner: crate::$($mod::)+generic::Decoder::new(
244                        read,
245                        crate::codec::XzDecoder::with_memlimit(memlimit),
246                    ),
247                }
248            }
249        }
250        );
251
252        algos!(@algo lzma ["lzma"] LzmaDecoder LzmaEncoder <$inner>
253        { @enc
254
255            pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
256                Self {
257                    inner: crate::$($mod::)+generic::Encoder::new(
258                        inner,
259                        crate::codec::LzmaEncoder::new(level.into_xz2()),
260                    ),
261                }
262            }
263        }
264        { @dec
265            /// Creates a new decoder with the specified limit of memory.
266            ///
267            /// # Errors
268            ///
269            /// An IO error may be returned during decoding if the specified limit is too small.
270            pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
271                Self {
272                    inner: crate::$($mod::)+generic::Decoder::new(
273                        read,
274                        crate::codec::LzmaDecoder::with_memlimit(memlimit),
275                    ),
276                }
277            }
278
279        }
280        );
281    }
282}