1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-21 13:15:38 +02:00

body ergonomics v3 (#2468)

This commit is contained in:
Rob Ede
2021-12-04 19:40:47 +00:00
committed by GitHub
parent a2d5c5a058
commit c7c02ef99d
84 changed files with 2134 additions and 1685 deletions

View File

@@ -1,21 +1,21 @@
use std::future::Future;
use actix_service::{
boxed::{self, BoxServiceFactory},
fn_service,
};
use actix_service::{boxed, fn_service};
use crate::{
service::{ServiceRequest, ServiceResponse},
Error, FromRequest, HttpResponse, Responder,
body::MessageBody,
service::{BoxedHttpServiceFactory, ServiceRequest, ServiceResponse},
BoxError, FromRequest, HttpResponse, Responder,
};
/// A request handler is an async function that accepts zero or more parameters that can be
/// extracted from a request (i.e., [`impl FromRequest`](crate::FromRequest)) and returns a type
/// that can be converted into an [`HttpResponse`] (that is, it impls the [`Responder`] trait).
/// extracted from a request (i.e., [`impl FromRequest`]) and returns a type that can be converted
/// into an [`HttpResponse`] (that is, it impls the [`Responder`] trait).
///
/// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not
/// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information.
/// a valid handler. See <https://actix.rs/docs/handlers> for more information.
///
/// [`impl FromRequest`]: crate::FromRequest
pub trait Handler<T, R>: Clone + 'static
where
R: Future,
@@ -24,29 +24,44 @@ where
fn call(&self, param: T) -> R;
}
pub fn handler_service<F, T, R>(
handler: F,
) -> BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>
pub(crate) fn handler_service<F, T, R>(handler: F) -> BoxedHttpServiceFactory
where
F: Handler<T, R>,
T: FromRequest,
R: Future,
R::Output: Responder,
<R::Output as Responder>::Body: MessageBody,
<<R::Output as Responder>::Body as MessageBody>::Error: Into<BoxError>,
{
boxed::factory(fn_service(move |req: ServiceRequest| {
let handler = handler.clone();
async move {
let (req, mut payload) = req.into_parts();
let res = match T::from_request(&req, &mut payload).await {
Err(err) => HttpResponse::from_error(err),
Ok(data) => handler.call(data).await.respond_to(&req),
Ok(data) => handler
.call(data)
.await
.respond_to(&req)
.map_into_boxed_body(),
};
Ok(ServiceResponse::new(req, res))
}
}))
}
/// FromRequest trait impl for tuples
/// Generates a [`Handler`] trait impl for N-ary functions where N is specified with a sequence of
/// space separated type parameters.
///
/// # Examples
/// ```ignore
/// factory_tuple! {} // implements Handler for types: fn() -> Res
/// factory_tuple! { A B C } // implements Handler for types: fn(A, B, C) -> Res
/// ```
macro_rules! factory_tuple ({ $($param:ident)* } => {
impl<Func, $($param,)* Res> Handler<($($param,)*), Res> for Func
where Func: Fn($($param),*) -> Res + Clone + 'static,