|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rust.codegen.server.smithy.generators |
| 7 | + |
| 8 | +import software.amazon.smithy.model.knowledge.TopDownIndex |
| 9 | +import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter |
| 10 | +import software.amazon.smithy.rust.codegen.core.rustlang.Writable |
| 11 | +import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate |
| 12 | +import software.amazon.smithy.rust.codegen.core.rustlang.writable |
| 13 | +import software.amazon.smithy.rust.codegen.core.util.toPascalCase |
| 14 | +import software.amazon.smithy.rust.codegen.core.util.toSnakeCase |
| 15 | +import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency |
| 16 | +import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext |
| 17 | + |
| 18 | +class ScopeMacroGenerator( |
| 19 | + private val codegenContext: ServerCodegenContext, |
| 20 | +) { |
| 21 | + private val runtimeConfig = codegenContext.runtimeConfig |
| 22 | + private val codegenScope = |
| 23 | + arrayOf( |
| 24 | + "SmithyHttpServer" to ServerCargoDependency.smithyHttpServer(runtimeConfig).toType(), |
| 25 | + ) |
| 26 | + |
| 27 | + /** Calculate all `operationShape`s contained within the `ServiceShape`. */ |
| 28 | + private val index = TopDownIndex.of(codegenContext.model) |
| 29 | + private val operations = index.getContainedOperations(codegenContext.serviceShape).toSortedSet(compareBy { it.id }) |
| 30 | + |
| 31 | + private fun macro(): Writable = writable { |
| 32 | + val firstOperationName = codegenContext.symbolProvider.toSymbol(operations.first()).name.toPascalCase() |
| 33 | + val operationNames = operations.joinToString(" ") { |
| 34 | + codegenContext.symbolProvider.toSymbol(it).name.toPascalCase() |
| 35 | + } |
| 36 | + |
| 37 | + // When writing `macro_rules!` we add whitespace between `$` and the arguments to avoid Kotlin templating. |
| 38 | + |
| 39 | + // To acheive the desired API we need to calculate the set theoretic complement `B \ A`. |
| 40 | + // The macro below, for rules prefixed with `@`, encodes a state machine which performs this. |
| 41 | + // The initial state is `(A) () (B)`, where `A` and `B` are lists of elements of `A` and `B`. |
| 42 | + // The rules, in order: |
| 43 | + // - Terminate on pattern `() (t0, t1, ...) (b0, b1, ...)`, the complement has been calculated as |
| 44 | + // `{ t0, t1, ..., b0, b1, ...}`. |
| 45 | + // - Send pattern `(x, a0, a1, ...) (t0, t1, ...) (x, b0, b1, ...)` to |
| 46 | + // `(a0, a1, ...) (t0, t1, ...) (b0, b1, ...)`, eliminating a matching `x` from `A` and `B`. |
| 47 | + // - Send pattern `(a0, a1, ...) (t0, t1, ...) ()` to `(a0, a1, ...) () (t0, t1, ...)`, restarting the search. |
| 48 | + // - Send pattern `(a0, a1, ...) (t0, t1, ...) (b0, b1, ...)` to `(a0, a1, ...) (b0, t0, t1, ...) (b1, ...)`, |
| 49 | + // iterating through the `B`. |
| 50 | + val operationBranches = operations |
| 51 | + .map { codegenContext.symbolProvider.toSymbol(it).name.toPascalCase() }.joinToString("") { |
| 52 | + """ |
| 53 | + // $it match found, pop from both `member` and `not_member` |
| 54 | + (@ $ name: ident, $ contains: ident ($it $($ member: ident)*) ($($ temp: ident)*) ($it $($ not_member: ident)*)) => { |
| 55 | + scope! { @ $ name, $ contains ($($ member)*) ($($ temp)*) ($($ not_member)*) } |
| 56 | + }; |
| 57 | + // $it match not found, pop from `not_member` into `temp` stack |
| 58 | + (@ $ name: ident, $ contains: ident ($it $($ member: ident)*) ($($ temp: ident)*) ($ other: ident $($ not_member: ident)*)) => { |
| 59 | + scope! { @ $ name, $ contains ($it $($ member)*) ($ other $($ temp)*) ($($ not_member)*) } |
| 60 | + }; |
| 61 | + """ |
| 62 | + } |
| 63 | + val crateName = codegenContext.moduleName.toSnakeCase() |
| 64 | + |
| 65 | + // If we have a second operation we can perform further checks |
| 66 | + val otherOperationName: String? = operations.toList().getOrNull(1)?.let { |
| 67 | + codegenContext.symbolProvider.toSymbol(it).name |
| 68 | + } |
| 69 | + val furtherTests = if (otherOperationName != null) { |
| 70 | + writable { |
| 71 | + rustTemplate( |
| 72 | + """ |
| 73 | + /// ## let a = Plugin::<(), $otherOperationName, u64>::apply(&scoped_a, 6); |
| 74 | + /// ## let b = Plugin::<(), $otherOperationName, u64>::apply(&scoped_b, 6); |
| 75 | + /// ## assert_eq!(a, 6_u64); |
| 76 | + /// ## assert_eq!(b, 3_u32); |
| 77 | + """, |
| 78 | + ) |
| 79 | + } |
| 80 | + } else { |
| 81 | + writable {} |
| 82 | + } |
| 83 | + |
| 84 | + rustTemplate( |
| 85 | + """ |
| 86 | + /// A macro to help with scoping [plugins](#{SmithyHttpServer}::plugin) to a subset of all operations. |
| 87 | + /// |
| 88 | + /// In contrast to [`aws_smithy_http_server::scope`](#{SmithyHttpServer}::scope), this macro has knowledge |
| 89 | + /// of the service and any operations _not_ specified will be placed in the opposing group. |
| 90 | + /// |
| 91 | + /// ## Example |
| 92 | + /// |
| 93 | + /// ```rust |
| 94 | + /// scope! { |
| 95 | + /// /// Includes [`$firstOperationName`], excluding all other operations. |
| 96 | + /// struct ScopeA { |
| 97 | + /// includes: [$firstOperationName] |
| 98 | + /// } |
| 99 | + /// } |
| 100 | + /// |
| 101 | + /// scope! { |
| 102 | + /// /// Excludes [`$firstOperationName`], excluding all other operations. |
| 103 | + /// struct ScopeB { |
| 104 | + /// excludes: [$firstOperationName] |
| 105 | + /// } |
| 106 | + /// } |
| 107 | + /// |
| 108 | + /// ## use #{SmithyHttpServer}::plugin::{Plugin, Scoped}; |
| 109 | + /// ## use $crateName::scope; |
| 110 | + /// ## struct MockPlugin; |
| 111 | + /// ## impl<P, Op, S> Plugin<P, Op, S> for MockPlugin { type Service = u32; fn apply(&self, svc: S) -> u32 { 3 } } |
| 112 | + /// ## let scoped_a = Scoped::new::<ScopeA>(MockPlugin); |
| 113 | + /// ## let scoped_b = Scoped::new::<ScopeB>(MockPlugin); |
| 114 | + /// ## let a = Plugin::<(), $crateName::operation_shape::$firstOperationName, u64>::apply(&scoped_a, 6); |
| 115 | + /// ## let b = Plugin::<(), $crateName::operation_shape::$firstOperationName, u64>::apply(&scoped_b, 6); |
| 116 | + /// ## assert_eq!(a, 3_u32); |
| 117 | + /// ## assert_eq!(b, 6_u64); |
| 118 | + /// ``` |
| 119 | + ##[macro_export] |
| 120 | + macro_rules! scope { |
| 121 | + // Completed, render impls |
| 122 | + (@ $ name: ident, $ contains: ident () ($($ temp: ident)*) ($($ not_member: ident)*)) => { |
| 123 | + $( |
| 124 | + impl #{SmithyHttpServer}::plugin::scoped::Membership<$ temp> for $ name { |
| 125 | + type Contains = #{SmithyHttpServer}::plugin::scoped::$ contains; |
| 126 | + } |
| 127 | + )* |
| 128 | + $( |
| 129 | + impl #{SmithyHttpServer}::plugin::scoped::Membership<$ not_member> for $ name { |
| 130 | + type Contains = #{SmithyHttpServer}::plugin::scoped::$ contains; |
| 131 | + } |
| 132 | + )* |
| 133 | + }; |
| 134 | + // All `not_member`s exhausted, move `temp` into `not_member` |
| 135 | + (@ $ name: ident, $ contains: ident ($($ member: ident)*) ($($ temp: ident)*) ()) => { |
| 136 | + scope! { @ $ name, $ contains ($($ member)*) () ($($ temp)*) } |
| 137 | + }; |
| 138 | + $operationBranches |
| 139 | + ( |
| 140 | + $(##[$ attrs:meta])* |
| 141 | + $ vis:vis struct $ name:ident { |
| 142 | + includes: [$($ include:ident),*] |
| 143 | + } |
| 144 | + ) => { |
| 145 | + use $ crate::operation_shape::*; |
| 146 | + #{SmithyHttpServer}::scope! { |
| 147 | + $(##[$ attrs])* |
| 148 | + $ vis struct $ name { |
| 149 | + includes: [$($ include),*], |
| 150 | + excludes: [] |
| 151 | + } |
| 152 | + } |
| 153 | + scope! { @ $ name, False ($($ include)*) () ($operationNames) } |
| 154 | + }; |
| 155 | + ( |
| 156 | + $(##[$ attrs:meta])* |
| 157 | + $ vis:vis struct $ name:ident { |
| 158 | + excludes: [$($ exclude:ident),*] |
| 159 | + } |
| 160 | + ) => { |
| 161 | + use $ crate::operation_shape::*; |
| 162 | +
|
| 163 | + #{SmithyHttpServer}::scope! { |
| 164 | + $(##[$ attrs])* |
| 165 | + $ vis struct $ name { |
| 166 | + includes: [], |
| 167 | + excludes: [$($ exclude),*] |
| 168 | + } |
| 169 | + } |
| 170 | + scope! { @ $ name, True ($($ exclude)*) () ($operationNames) } |
| 171 | + }; |
| 172 | + } |
| 173 | + """, |
| 174 | + *codegenScope, |
| 175 | + "FurtherTests" to furtherTests, |
| 176 | + ) |
| 177 | + } |
| 178 | + |
| 179 | + fun render(writer: RustWriter) { |
| 180 | + macro()(writer) |
| 181 | + } |
| 182 | +} |
0 commit comments