kutil_http/headers/
encoding.rs

1use super::into::*;
2
3use {
4    http::header::*,
5    kutil_std::string::*,
6    kutil_transcoding::*,
7    std::{convert::*, fmt, str::*},
8};
9
10impl IntoHeaderValue for Encoding {
11    fn into_header_value(self) -> HeaderValue {
12        let value: EncodingHeaderValue = self.into();
13        value.into()
14    }
15}
16
17//
18// EncodingHeaderValue
19//
20
21/// [Encoding] header value.
22#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
23pub enum EncodingHeaderValue {
24    /// Identity.
25    #[default]
26    Identity,
27
28    /// Brotli.
29    Brotli,
30
31    /// Deflate.
32    Deflate,
33
34    /// GZip.
35    GZip,
36
37    /// Zstandard.
38    Zstandard,
39}
40
41impl From<Encoding> for EncodingHeaderValue {
42    fn from(encoding: Encoding) -> Self {
43        match encoding {
44            Encoding::Identity => Self::Identity,
45            Encoding::Brotli => Self::Brotli,
46            Encoding::Deflate => Self::Deflate,
47            Encoding::GZip => Self::GZip,
48            Encoding::Zstandard => Self::Zstandard,
49        }
50    }
51}
52
53impl Into<Encoding> for EncodingHeaderValue {
54    fn into(self) -> Encoding {
55        match self {
56            Self::Identity => Encoding::Identity,
57            Self::Brotli => Encoding::Brotli,
58            Self::Deflate => Encoding::Deflate,
59            Self::GZip => Encoding::GZip,
60            Self::Zstandard => Encoding::Zstandard,
61        }
62    }
63}
64
65impl Into<HeaderValue> for EncodingHeaderValue {
66    fn into(self) -> HeaderValue {
67        HeaderValue::from_static(match self {
68            Self::Identity => "identity",
69            Self::Brotli => "br",
70            Self::Deflate => "deflate",
71            Self::GZip => "gzip",
72            Self::Zstandard => "zstd",
73        })
74    }
75}
76
77impl FromStr for EncodingHeaderValue {
78    type Err = ParseError;
79
80    fn from_str(representation: &str) -> Result<Self, Self::Err> {
81        if representation.eq_ignore_ascii_case("identity") {
82            Ok(Self::Identity)
83        } else if representation.eq_ignore_ascii_case("br") {
84            Ok(Self::Brotli)
85        } else if representation.eq_ignore_ascii_case("deflate") {
86            Ok(Self::Deflate)
87        } else if representation.eq_ignore_ascii_case("gzip") {
88            Ok(Self::GZip)
89        } else if representation.eq_ignore_ascii_case("zstd") {
90            Ok(Self::Zstandard)
91        } else {
92            Err(format!("unsupported: {}", representation).into())
93        }
94    }
95}
96
97impl fmt::Display for EncodingHeaderValue {
98    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
99        fmt::Display::fmt(
100            match self {
101                Self::Identity => "identity",
102                Self::Brotli => "br",
103                Self::Deflate => "deflate",
104                Self::GZip => "gzip",
105                Self::Zstandard => "zstd",
106            },
107            formatter,
108        )
109    }
110}