1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 11:02:08 +01:00

Don't leak internal macros (#2290)

This commit is contained in:
Ibraheem Ahmed 2021-06-25 07:25:50 -04:00 committed by GitHub
parent 767e4efe22
commit 2eacb735a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 113 additions and 112 deletions

View File

@ -1,6 +1,4 @@
#[macro_export] macro_rules! downcast_get_type_id {
#[doc(hidden)]
macro_rules! __downcast_get_type_id {
() => { () => {
/// A helper method to get the type ID of the type /// A helper method to get the type ID of the type
/// this trait is implemented on. /// this trait is implemented on.
@ -30,10 +28,8 @@ macro_rules! __downcast_get_type_id {
}; };
} }
//Generate implementation for dyn $name // Generate implementation for dyn $name
#[doc(hidden)] macro_rules! downcast_dyn {
#[macro_export]
macro_rules! __downcast_dyn {
($name:ident) => { ($name:ident) => {
/// A struct with a private constructor, for use with /// A struct with a private constructor, for use with
/// `__private_get_type_id__`. Its single field is private, /// `__private_get_type_id__`. Its single field is private,
@ -80,15 +76,17 @@ macro_rules! __downcast_dyn {
}; };
} }
pub(crate) use {downcast_dyn, downcast_get_type_id};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#![allow(clippy::upper_case_acronyms)] #![allow(clippy::upper_case_acronyms)]
trait MB { trait MB {
__downcast_get_type_id!(); downcast_get_type_id!();
} }
__downcast_dyn!(MB); downcast_dyn!(MB);
impl MB for String {} impl MB for String {}
impl MB for () {} impl MB for () {}

View File

@ -18,6 +18,7 @@ mod response_error;
pub use self::error::Error; pub use self::error::Error;
pub use self::internal::*; pub use self::internal::*;
pub use self::response_error::ResponseError; pub use self::response_error::ResponseError;
pub(crate) use macros::{downcast_dyn, downcast_get_type_id};
/// A convenience [`Result`](std::result::Result) for Actix Web operations. /// A convenience [`Result`](std::result::Result) for Actix Web operations.
/// ///

View File

@ -9,7 +9,7 @@ use std::{
use actix_http::{body::AnyBody, header, Response, StatusCode}; use actix_http::{body::AnyBody, header, Response, StatusCode};
use bytes::BytesMut; use bytes::BytesMut;
use crate::{__downcast_dyn, __downcast_get_type_id}; use crate::error::{downcast_dyn, downcast_get_type_id};
use crate::{helpers, HttpResponse}; use crate::{helpers, HttpResponse};
/// Errors that can generate responses. /// Errors that can generate responses.
@ -41,10 +41,10 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
res.set_body(AnyBody::from(buf)) res.set_body(AnyBody::from(buf))
} }
__downcast_get_type_id!(); downcast_get_type_id!();
} }
__downcast_dyn!(ResponseError); downcast_dyn!(ResponseError);
impl ResponseError for Box<dyn StdError + 'static> {} impl ResponseError for Box<dyn StdError + 'static> {}

View File

