Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple FromParts in handlers #1999

Merged
merged 6 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions rust-runtime/aws-smithy-http-server/src/operation/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ where
}
}

// fn(Input, Ext0) -> Output
impl<Op, F, Fut, Ext0> Handler<Op, (Ext0,)> for F
where
Op: OperationShape,
Expand All @@ -77,21 +76,36 @@ where
}
}

// fn(Input, Ext0, Ext1) -> Output
impl<Op, F, Fut, Ext0, Ext1> Handler<Op, (Ext0, Ext1)> for F
where
Op: OperationShape,
F: Fn(Op::Input, Ext0, Ext1) -> Fut,
Fut: Future,
Fut::Output: IntoResult<Op::Output, Op::Error>,
{
type Future = Map<Fut, fn(Fut::Output) -> Result<Op::Output, Op::Error>>;

fn call(&mut self, input: Op::Input, exts: (Ext0, Ext1)) -> Self::Future {
(self)(input, exts.0, exts.1).map(IntoResult::into_result)
}
// fn(Input, Ext_i) -> Output
macro_rules! impl_handler {
($($var:ident),+) => (
impl<Op, F, Fut, $($var,)*> Handler<Op, ($($var),*)> for F
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
where
Op: OperationShape,
F: Fn(Op::Input, $($var,)*) -> Fut,
Fut: Future,
Fut::Output: IntoResult<Op::Output, Op::Error>,
{
type Future = Map<Fut, fn(Fut::Output) -> Result<Op::Output, Op::Error>>;

fn call(&mut self, input: Op::Input, exts: ($($var,)*)) -> Self::Future {
#[allow(non_snake_case)]
let ($($var,)*) = exts;
(self)(input, $($var,)*).map(IntoResult::into_result)
}
}
)
}

impl_handler!(Exts0, Exts1);
impl_handler!(Exts0, Exts1, Exts2);
impl_handler!(Exts0, Exts1, Exts2, Exts3);
impl_handler!(Exts0, Exts1, Exts2, Exts3, Exts4);
impl_handler!(Exts0, Exts1, Exts2, Exts3, Exts4, Exts5);
impl_handler!(Exts0, Exts1, Exts2, Exts3, Exts4, Exts5, Exts6);
impl_handler!(Exts0, Exts1, Exts2, Exts3, Exts4, Exts5, Exts6, Exts7);
impl_handler!(Exts0, Exts1, Exts2, Exts3, Exts4, Exts5, Exts6, Exts7, Exts8);

/// An extension trait for [`Handler`].
pub trait HandlerExt<Op, Exts>: Handler<Op, Exts>
where
Expand Down
47 changes: 32 additions & 15 deletions rust-runtime/aws-smithy-http-server/src/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,38 @@ convert_to_request_rejection!(hyper::Error, HttpBody);
// Required in order to accept Lambda HTTP requests using `Router<lambda_http::Body>`.
convert_to_request_rejection!(lambda_http::Error, HttpBody);

/// A sum type rejection, implementing [`IntoResponse`] when both variants do.
pub enum EitherRejection<Left, Right> {
Left(Left),
Right(Right),
}
macro_rules! any_rejection {
82marbag marked this conversation as resolved.
Show resolved Hide resolved
($name:ident, $($var:ident),+) => (
pub enum $name<$($var),*> {
$($var ($var),)*
}

impl<P, L, R> IntoResponse<P> for EitherRejection<L, R>
where
L: IntoResponse<P>,
R: IntoResponse<P>,
{
fn into_response(self) -> http::Response<crate::body::BoxBody> {
match self {
EitherRejection::Left(left) => left.into_response(),
EitherRejection::Right(right) => right.into_response(),
impl<P, $($var,)*> IntoResponse<P> for $name<$($var),*>
where
$($var: IntoResponse<P>,)*
{
#[allow(non_snake_case)]
fn into_response(self) -> http::Response<crate::body::BoxBody> {
match self {
$($name::$var ($var) => $var.into_response(),)*
}
}
}
}
)
}

pub mod any_rejections {
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
//! This module hosts enums, up to size 8, which implement [`IntoResponse`] when their variants implement
//! [`IntoResponse`].

use super::IntoResponse;

// any_rejection!(One, A);
any_rejection!(Two, A, B);
any_rejection!(Three, A, B, C);
any_rejection!(Four, A, B, C, D);
any_rejection!(Five, A, B, C, D, E);
any_rejection!(Six, A, B, C, D, E, F);
any_rejection!(Seven, A, B, C, D, E, F, G);
any_rejection!(Eight, A, B, C, D, E, F, G, H);
}
44 changes: 28 additions & 16 deletions rust-runtime/aws-smithy-http-server/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use futures_util::{
};
use http::{request::Parts, Extensions, HeaderMap, Request, Uri};

use crate::{rejection::EitherRejection, response::IntoResponse};
use crate::{rejection::any_rejections, response::IntoResponse};

#[doc(hidden)]
#[derive(Debug)]
Expand Down Expand Up @@ -139,20 +139,32 @@ where
}
}

impl<P, T1, T2> FromParts<P> for (T1, T2)
where
T1: FromParts<P>,
T2: FromParts<P>,
{
type Rejection = EitherRejection<T1::Rejection, T2::Rejection>;

fn from_parts(parts: &mut Parts) -> Result<Self, Self::Rejection> {
let t1 = T1::from_parts(parts).map_err(EitherRejection::Left)?;
let t2 = T2::from_parts(parts).map_err(EitherRejection::Right)?;
Ok((t1, t2))
}
macro_rules! impl_from_parts {
($error_name:ident, $($var:ident),+) => (
impl<P, $($var,)*> FromParts<P> for ($($var),*)
where
$($var: FromParts<P>,)*
{
type Rejection = any_rejections::$error_name<$($var::Rejection),*>;

fn from_parts(parts: &mut Parts) -> Result<Self, Self::Rejection> {
let tuple = (
$($var::from_parts(parts).map_err(any_rejections::$error_name::$var)?,)*
);
Ok(tuple)
}
}
)
}

impl_from_parts!(Two, A, B);
impl_from_parts!(Three, A, B, C);
impl_from_parts!(Four, A, B, C, D);
impl_from_parts!(Five, A, B, C, D, E);
impl_from_parts!(Six, A, B, C, D, E, F);
impl_from_parts!(Seven, A, B, C, D, E, F, G);
impl_from_parts!(Eight, A, B, C, D, E, F, G, H);

/// Provides a protocol aware extraction from a [`Request`]. This consumes the
/// [`Request`], in contrast to [`FromParts`].
pub trait FromRequest<Protocol, B>: Sized {
Expand Down Expand Up @@ -180,14 +192,14 @@ where
T1: FromRequest<P, B>,
T2: FromParts<P>,
{
type Rejection = EitherRejection<T1::Rejection, T2::Rejection>;
type Rejection = any_rejections::Two<T1::Rejection, T2::Rejection>;
type Future = TryJoin<MapErr<T1::Future, fn(T1::Rejection) -> Self::Rejection>, Ready<Result<T2, Self::Rejection>>>;

fn from_request(request: Request<B>) -> Self::Future {
let (mut parts, body) = request.into_parts();
let t2_result = T2::from_parts(&mut parts).map_err(EitherRejection::Right);
let t2_result = T2::from_parts(&mut parts).map_err(any_rejections::Two::B);
try_join(
T1::from_request(Request::from_parts(parts, body)).map_err(EitherRejection::Left),
T1::from_request(Request::from_parts(parts, body)).map_err(any_rejections::Two::A),
ready(t2_result),
)
}
Expand Down