Skip to content

Commit

Permalink
Refactor: enable specific compression variants according to features
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood authored and seanmonstar committed Nov 4, 2021
1 parent 41986d2 commit ea181a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/filters/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<CompressionAlgo> 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",
})
}
}

Expand All @@ -55,6 +66,7 @@ pub struct Compression<F> {
/// .and(warp::fs::file("./README.md"))
/// .with(warp::compression::gzip());
/// ```
#[cfg(feature = "compression-gzip")]
pub fn gzip() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
let func = move |mut props: CompressionProps| {
let body = Body::wrap_stream(ReaderStream::new(GzipEncoder::new(StreamReader::new(
Expand Down Expand Up @@ -83,6 +95,7 @@ pub fn gzip() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
/// .and(warp::fs::file("./README.md"))
/// .with(warp::compression::deflate());
/// ```
#[cfg(feature = "compression-gzip")]
pub fn deflate() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
let func = move |mut props: CompressionProps| {
let body = Body::wrap_stream(ReaderStream::new(DeflateEncoder::new(StreamReader::new(
Expand Down Expand Up @@ -111,6 +124,7 @@ pub fn deflate() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
/// .and(warp::fs::file("./README.md"))
/// .with(warp::compression::brotli());
/// ```
#[cfg(feature = "compression-brotli")]
pub fn brotli() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
let func = move |mut props: CompressionProps| {
let body = Body::wrap_stream(ReaderStream::new(BrotliEncoder::new(StreamReader::new(
Expand Down
2 changes: 1 addition & 1 deletion src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ea181a6

Please sign in to comment.