Unverified Commit cdc710dd authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Make modules in `codegen-core` configurable (#2336)

* Refactor modules to be configurable in `codegen-core`
* Remove panicking default test symbol provider
* Remove as many references to Error/Types as possible
* Rename module constants
parent de186678
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ private class AwsClientGenerics(private val types: Types) : FluentClientGenerics
    override fun sendBounds(
        operation: Symbol,
        operationOutput: Symbol,
        operationError: RuntimeType,
        operationError: Symbol,
        retryClassifier: RuntimeType,
    ): Writable =
        writable { }
+1 −2
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBoundProtocolPayloadGenerator
import software.amazon.smithy.rust.codegen.core.util.cloneOperation
import software.amazon.smithy.rust.codegen.core.util.expectTrait
@@ -155,7 +154,7 @@ class AwsInputPresignedMethod(
        }

    private fun RustWriter.writeInputPresignedMethod(section: OperationSection.InputImpl) {
        val operationError = operationShape.errorSymbol(symbolProvider)
        val operationError = symbolProvider.symbolForOperationError(operationShape)
        val presignableOp = PRESIGNABLE_OPERATIONS.getValue(operationShape.id)

        val makeOperationOp = if (presignableOp.hasModelTransforms()) {
+2 −2
Original line number Diff line number Diff line
@@ -6,10 +6,10 @@
package software.amazon.smithy.rustsdk

import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
@@ -105,7 +105,7 @@ class SdkConfigDecorator : ClientCodegenDecorator {
        val codegenScope = arrayOf(
            "SdkConfig" to AwsRuntimeType.awsTypes(codegenContext.runtimeConfig).resolve("sdk_config::SdkConfig"),
        )
        rustCrate.withModule(RustModule.Config) {
        rustCrate.withModule(ClientRustModule.Config) {
            rustTemplate(
                """
                impl From<&#{SdkConfig}> for Builder {
+10 −26
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.Cli
import software.amazon.smithy.rust.codegen.client.smithy.protocols.ClientProtocolLoader
import software.amazon.smithy.rust.codegen.client.smithy.transformers.AddErrorMessage
import software.amazon.smithy.rust.codegen.client.smithy.transformers.RemoveEventStreamOperations
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.smithy.DirectedWalker
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
@@ -33,15 +32,12 @@ import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerat
import software.amazon.smithy.rust.codegen.core.smithy.generators.StructureGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.OperationErrorGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.eventStreamErrorSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock
import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolGeneratorFactory
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrait
import software.amazon.smithy.rust.codegen.core.smithy.transformers.EventStreamNormalizer
import software.amazon.smithy.rust.codegen.core.smithy.transformers.OperationNormalizer
import software.amazon.smithy.rust.codegen.core.smithy.transformers.RecursiveShapeBoxer
import software.amazon.smithy.rust.codegen.core.smithy.transformers.eventStreamErrors
import software.amazon.smithy.rust.codegen.core.smithy.transformers.operationErrors
import software.amazon.smithy.rust.codegen.core.util.CommandFailed
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.isEventStream
@@ -56,7 +52,6 @@ class ClientCodegenVisitor(
    context: PluginContext,
    private val codegenDecorator: ClientCodegenDecorator,
) : ShapeVisitor.Default<Unit>() {

    private val logger = Logger.getLogger(javaClass.name)
    private val settings = ClientRustSettings.from(context.model, context.settings)

@@ -69,11 +64,11 @@ class ClientCodegenVisitor(
    private val protocolGenerator: ClientProtocolGenerator

    init {
        val symbolVisitorConfig =
            SymbolVisitorConfig(
        val symbolVisitorConfig = SymbolVisitorConfig(
            runtimeConfig = settings.runtimeConfig,
            renameExceptions = settings.codegenConfig.renameExceptions,
            nullabilityCheckMode = NullableIndex.CheckMode.CLIENT_ZERO_VALUE_V1,
            moduleProvider = ClientModuleProvider,
        )
        val baseModel = baselineTransform(context.model)
        val untransformedService = settings.getService(baseModel)
@@ -224,13 +219,8 @@ class ClientCodegenVisitor(
            UnionGenerator(model, symbolProvider, this, shape, renderUnknownVariant = true).render()
        }
        if (shape.isEventStream()) {
            rustCrate.withModule(RustModule.Error) {
                val symbol = symbolProvider.toSymbol(shape)
                val errors = shape.eventStreamErrors()
                    .map { model.expectShape(it.asMemberShape().get().target, StructureShape::class.java) }
                val errorSymbol = shape.eventStreamErrorSymbol(symbolProvider)
                OperationErrorGenerator(model, symbolProvider, symbol, errors)
                    .renderErrors(this, errorSymbol, symbol)
            rustCrate.withModule(ClientRustModule.Error) {
                OperationErrorGenerator(model, symbolProvider, shape).render(this)
            }
        }
    }
@@ -239,14 +229,8 @@ class ClientCodegenVisitor(
     * Generate errors for operation shapes
     */
    override fun operationShape(shape: OperationShape) {
        rustCrate.withModule(RustModule.Error) {
            val operationSymbol = symbolProvider.toSymbol(shape)
            OperationErrorGenerator(
                model,
                symbolProvider,
                operationSymbol,
                shape.operationErrors(model).map { it.asStructureShape().get() },
            ).render(this)
        rustCrate.withModule(ClientRustModule.Error) {
            OperationErrorGenerator(model, symbolProvider, shape).render(this)
        }
    }
}
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package software.amazon.smithy.rust.codegen.client.smithy

import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.ErrorTrait
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.smithy.ModuleProvider
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticInputTrait
import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticOutputTrait
import software.amazon.smithy.rust.codegen.core.util.hasTrait

/**
 * Modules for code generated client crates.
 */
object ClientRustModule {
    /** crate::client */
    val client = Client.self
    object Client {
        /** crate::client */
        val self = RustModule.public("client", "Client and fluent builders for calling the service.")

        /** crate::client::customize */
        val customize = RustModule.public("customize", "Operation customization and supporting types", parent = self)
    }

    val Config = RustModule.public("config", documentation = "Configuration for the service.")
    val Error = RustModule.public("error", documentation = "All error types that operations can return. Documentation on these types is copied from the model.")
    val Operation = RustModule.public("operation", documentation = "All operations that this crate can perform.")
    val Model = RustModule.public("model", documentation = "Data structures used by operation inputs/outputs. Documentation on these types is copied from the model.")
    val Input = RustModule.public("input", documentation = "Input structures for operations. Documentation on these types is copied from the model.")
    val Output = RustModule.public("output", documentation = "Output structures for operations. Documentation on these types is copied from the model.")
    val Types = RustModule.public("types", documentation = "Data primitives referenced by other data types.")
}

object ClientModuleProvider : ModuleProvider {
    override fun moduleForShape(shape: Shape): RustModule.LeafModule = when (shape) {
        is OperationShape -> ClientRustModule.Operation
        is StructureShape -> when {
            shape.hasTrait<ErrorTrait>() -> ClientRustModule.Error
            shape.hasTrait<SyntheticInputTrait>() -> ClientRustModule.Input
            shape.hasTrait<SyntheticOutputTrait>() -> ClientRustModule.Output
            else -> ClientRustModule.Model
        }
        else -> ClientRustModule.Model
    }

    override fun moduleForOperationError(operation: OperationShape): RustModule.LeafModule =
        ClientRustModule.Error

    override fun moduleForEventStreamError(eventStream: UnionShape): RustModule.LeafModule =
        ClientRustModule.Error
}
Loading