From 91ddc2d49e3d0cde9cee2086363bf464a8cf71bd Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Thu, 17 Nov 2022 20:19:15 +0000 Subject: [PATCH 1/6] Add utility plugins --- .../src/plugin/closure.rs | 29 +++++++++++++++++++ .../src/plugin/layer.rs | 20 +++++++++++++ .../aws-smithy-http-server/src/plugin/mod.rs | 4 +++ 3 files changed, 53 insertions(+) create mode 100644 rust-runtime/aws-smithy-http-server/src/plugin/closure.rs create mode 100644 rust-runtime/aws-smithy-http-server/src/plugin/layer.rs diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs new file mode 100644 index 0000000000..94e16bdc90 --- /dev/null +++ b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs @@ -0,0 +1,29 @@ +use tower::layer::util::Stack; + +use crate::operation::{Operation, OperationShape}; + +use super::Plugin; + +/// A [`Plugin`] implemented by a closure over the operation name. See [`plugin_from_operation_name_fn`] for more details. +pub struct OperationNameFn { + f: F, +} + +impl Plugin for OperationNameFn +where + F: Fn(&'static str) -> NewLayer, + Op: OperationShape, +{ + type Service = S; + type Layer = Stack; + + fn map(&self, input: Operation) -> Operation { + input.layer((self.f)(Op::NAME)) + } +} + +/// Constructs a [`Plugin`] using a closure over the operation name `F: Fn(&'static str) -> L` where `L` is a HTTP +/// [`Layer`](tower::Layer). +pub fn plugin_from_operation_name_fn(f: F) -> OperationNameFn { + OperationNameFn { f } +} diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs b/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs new file mode 100644 index 0000000000..26d49af30c --- /dev/null +++ b/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs @@ -0,0 +1,20 @@ +use tower::layer::util::Stack; + +use crate::operation::Operation; + +use super::Plugin; + +/// A [`Plugin`] which appends a HTTP [`Layer`](tower::Layer) to the existing `L` in [`Operation`](Operation). +pub struct HttpLayer(pub L); + +impl Plugin for HttpLayer +where + NewLayer: Clone, +{ + type Service = S; + type Layer = Stack; + + fn map(&self, input: Operation) -> Operation { + input.layer(self.0.clone()) + } +} diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/mod.rs b/rust-runtime/aws-smithy-http-server/src/plugin/mod.rs index 8ee496774f..c6c13aa6c0 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/mod.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/mod.rs @@ -3,15 +3,19 @@ * SPDX-License-Identifier: Apache-2.0 */ +mod closure; mod filter; mod identity; +mod layer; mod pipeline; mod stack; use crate::operation::Operation; +pub use closure::{plugin_from_operation_name_fn, OperationNameFn}; pub use filter::{filter_by_operation_name, FilterByOperationName}; pub use identity::IdentityPlugin; +pub use layer::HttpLayer; pub use pipeline::PluginPipeline; pub use stack::PluginStack; From 9d9fdfe722bf11b31c568da35d69eca4cc282c56 Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Fri, 18 Nov 2022 09:32:55 +0000 Subject: [PATCH 2/6] Add copyright licenses --- rust-runtime/aws-smithy-http-server/src/plugin/closure.rs | 5 +++++ rust-runtime/aws-smithy-http-server/src/plugin/layer.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs index 94e16bdc90..e98fbea99c 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs @@ -1,3 +1,8 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + use tower::layer::util::Stack; use crate::operation::{Operation, OperationShape}; diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs b/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs index 26d49af30c..81bf70ecab 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs @@ -1,3 +1,8 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + use tower::layer::util::Stack; use crate::operation::Operation; From baf464119bdb3c2b8fac9d56e80940b972c68288 Mon Sep 17 00:00:00 2001 From: Harry Barber <106155934+hlbarber@users.noreply.github.com> Date: Fri, 18 Nov 2022 11:02:01 +0000 Subject: [PATCH 3/6] Update rust-runtime/aws-smithy-http-server/src/plugin/layer.rs Co-authored-by: Julian Antonielli --- rust-runtime/aws-smithy-http-server/src/plugin/layer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs b/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs index 81bf70ecab..d7a055f291 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/layer.rs @@ -9,7 +9,7 @@ use crate::operation::Operation; use super::Plugin; -/// A [`Plugin`] which appends a HTTP [`Layer`](tower::Layer) to the existing `L` in [`Operation`](Operation). +/// A [`Plugin`] which appends a HTTP [`Layer`](tower::Layer) `L` to the existing `Layer` in [`Operation`](Operation). pub struct HttpLayer(pub L); impl Plugin for HttpLayer From 6ff052b2c79990d91eea2704966a97cc2678352b Mon Sep 17 00:00:00 2001 From: Harry Barber <106155934+hlbarber@users.noreply.github.com> Date: Fri, 18 Nov 2022 11:04:21 +0000 Subject: [PATCH 4/6] Update rust-runtime/aws-smithy-http-server/src/plugin/closure.rs Co-authored-by: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> --- rust-runtime/aws-smithy-http-server/src/plugin/closure.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs index e98fbea99c..470511f8b3 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs @@ -9,7 +9,7 @@ use crate::operation::{Operation, OperationShape}; use super::Plugin; -/// A [`Plugin`] implemented by a closure over the operation name. See [`plugin_from_operation_name_fn`] for more details. +/// An adapter to convert a `Fn(&'static str) -> Layer` closure into a [`Plugin`]. See [`plugin_from_operation_name_fn`] for more details. pub struct OperationNameFn { f: F, } From 0bc79f40d171e04006a7bf1c046fdf5945dc2f6c Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Fri, 18 Nov 2022 11:05:27 +0000 Subject: [PATCH 5/6] Add `plugin_from_operation_name_fn` example --- .../src/plugin/closure.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs index 470511f8b3..5e3fe60cce 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs @@ -29,6 +29,30 @@ where /// Constructs a [`Plugin`] using a closure over the operation name `F: Fn(&'static str) -> L` where `L` is a HTTP /// [`Layer`](tower::Layer). +/// +/// # Example +/// +/// ```rust +/// use aws_smithy_http_server::plugin::plugin_from_operation_name_fn; +/// use tower::layer::layer_fn; +/// +/// // A `Service` which prints the operation name before calling `S`. +/// struct PrintService { +/// operation_name: &'static str, +/// inner: S +/// } +/// +/// // A `Layer` applying `PrintService`. +/// struct PrintLayer { +/// operation_name: &'static str +/// } +/// +/// // Defines a closure taking the operation name to `PrintLayer`. +/// let f = |operation_name| PrintLayer { operation_name }; +/// +/// // This plugin applies the `PrintService` middleware around every operation. +/// let plugin = plugin_from_operation_name_fn(f); +/// ``` pub fn plugin_from_operation_name_fn(f: F) -> OperationNameFn { OperationNameFn { f } } From b387755417d17e82396fb97f1d74431d540269de Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Fri, 18 Nov 2022 11:16:13 +0000 Subject: [PATCH 6/6] Add bounds on `plugin_from_operation_name_fn` --- rust-runtime/aws-smithy-http-server/src/plugin/closure.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs index 5e3fe60cce..75685c97b9 100644 --- a/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs +++ b/rust-runtime/aws-smithy-http-server/src/plugin/closure.rs @@ -53,6 +53,9 @@ where /// // This plugin applies the `PrintService` middleware around every operation. /// let plugin = plugin_from_operation_name_fn(f); /// ``` -pub fn plugin_from_operation_name_fn(f: F) -> OperationNameFn { +pub fn plugin_from_operation_name_fn(f: F) -> OperationNameFn +where + F: Fn(&'static str) -> L, +{ OperationNameFn { f } }