Merge pull request #492 from hyperium/header-macro-example
fix(header): make test_module of header! optional
This commit is contained in:
13
examples/headers.rs
Normal file
13
examples/headers.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
#[macro_use]
|
||||
// TODO: only import header!, blocked by https://github.com/rust-lang/rust/issues/25003
|
||||
extern crate hyper;
|
||||
|
||||
// A header in the form of `X-Foo: some random string`
|
||||
header! {
|
||||
(Foo, "X-Foo") => [String]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
@@ -96,6 +96,21 @@ macro_rules! deref(
|
||||
}
|
||||
);
|
||||
|
||||
macro_rules! tm {
|
||||
($id:ident, $tm:ident{$($tf:item)*}) => {
|
||||
#[allow(unused_imports)]
|
||||
mod $tm{
|
||||
use std::str;
|
||||
use $crate::header::*;
|
||||
use $crate::mime::*;
|
||||
use $crate::method::Method;
|
||||
use super::$id as HeaderField;
|
||||
$($tf)*
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! test_header {
|
||||
($id:ident, $raw:expr) => {
|
||||
@@ -136,7 +151,7 @@ macro_rules! header {
|
||||
// $nn:expr: Nice name of the header
|
||||
|
||||
// List header, zero or more items
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)*) => {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct $id(pub Vec<$item>);
|
||||
@@ -160,19 +175,9 @@ macro_rules! header {
|
||||
self.fmt_header(f)
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
mod $tm{
|
||||
use std::str;
|
||||
use $crate::header::*;
|
||||
use $crate::mime::*;
|
||||
use $crate::method::Method;
|
||||
use super::$id as HeaderField;
|
||||
$($tf)*
|
||||
}
|
||||
|
||||
};
|
||||
// List header, one or more items
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+) => {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct $id(pub Vec<$item>);
|
||||
@@ -196,18 +201,9 @@ macro_rules! header {
|
||||
self.fmt_header(f)
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
mod $tm{
|
||||
use std::str;
|
||||
use $crate::header::*;
|
||||
use $crate::mime::*;
|
||||
use $crate::method::Method;
|
||||
use super::$id as HeaderField;
|
||||
$($tf)*
|
||||
}
|
||||
};
|
||||
// Single value header
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => [$value:ty] $tm:ident{$($tf:item)*}) => {
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => [$value:ty]) => {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct $id(pub $value);
|
||||
@@ -230,18 +226,9 @@ macro_rules! header {
|
||||
::std::fmt::Display::fmt(&**self, f)
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
mod $tm{
|
||||
use std::str;
|
||||
use $crate::header::*;
|
||||
use $crate::mime::*;
|
||||
use $crate::method::Method;
|
||||
use super::$id as HeaderField;
|
||||
$($tf)*
|
||||
}
|
||||
};
|
||||
// List header, one or more items with "*" option
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => {Any / ($item:ty)+}) => {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum $id {
|
||||
@@ -280,18 +267,44 @@ macro_rules! header {
|
||||
self.fmt_header(f)
|
||||
}
|
||||
}
|
||||
#[allow(unused_imports)]
|
||||
mod $tm{
|
||||
use std::str;
|
||||
use $crate::header::*;
|
||||
use $crate::mime::*;
|
||||
use $crate::method::Method;
|
||||
use super::$id as HeaderField;
|
||||
$($tf)*
|
||||
};
|
||||
|
||||
// optional test module
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
||||
header! {
|
||||
$(#[$a])*
|
||||
($id, $n) => ($item)*
|
||||
}
|
||||
|
||||
tm! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
||||
header! {
|
||||
$(#[$a])*
|
||||
($id, $n) => ($item)+
|
||||
}
|
||||
|
||||
tm! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
|
||||
header! {
|
||||
$(#[$a])*
|
||||
($id, $n) => [$item]
|
||||
}
|
||||
|
||||
tm! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
||||
header! {
|
||||
$(#[$a])*
|
||||
($id, $n) => {Any / ($item)+}
|
||||
}
|
||||
|
||||
tm! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
mod accept;
|
||||
mod access_control_allow_headers;
|
||||
mod access_control_allow_methods;
|
||||
|
||||
Reference in New Issue
Block a user