Add support for extracting typed headers (#18)

Uses the `headers` crate.
This commit is contained in:
David Pedersen
2021-06-15 21:27:21 +02:00
committed by GitHub
parent 2f6699aeae
commit b6e67eefd7
5 changed files with 91 additions and 1 deletions
+20
View File
@@ -1,5 +1,6 @@
//! Rejection response types.
use http::StatusCode;
use tower::BoxError;
use super::IntoResponse;
@@ -341,3 +342,22 @@ where
}
}
}
/// Rejection used for [`TypedHeader`](super::TypedHeader).
#[cfg(feature = "headers")]
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
#[derive(Debug)]
pub struct TypedHeaderRejection {
pub(super) name: &'static http::header::HeaderName,
pub(super) err: headers::Error,
}
#[cfg(feature = "headers")]
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
impl IntoResponse for TypedHeaderRejection {
fn into_response(self) -> http::Response<Body> {
let mut res = format!("{} ({})", self.err, self.name).into_response();
*res.status_mut() = StatusCode::BAD_REQUEST;
res
}
}