generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 230
Improve Plugin toolkit
#2003
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
Merged
Merged
Improve Plugin toolkit
#2003
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91ddc2d
Add utility plugins
9d9fdfe
Add copyright licenses
baf4641
Update rust-runtime/aws-smithy-http-server/src/plugin/layer.rs
hlbarber 6ff052b
Update rust-runtime/aws-smithy-http-server/src/plugin/closure.rs
hlbarber 0bc79f4
Add `plugin_from_operation_name_fn` example
b387755
Add bounds on `plugin_from_operation_name_fn`
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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}; | ||
|
|
||
| use super::Plugin; | ||
|
|
||
| /// 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: 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). | ||
| /// | ||
| /// # 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<S> { | ||
| /// 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<L, F>(f: F) -> OperationNameFn<F> | ||
| where | ||
| F: Fn(&'static str) -> L, | ||
| { | ||
| OperationNameFn { f } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| use super::Plugin; | ||
|
|
||
| /// A [`Plugin`] which appends a HTTP [`Layer`](tower::Layer) `L` to the existing `Layer` in [`Operation<S, Layer>`](Operation). | ||
| 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()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.