Skip to content

Commit

Permalink
Hide the operation parse impl structs
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Feb 22, 2023
1 parent 588592e commit 1cd10b9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package software.amazon.smithy.rust.codegen.client.smithy.generators.protocol

import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.derive
Expand All @@ -14,7 +16,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.docLink
import software.amazon.smithy.rust.codegen.core.rustlang.implBlock
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
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
Expand All @@ -26,7 +27,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.core.util.inputShape

open class ClientProtocolGenerator(
codegenContext: CodegenContext,
private val codegenContext: ClientCodegenContext,
private val protocol: Protocol,
/**
* Operations generate a `make_operation(&config)` method to build a `aws_smithy_http::Operation` that can be dispatched
Expand All @@ -52,7 +53,6 @@ open class ClientProtocolGenerator(
val inputShape = operationShape.inputShape(model)

// impl OperationInputShape { ... }
val operationName = symbolProvider.toSymbol(operationShape).name
inputWriter.implBlock(symbolProvider.toSymbol(inputShape)) {
writeCustomizations(
customizations,
Expand All @@ -61,6 +61,48 @@ open class ClientProtocolGenerator(
makeOperationGenerator.generateMakeOperation(this, operationShape, customizations)
}

when (codegenContext.settings.codegenConfig.enableNewCrateOrganizationScheme) {
true -> renderOperationStruct(operationWriter, operationShape, customizations)
else -> oldRenderOperationStruct(operationWriter, operationShape, inputShape, customizations)
}
}

private fun renderOperationStruct(
operationWriter: RustWriter,
operationShape: OperationShape,
customizations: List<OperationCustomization>,
) {
val operationName = symbolProvider.toSymbol(operationShape).name

// pub struct Operation { ... }
operationWriter.rust(
"""
/// `ParseStrictResponse` impl for `$operationName`.
""",
)
Attribute(derive(RuntimeType.Clone, RuntimeType.Default, RuntimeType.Debug)).render(operationWriter)
Attribute.NonExhaustive.render(operationWriter)
Attribute.DocHidden.render(operationWriter)
operationWriter.rust("pub struct $operationName;")
operationWriter.implBlock(symbolProvider.toSymbol(operationShape)) {
rustBlock("pub(crate) fn new() -> Self") {
rust("Self")
}

writeCustomizations(customizations, OperationSection.OperationImplBlock(customizations))
}
traitGenerator.generateTraitImpls(operationWriter, operationShape, customizations)
}

// TODO(CrateReorganization): Remove this function when removing `enableNewCrateOrganizationScheme`
private fun oldRenderOperationStruct(
operationWriter: RustWriter,
operationShape: OperationShape,
inputShape: StructureShape,
customizations: List<OperationCustomization>,
) {
val operationName = symbolProvider.toSymbol(operationShape).name

// pub struct Operation { ... }
val fluentBuilderName = FluentClientGenerator.clientOperationFnName(operationShape, symbolProvider)
operationWriter.rust(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ErrorTrait
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.ResponseBindingGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.MakeOperationGenerator
Expand Down Expand Up @@ -47,7 +48,7 @@ import software.amazon.smithy.rust.codegen.core.util.outputShape
import software.amazon.smithy.rust.codegen.core.util.toSnakeCase

class HttpBoundProtocolGenerator(
codegenContext: CodegenContext,
codegenContext: ClientCodegenContext,
protocol: Protocol,
) : ClientProtocolGenerator(
codegenContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private class TestProtocolMakeOperationGenerator(

// A stubbed test protocol to do enable testing intentionally broken protocols
private class TestProtocolGenerator(
codegenContext: CodegenContext,
codegenContext: ClientCodegenContext,
protocol: Protocol,
httpRequestBuilder: String,
body: String,
Expand Down

0 comments on commit 1cd10b9

Please sign in to comment.