http_compress/compress/
impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use super::r#type::Compress;
use crate::{brotli, deflate, gzip};
use http_constant::*;
use std::{borrow::Cow, collections::HashMap, fmt, str::FromStr};

impl Default for Compress {
    fn default() -> Self {
        Self::Unknown
    }
}

impl FromStr for Compress {
    type Err = ();

    fn from_str(data: &str) -> Result<Self, Self::Err> {
        match data.to_lowercase().as_str() {
            _data if _data == CONTENT_ENCODING_GZIP => Ok(Self::Gzip),
            _data if _data == CONTENT_ENCODING_DEFLATE => Ok(Self::Deflate),
            _data if _data == CONTENT_ENCODING_BROTLI => Ok(Self::Br),
            _ => Ok(Self::Unknown),
        }
    }
}

impl fmt::Display for Compress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let display_str = match *self {
            Compress::Gzip => CONTENT_ENCODING_GZIP,
            Compress::Deflate => CONTENT_ENCODING_DEFLATE,
            Compress::Br => CONTENT_ENCODING_BROTLI,
            Compress::Unknown => EMPTY_STR,
        };
        write!(f, "{}", display_str)
    }
}

impl Compress {
    /// Checks if the current instance is of the `Unknown` type.
    ///
    /// This method compares the current instance with the `Unknown` variant of the enum.
    /// It returns `true` if the instance is of type `Unknown`, otherwise `false`.
    ///
    /// # Returns
    /// - `true` if the instance is of type `Unknown`.
    /// - `false` otherwise.
    #[inline]
    pub fn is_unknown(&self) -> bool {
        *self == Self::Unknown
    }

    /// Extracts the compression type from an HTTP header.
    ///
    /// This function looks for the `Content-Encoding` header in the provided `Header` and attempts
    /// to parse it into a `Compress` enum value.
    ///
    /// # Arguments
    /// - `header` - The HTTP header from which the compression type is to be extracted.
    ///
    /// # Returns
    /// - The `Compress` value corresponding to the `Content-Encoding` header, or `Compress::Unknown`
    ///   if the header does not match any known compression types.
    #[inline]
    pub fn from(header: &HashMap<String, String>) -> Self {
        let content_encoding_key: String = CONTENT_ENCODING.to_lowercase();
        let mut compress: Compress = Self::default();
        for (key, value) in header {
            if key.to_lowercase() == content_encoding_key {
                compress = value.parse::<Compress>().unwrap_or_default();
                break;
            }
        }
        compress
    }

    /// Decompresses the given data based on the selected compression algorithm.
    ///
    /// This method takes a byte slice of compressed data and decompresses it using one of the following
    /// compression algorithms, depending on the variant of the enum it is called on:
    /// - `Gzip` - Decompresses using Gzip compression.
    /// - `Deflate` - Decompresses using Deflate compression.
    /// - `Br` - Decompresses using Brotli compression.
    /// - `Unknown` - Returns the input data as-is (no decompression performed).
    ///
    /// # Parameters
    /// - `data` - A reference to a byte slice (`&[u8]`) containing the compressed data to be decoded.
    /// - `buffer_size` - The buffer size to use for the decompression process. A larger buffer size can
    ///   improve performance for larger datasets.
    ///
    /// # Returns
    /// - `Cow<Vec<u8>>` - The decompressed data as a `Cow<Vec<u8>>`. If the compression algorithm
    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
    ///   the decompressed data is returned as an owned `Vec<u8>`.
    #[inline]
    pub fn decode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
        match self {
            Self::Gzip => gzip::decode::decode(data, buffer_size),
            Self::Deflate => deflate::decode::decode(data, buffer_size),
            Self::Br => brotli::decode::decode(data, buffer_size),
            Self::Unknown => Cow::Owned(data.to_vec()),
        }
    }

    /// Compresses the given data based on the selected compression algorithm.
    ///
    /// This method takes a byte slice of data and compresses it using one of the following
    /// compression algorithms, depending on the variant of the enum it is called on:
    /// - `Gzip` - Compresses using Gzip compression.
    /// - `Deflate` - Compresses using Deflate compression.
    /// - `Br` - Compresses using Brotli compression.
    /// - `Unknown` - Returns the input data as-is (no compression performed).
    ///
    /// # Parameters
    /// - `data` - A reference to a byte slice (`&[u8]`) containing the data to be compressed.
    /// - `buffer_size` - The buffer size to use for the compression process. A larger buffer size can
    ///   improve performance for larger datasets.
    ///
    /// # Returns
    /// - `Cow<Vec<u8>>` - The compressed data as a `Cow<Vec<u8>>`. If the compression algorithm
    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
    ///   the compressed data is returned as an owned `Vec<u8>`.
    #[inline]
    pub fn encode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
        match self {
            Self::Gzip => gzip::encode::encode(data, buffer_size),
            Self::Deflate => deflate::encode::encode(data, buffer_size),
            Self::Br => brotli::encode::encode(data),
            Self::Unknown => Cow::Owned(data.to_vec()),
        }
    }
}