-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow typed headers to be used for IntoResponse(Parts)
- Loading branch information
Showing
5 changed files
with
77 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use super::{IntoResponse, IntoResponseParts, Response, ResponseParts}; | ||
use crate::headers::HeaderMapExt; | ||
|
||
use headers::Header; | ||
use rama_utils::macros::all_the_tuples_no_last_special_case; | ||
|
||
/// Use typed [`Header`]s i a response. | ||
pub struct Headers<T>(pub T); | ||
|
||
impl<H: Header> Headers<(H,)> { | ||
/// Create a Header singleton tuple. | ||
pub fn single(h: H) -> Self { | ||
Self((h,)) | ||
} | ||
} | ||
|
||
macro_rules! headers_into_response { | ||
( $($ty:ident),* $(,)? ) => { | ||
#[allow(non_snake_case)] | ||
impl<$($ty),+> IntoResponse for Headers<($($ty),+,)> | ||
where | ||
$( | ||
$ty: $crate::headers::Header, | ||
)+ | ||
{ | ||
fn into_response(self) -> Response { | ||
(self, ()).into_response() | ||
} | ||
} | ||
} | ||
} | ||
|
||
all_the_tuples_no_last_special_case!(headers_into_response); | ||
|
||
macro_rules! headers_into_response_parts { | ||
( $($ty:ident),* $(,)? ) => { | ||
#[allow(non_snake_case)] | ||
impl<$($ty),+> IntoResponseParts for Headers<($($ty),+,)> | ||
where | ||
$( | ||
$ty: $crate::headers::Header, | ||
)+ | ||
{ | ||
type Error = std::convert::Infallible; | ||
|
||
fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> { | ||
let Headers(( | ||
$($ty),+ | ||
, | ||
)) = self; | ||
$( | ||
res.headers_mut().typed_insert($ty); | ||
)+ | ||
Ok(res) | ||
} | ||
} | ||
} | ||
} | ||
|
||
all_the_tuples_no_last_special_case!(headers_into_response_parts); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters