diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialCaches.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialCaches.kt index 03f22cb21f6a1ad1bc8560e0ec8ffd500b433f8c..5aaaeab950644c30307371e0c78a4316898a2dea 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialCaches.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialCaches.kt @@ -15,10 +15,10 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig -import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization 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.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization class CredentialsCacheDecorator : ClientCodegenDecorator { override val name: String = "CredentialsCache" @@ -38,12 +38,10 @@ class CredentialsCacheDecorator : ClientCodegenDecorator { return baseCustomizations + CredentialsCacheFeature(codegenContext.runtimeConfig) } - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> = + override fun extraSections(codegenContext: ClientCodegenContext): List = listOf( - SdkConfigSection.create { section -> - writable { - rust("${section.serviceConfigBuilder}.set_credentials_cache(${section.sdkConfig}.credentials_cache().cloned());") - } + adhocCustomization { section -> + rust("${section.serviceConfigBuilder}.set_credentials_cache(${section.sdkConfig}.credentials_cache().cloned());") }, ) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt index dc6f8e9d0ae3b898a6a256c5946a3901ec41c528..c9499c39203d8d33524f8ec0fe8ad5d1a99487a7 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt @@ -17,8 +17,8 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable 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.RustCrate -import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection -import software.amazon.smithy.rust.codegen.core.smithy.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization +import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection @@ -40,12 +40,10 @@ class CredentialsProviderDecorator : ClientCodegenDecorator { return baseCustomizations + PubUseCredentials(codegenContext.runtimeConfig) } - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> = + override fun extraSections(codegenContext: ClientCodegenContext): List = listOf( - SdkConfigSection.create { section -> - writable { - rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider().cloned());") - } + adhocCustomization { section -> + rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider().cloned());") }, ) diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/EndpointBuiltInsDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/EndpointBuiltInsDecorator.kt index 0b869e7c6f54d7ccff8dcb1e2d3f4b87799ff2ac..1ac83c10ca2ce837415fdf710d59bcf813185d82 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/EndpointBuiltInsDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/EndpointBuiltInsDecorator.kt @@ -29,8 +29,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection -import software.amazon.smithy.rust.codegen.core.smithy.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization import software.amazon.smithy.rust.codegen.core.util.PANIC import software.amazon.smithy.rust.codegen.core.util.dq import software.amazon.smithy.rust.codegen.core.util.extendIf @@ -71,7 +70,7 @@ fun Model.sdkConfigSetter( serviceId: ShapeId, builtInSrc: Parameter, configParameterNameOverride: String?, -): Pair, (Section) -> Writable>? { +): AdHocCustomization? { val builtIn = loadBuiltIn(serviceId, builtInSrc) ?: return null val fieldName = configParameterNameOverride ?: builtIn.name.rustName() @@ -79,7 +78,7 @@ fun Model.sdkConfigSetter( ParameterType.STRING -> writable { rust("|s|s.to_string()") } ParameterType.BOOLEAN -> null } - return SdkConfigSection.copyField(fieldName, map) + return SdkConfigCustomization.copyField(fieldName, map) } /** @@ -99,7 +98,7 @@ fun decoratorForBuiltIn( private fun rulesetContainsBuiltIn(codegenContext: ClientCodegenContext) = codegenContext.getBuiltIn(builtIn) != null - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> { + override fun extraSections(codegenContext: ClientCodegenContext): List { return listOfNotNull( codegenContext.model.sdkConfigSetter(codegenContext.serviceShape.id, builtIn, clientParam?.name), ) diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt index fef37957bb8accc6c4ce1fec9f1f416874898ab1..70775e2cda4f94aa7465cb48764ac2f512b3f4d7 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt @@ -20,10 +20,10 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig -import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization 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.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection import software.amazon.smithy.rust.codegen.core.util.dq @@ -108,17 +108,15 @@ class RegionDecorator : ClientCodegenDecorator { return baseCustomizations.extendIf(usesRegion(codegenContext)) { PubUseRegion(codegenContext.runtimeConfig) } } - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> { + override fun extraSections(codegenContext: ClientCodegenContext): List { return usesRegion(codegenContext).thenSingletonListOf { - SdkConfigSection.create { section -> - { - rust( - """ - ${section.serviceConfigBuilder} = - ${section.serviceConfigBuilder}.region(${section.sdkConfig}.region().cloned()); - """, - ) - } + adhocCustomization { section -> + rust( + """ + ${section.serviceConfigBuilder} = + ${section.serviceConfigBuilder}.region(${section.sdkConfig}.region().cloned()); + """, + ) } } } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt index 2d1ff40a1fec50af120d218eae7494b03d8e9e19..f0e2a7e7d642bbc5d150ed1e7780b75ae32343d3 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/SdkConfigDecorator.kt @@ -11,19 +11,17 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.config.Confi 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.join import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RustCrate +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection -import software.amazon.smithy.rust.codegen.core.smithy.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization +import software.amazon.smithy.rust.codegen.core.smithy.customize.writeCustomizations -/** - * Section enabling linkage between `SdkConfig` and ::Config - */ -object SdkConfigSection : AdHocSection("SdkConfig") { +sealed class SdkConfigSection(name: String) : AdHocSection(name) { /** * [sdkConfig]: A reference to the SDK config struct * [serviceConfigBuilder]: A reference (owned) to the `::config::Builder` struct. @@ -34,8 +32,13 @@ object SdkConfigSection : AdHocSection::Config + */ +object SdkConfigCustomization { /** * Copy a field from SDK config to service config with an optional map block. * @@ -43,18 +46,17 @@ object SdkConfigSection : AdHocSection - { + fun copyField(fieldName: String, map: Writable?) = + adhocCustomization { section -> val mapBlock = map?.let { writable { rust(".map(#W)", it) } } ?: writable { } rustTemplate( "${section.serviceConfigBuilder}.set_$fieldName(${section.sdkConfig}.$fieldName()#{map});", "map" to mapBlock, ) } - } } /** @@ -64,22 +66,20 @@ class GenericSmithySdkConfigSettings : ClientCodegenDecorator { override val name: String = "GenericSmithySdkConfigSettings" override val order: Byte = 0 - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> = + override fun extraSections(codegenContext: ClientCodegenContext): List = listOf( - SdkConfigSection.create { section -> - writable { - rust( - """ - // resiliency - ${section.serviceConfigBuilder}.set_retry_config(${section.sdkConfig}.retry_config().cloned()); - ${section.serviceConfigBuilder}.set_timeout_config(${section.sdkConfig}.timeout_config().cloned()); - ${section.serviceConfigBuilder}.set_sleep_impl(${section.sdkConfig}.sleep_impl()); - - ${section.serviceConfigBuilder}.set_http_connector(${section.sdkConfig}.http_connector().cloned()); - - """, - ) - } + adhocCustomization { section -> + rust( + """ + // resiliency + ${section.serviceConfigBuilder}.set_retry_config(${section.sdkConfig}.retry_config().cloned()); + ${section.serviceConfigBuilder}.set_timeout_config(${section.sdkConfig}.timeout_config().cloned()); + ${section.serviceConfigBuilder}.set_sleep_impl(${section.sdkConfig}.sleep_impl()); + + ${section.serviceConfigBuilder}.set_http_connector(${section.sdkConfig}.http_connector().cloned()); + + """, + ) }, ) } @@ -111,8 +111,7 @@ class SdkConfigDecorator : ClientCodegenDecorator { impl From<&#{SdkConfig}> for Builder { fn from(input: &#{SdkConfig}) -> Self { let mut builder = Builder::default(); - #{augmentBuilder} - + #{augmentBuilder:W} builder } @@ -124,15 +123,15 @@ class SdkConfigDecorator : ClientCodegenDecorator { } } """, - "augmentBuilder" to codegenContext.rootDecorator.extraSections(codegenContext) - .filter { (t, _) -> t is SdkConfigSection }.map { (_, sectionWriter) -> - sectionWriter( - SdkConfigSection.CopySdkConfigToClientConfig( - sdkConfig = "input", - serviceConfigBuilder = "builder", - ), - ) - }.join("\n"), + "augmentBuilder" to writable { + writeCustomizations( + codegenContext.rootDecorator.extraSections(codegenContext), + SdkConfigSection.CopySdkConfigToClientConfig( + sdkConfig = "input", + serviceConfigBuilder = "builder", + ), + ) + }, *codegenScope, ) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt index 54dc4f5586cb5b0770f5eaf5f89615ca00e76f65..8a2a27f66fb600f6d9512ef2a41f76a4858ceaa1 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/UserAgentDecorator.kt @@ -16,10 +16,10 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig -import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization 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.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection import software.amazon.smithy.rust.codegen.core.util.dq @@ -56,10 +56,10 @@ class UserAgentDecorator : ClientCodegenDecorator { return baseCustomizations + UserAgentFeature(codegenContext.runtimeConfig) } - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> { + override fun extraSections(codegenContext: ClientCodegenContext): List { return listOf( - SdkConfigSection.create { section -> - writable { rust("${section.serviceConfigBuilder}.set_app_name(${section.sdkConfig}.app_name().cloned());") } + adhocCustomization { section -> + rust("${section.serviceConfigBuilder}.set_app_name(${section.sdkConfig}.app_name().cloned());") }, ) } diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/endpoints/AwsEndpointDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/endpoints/AwsEndpointDecorator.kt index 5973d5e3c5fdabfcf3fbc30af7108af1f71d5f3f..ab9ce5bf1f7bb96a97b0c1b9e4f77372bbf77c5e 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/endpoints/AwsEndpointDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/endpoints/AwsEndpointDecorator.kt @@ -29,8 +29,8 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RustCrate -import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocSection -import software.amazon.smithy.rust.codegen.core.smithy.customize.Section +import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization +import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection import software.amazon.smithy.rust.codegen.core.util.extendIf @@ -133,16 +133,14 @@ class AwsEndpointDecorator : ClientCodegenDecorator { } } - override fun extraSections(codegenContext: ClientCodegenContext): List, (Section) -> Writable>> { + override fun extraSections(codegenContext: ClientCodegenContext): List { return codegenContext.isRegionalized().thenSingletonListOf { - SdkConfigSection.create { section -> - { - rust( - """ - ${section.serviceConfigBuilder}.set_aws_endpoint_resolver(${section.sdkConfig}.endpoint_resolver().clone()); - """, - ) - } + adhocCustomization { section -> + rust( + """ + ${section.serviceConfigBuilder}.set_aws_endpoint_resolver(${section.sdkConfig}.endpoint_resolver().clone()); + """, + ) } } } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt index 1691dffe1b42935d305b0c3e396a4677c5ebf796..b6fced279fbb2b59d578ef6d57a48ce331db9b21 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientDecorator.kt @@ -17,7 +17,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable 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.RustCrate -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection @@ -72,7 +72,7 @@ sealed class FluentClientSection(name: String) : Section(name) { data class FluentClientDocs(val serviceShape: ServiceShape) : FluentClientSection("FluentClientDocs") } -abstract class FluentClientCustomization : NamedSectionGenerator() +abstract class FluentClientCustomization : NamedCustomization() class GenericFluentClient(codegenContext: CodegenContext) : FluentClientCustomization() { private val moduleUseName = codegenContext.moduleUseName() diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/IdempotencyTokenProviderCustomization.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/IdempotencyTokenProviderCustomization.kt index eb7057c32c64724c460b1b03fd45547b1974616c..7cda821957ec6b28f56273685d4030d0c3c4ab58 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/IdempotencyTokenProviderCustomization.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/IdempotencyTokenProviderCustomization.kt @@ -10,12 +10,12 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization /** * Add a `make_token` field to Service config. See below for the resulting generated code. */ -class IdempotencyTokenProviderCustomization : NamedSectionGenerator() { +class IdempotencyTokenProviderCustomization : NamedCustomization() { override fun section(section: ServiceConfig): Writable { return when (section) { is ServiceConfig.ConfigStruct -> writable { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGenerator.kt index 5966c401c33466c343a46e965bd2bb002dacfd49..00bc592ff485b965b848f188aadc53158fa0cf15 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGenerator.kt @@ -23,7 +23,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.makeOptional import software.amazon.smithy.rust.codegen.core.util.hasTrait @@ -39,7 +39,7 @@ import software.amazon.smithy.rust.codegen.core.util.hasTrait * * Usage: * ```kotlin - * class AddRegion : NamedSectionGenerator() { + * class AddRegion : NamedCustomization() { * override fun section(section: ServiceConfig): Writable { * return when (section) { * is ServiceConfig.ConfigStruct -> writeable { @@ -156,7 +156,7 @@ fun ServiceShape.needsIdempotencyToken(model: Model): Boolean { .any { it.hasTrait() } } -typealias ConfigCustomization = NamedSectionGenerator +typealias ConfigCustomization = NamedCustomization /** * Generate a `Config` struct, implementation & builder for a given service, approximately: diff --git a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGeneratorTest.kt b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGeneratorTest.kt index 0241133f231c34ff79c91be064545853d92b652f..366ff370dc076c88d4841d925107613275d3cf95 100644 --- a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGeneratorTest.kt +++ b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/config/ServiceConfigGeneratorTest.kt @@ -13,7 +13,7 @@ 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.writable -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest @@ -78,7 +78,7 @@ internal class ServiceConfigGeneratorTest { @Test fun `generate customizations as specified`() { - class ServiceCustomizer : NamedSectionGenerator() { + class ServiceCustomizer : NamedCustomization() { override fun section(section: ServiceConfig): Writable { return when (section) { ServiceConfig.ConfigStructAdditionalDocs -> emptySection diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt index 6a33d82c96054dea40fc787dc368360dd786d92f..c886c4ed97cb023469a7df23c8ed988f6ab135ed 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/CoreCodegenDecorator.kt @@ -8,7 +8,6 @@ package software.amazon.smithy.rust.codegen.core.smithy.customize import software.amazon.smithy.build.PluginContext import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape -import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization import software.amazon.smithy.rust.codegen.core.smithy.generators.ManifestCustomizations @@ -66,7 +65,7 @@ interface CoreCodegenDecorator { /** * Extra sections allow one decorator to influence another. This is intended to be used by querying the `rootDecorator` */ - fun extraSections(codegenContext: CodegenContext): List, (Section) -> Writable>> = listOf() + fun extraSections(codegenContext: CodegenContext): List = listOf() } /** @@ -99,7 +98,7 @@ abstract class CombinedCoreCodegenDecorator, (Section) -> Writable>> = + final override fun extraSections(codegenContext: CodegenContext): List = addCustomizations { decorator -> decorator.extraSections(codegenContext) } /** diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/NamedSectionGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/Customization.kt similarity index 61% rename from codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/NamedSectionGenerator.kt rename to codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/Customization.kt index ccc9702e09a2a034a6007d98b2fc90707c820849..d563349867db4b3ff0993d083059e6689a46b0eb 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/NamedSectionGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/Customization.kt @@ -23,31 +23,39 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable */ abstract class Section(val name: String) +/** Customization type returned by [adhocCustomization] */ +typealias AdHocCustomization = NamedCustomization + +/** Adhoc section for code generation. Similar to [Section], but for use with [adhocCustomization]. */ +abstract class AdHocSection(name: String) : Section(name) + /** - * Detatched section abstraction to allow adhoc sections to be created. By using the `.writer` method, a instantiation - * of this section can be easily created. + * Allows for one-off customizations such that a `when` statement switching on + * the section type is not necessary. */ -abstract class AdHocSection(val name: String) { - /** - * Helper to enable easily combining detached sections with the [CoreCodegenDecorator.extraSections] method. - */ - fun create(w: (T) -> Writable): Pair, (Section) -> Writable> = this to { s: Section -> - w((s as T)) +inline fun adhocCustomization( + crossinline customization: RustWriter.(T) -> Unit, +): AdHocCustomization = + object : AdHocCustomization() { + override fun section(section: AdHocSection): Writable = writable { + if (section is T) { + customization(section) + } + } } -} /** - * A named section generator allows customization via a predefined set of named sections. + * A named section generator that allows customization via a predefined set of named sections. * * Implementors MUST override section and use a `when` clause to handle each section individually */ -abstract class NamedSectionGenerator { +abstract class NamedCustomization { abstract fun section(section: T): Writable protected val emptySection = writable { } } /** Convenience for rendering a list of customizations for a given section */ -fun RustWriter.writeCustomizations(customizations: List>, section: T) { +fun RustWriter.writeCustomizations(customizations: List>, section: T) { for (customization in customizations) { customization.section(section)(this) } diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/OperationCustomization.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/OperationCustomization.kt index a7dd176a9e227a208f39b46cdb3756026700073c..d11f98a5c7a1325744a7da57e1159e6d548d1860 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/OperationCustomization.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customize/OperationCustomization.kt @@ -55,7 +55,7 @@ sealed class OperationSection(name: String) : Section(name) { ) : OperationSection("MutateOutput") } -abstract class OperationCustomization : NamedSectionGenerator() { +abstract class OperationCustomization : NamedCustomization() { open fun retryType(): RuntimeType? = null /** diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/Instantiator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/Instantiator.kt index 01c7a8d740cf610e9f21094a6b3c39c3c506bc34..a2683ab7139c410e51c4534c6b7e19e03ab3ba67 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/Instantiator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/Instantiator.kt @@ -46,7 +46,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.writable 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.RustSymbolProvider -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.isOptional import software.amazon.smithy.rust.codegen.core.smithy.rustType @@ -66,7 +66,7 @@ sealed class InstantiatorSection(name: String) : Section(name) { /** * Customization for the instantiator. */ -typealias InstantiatorCustomization = NamedSectionGenerator +typealias InstantiatorCustomization = NamedCustomization /** * Instantiator generates code to instantiate a given shape given a `Node` representing the value. diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/LibRsGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/LibRsGenerator.kt index c15d545e6b6cbcaa8f4dc13ecd47ad02d0f6688b..359386e34da2fc23c1c9a5eed3ae99e9ad2a8df9 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/LibRsGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/LibRsGenerator.kt @@ -13,7 +13,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.escape import software.amazon.smithy.rust.codegen.core.rustlang.isEmpty import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.smithy.CoreRustSettings -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.util.getTrait @@ -27,7 +27,7 @@ sealed class LibRsSection(name: String) : Section(name) { } } -typealias LibRsCustomization = NamedSectionGenerator +typealias LibRsCustomization = NamedCustomization class LibRsGenerator( private val settings: CoreRustSettings, diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/http/HttpBindingGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/http/HttpBindingGenerator.kt index 597ff267e26b1cae641ce2070f8cedbec5dc1330..b2ca5e066fed9cec516d0681d34f547fdb411105 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/http/HttpBindingGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/http/HttpBindingGenerator.kt @@ -41,7 +41,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.withBlock import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.generators.OperationBuildError import software.amazon.smithy.rust.codegen.core.smithy.generators.operationBuildError @@ -90,7 +90,7 @@ sealed class HttpBindingSection(name: String) : Section(name) { HttpBindingSection("AfterDeserializingIntoAHashMapOfHttpPrefixHeaders") } -typealias HttpBindingCustomization = NamedSectionGenerator +typealias HttpBindingCustomization = NamedCustomization /** * This class generates Rust functions that (de)serialize data from/to an HTTP message. diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt index 2f1e30a38f618ece431a6da6b7dac05179748bfd..34a47e597616fec2201a5f54b06bff84d527a468 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt @@ -40,7 +40,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.canUseDefault -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.generators.TypeConversionGenerator import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator @@ -70,7 +70,7 @@ sealed class JsonParserSection(name: String) : Section(name) { /** * Customization for the JSON parser. */ -typealias JsonParserCustomization = NamedSectionGenerator +typealias JsonParserCustomization = NamedCustomization data class ReturnSymbolToParse(val symbol: Symbol, val isUnconstrained: Boolean) diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt index 69deb870d70c44f34bebe94f5d86acdafe0d1c77..89dd945d30ba38345458dd6d9ec3d2915b9f9c1a 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt @@ -36,7 +36,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.withBlock 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.RustSymbolProvider -import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.core.smithy.customize.NamedCustomization import software.amazon.smithy.rust.codegen.core.smithy.customize.Section import software.amazon.smithy.rust.codegen.core.smithy.generators.TypeConversionGenerator import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator @@ -81,7 +81,7 @@ sealed class JsonSerializerSection(name: String) : Section(name) { /** * Customization for the JSON serializer. */ -typealias JsonSerializerCustomization = NamedSectionGenerator +typealias JsonSerializerCustomization = NamedCustomization class JsonSerializerGenerator( codegenContext: CodegenContext,