Skip to content

Commit

Permalink
Rename Rust server codegen plugins (smithy-lang#2306)
Browse files Browse the repository at this point in the history
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 smithy-lang#2302.
  • Loading branch information
david-perez authored Feb 6, 2023
1 parent 7fdb5c9 commit 0226446
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ import java.util.function.Predicate
class DirectedWalker(model: Model) {
private val inner = Walker(model)

fun walkShapes(shape: Shape): Set<Shape> {
return walkShapes(shape) { _ -> true }
}
fun walkShapes(shape: Shape): Set<Shape> = walkShapes(shape) { true }

fun walkShapes(shape: Shape, predicate: Predicate<Relationship>): Set<Shape> {
return inner.walkShapes(shape) { rel -> predicate.test(rel) && rel.direction == RelationshipDirection.DIRECTED }
}
fun walkShapes(shape: Shape, predicate: Predicate<Relationship>): Set<Shape> =
inner.walkShapes(shape) { rel -> predicate.test(rel) && rel.direction == RelationshipDirection.DIRECTED }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ open class ServerCodegenVisitor(
service,
symbolVisitorConfig,
settings.codegenConfig.publicConstrainedTypes,
RustCodegenServerPlugin::baseSymbolProvider,
RustServerCodegenPlugin::baseSymbolProvider,
)

codegenContext = ServerCodegenContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.util.Optional
*/

/**
* Settings used by [RustCodegenServerPlugin].
* Settings used by [RustServerCodegenPlugin].
*/
data class ServerRustSettings(
override val service: ShapeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ data class ValidationResult(val shouldAbort: Boolean, val messages: List<LogMess

private val unsupportedConstraintsOnMemberShapes = allConstraintTraits - RequiredTrait::class.java

/**
* Validate that all constrained operations have the shape [validationExceptionShapeId] shape attached to their errors.
*/
fun validateOperationsWithConstrainedInputHaveValidationExceptionAttached(
model: Model,
service: ServiceShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ServerCodegenDecorator : CoreCodegenDecorator<ServerCodegenContext> {
*
* This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators.
*/
class CombinedServerCodegenDecorator(decorators: List<ServerCodegenDecorator>) :
class CombinedServerCodegenDecorator(private val decorators: List<ServerCodegenDecorator>) :
CombinedCoreCodegenDecorator<ServerCodegenContext, ServerCodegenDecorator>(decorators),
ServerCodegenDecorator {
override val name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +53,7 @@ fun serverTestSymbolProviders(
(serviceShape ?: testServiceShapeFor(model)).id,
)
).codegenConfig.publicConstrainedTypes,
RustCodegenServerPlugin::baseSymbolProvider,
RustServerCodegenPlugin::baseSymbolProvider,
)

fun serverTestRustSettings(
Expand Down Expand Up @@ -98,7 +98,7 @@ fun serverTestCodegenContext(
service,
ServerTestSymbolVisitorConfig,
settings.codegenConfig.publicConstrainedTypes,
RustCodegenServerPlugin::baseSymbolProvider,
RustServerCodegenPlugin::baseSymbolProvider,
)

return ServerCodegenContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0226446

Please sign in to comment.