http_types/content/
content_encoding.rs1use crate::content::{Encoding, EncodingProposal};
4use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, CONTENT_ENCODING};
5
6use std::fmt::{self, Debug};
7use std::ops::{Deref, DerefMut};
8use std::option;
9
10pub struct ContentEncoding {
34 inner: Encoding,
35}
36
37impl ContentEncoding {
38 pub fn new(encoding: Encoding) -> Self {
40 Self { inner: encoding }
41 }
42
43 pub fn from_headers(headers: impl AsRef<Headers>) -> crate::Result<Option<Self>> {
45 let headers = match headers.as_ref().get(CONTENT_ENCODING) {
46 Some(headers) => headers,
47 None => return Ok(None),
48 };
49
50 let mut inner = None;
51
52 for value in headers {
53 if let Some(entry) = Encoding::from_str(value.as_str()) {
54 inner = Some(entry);
55 }
56 }
57
58 let inner = inner.expect("Headers instance with no entries found");
59 Ok(Some(Self { inner }))
60 }
61
62 pub fn apply(&self, mut headers: impl AsMut<Headers>) {
64 headers.as_mut().insert(CONTENT_ENCODING, self.value());
65 }
66
67 pub fn name(&self) -> HeaderName {
69 CONTENT_ENCODING
70 }
71
72 pub fn value(&self) -> HeaderValue {
74 self.inner.into()
75 }
76
77 pub fn encoding(&self) -> Encoding {
79 self.inner
80 }
81}
82
83impl ToHeaderValues for ContentEncoding {
84 type Iter = option::IntoIter<HeaderValue>;
85 fn to_header_values(&self) -> crate::Result<Self::Iter> {
86 Ok(self.value().to_header_values().unwrap())
88 }
89}
90
91impl Deref for ContentEncoding {
92 type Target = Encoding;
93 fn deref(&self) -> &Self::Target {
94 &self.inner
95 }
96}
97
98impl DerefMut for ContentEncoding {
99 fn deref_mut(&mut self) -> &mut Self::Target {
100 &mut self.inner
101 }
102}
103
104impl PartialEq<Encoding> for ContentEncoding {
105 fn eq(&self, other: &Encoding) -> bool {
106 &self.inner == other
107 }
108}
109
110impl PartialEq<&Encoding> for ContentEncoding {
111 fn eq(&self, other: &&Encoding) -> bool {
112 &&self.inner == other
113 }
114}
115
116impl From<Encoding> for ContentEncoding {
117 fn from(encoding: Encoding) -> Self {
118 Self { inner: encoding }
119 }
120}
121
122impl From<&Encoding> for ContentEncoding {
123 fn from(encoding: &Encoding) -> Self {
124 Self { inner: *encoding }
125 }
126}
127
128impl From<EncodingProposal> for ContentEncoding {
129 fn from(encoding: EncodingProposal) -> Self {
130 Self {
131 inner: encoding.encoding,
132 }
133 }
134}
135
136impl From<&EncodingProposal> for ContentEncoding {
137 fn from(encoding: &EncodingProposal) -> Self {
138 Self {
139 inner: encoding.encoding,
140 }
141 }
142}
143
144impl Debug for ContentEncoding {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 self.inner.fmt(f)
147 }
148}