Remove B type param (#1751)

Co-authored-by: Jonas Platte <[email protected]>
Co-authored-by: Michael Scofield <[email protected]>
This commit is contained in:
David Pedersen
2023-04-21 17:45:31 +02:00
co-authored by Jonas Platte Michael Scofield
parent 9be0ea934c
commit 4e4c29175f
100 changed files with 966 additions and 1160 deletions
+25 -26
View File
@@ -1,6 +1,7 @@
//! Additional types for defining routes.
use axum::{
body::Body,
http::Request,
response::{IntoResponse, Redirect, Response},
routing::{any, MethodRouter},
@@ -26,7 +27,7 @@ pub use axum_macros::TypedPath;
pub use self::typed::{SecondElementIs, TypedPath};
/// Extension trait that adds additional methods to [`Router`].
pub trait RouterExt<S, B>: sealed::Sealed {
pub trait RouterExt<S>: sealed::Sealed {
/// Add a typed `GET` route to the router.
///
/// The path will be inferred from the first argument to the handler function which must
@@ -36,7 +37,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_get<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -49,7 +50,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_delete<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -62,7 +63,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_head<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -75,7 +76,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_options<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -88,7 +89,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_patch<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -101,7 +102,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_post<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -114,7 +115,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_put<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -127,7 +128,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
#[cfg(feature = "typed-routing")]
fn typed_trace<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath;
@@ -156,7 +157,7 @@ pub trait RouterExt<S, B>: sealed::Sealed {
/// .route_with_tsr("/bar/", get(|| async {}));
/// # let _: Router = app;
/// ```
fn route_with_tsr(self, path: &str, method_router: MethodRouter<S, B>) -> Self
fn route_with_tsr(self, path: &str, method_router: MethodRouter<S>) -> Self
where
Self: Sized;
@@ -165,21 +166,20 @@ pub trait RouterExt<S, B>: sealed::Sealed {
/// This works like [`RouterExt::route_with_tsr`] but accepts any [`Service`].
fn route_service_with_tsr<T>(self, path: &str, service: T) -> Self
where
T: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
T: Service<Request<Body>, Error = Infallible> + Clone + Send + 'static,
T::Response: IntoResponse,
T::Future: Send + 'static,
Self: Sized;
}
impl<S, B> RouterExt<S, B> for Router<S, B>
impl<S> RouterExt<S> for Router<S>
where
B: axum::body::HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
#[cfg(feature = "typed-routing")]
fn typed_get<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -189,7 +189,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_delete<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -199,7 +199,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_head<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -209,7 +209,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_options<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -219,7 +219,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_patch<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -229,7 +229,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_post<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -239,7 +239,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_put<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -249,7 +249,7 @@ where
#[cfg(feature = "typed-routing")]
fn typed_trace<H, T, P>(self, handler: H) -> Self
where
H: axum::handler::Handler<T, S, B>,
H: axum::handler::Handler<T, S>,
T: SecondElementIs<P> + 'static,
P: TypedPath,
{
@@ -257,7 +257,7 @@ where
}
#[track_caller]
fn route_with_tsr(mut self, path: &str, method_router: MethodRouter<S, B>) -> Self
fn route_with_tsr(mut self, path: &str, method_router: MethodRouter<S>) -> Self
where
Self: Sized,
{
@@ -269,7 +269,7 @@ where
#[track_caller]
fn route_service_with_tsr<T>(mut self, path: &str, service: T) -> Self
where
T: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
T: Service<Request<Body>, Error = Infallible> + Clone + Send + 'static,
T::Response: IntoResponse,
T::Future: Send + 'static,
Self: Sized,
@@ -287,9 +287,8 @@ fn validate_tsr_path(path: &str) {
}
}
fn add_tsr_redirect_route<S, B>(router: Router<S, B>, path: &str) -> Router<S, B>
fn add_tsr_redirect_route<S>(router: Router<S>, path: &str) -> Router<S>
where
B: axum::body::HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
async fn redirect_handler(uri: Uri) -> Response {
@@ -337,7 +336,7 @@ where
mod sealed {
pub trait Sealed {}
impl<S, B> Sealed for axum::Router<S, B> {}
impl<S> Sealed for axum::Router<S> {}
}
#[cfg(test)]
+17 -21
View File
@@ -1,5 +1,4 @@
use axum::{
body::Body,
handler::Handler,
routing::{delete, get, on, post, MethodFilter, MethodRouter},
Router,
@@ -34,14 +33,13 @@ use axum::{
/// ```
#[derive(Debug)]
#[must_use]
pub struct Resource<S = (), B = Body> {
pub struct Resource<S = ()> {
pub(crate) name: String,
pub(crate) router: Router<S, B>,
pub(crate) router: Router<S>,
}
impl<S, B> Resource<S, B>
impl<S> Resource<S>
where
B: axum::body::HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
/// Create a `Resource` with the given name.
@@ -57,7 +55,7 @@ where
/// Add a handler at `GET /{resource_name}`.
pub fn index<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = self.index_create_path();
@@ -67,7 +65,7 @@ where
/// Add a handler at `POST /{resource_name}`.
pub fn create<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = self.index_create_path();
@@ -77,7 +75,7 @@ where
/// Add a handler at `GET /{resource_name}/new`.
pub fn new<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = format!("/{}/new", self.name);
@@ -87,7 +85,7 @@ where
/// Add a handler at `GET /{resource_name}/:{resource_name}_id`.
pub fn show<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = self.show_update_destroy_path();
@@ -97,7 +95,7 @@ where
/// Add a handler at `GET /{resource_name}/:{resource_name}_id/edit`.
pub fn edit<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = format!("/{0}/:{0}_id/edit", self.name);
@@ -107,7 +105,7 @@ where
/// Add a handler at `PUT or PATCH /resource_name/:{resource_name}_id`.
pub fn update<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = self.show_update_destroy_path();
@@ -117,7 +115,7 @@ where
/// Add a handler at `DELETE /{resource_name}/:{resource_name}_id`.
pub fn destroy<H, T>(self, handler: H) -> Self
where
H: Handler<T, S, B>,
H: Handler<T, S>,
T: 'static,
{
let path = self.show_update_destroy_path();
@@ -132,14 +130,14 @@ where
format!("/{0}/:{0}_id", self.name)
}
fn route(mut self, path: &str, method_router: MethodRouter<S, B>) -> Self {
fn route(mut self, path: &str, method_router: MethodRouter<S>) -> Self {
self.router = self.router.route(path, method_router);
self
}
}
impl<S, B> From<Resource<S, B>> for Router<S, B> {
fn from(resource: Resource<S, B>) -> Self {
impl<S> From<Resource<S>> for Router<S> {
fn from(resource: Resource<S>) -> Self {
resource.router
}
}
@@ -148,9 +146,9 @@ impl<S, B> From<Resource<S, B>> for Router<S, B> {
mod tests {
#[allow(unused_imports)]
use super::*;
use axum::{extract::Path, http::Method, Router};
use axum::{body::Body, extract::Path, http::Method, Router};
use http::Request;
use tower::{Service, ServiceExt};
use tower::ServiceExt;
#[tokio::test]
async fn works() {
@@ -208,10 +206,8 @@ mod tests {
async fn call_route(app: &mut Router, method: Method, uri: &str) -> String {
let res = app
.ready()
.await
.unwrap()
.call(
.clone()
.oneshot(
Request::builder()
.method(method)
.uri(uri)