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 7f9c07c288..1087a63feb 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 @@ -18,6 +18,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customizations.Sensitiv import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations +import software.amazon.smithy.rust.codegen.client.smithy.customize.SerdeDecorator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointParamsDecorator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointsDecorator import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator @@ -59,6 +60,7 @@ class RustClientCodegenPlugin : ClientDecoratableBuildPlugin() { val codegenDecorator = CombinedClientCodegenDecorator.fromClasspath( context, + SerdeDecorator(), ClientCustomizations(), RequiredCustomizations(), FluentClientDecorator(), diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/SerdeDecorator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/SerdeDecorator.kt new file mode 100644 index 0000000000..1a14fb7049 --- /dev/null +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/customize/SerdeDecorator.kt @@ -0,0 +1,81 @@ +/* +* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +* SPDX-License-Identifier: Apache-2.0 +*/ + +package software.amazon.smithy.rust.codegen.client.smithy.customize + +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext +import software.amazon.smithy.rust.codegen.core.rustlang.Feature +import software.amazon.smithy.rust.codegen.core.rustlang.Writable +import software.amazon.smithy.rust.codegen.core.rustlang.containerDocs +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.LibRsSection +import software.amazon.smithy.rust.codegen.core.smithy.generators.ModuleDocSection + +/** + * Decorator that adds the `serde-serialize` and `serde-deserialize` features. + */ +class SerdeDecorator : ClientCodegenDecorator { + override val name: String = "SerdeDecorator" + override val order: Byte = 5 + + override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { + fun feature(featureName: String): Feature { + return Feature(featureName, false, listOf("aws-smithy-types/$featureName")) + } + rustCrate.mergeFeature(feature("serde-serialize")) + rustCrate.mergeFeature(feature("serde-deserialize")) + } + + override fun libRsCustomizations( + codegenContext: ClientCodegenContext, + baseCustomizations: List, + ): List { + return baseCustomizations + SerdeDocGenerator(codegenContext) + } +} + +class SerdeDocGenerator(private val codegenContext: ClientCodegenContext) : LibRsCustomization() { + override fun section(section: LibRsSection): Writable { + return when (section) { + is LibRsSection.ModuleDoc -> + if (section.subsection is ModuleDocSection.AWSSdkUnstable) { + serdeInfoText() + } else { + emptySection + } + + else -> emptySection + } + } + + private fun serdeInfoText(): Writable { + return writable { + containerDocs( + """ + ## How to enable `Serialize` and `Deserialize` + + This data type implements `Serialize` and `Deserialize` traits from the popular serde crate, but those traits are behind feature gate. + + As they increase it's compile time dramatically, you should not turn them on unless it's necessary. + Implementation of `serde` traits in AWS SDK for Rust is still unstable, and implementation may change anytime in future. + + To enable traits, you must pass `aws_sdk_unstable` to RUSTFLAGS and enable `serde-serialize` or `serde-deserialize` feature. + + e.g. + ```bash,no_run + export RUSTFLAGS="--cfg aws_sdk_unstable" + cargo build --features serde-serialize serde-deserialize + ``` + + If you enable `serde-serialize` and/or `serde-deserialize` without `RUSTFLAGS="--cfg aws_sdk_unstable"`, + compilation will fail with warning. + + """.trimIndent(), + ) + } + } +} 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 24beaa51d8..b5e0be5ab6 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 @@ -25,6 +25,7 @@ sealed class ModuleDocSection { object CrateOrganization : ModuleDocSection() object Examples : ModuleDocSection() + object AWSSdkUnstable : ModuleDocSection() } sealed class LibRsSection(name: String) : Section(name) { @@ -78,6 +79,15 @@ class LibRsGenerator( } } + docSection(ModuleDocSection.AWSSdkUnstable).also { docs -> + if (docs.isNotEmpty()) { + containerDocs("\n## Enabling Unstable Features") + docs.forEach { writeTo -> + writeTo(this) + } + } + } + // Examples docSection(ModuleDocSection.Examples).also { docs -> if (docs.isNotEmpty() || settings.examplesUri != null) {