1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-20 01:08:10 +00:00
actix-web/actix-web/src/http/header/encoding.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

2019-02-07 21:24:24 +00:00
use std::{fmt, str};
2022-01-03 13:17:57 +00:00
use actix_http::ContentEncoding;
2019-02-07 21:24:24 +00:00
2022-01-03 13:17:57 +00:00
/// A value to represent an encoding used in the `Accept-Encoding` and `Content-Encoding` header.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-02-07 21:24:24 +00:00
pub enum Encoding {
2022-01-03 13:17:57 +00:00
/// A supported content encoding. See [`ContentEncoding`] for variants.
Known(ContentEncoding),
2022-01-03 13:17:57 +00:00
/// Some other encoding that is less common, can be any string.
Unknown(String),
}
2022-01-03 13:17:57 +00:00
impl Encoding {
pub const fn identity() -> Self {
Self::Known(ContentEncoding::Identity)
}
2022-01-03 13:17:57 +00:00
pub const fn brotli() -> Self {
Self::Known(ContentEncoding::Brotli)
}
2022-01-03 13:17:57 +00:00
pub const fn deflate() -> Self {
Self::Known(ContentEncoding::Deflate)
}
2022-01-03 13:17:57 +00:00
pub const fn gzip() -> Self {
Self::Known(ContentEncoding::Gzip)
}
2022-01-03 13:17:57 +00:00
pub const fn zstd() -> Self {
Self::Known(ContentEncoding::Zstd)
}
2019-02-07 21:24:24 +00:00
}
impl fmt::Display for Encoding {
2019-12-07 18:46:51 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022-01-03 13:17:57 +00:00
f.write_str(match self {
Encoding::Known(enc) => enc.as_str(),
Encoding::Unknown(enc) => enc.as_str(),
2019-02-07 21:24:24 +00:00
})
}
}
impl str::FromStr for Encoding {
type Err = crate::error::ParseError;
2022-01-03 13:17:57 +00:00
fn from_str(enc: &str) -> Result<Self, crate::error::ParseError> {
match enc.parse::<ContentEncoding>() {
Ok(enc) => Ok(Self::Known(enc)),
Err(_) => Ok(Self::Unknown(enc.to_owned())),
2019-02-07 21:24:24 +00:00
}
}
}