From 791d5038a927add3f8ff1d5c1010cd0b24fdda77 Mon Sep 17 00:00:00 2001 From: Joshua Chapman Date: Wed, 10 Jan 2024 12:17:03 +0100 Subject: [PATCH] Add is_missing method to typed header rejection reason (#2503) --- axum-extra/CHANGELOG.md | 2 ++ axum-extra/src/typed_header.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 46117010..67289eae 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning]. - **added:** Implement `TypedPath` for `WithRejection` - **fixed:** Documentation link to `serde::Deserialize` in `JsonDeserializer` extractor ([#2498]) +- **added:** Add `is_missing` function for `TypedHeaderRejection` and `TypedHeaderRejectionReason` ([#2503]) [#2498]: https://github.com/tokio-rs/axum/pull/2498 +[#2503]: https://github.com/tokio-rs/axum/pull/2503 # 0.9.1 (29. December, 2023) diff --git a/axum-extra/src/typed_header.rs b/axum-extra/src/typed_header.rs index f56f20b1..14cf39e8 100644 --- a/axum-extra/src/typed_header.rs +++ b/axum-extra/src/typed_header.rs @@ -123,6 +123,14 @@ impl TypedHeaderRejection { pub fn reason(&self) -> &TypedHeaderRejectionReason { &self.reason } + + /// Returns `true` if the typed header rejection reason is [`Missing`]. + /// + /// [`Missing`]: TypedHeaderRejectionReason::Missing + #[must_use] + pub fn is_missing(&self) -> bool { + self.reason.is_missing() + } } /// Additional information regarding a [`TypedHeaderRejection`] @@ -136,6 +144,16 @@ pub enum TypedHeaderRejectionReason { Error(headers::Error), } +impl TypedHeaderRejectionReason { + /// Returns `true` if the typed header rejection reason is [`Missing`]. + /// + /// [`Missing`]: TypedHeaderRejectionReason::Missing + #[must_use] + pub fn is_missing(&self) -> bool { + matches!(self, Self::Missing) + } +} + impl IntoResponse for TypedHeaderRejection { fn into_response(self) -> Response { (http::StatusCode::BAD_REQUEST, self.to_string()).into_response()