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
+2 -2
View File
@@ -255,7 +255,7 @@ mod tests {
custom_key: CustomKey(Key::generate()),
};
let app = Router::<_, Body>::new()
let app = Router::new()
.route("/set", get(set_cookie))
.route("/get", get(get_cookie))
.route("/remove", get(remove_cookie))
@@ -352,7 +352,7 @@ mod tests {
custom_key: CustomKey(Key::generate()),
};
let app = Router::<_, Body>::new()
let app = Router::new()
.route("/get", get(get_cookie))
.with_state(state);
+4 -7
View File
@@ -1,9 +1,9 @@
use axum::{
async_trait,
body::HttpBody,
body::Body,
extract::{rejection::RawFormRejection, FromRequest, RawForm},
response::{IntoResponse, Response},
BoxError, Error, RequestExt,
Error, RequestExt,
};
use http::{Request, StatusCode};
use serde::de::DeserializeOwned;
@@ -46,17 +46,14 @@ pub struct Form<T>(pub T);
axum_core::__impl_deref!(Form);
#[async_trait]
impl<T, S, B> FromRequest<S, B> for Form<T>
impl<T, S> FromRequest<S> for Form<T>
where
T: DeserializeOwned,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<BoxError>,
S: Send + Sync,
{
type Rejection = FormRejection;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
let RawForm(bytes) = req
.extract()
.await
+8 -12
View File
@@ -4,8 +4,8 @@
use axum::{
async_trait,
body::{Bytes, HttpBody},
extract::{BodyStream, FromRequest},
body::{Body, Bytes},
extract::FromRequest,
response::{IntoResponse, Response},
BoxError, RequestExt,
};
@@ -91,22 +91,18 @@ pub struct Multipart {
}
#[async_trait]
impl<S, B> FromRequest<S, B> for Multipart
impl<S> FromRequest<S> for Multipart
where
B: HttpBody + Send + 'static,
B::Data: Into<Bytes>,
B::Error: Into<BoxError>,
S: Send + Sync,
{
type Rejection = MultipartRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
let boundary = parse_boundary(req.headers()).ok_or(InvalidBoundary)?;
let stream_result = match req.with_limited_body() {
Ok(limited) => BodyStream::from_request(limited, state).await,
Err(unlimited) => BodyStream::from_request(unlimited, state).await,
let stream = match req.with_limited_body() {
Ok(limited) => Body::new(limited),
Err(unlimited) => unlimited.into_body(),
};
let stream = stream_result.unwrap_or_else(|err| match err {});
let multipart = multer::Multipart::new(stream, boundary);
Ok(Self { inner: multipart })
}
@@ -452,7 +448,7 @@ mod tests {
// No need for this to be a #[test], we just want to make sure it compiles
fn _multipart_from_request_limited() {
async fn handler(_: Multipart) {}
let _app: Router<(), http_body::Limited<Body>> = Router::new().route("/", post(handler));
let _app: Router<()> = Router::new().route("/", post(handler));
}
#[tokio::test]
+5 -5
View File
@@ -1,4 +1,5 @@
use axum::async_trait;
use axum::body::Body;
use axum::extract::{FromRequest, FromRequestParts};
use axum::response::IntoResponse;
use http::request::Parts;
@@ -109,16 +110,15 @@ impl<E, R> DerefMut for WithRejection<E, R> {
}
#[async_trait]
impl<B, E, R, S> FromRequest<S, B> for WithRejection<E, R>
impl<E, R, S> FromRequest<S> for WithRejection<E, R>
where
B: Send + 'static,
S: Send + Sync,
E: FromRequest<S, B>,
E: FromRequest<S>,
R: From<E::Rejection> + IntoResponse,
{
type Rejection = R;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
let extractor = E::from_request(req, state).await?;
Ok(WithRejection(extractor, PhantomData))
}
@@ -180,7 +180,7 @@ mod tests {
}
}
let req = Request::new(());
let req = Request::new(Body::empty());
let result = WithRejection::<TestExtractor, TestRejection>::from_request(req, &()).await;
assert!(matches!(result, Err(TestRejection)));
+17 -16
View File
@@ -1,5 +1,6 @@
//! Additional handler utilities.
use axum::body::Body;
use axum::{
extract::FromRequest,
handler::Handler,
@@ -19,15 +20,15 @@ pub use self::or::Or;
///
/// The drawbacks of this trait is that you cannot apply middleware to individual handlers like you
/// can with [`Handler::layer`].
pub trait HandlerCallWithExtractors<T, S, B>: Sized {
pub trait HandlerCallWithExtractors<T, S>: Sized {
/// The type of future calling this handler returns.
type Future: Future<Output = Response> + Send + 'static;
/// Call the handler with the extracted inputs.
fn call(self, extractors: T, state: S) -> <Self as HandlerCallWithExtractors<T, S, B>>::Future;
fn call(self, extractors: T, state: S) -> <Self as HandlerCallWithExtractors<T, S>>::Future;
/// Conver this `HandlerCallWithExtractors` into [`Handler`].
fn into_handler(self) -> IntoHandler<Self, T, S, B> {
fn into_handler(self) -> IntoHandler<Self, T, S> {
IntoHandler {
handler: self,
_marker: PhantomData,
@@ -102,9 +103,9 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
/// );
/// # let _: Router = app;
/// ```
fn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S, B>
fn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S>
where
R: HandlerCallWithExtractors<Rt, S, B>,
R: HandlerCallWithExtractors<Rt, S>,
{
Or {
lhs: self,
@@ -117,7 +118,7 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
macro_rules! impl_handler_call_with {
( $($ty:ident),* $(,)? ) => {
#[allow(non_snake_case)]
impl<F, Fut, S, B, $($ty,)*> HandlerCallWithExtractors<($($ty,)*), S, B> for F
impl<F, Fut, S, $($ty,)*> HandlerCallWithExtractors<($($ty,)*), S> for F
where
F: FnOnce($($ty,)*) -> Fut,
Fut: Future + Send + 'static,
@@ -130,7 +131,7 @@ macro_rules! impl_handler_call_with {
self,
($($ty,)*): ($($ty,)*),
_state: S,
) -> <Self as HandlerCallWithExtractors<($($ty,)*), S, B>>::Future {
) -> <Self as HandlerCallWithExtractors<($($ty,)*), S>>::Future {
self($($ty,)*).map(IntoResponse::into_response)
}
}
@@ -159,22 +160,22 @@ impl_handler_call_with!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,
///
/// Created with [`HandlerCallWithExtractors::into_handler`].
#[allow(missing_debug_implementations)]
pub struct IntoHandler<H, T, S, B> {
pub struct IntoHandler<H, T, S> {
handler: H,
_marker: PhantomData<fn() -> (T, S, B)>,
_marker: PhantomData<fn() -> (T, S)>,
}
impl<H, T, S, B> Handler<T, S, B> for IntoHandler<H, T, S, B>
impl<H, T, S> Handler<T, S> for IntoHandler<H, T, S>
where
H: HandlerCallWithExtractors<T, S, B> + Clone + Send + 'static,
T: FromRequest<S, B> + Send + 'static,
H: HandlerCallWithExtractors<T, S> + Clone + Send + 'static,
T: FromRequest<S> + Send + 'static,
T::Rejection: Send,
B: Send + 'static,
S: Send + Sync + 'static,
{
type Future = BoxFuture<'static, Response>;
fn call(self, req: http::Request<B>, state: S) -> Self::Future {
fn call(self, req: http::Request<Body>, state: S) -> Self::Future {
let req = req.map(Body::new);
Box::pin(async move {
match T::from_request(req, &state).await {
Ok(t) => self.handler.call(t, state).await,
@@ -184,9 +185,9 @@ where
}
}
impl<H, T, S, B> Copy for IntoHandler<H, T, S, B> where H: Copy {}
impl<H, T, S> Copy for IntoHandler<H, T, S> where H: Copy {}
impl<H, T, S, B> Clone for IntoHandler<H, T, S, B>
impl<H, T, S> Clone for IntoHandler<H, T, S>
where
H: Clone,
{
+14 -15
View File
@@ -1,6 +1,7 @@
use super::HandlerCallWithExtractors;
use crate::either::Either;
use axum::{
body::Body,
extract::{FromRequest, FromRequestParts},
handler::Handler,
http::Request,
@@ -14,19 +15,18 @@ use std::{future::Future, marker::PhantomData};
///
/// Created with [`HandlerCallWithExtractors::or`](super::HandlerCallWithExtractors::or).
#[allow(missing_debug_implementations)]
pub struct Or<L, R, Lt, Rt, S, B> {
pub struct Or<L, R, Lt, Rt, S> {
pub(super) lhs: L,
pub(super) rhs: R,
pub(super) _marker: PhantomData<fn() -> (Lt, Rt, S, B)>,
pub(super) _marker: PhantomData<fn() -> (Lt, Rt, S)>,
}
impl<S, B, L, R, Lt, Rt> HandlerCallWithExtractors<Either<Lt, Rt>, S, B> for Or<L, R, Lt, Rt, S, B>
impl<S, L, R, Lt, Rt> HandlerCallWithExtractors<Either<Lt, Rt>, S> for Or<L, R, Lt, Rt, S>
where
L: HandlerCallWithExtractors<Lt, S, B> + Send + 'static,
R: HandlerCallWithExtractors<Rt, S, B> + Send + 'static,
L: HandlerCallWithExtractors<Lt, S> + Send + 'static,
R: HandlerCallWithExtractors<Rt, S> + Send + 'static,
Rt: Send + 'static,
Lt: Send + 'static,
B: Send + 'static,
{
// this puts `futures_util` in our public API but thats fine in axum-extra
type Future = EitherFuture<
@@ -38,7 +38,7 @@ where
self,
extractors: Either<Lt, Rt>,
state: S,
) -> <Self as HandlerCallWithExtractors<Either<Lt, Rt>, S, B>>::Future {
) -> <Self as HandlerCallWithExtractors<Either<Lt, Rt>, S>>::Future {
match extractors {
Either::E1(lt) => self
.lhs
@@ -54,21 +54,20 @@ where
}
}
impl<S, B, L, R, Lt, Rt, M> Handler<(M, Lt, Rt), S, B> for Or<L, R, Lt, Rt, S, B>
impl<S, L, R, Lt, Rt, M> Handler<(M, Lt, Rt), S> for Or<L, R, Lt, Rt, S>
where
L: HandlerCallWithExtractors<Lt, S, B> + Clone + Send + 'static,
R: HandlerCallWithExtractors<Rt, S, B> + Clone + Send + 'static,
L: HandlerCallWithExtractors<Lt, S> + Clone + Send + 'static,
R: HandlerCallWithExtractors<Rt, S> + Clone + Send + 'static,
Lt: FromRequestParts<S> + Send + 'static,
Rt: FromRequest<S, B, M> + Send + 'static,
Rt: FromRequest<S, M> + Send + 'static,
Lt::Rejection: Send,
Rt::Rejection: Send,
B: Send + 'static,
S: Send + Sync + 'static,
{
// this puts `futures_util` in our public API but thats fine in axum-extra
type Future = BoxFuture<'static, Response>;
fn call(self, req: Request<B>, state: S) -> Self::Future {
fn call(self, req: Request<Body>, state: S) -> Self::Future {
Box::pin(async move {
let (mut parts, body) = req.into_parts();
@@ -86,14 +85,14 @@ where
}
}
impl<L, R, Lt, Rt, S, B> Copy for Or<L, R, Lt, Rt, S, B>
impl<L, R, Lt, Rt, S> Copy for Or<L, R, Lt, Rt, S>
where
L: Copy,
R: Copy,
{
}
impl<L, R, Lt, Rt, S, B> Clone for Or<L, R, Lt, Rt, S, B>
impl<L, R, Lt, Rt, S> Clone for Or<L, R, Lt, Rt, S>
where
L: Clone,
R: Clone,
+6 -33
View File
@@ -2,12 +2,12 @@
use axum::{
async_trait,
body::{HttpBody, StreamBody},
body::{Body, StreamBody},
extract::FromRequest,
response::{IntoResponse, Response},
BoxError,
};
use bytes::{BufMut, Bytes, BytesMut};
use bytes::{BufMut, BytesMut};
use futures_util::stream::{BoxStream, Stream, TryStream, TryStreamExt};
use http::Request;
use pin_project_lite::pin_project;
@@ -101,26 +101,19 @@ impl<S> JsonLines<S, AsResponse> {
}
#[async_trait]
impl<S, B, T> FromRequest<S, B> for JsonLines<T, AsExtractor>
impl<S, T> FromRequest<S> for JsonLines<T, AsExtractor>
where
B: HttpBody + Send + 'static,
B::Data: Into<Bytes>,
B::Error: Into<BoxError>,
T: DeserializeOwned,
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
// `Stream::lines` isn't a thing so we have to convert it into an `AsyncRead`
// so we can call `AsyncRead::lines` and then convert it back to a `Stream`
let body = BodyStream {
body: req.into_body(),
};
let body = req.into_body();
let stream = body
.map_ok(Into::into)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err));
let stream = TryStreamExt::map_err(body, |err| io::Error::new(io::ErrorKind::Other, err));
let read = StreamReader::new(stream);
let lines_stream = LinesStream::new(read.lines());
@@ -140,26 +133,6 @@ where
}
}
// like `axum::extract::BodyStream` except it doesn't box the inner body
// we don't need that since we box the final stream in `Inner::Extractor`
pin_project! {
struct BodyStream<B> {
#[pin]
body: B,
}
}
impl<B> Stream for BodyStream<B>
where
B: HttpBody + Send + 'static,
{
type Item = Result<B::Data, B::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().body.poll_data(cx)
}
}
impl<T> Stream for JsonLines<T, AsExtractor> {
type Item = Result<T, axum::Error>;
+4 -8
View File
@@ -2,12 +2,11 @@
use axum::{
async_trait,
body::{Bytes, HttpBody},
body::Body,
extract::{rejection::BytesRejection, FromRequest},
response::{IntoResponse, Response},
BoxError,
};
use bytes::BytesMut;
use bytes::{Bytes, BytesMut};
use http::{Request, StatusCode};
use prost::Message;
@@ -97,17 +96,14 @@ use prost::Message;
pub struct Protobuf<T>(pub T);
#[async_trait]
impl<T, S, B> FromRequest<S, B> for Protobuf<T>
impl<T, S> FromRequest<S> for Protobuf<T>
where
T: Message + Default,
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<BoxError>,
S: Send + Sync,
{
type Rejection = ProtobufRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
let mut bytes = Bytes::from_request(req, state).await?;
match T::decode(&mut bytes) {
+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)