use crate::enums::http_error::HttpError;
use core::fmt;
use std::str::FromStr;
use super::http_error::HttpErrorKind;
const COMPRESS: &str = "compressed";
pub const DEFLATE: &str = "deflate";
pub const GZIP: &str = "gzip";
pub const IDENTITY: &str = "identity";
const BROTLI: &str = "br";
const Z_STANDARD: &str = "zstd";
#[derive(Clone, PartialEq, Copy)]
pub enum ContentEncoding {
Gzip,
Compress,
Deflate,
Br,
ZStandard,
}
impl FromStr for ContentEncoding {
type Err = HttpError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let encoding = match s.trim() {
BROTLI => Self::Br,
COMPRESS => Self::Compress,
DEFLATE => Self::Deflate,
GZIP => Self::Gzip,
Z_STANDARD => Self::ZStandard,
_ => return Err(HttpErrorKind::UnsupportedContentEncoding.into()),
};
Ok(encoding)
}
}
impl fmt::Display for ContentEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let encoding = match self {
Self::Br => BROTLI,
Self::Compress => COMPRESS,
Self::Deflate => DEFLATE,
Self::Gzip => GZIP,
Self::ZStandard => Z_STANDARD,
};
write!(f, "{}", encoding)
}
}
impl fmt::Debug for ContentEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}