Skip to content

Commit 2fe75b4

Browse files
committed
creating a ContentCoding type defined via a macro
1 parent 23c78fa commit 2fe75b4

File tree

3 files changed

+103
-28
lines changed

3 files changed

+103
-28
lines changed

src/common/content_coding.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
macro_rules! define_content_coding {
2+
($($coding:ident; $str:expr,)+) => {
3+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
4+
/// Values that are used with headers like `Content-Encoding`
5+
/// [RFC7231](https://www.iana.org/assignments/http-parameters/http-parameters.xhtml)
6+
///
7+
pub enum ContentCoding {
8+
$(
9+
#[doc = $str]
10+
$coding,
11+
)+
12+
}
13+
14+
impl ContentCoding {
15+
/// Returns a static str for a ContentCoding
16+
#[inline]
17+
pub fn to_static(&self) -> &'static str {
18+
match *self {
19+
$(ContentCoding::$coding => $str,)+
20+
}
21+
}
22+
}
23+
24+
impl std::string::ToString for ContentCoding {
25+
#[inline]
26+
fn to_string(&self) -> String {
27+
match *self {
28+
$(ContentCoding::$coding => $str.to_string(),)+
29+
}
30+
}
31+
}
32+
33+
impl std::str::FromStr for ContentCoding {
34+
type Err = &'static str;
35+
36+
fn from_str(s: &str) -> Result<Self, Self::Err> {
37+
match s {
38+
$(
39+
stringify!($coding)
40+
| $str => Ok(ContentCoding::$coding),
41+
)+
42+
_ => Err("invalid content coding")
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
define_content_coding! {
50+
BROTLI; "br",
51+
COMPRESS; "compress",
52+
DEFLATE; "deflate",
53+
GZIP; "gzip",
54+
IDENTITY; "identity",
55+
}
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::ContentCoding;
60+
use std::str::FromStr;
61+
62+
#[test]
63+
fn to_static() {
64+
assert_eq!(ContentCoding::GZIP.to_static(), "gzip");
65+
}
66+
67+
#[test]
68+
fn to_string() {
69+
assert_eq!(ContentCoding::DEFLATE.to_string(), "deflate".to_string());
70+
}
71+
72+
#[test]
73+
fn from_str() {
74+
assert_eq!(ContentCoding::from_str("br"), Ok(ContentCoding::BROTLI));
75+
assert_eq!(ContentCoding::from_str("GZIP"), Ok(ContentCoding::GZIP));
76+
assert_eq!(ContentCoding::from_str("blah blah"), Err("invalid content coding"));
77+
}
78+
}

src/common/content_encoding.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,30 @@ derive_header! {
3939
name: CONTENT_ENCODING
4040
}
4141

42-
impl ContentEncoding {
43-
/// A constructor to easily create a `Content-Encoding: gzip` header.
44-
#[inline]
45-
pub fn gzip() -> ContentEncoding {
46-
ContentEncoding(HeaderValue::from_static("gzip").into())
47-
}
48-
49-
/// A constructor to easily create a `Content-Encoding: compress` header.
50-
#[inline]
51-
pub fn compress() -> ContentEncoding {
52-
ContentEncoding(HeaderValue::from_static("compress").into())
53-
}
54-
55-
/// A constructor to easily create a `Content-Encoding: deflate` header.
56-
#[inline]
57-
pub fn deflate() -> ContentEncoding {
58-
ContentEncoding(HeaderValue::from_static("deflate").into())
59-
}
60-
61-
/// A constructor to easily create a `Content-Encoding: identity` header.
62-
#[inline]
63-
pub fn identity() -> ContentEncoding {
64-
ContentEncoding(HeaderValue::from_static("identity").into())
65-
}
42+
macro_rules! derive_constructor {
43+
($(doc = $doc:expr; $coding:ident,)+) => {
44+
$(
45+
#[doc = $doc]
46+
#[inline]
47+
pub fn $coding() -> ContentEncoding {
48+
ContentEncoding(HeaderValue::from_static(stringify!($coding)).into())
49+
}
50+
)+
51+
};
52+
}
6653

67-
/// A constructor to easily create a `Content-Encoding: br` header.
68-
#[inline]
69-
pub fn br() -> ContentEncoding {
70-
ContentEncoding(HeaderValue::from_static("br").into())
54+
impl ContentEncoding {
55+
derive_constructor! {
56+
doc = "A constructor to easily create a `Content-Encoding`: gzip header";
57+
gzip,
58+
doc = "A constructor to easily create a `Content-Encoding`: compress header";
59+
compress,
60+
doc = "A constructor to easily create a `Content-Encoding`: deflate header";
61+
deflate,
62+
doc = "A constructor to easily create a `Content-Encoding`: identity header";
63+
identity,
64+
doc = "A constructor to easily create a `Content-Encoding`: br header";
65+
br,
7166
}
7267

7368
/// Check if this header contains a given "coding".

src/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub use self::allow::Allow;
2323
pub use self::authorization::Authorization;
2424
pub use self::cache_control::CacheControl;
2525
pub use self::connection::Connection;
26+
pub use self::content_coding::ContentCoding;
2627
pub use self::content_disposition::ContentDisposition;
2728
pub use self::content_encoding::ContentEncoding;
2829
//pub use self::content_language::ContentLanguage;
@@ -142,6 +143,7 @@ mod allow;
142143
pub mod authorization;
143144
mod cache_control;
144145
mod connection;
146+
mod content_coding;
145147
mod content_disposition;
146148
mod content_encoding;
147149
//mod content_language;

0 commit comments

Comments
 (0)