@ -5,7 +5,7 @@ use mime::Mime;
use super::{qitem, QualityItem}; use super::{qitem, QualityItem};
use crate::http::header; use crate::http::header;
crate::__define_common_header! { crate::http::header::common_header! {
/// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2) /// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)
/// ///
/// The `Accept` header field can be used by user agents to specify /// The `Accept` header field can be used by user agents to specify
@ -81,14 +81,14 @@ crate::__define_common_header! {
test_accept { test_accept {
// Tests from the RFC // Tests from the RFC
crate::__common_header_test!( crate::http::header::common_header_test!(
test1, test1,
vec![b"audio/*; q=0.2, audio/basic"], vec![b"audio/*; q=0.2, audio/basic"],
Some(Accept(vec![ Some(Accept(vec![
QualityItem::new("audio/*".parse().unwrap(), q(200)), QualityItem::new("audio/*".parse().unwrap(), q(200)),
qitem("audio/basic".parse().unwrap()), qitem("audio/basic".parse().unwrap()),
]))); ])));
crate::__common_header_test!( crate::http::header::common_header_test!(
test2, test2,
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"], vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
Some(Accept(vec![ Some(Accept(vec![
@ -100,13 +100,13 @@ crate::__define_common_header! {
qitem("text/x-c".parse().unwrap()), qitem("text/x-c".parse().unwrap()),
]))); ])));
// Custom tests // Custom tests
crate::__common_header_test!( crate::http::header::common_header_test!(
test3, test3,
vec![b"text/plain; charset=utf-8"], vec![b"text/plain; charset=utf-8"],
Some(Accept(vec![ Some(Accept(vec![
qitem(mime::TEXT_PLAIN_UTF_8), qitem(mime::TEXT_PLAIN_UTF_8),
]))); ])));
crate::__common_header_test!( crate::http::header::common_header_test!(
test4, test4,
vec![b"text/plain; charset=utf-8; q=0.5"], vec![b"text/plain; charset=utf-8; q=0.5"],
Some(Accept(vec![ Some(Accept(vec![

View File

@ -1,6 +1,6 @@
use super::{Charset, QualityItem, ACCEPT_CHARSET}; use super::{Charset, QualityItem, ACCEPT_CHARSET};
crate::__define_common_header! { crate::http::header::common_header! {
/// `Accept-Charset` header, defined in /// `Accept-Charset` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.3) /// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.3)
/// ///
@ -57,6 +57,6 @@ crate::__define_common_header! {
test_accept_charset { test_accept_charset {
// Test case from RFC // Test case from RFC
crate::__common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]); crate::http::header::common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
} }
} }

View File

@ -64,12 +64,12 @@ header! {
test_accept_encoding { test_accept_encoding {
// From the RFC // From the RFC
crate::__common_header_test!(test1, vec![b"compress, gzip"]); crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]);
crate::__common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![]))); crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
crate::__common_header_test!(test3, vec![b"*"]); crate::http::header::common_header_test!(test3, vec![b"*"]);
// Note: Removed quality 1 from gzip // Note: Removed quality 1 from gzip
crate::__common_header_test!(test4, vec![b"compress;q=0.5, gzip"]); crate::http::header::common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
// Note: Removed quality 1 from gzip // Note: Removed quality 1 from gzip
crate::__common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]); crate::http::header::common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
} }
} }

View File

@ -2,7 +2,7 @@ use language_tags::LanguageTag;
use super::{QualityItem, ACCEPT_LANGUAGE}; use super::{QualityItem, ACCEPT_LANGUAGE};
crate::__define_common_header! { crate::http::header::common_header! {
/// `Accept-Language` header, defined in /// `Accept-Language` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5) /// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
/// ///
@ -53,9 +53,9 @@ crate::__define_common_header! {
test_accept_language { test_accept_language {
// From the RFC // From the RFC
crate::__common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]); crate::http::header::common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
// Own test // Own test
crate::__common_header_test!( crate::http::header::common_header_test!(
test2, vec![b"en-US, en; q=0.5, fr"], test2, vec![b"en-US, en; q=0.5, fr"],
Some(AcceptLanguage(vec![ Some(AcceptLanguage(vec![
qitem("en-US".parse().unwrap()), qitem("en-US".parse().unwrap()),

View File

@ -1,7 +1,7 @@
use crate::http::header; use crate::http::header;
use actix_http::http::Method; use actix_http::http::Method;
crate::__define_common_header! { crate::http::header::common_header! {
/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1) /// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
/// ///
/// The `Allow` header field lists the set of methods advertised as /// The `Allow` header field lists the set of methods advertised as
@ -49,12 +49,12 @@ crate::__define_common_header! {
test_allow { test_allow {
// From the RFC // From the RFC
crate::__common_header_test!( crate::http::header::common_header_test!(
test1, test1,
vec![b"GET, HEAD, PUT"], vec![b"GET, HEAD, PUT"],
Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT]))); Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT])));
// Own tests // Own tests
crate::__common_header_test!( crate::http::header::common_header_test!(
test2, test2,
vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"], vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"],
Some(HeaderField(vec![ Some(HeaderField(vec![
@ -67,7 +67,7 @@ crate::__define_common_header! {
Method::TRACE, Method::TRACE,
Method::CONNECT, Method::CONNECT,
Method::PATCH]))); Method::PATCH])));
crate::__common_header_test!( crate::http::header::common_header_test!(
test3, test3,
vec![b""], vec![b""],
Some(HeaderField(Vec::<Method>::new()))); Some(HeaderField(Vec::<Method>::new())));

View File

@ -49,9 +49,9 @@ use crate::http::header;
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
pub struct CacheControl(pub Vec<CacheDirective>); pub struct CacheControl(pub Vec<CacheDirective>);
crate::__common_header_deref!(CacheControl => Vec<CacheDirective>); crate::http::header::common_header_deref!(CacheControl => Vec<CacheDirective>);
// TODO: this could just be the __define_common_header! macro // TODO: this could just be the crate::http::header::common_header! macro
impl Header for CacheControl { impl Header for CacheControl {
fn name() -> header::HeaderName { fn name() -> header::HeaderName {
header::CACHE_CONTROL header::CACHE_CONTROL

View File

@ -1,7 +1,7 @@
use super::{QualityItem, CONTENT_LANGUAGE}; use super::{QualityItem, CONTENT_LANGUAGE};
use language_tags::LanguageTag; use language_tags::LanguageTag;
crate::__define_common_header! { crate::http::header::common_header! {
/// `Content-Language` header, defined in /// `Content-Language` header, defined in
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2) /// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
/// ///
@ -50,7 +50,7 @@ crate::__define_common_header! {
(ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+ (ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+
test_content_language { test_content_language {
crate::__common_header_test!(test1, vec![b"da"]); crate::http::header::common_header_test!(test1, vec![b"da"]);
crate::__common_header_test!(test2, vec![b"mi, en"]); crate::http::header::common_header_test!(test2, vec![b"mi, en"]);
} }
} }

View File

@ -4,65 +4,65 @@ use std::str::FromStr;
use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE}; use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE};
use crate::error::ParseError; use crate::error::ParseError;
crate::__define_common_header! { crate::http::header::common_header! {
/// `Content-Range` header, defined in /// `Content-Range` header, defined in
/// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2) /// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2)
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec] (ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
test_content_range { test_content_range {
crate::__common_header_test!(test_bytes, crate::http::header::common_header_test!(test_bytes,
vec![b"bytes 0-499/500"], vec![b"bytes 0-499/500"],
Some(ContentRange(ContentRangeSpec::Bytes { Some(ContentRange(ContentRangeSpec::Bytes {
range: Some((0, 499)), range: Some((0, 499)),
instance_length: Some(500) instance_length: Some(500)
}))); })));
crate::__common_header_test!(test_bytes_unknown_len, crate::http::header::common_header_test!(test_bytes_unknown_len,
vec![b"bytes 0-499/*"], vec![b"bytes 0-499/*"],
Some(ContentRange(ContentRangeSpec::Bytes { Some(ContentRange(ContentRangeSpec::Bytes {
range: Some((0, 499)), range: Some((0, 499)),
instance_length: None instance_length: None
}))); })));
crate::__common_header_test!(test_bytes_unknown_range, crate::http::header::common_header_test!(test_bytes_unknown_range,
vec![b"bytes */500"], vec![b"bytes */500"],
Some(ContentRange(ContentRangeSpec::Bytes { Some(ContentRange(ContentRangeSpec::Bytes {
range: None, range: None,
instance_length: Some(500) instance_length: Some(500)
}))); })));
crate::__common_header_test!(test_unregistered, crate::http::header::common_header_test!(test_unregistered,
vec![b"seconds 1-2"], vec![b"seconds 1-2"],
Some(ContentRange(ContentRangeSpec::Unregistered { Some(ContentRange(ContentRangeSpec::Unregistered {
unit: "seconds".to_owned(), unit: "seconds".to_owned(),
resp: "1-2".to_owned() resp: "1-2".to_owned()
}))); })));
crate::__common_header_test!(test_no_len, crate::http::header::common_header_test!(test_no_len,
vec![b"bytes 0-499"], vec![b"bytes 0-499"],
None::<ContentRange>); None::<ContentRange>);
crate::__common_header_test!(test_only_unit, crate::http::header::common_header_test!(test_only_unit,
vec![b"bytes"], vec![b"bytes"],
None::<ContentRange>); None::<ContentRange>);
crate::__common_header_test!(test_end_less_than_start, crate::http::header::common_header_test!(test_end_less_than_start,
vec![b"bytes 499-0/500"], vec![b"bytes 499-0/500"],
None::<ContentRange>); None::<ContentRange>);
crate::__common_header_test!(test_blank, crate::http::header::common_header_test!(test_blank,
vec![b""], vec![b""],
None::<ContentRange>); None::<ContentRange>);
crate::__common_header_test!(test_bytes_many_spaces, crate::http::header::common_header_test!(test_bytes_many_spaces,
vec![b"bytes 1-2/500 3"], vec![b"bytes 1-2/500 3"],
None::<ContentRange>); None::<ContentRange>);
crate::__common_header_test!(test_bytes_many_slashes, crate::http::header::common_header_test!(test_bytes_many_slashes,
vec![b"bytes 1-2/500/600"], vec![b"bytes 1-2/500/600"],
None::<ContentRange>); None::<ContentRange>);
crate::__common_header_test!(test_bytes_many_dashes, crate::http::header::common_header_test!(test_bytes_many_dashes,
vec![b"bytes 1-2-3/500"], vec![b"bytes 1-2-3/500"],
None::<ContentRange>); None::<ContentRange>);

View File

@ -1,7 +1,7 @@
use super::CONTENT_TYPE; use super::CONTENT_TYPE;
use mime::Mime; use mime::Mime;
crate::__define_common_header! { crate::http::header::common_header! {
/// `Content-Type` header, defined in /// `Content-Type` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5) /// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
/// ///
@ -52,7 +52,7 @@ crate::__define_common_header! {
(ContentType, CONTENT_TYPE) => [Mime] (ContentType, CONTENT_TYPE) => [Mime]
test_content_type { test_content_type {
crate::__common_header_test!( crate::http::header::common_header_test!(
test1, test1,
vec![b"text/html"], vec![b"text/html"],
Some(HeaderField(mime::TEXT_HTML))); Some(HeaderField(mime::TEXT_HTML)));

View File

@ -1,7 +1,7 @@
use super::{HttpDate, DATE}; use super::{HttpDate, DATE};
use std::time::SystemTime; use std::time::SystemTime;
crate::__define_common_header! { crate::http::header::common_header! {
/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2) /// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)
/// ///
/// The `Date` header field represents the date and time at which the /// The `Date` header field represents the date and time at which the
@ -32,7 +32,7 @@ crate::__define_common_header! {
(Date, DATE) => [HttpDate] (Date, DATE) => [HttpDate]
test_date { test_date {
crate::__common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]); crate::http::header::common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
} }
} }

View File

@ -1,6 +1,6 @@
use super::{EntityTag, ETAG}; use super::{EntityTag, ETAG};
crate::__define_common_header! { crate::http::header::common_header! {
/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3) /// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3)
/// ///
/// The `ETag` header field in a response provides the current entity-tag /// The `ETag` header field in a response provides the current entity-tag
@ -50,50 +50,50 @@ crate::__define_common_header! {
test_etag { test_etag {
// From the RFC // From the RFC
crate::__common_header_test!(test1, crate::http::header::common_header_test!(test1,
vec![b"\"xyzzy\""], vec![b"\"xyzzy\""],
Some(ETag(EntityTag::new(false, "xyzzy".to_owned())))); Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
crate::__common_header_test!(test2, crate::http::header::common_header_test!(test2,
vec![b"W/\"xyzzy\""], vec![b"W/\"xyzzy\""],
Some(ETag(EntityTag::new(true, "xyzzy".to_owned())))); Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
crate::__common_header_test!(test3, crate::http::header::common_header_test!(test3,
vec![b"\"\""], vec![b"\"\""],
Some(ETag(EntityTag::new(false, "".to_owned())))); Some(ETag(EntityTag::new(false, "".to_owned()))));
// Own tests // Own tests
crate::__common_header_test!(test4, crate::http::header::common_header_test!(test4,
vec![b"\"foobar\""], vec![b"\"foobar\""],
Some(ETag(EntityTag::new(false, "foobar".to_owned())))); Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
crate::__common_header_test!(test5, crate::http::header::common_header_test!(test5,
vec![b"\"\""], vec![b"\"\""],
Some(ETag(EntityTag::new(false, "".to_owned())))); Some(ETag(EntityTag::new(false, "".to_owned()))));
crate::__common_header_test!(test6, crate::http::header::common_header_test!(test6,
vec![b"W/\"weak-etag\""], vec![b"W/\"weak-etag\""],
Some(ETag(EntityTag::new(true, "weak-etag".to_owned())))); Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
crate::__common_header_test!(test7, crate::http::header::common_header_test!(test7,
vec![b"W/\"\x65\x62\""], vec![b"W/\"\x65\x62\""],
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned())))); Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
crate::__common_header_test!(test8, crate::http::header::common_header_test!(test8,
vec![b"W/\"\""], vec![b"W/\"\""],
Some(ETag(EntityTag::new(true, "".to_owned())))); Some(ETag(EntityTag::new(true, "".to_owned()))));
crate::__common_header_test!(test9, crate::http::header::common_header_test!(test9,
vec![b"no-dquotes"], vec![b"no-dquotes"],
None::<ETag>); None::<ETag>);
crate::__common_header_test!(test10, crate::http::header::common_header_test!(test10,
vec![b"w/\"the-first-w-is-case-sensitive\""], vec![b"w/\"the-first-w-is-case-sensitive\""],
None::<ETag>); None::<ETag>);
crate::__common_header_test!(test11, crate::http::header::common_header_test!(test11,
vec![b""], vec![b""],
None::<ETag>); None::<ETag>);
crate::__common_header_test!(test12, crate::http::header::common_header_test!(test12,
vec![b"\"unmatched-dquotes1"], vec![b"\"unmatched-dquotes1"],
None::<ETag>); None::<ETag>);
crate::__common_header_test!(test13, crate::http::header::common_header_test!(test13,
vec![b"unmatched-dquotes2\""], vec![b"unmatched-dquotes2\""],
None::<ETag>); None::<ETag>);
crate::__common_header_test!(test14, crate::http::header::common_header_test!(test14,
vec![b"matched-\"dquotes\""], vec![b"matched-\"dquotes\""],
None::<ETag>); None::<ETag>);
crate::__common_header_test!(test15, crate::http::header::common_header_test!(test15,
vec![b"\""], vec![b"\""],
None::<ETag>); None::<ETag>);
} }

View File

@ -1,6 +1,6 @@
use super::{HttpDate, EXPIRES}; use super::{HttpDate, EXPIRES};
crate::__define_common_header! { crate::http::header::common_header! {
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3) /// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
/// ///
/// The `Expires` header field gives the date/time after which the /// The `Expires` header field gives the date/time after which the
@ -36,6 +36,6 @@ crate::__define_common_header! {
test_expires { test_expires {
// Test case from RFC // Test case from RFC
crate::__common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]); crate::http::header::common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
} }
} }

View File

@ -1,6 +1,6 @@
use super::{EntityTag, IF_MATCH}; use super::{EntityTag, IF_MATCH};
crate::__define_common_header! { crate::http::header::common_header! {
/// `If-Match` header, defined in /// `If-Match` header, defined in
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1) /// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
/// ///
@ -53,18 +53,18 @@ crate::__define_common_header! {
(IfMatch, IF_MATCH) => {Any / (EntityTag)+} (IfMatch, IF_MATCH) => {Any / (EntityTag)+}
test_if_match { test_if_match {
crate::__common_header_test!( crate::http::header::common_header_test!(
test1, test1,
vec![b"\"xyzzy\""], vec![b"\"xyzzy\""],
Some(HeaderField::Items( Some(HeaderField::Items(
vec![EntityTag::new(false, "xyzzy".to_owned())]))); vec![EntityTag::new(false, "xyzzy".to_owned())])));
crate::__common_header_test!( crate::http::header::common_header_test!(
test2, test2,
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""], vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
Some(HeaderField::Items( Some(HeaderField::Items(
vec![EntityTag::new(false, "xyzzy".to_owned()), vec![EntityTag::new(false, "xyzzy".to_owned()),
EntityTag::new(false, "r2d2xxxx".to_owned()), EntityTag::new(false, "r2d2xxxx".to_owned()),
EntityTag::new(false, "c3piozzzz".to_owned())]))); EntityTag::new(false, "c3piozzzz".to_owned())])));
crate::__common_header_test!(test3, vec![b"*"], Some(IfMatch::Any)); crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
} }
} }

View File

@ -1,6 +1,6 @@
use super::{HttpDate, IF_MODIFIED_SINCE}; use super::{HttpDate, IF_MODIFIED_SINCE};
crate::__define_common_header! { crate::http::header::common_header! {
/// `If-Modified-Since` header, defined in /// `If-Modified-Since` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3) /// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
/// ///
@ -36,6 +36,6 @@ crate::__define_common_header! {
test_if_modified_since { test_if_modified_since {
// Test case from RFC // Test case from RFC
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]); crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
} }
} }

View File

@ -1,6 +1,6 @@
use super::{EntityTag, IF_NONE_MATCH}; use super::{EntityTag, IF_NONE_MATCH};
crate::__define_common_header! { crate::http::header::common_header! {
/// `If-None-Match` header, defined in /// `If-None-Match` header, defined in
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2) /// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
/// ///
@ -55,11 +55,11 @@ crate::__define_common_header! {
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+} (IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
test_if_none_match { test_if_none_match {
crate::__common_header_test!(test1, vec![b"\"xyzzy\""]); crate::http::header::common_header_test!(test1, vec![b"\"xyzzy\""]);
crate::__common_header_test!(test2, vec![b"W/\"xyzzy\""]); crate::http::header::common_header_test!(test2, vec![b"W/\"xyzzy\""]);
crate::__common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]); crate::http::header::common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
crate::__common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]); crate::http::header::common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
crate::__common_header_test!(test5, vec![b"*"]); crate::http::header::common_header_test!(test5, vec![b"*"]);
} }
} }

View File

@ -113,7 +113,7 @@ mod test_if_range {
use crate::http::header::*; use crate::http::header::*;
use std::str; use std::str;
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]); crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
crate::__common_header_test!(test2, vec![b"\"abc\""]); crate::http::header::common_header_test!(test2, vec![b"\"abc\""]);
crate::__common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>); crate::http::header::common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>);
} }

View File

@ -1,6 +1,6 @@
use super::{HttpDate, IF_UNMODIFIED_SINCE}; use super::{HttpDate, IF_UNMODIFIED_SINCE};
crate::__define_common_header! { crate::http::header::common_header! {
/// `If-Unmodified-Since` header, defined in /// `If-Unmodified-Since` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4) /// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
/// ///
@ -37,6 +37,6 @@ crate::__define_common_header! {
test_if_unmodified_since { test_if_unmodified_since {
// Test case from RFC // Test case from RFC
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]); crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
} }
} }

View File

@ -1,6 +1,6 @@
use super::{HttpDate, LAST_MODIFIED}; use super::{HttpDate, LAST_MODIFIED};
crate::__define_common_header! { crate::http::header::common_header! {
/// `Last-Modified` header, defined in /// `Last-Modified` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2) /// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
/// ///
@ -36,6 +36,6 @@ crate::__define_common_header! {
test_last_modified { test_last_modified {
// Test case from RFC // Test case from RFC
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]); crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
} }
} }

View File

@ -1,6 +1,4 @@
#[doc(hidden)] macro_rules! common_header_deref {
#[macro_export]
macro_rules! __common_header_deref {
($from:ty => $to:ty) => { ($from:ty => $to:ty) => {
impl ::std::ops::Deref for $from { impl ::std::ops::Deref for $from {
type Target = $to; type Target = $to;
@ -20,9 +18,7 @@ macro_rules! __common_header_deref {
}; };
} }
#[doc(hidden)] macro_rules! common_header_test_module {
#[macro_export]
macro_rules! __common_header_test_module {
($id:ident, $tm:ident{$($tf:item)*}) => { ($id:ident, $tm:ident{$($tf:item)*}) => {
#[allow(unused_imports)] #[allow(unused_imports)]
#[cfg(test)] #[cfg(test)]
@ -37,9 +33,8 @@ macro_rules! __common_header_test_module {
} }
} }
#[doc(hidden)] #[cfg(test)]
#[macro_export] macro_rules! common_header_test {
macro_rules! __common_header_test {
($id:ident, $raw:expr) => { ($id:ident, $raw:expr) => {
#[test] #[test]
fn $id() { fn $id() {
@ -99,9 +94,7 @@ macro_rules! __common_header_test {
}; };
} }
#[doc(hidden)] macro_rules! common_header {
#[macro_export]
macro_rules! __define_common_header {
// $a:meta: Attributes associated with the header item (usually docs) // $a:meta: Attributes associated with the header item (usually docs)
// $id:ident: Identifier of the header // $id:ident: Identifier of the header
// $n:expr: Lowercase name of the header // $n:expr: Lowercase name of the header
@ -112,7 +105,7 @@ macro_rules! __define_common_header {
$(#[$a])* $(#[$a])*
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct $id(pub Vec<$item>); pub struct $id(pub Vec<$item>);
crate::__common_header_deref!($id => Vec<$item>); crate::http::header::common_header_deref!($id => Vec<$item>);
impl $crate::http::header::Header for $id { impl $crate::http::header::Header for $id {
#[inline] #[inline]
fn name() -> $crate::http::header::HeaderName { fn name() -> $crate::http::header::HeaderName {
@ -148,7 +141,7 @@ macro_rules! __define_common_header {
$(#[$a])* $(#[$a])*
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct $id(pub Vec<$item>); pub struct $id(pub Vec<$item>);
crate::__common_header_deref!($id => Vec<$item>); crate::http::header::common_header_deref!($id => Vec<$item>);
impl $crate::http::header::Header for $id { impl $crate::http::header::Header for $id {
#[inline] #[inline]
fn name() -> $crate::http::header::HeaderName { fn name() -> $crate::http::header::HeaderName {
@ -184,7 +177,7 @@ macro_rules! __define_common_header {
$(#[$a])* $(#[$a])*
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct $id(pub $value); pub struct $id(pub $value);
crate::__common_header_deref!($id => $value); crate::http::header::common_header_deref!($id => $value);
impl $crate::http::header::Header for $id { impl $crate::http::header::Header for $id {
#[inline] #[inline]
fn name() -> $crate::http::header::HeaderName { fn name() -> $crate::http::header::HeaderName {
@ -267,34 +260,39 @@ macro_rules! __define_common_header {
// optional test module // optional test module
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => { ($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
crate::__define_common_header! { crate::http::header::common_header! {
$(#[$a])* $(#[$a])*
($id, $name) => ($item)* ($id, $name) => ($item)*
} }
crate::__common_header_test_module! { $id, $tm { $($tf)* }} crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
}; };
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => { ($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
crate::__define_common_header! { crate::http::header::common_header! {
$(#[$a])* $(#[$a])*
($id, $n) => ($item)+ ($id, $n) => ($item)+
} }
crate::__common_header_test_module! { $id, $tm { $($tf)* }} crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
}; };
($(#[$a:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => { ($(#[$a:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
crate::__define_common_header! { crate::http::header::common_header! {
$(#[$a])* ($id, $name) => [$item] $(#[$a])* ($id, $name) => [$item]
} }
crate::__common_header_test_module! { $id, $tm { $($tf)* }} crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
}; };
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => { ($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
crate::__define_common_header! { crate::http::header::common_header! {
$(#[$a])* $(#[$a])*
($id, $name) => {Any / ($item)+} ($id, $name) => {Any / ($item)+}
} }
crate::__common_header_test_module! { $id, $tm { $($tf)* }} crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
}; };
} }
pub(crate) use {common_header, common_header_deref, common_header_test_module};
#[cfg(test)]
pub(crate) use common_header_test;

View File

@ -84,4 +84,8 @@ mod if_none_match;
mod if_range; mod if_range;
mod if_unmodified_since; mod if_unmodified_since;
mod last_modified; mod last_modified;
mod macros; mod macros;
#[cfg(test)]
pub(crate) use macros::common_header_test;
pub(crate) use macros::{common_header, common_header_deref, common_header_test_module};