1#![allow(unexpected_cfgs)]
33# | [`futures::io::AsyncBufRead`](futures_io::AsyncBufRead), [`futures::io::AsyncWrite`](futures_io::AsyncWrite)"
36)]
37#![cfg_attr(
38 not(feature = "futures-io"),
39 doc = "`futures-io` (*inactive*) | `futures::io::AsyncBufRead`, `futures::io::AsyncWrite`"
40)]
41# | [`tokio::io::AsyncBufRead`](::tokio::io::AsyncBufRead), [`tokio::io::AsyncWrite`](::tokio::io::AsyncWrite)"
44)]
45#![cfg_attr(
46 not(feature = "tokio"),
47 doc = "`tokio` (*inactive*) | `tokio::io::AsyncBufRead`, `tokio::io::AsyncWrite`"
48)]
49#, [`BrotliDecoder`](?search=BrotliDecoder)"
62)]
63#![cfg_attr(
64 not(feature = "brotli"),
65 doc = "`brotli` (*inactive*) | `BrotliEncoder`, `BrotliDecoder`"
66)]
67#, [`BzDecoder`](?search=BzDecoder)"
70)]
71#![cfg_attr(
72 not(feature = "bzip2"),
73 doc = "`bzip2` (*inactive*) | `BzEncoder`, `BzDecoder`"
74)]
75#, [`DeflateDecoder`](?search=DeflateDecoder)"
78)]
79#![cfg_attr(
80 not(feature = "deflate"),
81 doc = "`deflate` (*inactive*) | `DeflateEncoder`, `DeflateDecoder`"
82)]
83#, [`GzipDecoder`](?search=GzipDecoder)"
86)]
87#![cfg_attr(
88 not(feature = "gzip"),
89 doc = "`gzip` (*inactive*) | `GzipEncoder`, `GzipDecoder`"
90)]
91#, [`LzmaDecoder`](?search=LzmaDecoder)"
94)]
95#![cfg_attr(
96 not(feature = "lzma"),
97 doc = "`lzma` (*inactive*) | `LzmaEncoder`, `LzmaDecoder`"
98)]
99#, [`XzDecoder`](?search=XzDecoder)"
102)]
103#![cfg_attr(
104 not(feature = "xz"),
105 doc = "`xz` (*inactive*) | `XzEncoder`, `XzDecoder`"
106)]
107#, [`ZlibDecoder`](?search=ZlibDecoder)"
110)]
111#![cfg_attr(
112 not(feature = "zlib"),
113 doc = "`zlib` (*inactive*) | `ZlibEncoder`, `ZlibDecoder`"
114)]
115#, [`ZstdDecoder`](?search=ZstdDecoder)"
118)]
119#![cfg_attr(
120 not(feature = "zstd"),
121 doc = "`zstd` (*inactive*) | `ZstdEncoder`, `ZstdDecoder`"
122)]
123#"
126)]
127#![cfg_attr(
128 not(feature = "deflate64"),
129 doc = "`deflate64` (*inactive*) | (encoder not implemented), `Deflate64Decoder`"
130)]
131#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
134#![warn(
135 missing_docs,
136 rust_2018_idioms,
137 missing_copy_implementations,
138 missing_debug_implementations
139)]
140#![cfg_attr(not(all), allow(unused))]
141
142#[cfg(any(feature = "bzip2", feature = "flate2", feature = "lzma"))]
143use std::convert::TryInto;
144
145#[macro_use]
146mod macros;
147mod codec;
148
149#[cfg(feature = "futures-io")]
150pub mod futures;
151#[cfg(feature = "tokio")]
152pub mod tokio;
153
154mod unshared;
155mod util;
156
157#[cfg(feature = "brotli")]
158pub mod brotli;
159#[cfg(feature = "zstd")]
160pub mod zstd;
161
162#[non_exhaustive]
164#[derive(Clone, Copy, Debug)]
165pub enum Level {
166 Fastest,
168
169 Best,
171
172 Default,
174
175 Precise(i32),
179}
180
181impl Level {
182 #[cfg(feature = "brotli")]
183 fn into_brotli(
184 self,
185 mut params: ::brotli::enc::backward_references::BrotliEncoderParams,
186 ) -> ::brotli::enc::backward_references::BrotliEncoderParams {
187 match self {
188 Self::Fastest => params.quality = 0,
189 Self::Best => params.quality = 11,
190 Self::Precise(quality) => params.quality = quality.clamp(0, 11),
191 Self::Default => (),
192 }
193
194 params
195 }
196
197 #[cfg(feature = "bzip2")]
198 fn into_bzip2(self) -> bzip2::Compression {
199 let fastest = bzip2::Compression::fast();
200 let best = bzip2::Compression::best();
201
202 match self {
203 Self::Fastest => fastest,
204 Self::Best => best,
205 Self::Precise(quality) => bzip2::Compression::new(
206 quality
207 .try_into()
208 .unwrap_or(0)
209 .clamp(fastest.level(), best.level()),
210 ),
211 Self::Default => bzip2::Compression::default(),
212 }
213 }
214
215 #[cfg(feature = "flate2")]
216 fn into_flate2(self) -> flate2::Compression {
217 let fastest = flate2::Compression::fast();
218 let best = flate2::Compression::best();
219 let none = flate2::Compression::none();
220
221 match self {
222 Self::Fastest => fastest,
223 Self::Best => best,
224 Self::Precise(quality) => flate2::Compression::new(
225 quality
226 .try_into()
227 .unwrap_or(0)
228 .clamp(none.level(), best.level()),
229 ),
230 Self::Default => flate2::Compression::default(),
231 }
232 }
233
234 #[cfg(feature = "zstd")]
235 fn into_zstd(self) -> i32 {
236 let (fastest, best) = libzstd::compression_level_range().into_inner();
237 match self {
238 Self::Fastest => fastest,
239 Self::Best => best,
240 Self::Precise(quality) => quality.clamp(fastest, best),
241 Self::Default => libzstd::DEFAULT_COMPRESSION_LEVEL,
242 }
243 }
244
245 #[cfg(feature = "lzma")]
246 fn into_xz2(self) -> u32 {
247 match self {
248 Self::Fastest => 0,
249 Self::Best => 9,
250 Self::Precise(quality) => quality.try_into().unwrap_or(0).min(9),
251 Self::Default => 5,
252 }
253 }
254}