actix_web/http/header/
encoding.rs

1use std::{fmt, 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 Encoding {
16    pub const fn identity() -> Self {
17        Self::Known(ContentEncoding::Identity)
18    }
19
20    pub const fn brotli() -> Self {
21        Self::Known(ContentEncoding::Brotli)
22    }
23
24    pub const fn deflate() -> Self {
25        Self::Known(ContentEncoding::Deflate)
26    }
27
28    pub const fn gzip() -> Self {
29        Self::Known(ContentEncoding::Gzip)
30    }
31
32    pub const fn zstd() -> Self {
33        Self::Known(ContentEncoding::Zstd)
34    }
35}
36
37impl fmt::Display for Encoding {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(match self {
40            Encoding::Known(enc) => enc.as_str(),
41            Encoding::Unknown(enc) => enc.as_str(),
42        })
43    }
44}
45
46impl str::FromStr for Encoding {
47    type Err = crate::error::ParseError;
48
49    fn from_str(enc: &str) -> Result<Self, crate::error::ParseError> {
50        match enc.parse::<ContentEncoding>() {
51            Ok(enc) => Ok(Self::Known(enc)),
52            Err(_) => Ok(Self::Unknown(enc.to_owned())),
53        }
54    }
55}