Skip to main content

actix_web/http/header/
encoding.rs

1use std::str;
2
3use actix_http::ContentEncoding;
4
5/// A value to represent an encoding used in the `Accept-Encoding` and `Content-Encoding` header.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub enum Encoding {
8    /// A supported content encoding. See [`ContentEncoding`] for variants.
9    Known(ContentEncoding),
10
11    /// Some other encoding that is less common, can be any string.
12    Unknown(String),
13}
14
15impl_more::impl_display_enum! {
16    Encoding:
17    Known(enc) => ("{}", enc.as_str()),
18    Unknown(enc) => "{enc}",
19}
20
21impl Encoding {
22    pub const fn identity() -> Self {
23        Self::Known(ContentEncoding::Identity)
24    }
25
26    pub const fn brotli() -> Self {
27        Self::Known(ContentEncoding::Brotli)
28    }
29
30    pub const fn deflate() -> Self {
31        Self::Known(ContentEncoding::Deflate)
32    }
33
34    pub const fn gzip() -> Self {
35        Self::Known(ContentEncoding::Gzip)
36    }
37
38    pub const fn zstd() -> Self {
39        Self::Known(ContentEncoding::Zstd)
40    }
41}
42
43impl str::FromStr for Encoding {
44    type Err = crate::error::ParseError;
45
46    fn from_str(enc: &str) -> Result<Self, crate::error::ParseError> {
47        match enc.parse::<ContentEncoding>() {
48            Ok(enc) => Ok(Self::Known(enc)),
49            Err(_) => Ok(Self::Unknown(enc.to_owned())),
50        }
51    }
52}