Skip to content

Commit fc60897

Browse files
committed
Rename EncodingDirective to Encoding
1 parent 0b3bd28 commit fc60897

File tree

4 files changed

+73
-72
lines changed

4 files changed

+73
-72
lines changed

src/content/content_encoding.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Specify the compression algorithm.
22
3-
use crate::content::EncodingDirective;
3+
use crate::content::Encoding;
44
use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, CONTENT_ENCODING};
55

66
use std::fmt::{self, Debug, Write};
@@ -20,23 +20,23 @@ use std::slice;
2020
/// # fn main() -> http_types::Result<()> {
2121
/// #
2222
/// use http_types::Response;
23-
/// use http_types::content::{ContentEncoding, EncodingDirective};
23+
/// use http_types::content::{ContentEncoding, Encoding};
2424
/// let mut entries = ContentEncoding::new();
25-
/// entries.push(EncodingDirective::Gzip);
26-
/// entries.push(EncodingDirective::Identity);
25+
/// entries.push(Encoding::Gzip);
26+
/// entries.push(Encoding::Identity);
2727
///
2828
/// let mut res = Response::new(200);
2929
/// entries.apply(&mut res);
3030
///
3131
/// let entries = ContentEncoding::from_headers(res)?.unwrap();
3232
/// let mut entries = entries.iter();
33-
/// assert_eq!(entries.next().unwrap(), &EncodingDirective::Gzip);
34-
/// assert_eq!(entries.next().unwrap(), &EncodingDirective::Identity);
33+
/// assert_eq!(entries.next().unwrap(), &Encoding::Gzip);
34+
/// assert_eq!(entries.next().unwrap(), &Encoding::Identity);
3535
/// #
3636
/// # Ok(()) }
3737
/// ```
3838
pub struct ContentEncoding {
39-
entries: Vec<EncodingDirective>,
39+
entries: Vec<Encoding>,
4040
}
4141

4242
impl ContentEncoding {
@@ -57,7 +57,7 @@ impl ContentEncoding {
5757
for part in value.as_str().trim().split(',') {
5858
// Try and parse a directive from a str. If the directive is
5959
// unkown we skip it.
60-
if let Some(entry) = EncodingDirective::from_str(part) {
60+
if let Some(entry) = Encoding::from_str(part) {
6161
entries.push(entry);
6262
}
6363
}
@@ -91,7 +91,7 @@ impl ContentEncoding {
9191
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
9292
}
9393
/// Push a directive into the list of entries.
94-
pub fn push(&mut self, directive: EncodingDirective) {
94+
pub fn push(&mut self, directive: Encoding) {
9595
self.entries.push(directive);
9696
}
9797

@@ -111,7 +111,7 @@ impl ContentEncoding {
111111
}
112112

113113
impl IntoIterator for ContentEncoding {
114-
type Item = EncodingDirective;
114+
type Item = Encoding;
115115
type IntoIter = IntoIter;
116116

117117
#[inline]
@@ -123,7 +123,7 @@ impl IntoIterator for ContentEncoding {
123123
}
124124

125125
impl<'a> IntoIterator for &'a ContentEncoding {
126-
type Item = &'a EncodingDirective;
126+
type Item = &'a Encoding;
127127
type IntoIter = Iter<'a>;
128128

129129
#[inline]
@@ -133,7 +133,7 @@ impl<'a> IntoIterator for &'a ContentEncoding {
133133
}
134134

135135
impl<'a> IntoIterator for &'a mut ContentEncoding {
136-
type Item = &'a mut EncodingDirective;
136+
type Item = &'a mut Encoding;
137137
type IntoIter = IterMut<'a>;
138138

139139
#[inline]
@@ -145,11 +145,11 @@ impl<'a> IntoIterator for &'a mut ContentEncoding {
145145
/// A borrowing iterator over entries in `CacheControl`.
146146
#[derive(Debug)]
147147
pub struct IntoIter {
148-
inner: std::vec::IntoIter<EncodingDirective>,
148+
inner: std::vec::IntoIter<Encoding>,
149149
}
150150

151151
impl Iterator for IntoIter {
152-
type Item = EncodingDirective;
152+
type Item = Encoding;
153153

154154
fn next(&mut self) -> Option<Self::Item> {
155155
self.inner.next()
@@ -164,11 +164,11 @@ impl Iterator for IntoIter {
164164
/// A lending iterator over entries in `CacheControl`.
165165
#[derive(Debug)]
166166
pub struct Iter<'a> {
167-
inner: slice::Iter<'a, EncodingDirective>,
167+
inner: slice::Iter<'a, Encoding>,
168168
}
169169

170170
impl<'a> Iterator for Iter<'a> {
171-
type Item = &'a EncodingDirective;
171+
type Item = &'a Encoding;
172172

173173
fn next(&mut self) -> Option<Self::Item> {
174174
self.inner.next()
@@ -183,11 +183,11 @@ impl<'a> Iterator for Iter<'a> {
183183
/// A mutable iterator over entries in `CacheControl`.
184184
#[derive(Debug)]
185185
pub struct IterMut<'a> {
186-
inner: slice::IterMut<'a, EncodingDirective>,
186+
inner: slice::IterMut<'a, Encoding>,
187187
}
188188

189189
impl<'a> Iterator for IterMut<'a> {
190-
type Item = &'a mut EncodingDirective;
190+
type Item = &'a mut Encoding;
191191

192192
fn next(&mut self) -> Option<Self::Item> {
193193
self.inner.next()

src/content/encoding.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::headers::HeaderValue;
2+
3+
/// Available compression algorithms.
4+
#[non_exhaustive]
5+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
6+
pub enum Encoding {
7+
/// The Gzip encoding.
8+
Gzip,
9+
/// The Deflate encoding.
10+
Deflate,
11+
/// The Brotli encoding.
12+
Brotli,
13+
/// The Zstd encoding.
14+
Zstd,
15+
/// No encoding.
16+
Identity,
17+
}
18+
19+
impl Encoding {
20+
/// Parses a given string into its corresponding encoding.
21+
pub(crate) fn from_str(s: &str) -> Option<Encoding> {
22+
let s = s.trim();
23+
24+
// We're dealing with an empty string.
25+
if s.is_empty() {
26+
return None;
27+
}
28+
29+
match s {
30+
"gzip" => Some(Encoding::Gzip),
31+
"deflate" => Some(Encoding::Deflate),
32+
"br" => Some(Encoding::Brotli),
33+
"zstd" => Some(Encoding::Zstd),
34+
"identity" => Some(Encoding::Identity),
35+
_ => None,
36+
}
37+
}
38+
}
39+
40+
impl From<Encoding> for HeaderValue {
41+
fn from(directive: Encoding) -> Self {
42+
let h = |s: &str| unsafe { HeaderValue::from_bytes_unchecked(s.to_string().into_bytes()) };
43+
44+
match directive {
45+
Encoding::Gzip => h("gzip"),
46+
Encoding::Deflate => h("deflate"),
47+
Encoding::Brotli => h("br"),
48+
Encoding::Zstd => h("zstd"),
49+
Encoding::Identity => h("identity"),
50+
}
51+
}
52+
}

src/content/encoding_directive.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/content/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
33
pub mod content_encoding;
44

5-
mod encoding_directive;
5+
mod encoding;
66

77
#[doc(inline)]
88
pub use content_encoding::ContentEncoding;
9-
pub use encoding_directive::EncodingDirective;
9+
pub use encoding::Encoding;
10+
// EncodingProposal

0 commit comments

Comments
 (0)