summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pedersen <david.pdrsn@gmail.com>2024-01-04 21:08:20 +0100
committerGitHub <noreply@github.com>2024-01-04 21:08:20 +0100
commitc486cc8207a98a015aca76cb84f26ab1c6940d83 (patch)
tree7d9c9773374bc33e22b545e799f695007fd239fa
parentcdd0eccc0a673f2694e50e64dc5de27028767905 (diff)
Implement `TypedPath` for `WithRejection<TypedPath, _>` (#2491)
-rw-r--r--axum-extra/CHANGELOG.md2
-rw-r--r--axum-extra/src/extract/with_rejection.rs22
-rw-r--r--axum-extra/src/routing/typed.rs31
3 files changed, 52 insertions, 3 deletions
diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md
index 2dfa296f..ffca0333 100644
--- a/axum-extra/CHANGELOG.md
+++ b/axum-extra/CHANGELOG.md
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning].
# Unreleased
-- None.
+- **added:** Implement `TypedPath` for `WithRejection<TypedPath, _>`
# 0.9.1 (29. December, 2023)
diff --git a/axum-extra/src/extract/with_rejection.rs b/axum-extra/src/extract/with_rejection.rs
index 1227a1ab..59541f96 100644
--- a/axum-extra/src/extract/with_rejection.rs
+++ b/axum-extra/src/extract/with_rejection.rs
@@ -2,10 +2,13 @@ use axum::async_trait;
use axum::extract::{FromRequest, FromRequestParts, Request};
use axum::response::IntoResponse;
use http::request::Parts;
-use std::fmt::Debug;
+use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
+#[cfg(feature = "typed-routing")]
+use crate::routing::TypedPath;
+
/// Extractor for customizing extractor rejections
///
/// `WithRejection` wraps another extractor and gives you the result. If the
@@ -137,6 +140,23 @@ where
}
}
+#[cfg(feature = "typed-routing")]
+impl<E, R> TypedPath for WithRejection<E, R>
+where
+ E: TypedPath,
+{
+ const PATH: &'static str = E::PATH;
+}
+
+impl<E, R> Display for WithRejection<E, R>
+where
+ E: Display,
+{
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
#[cfg(test)]
mod tests {
use axum::body::Body;
diff --git a/axum-extra/src/routing/typed.rs b/axum-extra/src/routing/typed.rs
index ef30462a..1f495175 100644
--- a/axum-extra/src/routing/typed.rs
+++ b/axum-extra/src/routing/typed.rs
@@ -386,7 +386,15 @@ impl_second_element_is!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,
#[cfg(test)]
mod tests {
use super::*;
- use crate::routing::TypedPath;
+ use crate::{
+ extract::WithRejection,
+ routing::{RouterExt, TypedPath},
+ };
+ use axum::{
+ extract::rejection::PathRejection,
+ response::{IntoResponse, Response},
+ Router,
+ };
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
@@ -434,4 +442,25 @@ mod tests {
assert_eq!(uri, "/users/1?&foo=foo&bar=123&baz=true&qux=1337");
}
+
+ #[allow(dead_code)] // just needs to compile
+ fn supports_with_rejection() {
+ async fn handler(_: WithRejection<UsersShow, MyRejection>) {}
+
+ struct MyRejection {}
+
+ impl IntoResponse for MyRejection {
+ fn into_response(self) -> Response {
+ unimplemented!()
+ }
+ }
+
+ impl From<PathRejection> for MyRejection {
+ fn from(_: PathRejection) -> Self {
+ unimplemented!()
+ }
+ }
+
+ let _: Router = Router::new().typed_get(handler);
+ }
}