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    pub fn is_unknown(&self) -> bool {
44        *self == Self::Unknown
45    }
46
47    /// Extracts the compression type from an HTTP header.
48    ///
49    /// This function looks for the `Content-Encoding` header in the provided `Header` and attempts
50    /// to parse it into a `Compress` enum value.
51    ///
52    /// # Arguments
53    /// - `header` - The HTTP header from which the compression type is to be extracted.
54    ///
55    /// # Returns
56    /// - The `Compress` value corresponding to the `Content-Encoding` header, or `Compress::Unknown`
57    ///   if the header does not match any known compression types.
58    pub fn from(header: &HashMap<String, String, BuildHasherDefault<XxHash3_64>>) -> Self {
59        header
60            .get(CONTENT_ENCODING)
61            .map(|value| value.parse::<Compress>().unwrap_or_default())
62            .unwrap_or_default()
63    }
64
65    /// Decompresses the given data based on the selected compression algorithm.
66    ///
67    /// This method takes a byte slice of compressed data and decompresses it using one of the following
68    /// compression algorithms, depending on the variant of the enum it is called on:
69    /// - `Gzip` - Decompresses using Gzip compression.
70    /// - `Deflate` - Decompresses using Deflate compression.
71    /// - `Br` - Decompresses using Brotli compression.
72    /// - `Unknown` - Returns the input data as-is (no decompression performed).
73    ///
74    /// # Parameters
75    /// - `data` - A reference to a byte slice (`&[u8]`) containing the compressed data to be decoded.
76    /// - `buffer_size` - The buffer size to use for the decompression process. A larger buffer size can
77    ///   improve performance for larger datasets.
78    ///
79    /// # Returns
80    /// - `Cow<Vec<u8>>` - The decompressed data as a `Cow<Vec<u8>>`. If the compression algorithm
81    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
82    ///   the decompressed data is returned as an owned `Vec<u8>`.
83    pub fn decode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
84        match self {
85            Self::Gzip => gzip::decode::decode(data, buffer_size),
86            Self::Deflate => deflate::decode::decode(data, buffer_size),
87            Self::Br => brotli::decode::decode(data, buffer_size),
88            Self::Unknown => Cow::Owned(data.to_vec()),
89        }
90    }
91
92    /// Compresses the given data based on the selected compression algorithm.
93    ///
94    /// This method takes a byte slice of data and compresses it using one of the following
95    /// compression algorithms, depending on the variant of the enum it is called on:
96    /// - `Gzip` - Compresses using Gzip compression.
97    /// - `Deflate` - Compresses using Deflate compression.
98    /// - `Br` - Compresses using Brotli compression.
99    /// - `Unknown` - Returns the input data as-is (no compression performed).
100    ///
101    /// # Parameters
102    /// - `data` - A reference to a byte slice (`&[u8]`) containing the data to be compressed.
103    /// - `buffer_size` - The buffer size to use for the compression process. A larger buffer size can
104    ///   improve performance for larger datasets.
105    ///
106    /// # Returns
107    /// - `Cow<Vec<u8>>` - The compressed data as a `Cow<Vec<u8>>`. If the compression algorithm
108    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
109    ///   the compressed data is returned as an owned `Vec<u8>`.
110    pub fn encode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
111        match self {
112            Self::Gzip => gzip::encode::encode(data, buffer_size),
113            Self::Deflate => deflate::encode::encode(data, buffer_size),
114            Self::Br => brotli::encode::encode(data),
115            Self::Unknown => Cow::Owned(data.to_vec()),
116        }
117    }
118}