refactor(headers): add header macros and add two example uses

Add `impl_header!` and `impl_list_header!` macros. Rewrite `Accept-Encoding`
and `Server` header implementations to use the new macros.
This commit is contained in:
Pyfisch
2015-01-12 20:53:55 +01:00
parent a0bf2a49f8
commit dfa5e69d7c
3 changed files with 66 additions and 41 deletions

View File

@@ -1,6 +1,4 @@
use header::{Header, HeaderFormat};
use std::fmt;
use header::shared::util::from_one_raw_str;
use header;
/// The `Server` header field.
///
@@ -8,22 +6,8 @@ use header::shared::util::from_one_raw_str;
#[derive(Clone, PartialEq, Show)]
pub struct Server(pub String);
deref!(Server => String);
impl Header for Server {
fn header_name(_: Option<Server>) -> &'static str {
"Server"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Server> {
from_one_raw_str(raw).map(|s| Server(s))
}
}
impl HeaderFormat for Server {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(&*self.0)
}
}
impl_header!(Server,
"Server",
String);
bench_header!(bench, Server, { vec![b"Some String".to_vec()] });