From ea181a6dc593962b2d0117f681a71faae5fda912 Mon Sep 17 00:00:00 2001 From: Basti Ortiz <39114273+Some-Dood@users.noreply.github.com> Date: Thu, 4 Nov 2021 14:56:15 +0800 Subject: [PATCH] Refactor: enable specific compression variants according to features --- src/filters/compression.rs | 26 ++++++++++++++++++++------ src/filters/mod.rs | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/filters/compression.rs b/src/filters/compression.rs index 7280da1ae..244e76835 100644 --- a/src/filters/compression.rs +++ b/src/filters/compression.rs @@ -2,7 +2,12 @@ //! //! Filters that compress the body of a response. -use async_compression::tokio::bufread::{BrotliEncoder, DeflateEncoder, GzipEncoder}; +#[cfg(feature = "compression-brotli")] +use async_compression::tokio::bufread::BrotliEncoder; + +#[cfg(feature = "compression-gzip")] +use async_compression::tokio::bufread::{DeflateEncoder, GzipEncoder}; + use http::header::HeaderValue; use hyper::{ header::{CONTENT_ENCODING, CONTENT_LENGTH}, @@ -17,19 +22,25 @@ use crate::reply::{Reply, Response}; use self::internal::{CompressionProps, WithCompression}; enum CompressionAlgo { + #[cfg(feature = "compression-brotli")] BR, + #[cfg(feature = "compression-gzip")] DEFLATE, + #[cfg(feature = "compression-gzip")] GZIP, } impl From for HeaderValue { #[inline] fn from(algo: CompressionAlgo) -> Self { - match algo { - CompressionAlgo::BR => HeaderValue::from_static("br"), - CompressionAlgo::DEFLATE => HeaderValue::from_static("deflate"), - CompressionAlgo::GZIP => HeaderValue::from_static("gzip"), - } + HeaderValue::from_static(match algo { + #[cfg(feature = "compression-brotli")] + CompressionAlgo::BR => "br", + #[cfg(feature = "compression-gzip")] + CompressionAlgo::DEFLATE => "deflate", + #[cfg(feature = "compression-gzip")] + CompressionAlgo::GZIP => "gzip", + }) } } @@ -55,6 +66,7 @@ pub struct Compression { /// .and(warp::fs::file("./README.md")) /// .with(warp::compression::gzip()); /// ``` +#[cfg(feature = "compression-gzip")] pub fn gzip() -> Compression Response + Copy> { let func = move |mut props: CompressionProps| { let body = Body::wrap_stream(ReaderStream::new(GzipEncoder::new(StreamReader::new( @@ -83,6 +95,7 @@ pub fn gzip() -> Compression Response + Copy> { /// .and(warp::fs::file("./README.md")) /// .with(warp::compression::deflate()); /// ``` +#[cfg(feature = "compression-gzip")] pub fn deflate() -> Compression Response + Copy> { let func = move |mut props: CompressionProps| { let body = Body::wrap_stream(ReaderStream::new(DeflateEncoder::new(StreamReader::new( @@ -111,6 +124,7 @@ pub fn deflate() -> Compression Response + Copy> { /// .and(warp::fs::file("./README.md")) /// .with(warp::compression::brotli()); /// ``` +#[cfg(feature = "compression-brotli")] pub fn brotli() -> Compression Response + Copy> { let func = move |mut props: CompressionProps| { let body = Body::wrap_stream(ReaderStream::new(BrotliEncoder::new(StreamReader::new( diff --git a/src/filters/mod.rs b/src/filters/mod.rs index fd1fb4400..bd1c48718 100644 --- a/src/filters/mod.rs +++ b/src/filters/mod.rs @@ -6,7 +6,7 @@ pub mod addr; pub mod any; pub mod body; -#[cfg(feature = "compression")] +#[cfg(any(feature = "compression-brotli", feature = "compression-gzip"))] pub mod compression; pub mod cookie; pub mod cors;