Skip to content

Commit 8bf551b

Browse files
committed
Merge pull request #250 from pyfisch/headermacro2
refactor(headers): add header macros and add two example uses
2 parents a0bf2a4 + dfa5e69 commit 8bf551b

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed

src/header/common/accept_encoding.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::fmt;
2-
3-
use header;
4-
use header::shared;
1+
use header::{self, shared};
52

63
/// The `Accept-Encoding` header
74
///
@@ -10,23 +7,9 @@ use header::shared;
107
#[derive(Clone, PartialEq, Show)]
118
pub struct AcceptEncoding(pub Vec<shared::QualityItem<shared::Encoding>>);
129

13-
deref!(AcceptEncoding => Vec<shared::QualityItem<shared::Encoding>>);
14-
15-
impl header::Header for AcceptEncoding {
16-
fn header_name(_: Option<AcceptEncoding>) -> &'static str {
17-
"AcceptEncoding"
18-
}
19-
20-
fn parse_header(raw: &[Vec<u8>]) -> Option<AcceptEncoding> {
21-
shared::from_comma_delimited(raw).map(AcceptEncoding)
22-
}
23-
}
24-
25-
impl header::HeaderFormat for AcceptEncoding {
26-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27-
shared::fmt_comma_delimited(fmt, &self[])
28-
}
29-
}
10+
impl_list_header!(AcceptEncoding,
11+
"Accept-Encoding",
12+
Vec<shared::QualityItem<shared::Encoding>>);
3013

3114
#[test]
3215
fn test_parse_header() {

src/header/common/mod.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,64 @@ macro_rules! deref(
7676
}
7777
);
7878

79+
macro_rules! impl_list_header(
80+
($from:ident, $name:expr, $item:ty) => {
81+
deref!($from => $item);
82+
83+
impl header::Header for $from {
84+
fn header_name(_: Option<$from>) -> &'static str {
85+
$name
86+
}
87+
88+
fn parse_header(raw: &[Vec<u8>]) -> Option<$from> {
89+
$crate::header::shared::from_comma_delimited(raw).map($from)
90+
}
91+
}
92+
93+
impl header::HeaderFormat for $from {
94+
fn fmt_header(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
95+
$crate::header::shared::fmt_comma_delimited(fmt, &self[])
96+
}
97+
}
98+
99+
impl ::std::fmt::String for $from {
100+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
101+
use header::HeaderFormat;
102+
self.fmt_header(f)
103+
}
104+
}
105+
}
106+
);
107+
108+
macro_rules! impl_header(
109+
($from:ident, $name:expr, $item:ty) => {
110+
deref!($from => $item);
111+
112+
impl header::Header for $from {
113+
fn header_name(_: Option<$from>) -> &'static str {
114+
$name
115+
}
116+
117+
fn parse_header(raw: &[Vec<u8>]) -> Option<$from> {
118+
$crate::header::shared::from_one_raw_str(raw).map($from)
119+
}
120+
}
121+
122+
impl header::HeaderFormat for $from {
123+
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
124+
::std::fmt::String::fmt(&**self, f)
125+
}
126+
}
127+
128+
impl ::std::fmt::String for $from {
129+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
130+
use header::HeaderFormat;
131+
self.fmt_header(f)
132+
}
133+
}
134+
}
135+
);
136+
79137
/// Exposes the AccessControl* family of headers.
80138
pub mod access_control;
81139

src/header/common/server.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
use header::{Header, HeaderFormat};
2-
use std::fmt;
3-
use header::shared::util::from_one_raw_str;
1+
use header;
42

53
/// The `Server` header field.
64
///
75
/// They can contain any value, so it just wraps a `String`.
86
#[derive(Clone, PartialEq, Show)]
97
pub struct Server(pub String);
108

11-
deref!(Server => String);
12-
13-
impl Header for Server {
14-
fn header_name(_: Option<Server>) -> &'static str {
15-
"Server"
16-
}
17-
18-
fn parse_header(raw: &[Vec<u8>]) -> Option<Server> {
19-
from_one_raw_str(raw).map(|s| Server(s))
20-
}
21-
}
22-
23-
impl HeaderFormat for Server {
24-
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
25-
fmt.write_str(&*self.0)
26-
}
27-
}
9+
impl_header!(Server,
10+
"Server",
11+
String);
2812

2913
bench_header!(bench, Server, { vec![b"Some String".to_vec()] });

0 commit comments

Comments
 (0)