From dfa5e69d7c7b781943806af6882dfef82aaf1c55 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Mon, 12 Jan 2015 20:53:55 +0100 Subject: [PATCH] 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. --- src/header/common/accept_encoding.rs | 25 ++---------- src/header/common/mod.rs | 58 ++++++++++++++++++++++++++++ src/header/common/server.rs | 24 ++---------- 3 files changed, 66 insertions(+), 41 deletions(-) diff --git a/src/header/common/accept_encoding.rs b/src/header/common/accept_encoding.rs index 0595ce4f..2e630f09 100644 --- a/src/header/common/accept_encoding.rs +++ b/src/header/common/accept_encoding.rs @@ -1,7 +1,4 @@ -use std::fmt; - -use header; -use header::shared; +use header::{self, shared}; /// The `Accept-Encoding` header /// @@ -10,23 +7,9 @@ use header::shared; #[derive(Clone, PartialEq, Show)] pub struct AcceptEncoding(pub Vec>); -deref!(AcceptEncoding => Vec>); - -impl header::Header for AcceptEncoding { - fn header_name(_: Option) -> &'static str { - "AcceptEncoding" - } - - fn parse_header(raw: &[Vec]) -> Option { - shared::from_comma_delimited(raw).map(AcceptEncoding) - } -} - -impl header::HeaderFormat for AcceptEncoding { - fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - shared::fmt_comma_delimited(fmt, &self[]) - } -} +impl_list_header!(AcceptEncoding, + "Accept-Encoding", + Vec>); #[test] fn test_parse_header() { diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index ed684f84..b610abb9 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -76,6 +76,64 @@ macro_rules! deref( } ); +macro_rules! impl_list_header( + ($from:ident, $name:expr, $item:ty) => { + deref!($from => $item); + + impl header::Header for $from { + fn header_name(_: Option<$from>) -> &'static str { + $name + } + + fn parse_header(raw: &[Vec]) -> Option<$from> { + $crate::header::shared::from_comma_delimited(raw).map($from) + } + } + + impl header::HeaderFormat for $from { + fn fmt_header(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + $crate::header::shared::fmt_comma_delimited(fmt, &self[]) + } + } + + impl ::std::fmt::String for $from { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + use header::HeaderFormat; + self.fmt_header(f) + } + } + } +); + +macro_rules! impl_header( + ($from:ident, $name:expr, $item:ty) => { + deref!($from => $item); + + impl header::Header for $from { + fn header_name(_: Option<$from>) -> &'static str { + $name + } + + fn parse_header(raw: &[Vec]) -> Option<$from> { + $crate::header::shared::from_one_raw_str(raw).map($from) + } + } + + impl header::HeaderFormat for $from { + fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + ::std::fmt::String::fmt(&**self, f) + } + } + + impl ::std::fmt::String for $from { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + use header::HeaderFormat; + self.fmt_header(f) + } + } + } +); + /// Exposes the AccessControl* family of headers. pub mod access_control; diff --git a/src/header/common/server.rs b/src/header/common/server.rs index dcc55e00..d48f800c 100644 --- a/src/header/common/server.rs +++ b/src/header/common/server.rs @@ -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) -> &'static str { - "Server" - } - - fn parse_header(raw: &[Vec]) -> Option { - 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()] });