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