use super::*;
impl FromStr for Compress {
type Err = ();
#[inline(always)]
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 {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl Compress {
#[inline(always)]
pub fn is_unknown(&self) -> bool {
*self == Self::Unknown
}
#[inline(always)]
pub fn as_str(&self) -> &'static str {
match *self {
Self::Gzip => CONTENT_ENCODING_GZIP,
Self::Deflate => CONTENT_ENCODING_DEFLATE,
Self::Br => CONTENT_ENCODING_BROTLI,
Self::Unknown => EMPTY_STR,
}
}
#[inline]
pub fn from_accept_encoding<H>(header: H) -> Option<Self>
where
H: AsRef<str>,
{
let mut brotli: Option<f32> = None;
let mut gzip: Option<f32> = None;
let mut deflate: Option<f32> = None;
let mut wildcard: Option<f32> = None;
for item in header.as_ref().split(ACCEPT_ENCODING_SEPARATOR) {
let Some((coding, quality)) = Self::split_accept_item(item) else {
continue;
};
if coding == ACCEPT_ENCODING_WILDCARD {
wildcard = Some(quality);
} else if coding.eq_ignore_ascii_case(CONTENT_ENCODING_BROTLI) {
brotli = Some(quality);
} else if coding.eq_ignore_ascii_case(CONTENT_ENCODING_GZIP) {
gzip = Some(quality);
} else if coding.eq_ignore_ascii_case(CONTENT_ENCODING_DEFLATE) {
deflate = Some(quality);
}
}
let candidates: [(Self, Option<f32>); 3] = [
(Self::Br, brotli),
(Self::Gzip, gzip),
(Self::Deflate, deflate),
];
let mut best: Option<Self> = None;
let mut best_quality: f32 = 0.0;
for (coding, quality) in candidates {
let quality: f32 = quality.or(wildcard).unwrap_or(0.0);
if quality <= best_quality {
continue;
}
best = Some(coding);
best_quality = quality;
}
best
}
#[inline]
fn split_accept_item(item: &str) -> Option<(&str, f32)> {
let (coding, parameters): (&str, &str) = match item.split_once(PARAMETER_SEPARATOR) {
Some((coding, parameters)) => (coding, parameters),
None => (item, EMPTY_STR),
};
let coding: &str = coding.trim();
if coding.is_empty() {
return None;
}
for parameter in parameters.split(PARAMETER_SEPARATOR) {
let Some((name, value)) = parameter.split_once(PARAMETER_ASSIGN) else {
continue;
};
if !name.trim().eq_ignore_ascii_case(ACCEPT_ENCODING_QUALITY) {
continue;
}
let quality: f32 = value.trim().parse().ok()?;
if !(0.0..=1.0).contains(&quality) {
return None;
}
return Some((coding, quality));
}
Some((coding, DEFAULT_QUALITY))
}
#[inline(always)]
pub fn from(header: &HashMap<String, String, BuildHasherDefault<XxHash3_64>>) -> Self {
header
.get(CONTENT_ENCODING)
.map(|value: &String| value.parse::<Compress>().unwrap_or_default())
.unwrap_or_default()
}
pub fn decode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, [u8]> {
match self {
Self::Gzip => gzip::decode(data, buffer_size),
Self::Deflate => deflate::decode(data, buffer_size),
Self::Br => brotli::decode(data, buffer_size),
Self::Unknown => Cow::Owned(data.to_vec()),
}
}
pub fn encode<'a>(&self, data: &'a [u8], buffer_size: usize) -> Cow<'a, [u8]> {
match self {
Self::Gzip => gzip::encode(data, buffer_size),
Self::Deflate => deflate::encode(data, buffer_size),
Self::Br => brotli::encode(data, buffer_size),
Self::Unknown => Cow::Owned(data.to_vec()),
}
}
}