From 0226446121592a1d08ee45ce1c8fe2329870bdfd Mon Sep 17 00:00:00 2001 From: david-perez Date: Mon, 6 Feb 2023 19:16:43 +0100 Subject: [PATCH] Rename Rust server codegen plugins (#2306) Rename `RustCodegenServerPlugin` to `RustServerCodegenPlugin`, for consistency with `RustClientCodegenPlugin`. This is a better name, since the plugin is named `rust-server-codegen`. This commit also renames `PythonCodegenServerPlugin` to `RustServerCodegenPythonPlugin` for the same reasons. This commit also contains other drive-by improvements made while working on #2302. --- .../client/smithy/RustClientCodegenPlugin.kt | 4 +-- .../rust/codegen/core/rustlang/RustWriter.kt | 2 +- .../codegen/core/smithy/DirectedWalker.kt | 9 ++---- .../smithy/rust/codegen/core/testutil/Rust.kt | 3 +- .../smithy/PythonServerCodegenVisitor.kt | 2 +- ...in.kt => RustServerCodegenPythonPlugin.kt} | 14 ++++----- ...ware.amazon.smithy.build.SmithyBuildPlugin | 2 +- ...erPlugin.kt => RustServerCodegenPlugin.kt} | 29 +++++++++---------- .../server/smithy/ServerCodegenContext.kt | 2 +- .../server/smithy/ServerCodegenVisitor.kt | 2 +- .../server/smithy/ServerRustSettings.kt | 2 +- .../smithy/ValidateUnsupportedConstraints.kt | 3 ++ .../customize/ServerCodegenDecorator.kt | 2 +- .../smithy/testutil/ServerTestHelpers.kt | 6 ++-- ...ware.amazon.smithy.build.SmithyBuildPlugin | 2 +- 15 files changed, 40 insertions(+), 44 deletions(-) rename codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/{PythonCodegenServerPlugin.kt => RustServerCodegenPythonPlugin.kt} (90%) rename codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/{RustCodegenServerPlugin.kt => RustServerCodegenPlugin.kt} (77%) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt index 0189a4cb65..aa5d5bafb7 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt @@ -66,10 +66,10 @@ class RustClientCodegenPlugin : DecoratableBuildPlugin() { } companion object { - /** SymbolProvider + /** * When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider * - * The Symbol provider is composed of a base `SymbolVisitor` which handles the core functionality, then is layered + * The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered * with other symbol providers, documented inline, to handle the full scope of Smithy types. */ fun baseSymbolProvider(model: Model, serviceShape: ServiceShape, symbolVisitorConfig: SymbolVisitorConfig) = diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt index 9cd7d00bad..7fb70e483f 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt @@ -510,7 +510,7 @@ class RustWriter private constructor( * Callers must take care to use [this] when writing to ensure code is written to the right place: * ```kotlin * val writer = RustWriter.forModule("model") - * writer.withModule(RustModule.public("nested")) { + * writer.withInlineModule(RustModule.public("nested")) { * Generator(...).render(this) // GOOD * Generator(...).render(writer) // WRONG! * } diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/DirectedWalker.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/DirectedWalker.kt index 51c0b4ecf8..f48b996045 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/DirectedWalker.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/DirectedWalker.kt @@ -19,11 +19,8 @@ import java.util.function.Predicate class DirectedWalker(model: Model) { private val inner = Walker(model) - fun walkShapes(shape: Shape): Set { - return walkShapes(shape) { _ -> true } - } + fun walkShapes(shape: Shape): Set = walkShapes(shape) { true } - fun walkShapes(shape: Shape, predicate: Predicate): Set { - return inner.walkShapes(shape) { rel -> predicate.test(rel) && rel.direction == RelationshipDirection.DIRECTED } - } + fun walkShapes(shape: Shape, predicate: Predicate): Set = + inner.walkShapes(shape) { rel -> predicate.test(rel) && rel.direction == RelationshipDirection.DIRECTED } } diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt index c91581c052..f05e542381 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt @@ -194,8 +194,7 @@ fun generatePluginContext( ) } - val settings = settingsBuilder.merge(additionalSettings) - .build() + val settings = settingsBuilder.merge(additionalSettings).build() val pluginContext = PluginContext.builder().model(model).fileManifest(manifest).settings(settings).build() return pluginContext to testPath } diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt index 8fd2a07f58..edc5493d94 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt @@ -74,7 +74,7 @@ class PythonServerCodegenVisitor( serviceShape: ServiceShape, symbolVisitorConfig: SymbolVisitorConfig, publicConstrainedTypes: Boolean, - ) = PythonCodegenServerPlugin.baseSymbolProvider(model, serviceShape, symbolVisitorConfig, publicConstrainedTypes) + ) = RustServerCodegenPythonPlugin.baseSymbolProvider(model, serviceShape, symbolVisitorConfig, publicConstrainedTypes) val serverSymbolProviders = ServerSymbolProviders.from( model, diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/RustServerCodegenPythonPlugin.kt similarity index 90% rename from codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt rename to codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/RustServerCodegenPythonPlugin.kt index 7e277185f7..eb8ac1debb 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonCodegenServerPlugin.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/RustServerCodegenPythonPlugin.kt @@ -14,7 +14,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolP import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.EventStreamSymbolProvider -import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitorConfig import software.amazon.smithy.rust.codegen.server.python.smithy.customizations.DECORATORS import software.amazon.smithy.rust.codegen.server.smithy.ConstrainedShapeSymbolMetadataProvider @@ -26,16 +25,20 @@ import java.util.logging.Level import java.util.logging.Logger /** - * Rust with Python bindings Codegen Plugin. + * Rust Server with Python bindings Codegen Plugin. + * * This is the entrypoint for code generation, triggered by the smithy-build plugin. * `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which * enables the smithy-build plugin to invoke `execute` with all of the Smithy plugin context + models. */ -class PythonCodegenServerPlugin : SmithyBuildPlugin { +class RustServerCodegenPythonPlugin : SmithyBuildPlugin { private val logger = Logger.getLogger(javaClass.name) override fun getName(): String = "rust-server-codegen-python" + /** + * See [software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin]. + */ override fun execute(context: PluginContext) { // Suppress extremely noisy logs about reserved words Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF @@ -57,10 +60,7 @@ class PythonCodegenServerPlugin : SmithyBuildPlugin { companion object { /** - * When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider - * - * The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered - * with other symbol providers, documented inline, to handle the full scope of Smithy types. + * See [software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin]. */ fun baseSymbolProvider( model: Model, diff --git a/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin b/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin index 6dc5b76c78..000cc4b7ae 100644 --- a/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin +++ b/codegen-server/python/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin @@ -2,4 +2,4 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 # -software.amazon.smithy.rust.codegen.server.python.smithy.PythonCodegenServerPlugin +software.amazon.smithy.rust.codegen.server.python.smithy.RustServerCodegenPythonPlugin diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustServerCodegenPlugin.kt similarity index 77% rename from codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt rename to codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustServerCodegenPlugin.kt index 8a1dc17e54..68a538252c 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustServerCodegenPlugin.kt @@ -30,33 +30,30 @@ import java.util.logging.Logger * `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which * enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models. */ -class RustCodegenServerPlugin : SmithyBuildPlugin { +class RustServerCodegenPlugin : SmithyBuildPlugin { private val logger = Logger.getLogger(javaClass.name) override fun getName(): String = "rust-server-codegen" - override fun execute(context: PluginContext) { - // Suppress extremely noisy logs about reserved words + /** + * See [software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin]. + */ + override fun execute( + context: PluginContext, + ) { Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF - // Discover [RustCodegenDecorators] on the classpath. [RustCodegenDecorator] returns different types of - // customizations. A customization is a function of: - // - location (e.g. the mutate section of an operation) - // - context (e.g. the of the operation) - // - writer: The active RustWriter at the given location - val codegenDecorator: CombinedServerCodegenDecorator = - CombinedServerCodegenDecorator.fromClasspath(context, ServerRequiredCustomizations()) - - // ServerCodegenVisitor is the main driver of code generation that traverses the model and generates code + val codegenDecorator = + CombinedServerCodegenDecorator.fromClasspath( + context, + ServerRequiredCustomizations(), + ) logger.info("Loaded plugin to generate pure Rust bindings for the server SDK") ServerCodegenVisitor(context, codegenDecorator).execute() } companion object { /** - * When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider. - * - * The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered - * with other symbol providers, documented inline, to handle the full scope of Smithy types. + * See [software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin]. */ fun baseSymbolProvider( model: Model, diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt index a0ad38f04f..e71ae7ded1 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt @@ -13,7 +13,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider /** - * [ServerCodegenContext] contains code-generation context that is _specific_ to the [RustCodegenServerPlugin] plugin + * [ServerCodegenContext] contains code-generation context that is _specific_ to the [RustServerCodegenPlugin] plugin * from the `rust-codegen-server` subproject. * * It inherits from [CodegenContext], which contains code-generation context that is common to _all_ smithy-rs plugins. diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index 607aad2158..e693d1ac5e 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -129,7 +129,7 @@ open class ServerCodegenVisitor( service, symbolVisitorConfig, settings.codegenConfig.publicConstrainedTypes, - RustCodegenServerPlugin::baseSymbolProvider, + RustServerCodegenPlugin::baseSymbolProvider, ) codegenContext = ServerCodegenContext( diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustSettings.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustSettings.kt index dbfc8356a2..19f60aac93 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustSettings.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustSettings.kt @@ -25,7 +25,7 @@ import java.util.Optional */ /** - * Settings used by [RustCodegenServerPlugin]. + * Settings used by [RustServerCodegenPlugin]. */ data class ServerRustSettings( override val service: ShapeId, diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt index 8bf6f928d9..4ebe3299b4 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt @@ -134,6 +134,9 @@ data class ValidationResult(val shouldAbort: Boolean, val messages: List { * * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. */ -class CombinedServerCodegenDecorator(decorators: List) : +class CombinedServerCodegenDecorator(private val decorators: List) : CombinedCoreCodegenDecorator(decorators), ServerCodegenDecorator { override val name: String diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/testutil/ServerTestHelpers.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/testutil/ServerTestHelpers.kt index 9d49dfb9fe..dd80905a2e 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/testutil/ServerTestHelpers.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/testutil/ServerTestHelpers.kt @@ -19,7 +19,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitorConfig import software.amazon.smithy.rust.codegen.core.smithy.generators.StructureGenerator import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock import software.amazon.smithy.rust.codegen.core.testutil.TestRuntimeConfig -import software.amazon.smithy.rust.codegen.server.smithy.RustCodegenServerPlugin +import software.amazon.smithy.rust.codegen.server.smithy.RustServerCodegenPlugin import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenConfig import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.ServerRustSettings @@ -53,7 +53,7 @@ fun serverTestSymbolProviders( (serviceShape ?: testServiceShapeFor(model)).id, ) ).codegenConfig.publicConstrainedTypes, - RustCodegenServerPlugin::baseSymbolProvider, + RustServerCodegenPlugin::baseSymbolProvider, ) fun serverTestRustSettings( @@ -98,7 +98,7 @@ fun serverTestCodegenContext( service, ServerTestSymbolVisitorConfig, settings.codegenConfig.publicConstrainedTypes, - RustCodegenServerPlugin::baseSymbolProvider, + RustServerCodegenPlugin::baseSymbolProvider, ) return ServerCodegenContext( diff --git a/codegen-server/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin b/codegen-server/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin index 00f891cb22..392b1d4392 100644 --- a/codegen-server/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin +++ b/codegen-server/src/main/resources/META-INF/services/software.amazon.smithy.build.SmithyBuildPlugin @@ -2,4 +2,4 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 # -software.amazon.smithy.rust.codegen.server.smithy.RustCodegenServerPlugin +software.amazon.smithy.rust.codegen.server.smithy.RustServerCodegenPlugin