http_compress/compress/
impl.rs

1use crate::*;
2
3impl Default for Compress {
4    fn default() -> Self {
5        Self::Unknown
6    }
7}
8
9impl FromStr for Compress {
10    type Err = ();
11
12    fn from_str(data: &str) -> Result<Self, Self::Err> {
13        match data.to_lowercase().as_str() {
14            _data if _data == CONTENT_ENCODING_GZIP => Ok(Self::Gzip),
15            _data if _data == CONTENT_ENCODING_DEFLATE => Ok(Self::Deflate),
16            _data if _data == CONTENT_ENCODING_BROTLI => Ok(Self::Br),
17            _ => Ok(Self::Unknown),
18        }
19    }
20}
21
22impl fmt::Display for Compress {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        let display_str = match *self {
25            Compress::Gzip => CONTENT_ENCODING_GZIP,
26            Compress::Deflate => CONTENT_ENCODING_DEFLATE,
27            Compress::Br => CONTENT_ENCODING_BROTLI,
28            Compress::Unknown => EMPTY_STR,
29        };
30        write!(f, "{}", display_str)
31    }
32}
33
34impl Compress {
35    /// Checks if the current instance is of the `Unknown` type.
36    ///
37    /// This method compares the current instance with the `Unknown` variant of the enum.
38    /// It returns `true` if the instance is of type `Unknown`, otherwise `false`.
39    ///
40    /// # Returns
41    /// - `true` if the instance is of type `Unknown`.
42    /// - `false` otherwise.
43    #[inline]
44    pub fn is_unknown(&self) -> bool {
45        *self == Self::Unknown
46    }
47
48    /// Extracts the compression type from an HTTP header.
49    ///
50    /// This function looks for the `Content-Encoding` header in the provided `Header` and attempts
51    /// to parse it into a `Compress` enum value.
52    ///
53    /// # Arguments
54    /// - `header` - The HTTP header from which the compression type is to be extracted.
55    ///
56    /// # Returns
57    /// - The `Compress` value corresponding to the `Content-Encoding` header, or `Compress::Unknown`
58    ///   if the header does not match any known compression types.
59    #[inline]
60    pub fn from(header: &HashMap<String, String>) -> Self {
61        let content_encoding_key: String = CONTENT_ENCODING.to_lowercase();
62        let mut compress: Compress = Self::default();
63        for (key, value) in header {
64            if key.to_lowercase() == content_encoding_key {
65                compress = value.parse::<Compress>().unwrap_or_default();
66                break;
67            }
68        }
69        compress
70    }
71
72    /// Decompresses the given data based on the selected compression algorithm.
73    ///
74    /// This method takes a byte slice of compressed data and decompresses it using one of the following
75    /// compression algorithms, depending on the variant of the enum it is called on:
76    /// - `Gzip` - Decompresses using Gzip compression.
77    /// - `Deflate` - Decompresses using Deflate compression.
78    /// - `Br` - Decompresses using Brotli compression.
79    /// - `Unknown` - Returns the input data as-is (no decompression performed).
80    ///
81    /// # Parameters
82    /// - `data` - A reference to a byte slice (`&[u8]`) containing the compressed data to be decoded.
83    /// - `buffer_size` - The buffer size to use for the decompression process. A larger buffer size can
84    ///   improve performance for larger datasets.
85    ///
86    /// # Returns
87    /// - `Cow<Vec<u8>>` - The decompressed data as a `Cow<Vec<u8>>`. If the compression algorithm
88    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
89    ///   the decompressed data is returned as an owned `Vec<u8>`.
90    #[inline]
91    pub fn decode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
92        match self {
93            Self::Gzip => gzip::decode::decode(data, buffer_size),
94            Self::Deflate => deflate::decode::decode(data, buffer_size),
95            Self::Br => brotli::decode::decode(data, buffer_size),
96            Self::Unknown => Cow::Owned(data.to_vec()),
97        }
98    }
99
100    /// Compresses the given data based on the selected compression algorithm.
101    ///
102    /// This method takes a byte slice of data and compresses it using one of the following
103    /// compression algorithms, depending on the variant of the enum it is called on:
104    /// - `Gzip` - Compresses using Gzip compression.
105    /// - `Deflate` - Compresses using Deflate compression.
106    /// - `Br` - Compresses using Brotli compression.
107    /// - `Unknown` - Returns the input data as-is (no compression performed).
108    ///
109    /// # Parameters
110    /// - `data` - A reference to a byte slice (`&[u8]`) containing the data to be compressed.
111    /// - `buffer_size` - The buffer size to use for the compression process. A larger buffer size can
112    ///   improve performance for larger datasets.
113    ///
114    /// # Returns
115    /// - `Cow<Vec<u8>>` - The compressed data as a `Cow<Vec<u8>>`. If the compression algorithm
116    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
117    ///   the compressed data is returned as an owned `Vec<u8>`.
118    #[inline]
119    pub fn encode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
120        match self {
121            Self::Gzip => gzip::encode::encode(data, buffer_size),
122            Self::Deflate => deflate::encode::encode(data, buffer_size),
123            Self::Br => brotli::encode::encode(data),
124            Self::Unknown => Cow::Owned(data.to_vec()),
125        }
126    }
127}