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

Improve Plugin toolkit #2003

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
29 changes: 29 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/closure.rs
Original file line number Diff line number Diff line change
@@ -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.
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
pub struct OperationNameFn<F> {
f: F,
}

impl<P, Op, S, ExistingLayer, NewLayer, F> Plugin<P, Op, S, ExistingLayer> for OperationNameFn<F>
where
F: Fn(&'static str) -> NewLayer,
Op: OperationShape,
{
type Service = S;
type Layer = Stack<ExistingLayer, NewLayer>;

fn map(&self, input: Operation<S, ExistingLayer>) -> Operation<Self::Service, Self::Layer> {
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).
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
pub fn plugin_from_operation_name_fn<F>(f: F) -> OperationNameFn<F> {
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
OperationNameFn { f }
}
20 changes: 20 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/layer.rs
Original file line number Diff line number Diff line change
@@ -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<S, L>`](Operation).
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
pub struct HttpLayer<L>(pub L);

impl<P, Op, S, ExistingLayer, NewLayer> Plugin<P, Op, S, ExistingLayer> for HttpLayer<NewLayer>
where
NewLayer: Clone,
{
type Service = S;
type Layer = Stack<ExistingLayer, NewLayer>;

fn map(&self, input: Operation<S, ExistingLayer>) -> Operation<Self::Service, Self::Layer> {
input.layer(self.0.clone())
}
}
4 changes: 4 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down