-
Notifications
You must be signed in to change notification settings - Fork 190
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
Add Scoped
Plugin
#2759
Merged
Merged
Add Scoped
Plugin
#2759
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
72133d7
Add Scoped plugin
6a833de
Apply suggestions from code review
hlbarber 19b206f
Tweak exports
8564c71
Add codegenerated scope macro
dcd0a33
Merge branch 'main' into harryb/scoped-plugin
hlbarber bc6ddea
Fix doc issues
51cc3b3
Apply suggestions from code review
hlbarber 02002f5
Address comments
edb53ef
Fix documentation
30ad4d7
Contrast `FilterByOperationId` with `Scoped`
80e2fce
Update CHANGELOG.next.toml
5fd077c
Fix getOrNull
8a01f64
First operation casing
d9e4de0
Add macro explanation
a48dbc2
Improve documentation
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 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
180 changes: 180 additions & 0 deletions
180
rust-runtime/aws-smithy-http-server/src/plugin/scoped.rs
This file contains 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,180 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use std::marker::PhantomData; | ||
|
||
use super::Plugin; | ||
|
||
/// Marker struct for `true`. | ||
/// | ||
/// Implements [`ConditionalApply`] which applies the a [`Plugin`]. | ||
hlbarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct True; | ||
|
||
/// Marker struct for `false`. | ||
/// | ||
/// Implements [`ConditionalApply`] which does nothing. | ||
pub struct False; | ||
|
||
/// Conditionally applies a [`Plugin`] `Pl` to some service `S`. | ||
/// | ||
/// See [`True`] and [`False`]. | ||
pub trait ConditionalApply<P, Op, S, Pl> { | ||
type Service; | ||
|
||
fn apply(plugin: &Pl, svc: S) -> Self::Service; | ||
} | ||
|
||
impl<P, Op, S, Pl> ConditionalApply<P, Op, S, Pl> for True | ||
where | ||
Pl: Plugin<P, Op, S>, | ||
{ | ||
type Service = Pl::Service; | ||
|
||
fn apply(plugin: &Pl, svc: S) -> Self::Service { | ||
plugin.apply(svc) | ||
} | ||
} | ||
|
||
impl<P, Op, S, Pl> ConditionalApply<P, Op, S, Pl> for False { | ||
type Service = S; | ||
|
||
fn apply(_plugin: &Pl, svc: S) -> Self::Service { | ||
svc | ||
} | ||
} | ||
|
||
/// A [`Plugin`] which scopes the application of an inner [`Plugin`]. | ||
/// | ||
/// Operations within the scope will have the inner [`Plugin`] applied. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # struct OperationA; | ||
/// # struct OperationB; | ||
/// # struct OperationC; | ||
/// # let plugin = (); | ||
/// | ||
/// // Define a scope over a service with 3 operations | ||
/// scope! { | ||
/// struct OnlyAB { | ||
/// includes: [OperationA, OperationB], | ||
/// excludes: [OperationC] | ||
/// } | ||
/// } | ||
/// | ||
/// // Create a scoped plugin | ||
/// let scoped_plugin = Scoped::new::<OnlyAB>(plugin); | ||
/// ``` | ||
pub struct Scoped<Scope, Pl> { | ||
scope: PhantomData<Scope>, | ||
plugin: Pl, | ||
} | ||
|
||
impl<Pl> Scoped<(), Pl> { | ||
/// Creates a new [`Scoped`] from a `Scope` and [`Plugin`]. | ||
pub fn new<Scope>(plugin: Pl) -> Scoped<Scope, Pl> { | ||
Scoped { | ||
scope: PhantomData, | ||
plugin, | ||
} | ||
} | ||
} | ||
|
||
/// A trait marking which operations are in scope via the associated type [`Scoped::Contains`]. | ||
pub trait Membership<Op> { | ||
type Contains; | ||
} | ||
|
||
impl<P, Op, S, Scope, Pl> Plugin<P, Op, S> for Scoped<Scope, Pl> | ||
where | ||
Scope: Membership<Op>, | ||
Scope::Contains: ConditionalApply<P, Op, S, Pl>, | ||
Pl: Plugin<P, Op, S>, | ||
{ | ||
type Service = <Scope::Contains as ConditionalApply<P, Op, S, Pl>>::Service; | ||
|
||
fn apply(&self, svc: S) -> Self::Service { | ||
<Scope::Contains as ConditionalApply<P, Op, S, Pl>>::apply(&self.plugin, svc) | ||
} | ||
} | ||
|
||
/// A macro for creating subsets operations for [`Scoped`]. | ||
hlbarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// The scope must include or exclude _all_ operations. | ||
hlbarber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// scope! { | ||
/// struct OnlyAB { | ||
/// includes: [OperationA, OperationB], | ||
/// excludes: [OperationC] | ||
/// } | ||
/// } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! scope { | ||
( | ||
$(#[$attrs:meta])* | ||
$vis:vis struct $name:ident { | ||
includes: [$($include:ty),*], | ||
excludes: [$($exclude:ty),*] | ||
} | ||
) => { | ||
$(#[$attrs])* | ||
$vis struct $name; | ||
|
||
$( | ||
impl $crate::plugin::Membership<$include> for $name { | ||
type Contains = $crate::plugin::scoped::True; | ||
} | ||
)* | ||
$( | ||
impl $crate::plugin::Membership<$exclude> for $name { | ||
type Contains = $crate::plugin::scoped::False; | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::plugin::Plugin; | ||
|
||
use super::Scoped; | ||
|
||
struct OperationA; | ||
struct OperationB; | ||
|
||
scope! { | ||
/// Includes A, not B. | ||
pub struct AuthScope { | ||
includes: [OperationA], | ||
excludes: [OperationB] | ||
} | ||
} | ||
|
||
struct MockPlugin; | ||
|
||
impl<P, Op> Plugin<P, Op, u32> for MockPlugin { | ||
type Service = String; | ||
|
||
fn apply(&self, svc: u32) -> Self::Service { | ||
svc.to_string() | ||
} | ||
} | ||
|
||
#[test] | ||
fn scope() { | ||
let plugin = MockPlugin; | ||
let scoped_plugin = Scoped::new::<AuthScope>(plugin); | ||
|
||
let out: String = Plugin::<(), OperationA, _>::apply(&scoped_plugin, 3_u32); | ||
assert_eq!(out, "3".to_string()); | ||
let out: u32 = Plugin::<(), OperationB, _>::apply(&scoped_plugin, 3_u32); | ||
assert_eq!(out, 3); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why hidden?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like
Scoped
and thescope
macros to be the primary API and the#[doc(hidden)]
items to be implementation details.