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 {
14            _data if _data.eq_ignore_ascii_case(CONTENT_ENCODING_GZIP) => Ok(Self::Gzip),
15            _data if _data.eq_ignore_ascii_case(CONTENT_ENCODING_DEFLATE) => Ok(Self::Deflate),
16            _data if _data.eq_ignore_ascii_case(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: &DashMap<String, String, RandomState>) -> Self {
61        let mut compress: Compress = Self::default();
62        for tem in header {
63            let key: &String = tem.key();
64            let value: &String = tem.value();
65            if key.eq_ignore_ascii_case(CONTENT_ENCODING) {
66                compress = value.parse::<Compress>().unwrap_or_default();
67                break;
68            }
69        }
70        compress
71    }
72
73    /// Decompresses the given data based on the selected compression algorithm.
74    ///
75    /// This method takes a byte slice of compressed data and decompresses it using one of the following
76    /// compression algorithms, depending on the variant of the enum it is called on:
77    /// - `Gzip` - Decompresses using Gzip compression.
78    /// - `Deflate` - Decompresses using Deflate compression.
79    /// - `Br` - Decompresses using Brotli compression.
80    /// - `Unknown` - Returns the input data as-is (no decompression performed).
81    ///
82    /// # Parameters
83    /// - `data` - A reference to a byte slice (`&[u8]`) containing the compressed data to be decoded.
84    /// - `buffer_size` - The buffer size to use for the decompression process. A larger buffer size can
85    ///   improve performance for larger datasets.
86    ///
87    /// # Returns
88    /// - `Cow<Vec<u8>>` - The decompressed data as a `Cow<Vec<u8>>`. If the compression algorithm
89    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
90    ///   the decompressed data is returned as an owned `Vec<u8>`.
91    #[inline]
92    pub fn decode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
93        match self {
94            Self::Gzip => gzip::decode::decode(data, buffer_size),
95            Self::Deflate => deflate::decode::decode(data, buffer_size),
96            Self::Br => brotli::decode::decode(data, buffer_size),
97            Self::Unknown => Cow::Owned(data.to_vec()),
98        }
99    }
100
101    /// Compresses the given data based on the selected compression algorithm.
102    ///
103    /// This method takes a byte slice of data and compresses it using one of the following
104    /// compression algorithms, depending on the variant of the enum it is called on:
105    /// - `Gzip` - Compresses using Gzip compression.
106    /// - `Deflate` - Compresses using Deflate compression.
107    /// - `Br` - Compresses using Brotli compression.
108    /// - `Unknown` - Returns the input data as-is (no compression performed).
109    ///
110    /// # Parameters
111    /// - `data` - A reference to a byte slice (`&[u8]`) containing the data to be compressed.
112    /// - `buffer_size` - The buffer size to use for the compression process. A larger buffer size can
113    ///   improve performance for larger datasets.
114    ///
115    /// # Returns
116    /// - `Cow<Vec<u8>>` - The compressed data as a `Cow<Vec<u8>>`. If the compression algorithm
117    ///   is `Unknown`, the original data is returned unchanged, as a borrowed reference. Otherwise,
118    ///   the compressed data is returned as an owned `Vec<u8>`.
119    #[inline]
120    pub fn encode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, Vec<u8>> {
121        match self {
122            Self::Gzip => gzip::encode::encode(data, buffer_size),
123            Self::Deflate => deflate::encode::encode(data, buffer_size),
124            Self::Br => brotli::encode::encode(data),
125            Self::Unknown => Cow::Owned(data.to_vec()),
126        }
127    }
128}