Move Headers into axum

This commit is contained in:
David Pedersen
2022-01-23 20:25:12 +01:00
parent bf83f34617
commit d4849820ce
4 changed files with 22 additions and 21 deletions
+2 -14
View File
@@ -19,11 +19,6 @@ use http_body::{
};
use std::{borrow::Cow, convert::Infallible, iter};
mod headers;
#[doc(inline)]
pub use self::headers::Headers;
/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body
/// type used with axum.
pub type Response<T = BoxBody> = http::Response<T>;
@@ -356,17 +351,10 @@ impl IntoResponse for StatusCode {
}
}
impl<H> IntoResponse for H
where
H: IntoResponseHeaders,
{
impl IntoResponse for HeaderMap {
fn into_response(self) -> Response {
let mut res = Response::new(boxed(Empty::new()));
if let Err(e) = try_extend_headers(res.headers_mut(), self.into_headers()) {
return e;
}
*res.headers_mut() = self;
res
}
}
@@ -1,10 +1,9 @@
error[E0277]: the trait bound `bool: axum_core::response::IntoResponseHeaders` is not satisfied
error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
--> tests/fail/wrong_return_type.rs:4:23
|
4 | async fn handler() -> bool {
| ^^^^ the trait `axum_core::response::IntoResponseHeaders` is not implemented for `bool`
| ^^^^ the trait `IntoResponse` is not implemented for `bool`
|
= note: required because of the requirements on the impl of `IntoResponse` for `bool`
note: required by a bound in `__axum_debug_check_handler_into_response::{closure#0}::check`
--> tests/fail/wrong_return_type.rs:4:23
|
@@ -68,6 +68,19 @@ where
}
}
impl<H, K, V> IntoResponse for Headers<H>
where
H: IntoIterator<Item = (K, V)>,
K: TryInto<HeaderName>,
K::Error: fmt::Display,
V: TryInto<HeaderValue>,
V::Error: fmt::Display,
{
fn into_response(self) -> Response {
(self, ()).into_response()
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct IntoIter<H> {
@@ -124,7 +137,7 @@ mod tests {
let res = (Headers(vec![("user-agent", "axum")]), "foo").into_response();
assert_eq!(res.headers()["user-agent"], "axum");
let body = crate::body::to_bytes(res.into_body())
let body = hyper::body::to_bytes(res.into_body())
.now_or_never()
.unwrap()
.unwrap();
@@ -142,7 +155,7 @@ mod tests {
assert_eq!(res.headers()["user-agent"], "axum");
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let body = crate::body::to_bytes(res.into_body())
let body = hyper::body::to_bytes(res.into_body())
.now_or_never()
.unwrap()
.unwrap();
+3 -2
View File
@@ -4,6 +4,7 @@ use crate::body::{Bytes, Full};
use axum_core::body::boxed;
use http::{header, HeaderValue};
mod headers;
mod redirect;
pub mod sse;
@@ -13,10 +14,10 @@ pub mod sse;
pub use crate::Json;
#[doc(inline)]
pub use axum_core::response::{Headers, IntoResponse, Response};
pub use axum_core::response::{IntoResponse, IntoResponseHeaders, Response};
#[doc(inline)]
pub use self::{redirect::Redirect, sse::Sse};
pub use self::{headers::Headers, redirect::Redirect, sse::Sse};
/// An HTML response.
///