From cb50262c9395ac378ddebbbe0e64f13b387570d7 Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Thu, 11 Feb 2021 10:48:53 -0500 Subject: [PATCH] Add CredentialsProvider to config (and related refactorings) (#182) * Add credentials provider config & test * Prepare to use `Features` during Operation Generation * Fix sdk gradle build changes that were previously missed * Utilize the credentials provider in the feature * Update IdempotencyTokenProvider to be Send + Sync * Add accessors for credential providers * Fix build.gradle * Update auth references to be aws-auth * Update idempotency token test --- aws/rust-runtime/aws-auth/Cargo.toml | 1 + aws/rust-runtime/aws-auth/src/lib.rs | 23 +++++ aws/rust-runtime/aws-auth/src/provider.rs | 2 +- aws/sdk-codegen/build.gradle.kts | 1 + .../smithy/rustsdk/AwsCodegenDecorator.kt | 2 +- .../smithy/rustsdk/CredentialProviders.kt | 99 +++++++++++++++++++ ...y.rust.codegen.smithy.RustCodegenDecorator | 1 - ...egen.smithy.customize.RustCodegenDecorator | 2 + .../rustsdk/CredentialProviderConfigTest.kt | 17 ++++ aws/sdk/build.gradle.kts | 16 ++- .../rust/codegen/rustlang/CargoDependency.kt | 4 +- .../rust/codegen/smithy/CodegenVisitor.kt | 11 +-- .../rust/codegen/smithy/RustCodegenPlugin.kt | 1 + .../NamedSectionGenerator.kt | 2 +- .../{ => customize}/RustCodegenDecorator.kt | 33 ++++++- .../smithy/generators/BuilderGenerator.kt | 4 +- .../generators/CombinedErrorGenerator.kt | 2 +- .../generators/HttpProtocolGenerator.kt | 6 +- .../generators/OperationCustomization.kt | 24 +++++ .../smithy/generators/ServiceGenerator.kt | 16 ++- ... IdempotencyTokenProviderCustomization.kt} | 5 +- .../config/ServiceConfigGenerator.kt | 4 +- .../smithy/rust/codegen}/testutil/Rust.kt | 4 +- .../testutil/TestConfigCustomization.kt | 60 +++++++++++ .../rust/codegen}/testutil/TestHelpers.kt | 2 +- .../smithy/rust/codegen/SymbolBuilderTest.kt | 4 +- .../codegen/generators/EnumGeneratorTest.kt | 8 +- .../HttpTraitBindingGeneratorTest.kt | 10 +- .../generators/StructureGeneratorTest.kt | 10 +- .../codegen/generators/UnionGeneratorTest.kt | 6 +- .../codegen/rustlang/InlineDependencyTest.kt | 4 +- .../generators/CombinedErrorGeneratorTest.kt | 8 +- .../HttpProtocolTestGeneratorTest.kt | 6 +- .../smithy/generators/InstantiatorTest.kt | 10 +- .../generators/ModelBuilderGeneratorTest.kt | 4 +- ...empotencyTokenProviderCustomizationTest.kt | 16 +++ .../config/ServiceConfigGeneratorTest.kt | 11 ++- .../CustomSerializerGeneratorTest.kt | 8 +- .../transformers/OperationNormalizerTest.kt | 4 +- .../transformers/RecursiveShapeBoxerTest.kt | 2 +- .../RecursiveShapesIntegrationTest.kt | 6 +- .../amazon/smithy/rust/lang/RustWriterTest.kt | 12 +-- .../smithy/rust/lang/UseDeclarationsTest.kt | 2 +- rust-runtime/inlineable/Cargo.toml | 2 +- .../inlineable/src/idempotency_token.rs | 11 +-- rust-runtime/inlineable/src/lib.rs | 16 +++ 46 files changed, 401 insertions(+), 101 deletions(-) create mode 100644 aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt delete mode 100644 aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.RustCodegenDecorator create mode 100644 aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator create mode 100644 aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/CredentialProviderConfigTest.kt rename codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/{generators/config => customize}/NamedSectionGenerator.kt (96%) rename codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/{ => customize}/RustCodegenDecorator.kt (71%) create mode 100644 codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/OperationCustomization.kt rename codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/{IdempotencyProviderConfig.kt => IdempotencyTokenProviderCustomization.kt} (92%) rename codegen/src/{test/kotlin/software/amazon/smithy/rust => main/kotlin/software/amazon/smithy/rust/codegen}/testutil/Rust.kt (98%) create mode 100644 codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestConfigCustomization.kt rename codegen/src/{test/kotlin/software/amazon/smithy/rust => main/kotlin/software/amazon/smithy/rust/codegen}/testutil/TestHelpers.kt (97%) create mode 100644 codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomizationTest.kt diff --git a/aws/rust-runtime/aws-auth/Cargo.toml b/aws/rust-runtime/aws-auth/Cargo.toml index 878c8e4c8..aee28a671 100644 --- a/aws/rust-runtime/aws-auth/Cargo.toml +++ b/aws/rust-runtime/aws-auth/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +smithy-http = { path = "../../../rust-runtime/smithy-http" } diff --git a/aws/rust-runtime/aws-auth/src/lib.rs b/aws/rust-runtime/aws-auth/src/lib.rs index 8f752580c..b14680bc6 100644 --- a/aws/rust-runtime/aws-auth/src/lib.rs +++ b/aws/rust-runtime/aws-auth/src/lib.rs @@ -1,8 +1,10 @@ pub mod provider; +use smithy_http::property_bag::PropertyBag; use std::error::Error; use std::fmt; use std::fmt::{Debug, Display, Formatter}; +use std::sync::Arc; use std::time::SystemTime; /// AWS SDK Credentials @@ -53,6 +55,18 @@ impl Credentials { provider_name: STATIC_CREDENTIALS, } } + + pub fn access_key_id(&self) -> &str { + &self.access_key_id + } + + pub fn secret_access_key(&self) -> &str { + &self.secret_access_key + } + + pub fn session_token(&self) -> Option<&str> { + self.session_token.as_deref() + } } #[derive(Debug)] @@ -90,8 +104,17 @@ pub trait ProvideCredentials: Send + Sync { fn credentials(&self) -> Result; } +pub fn default_provider() -> impl ProvideCredentials { + // TODO: this should be a chain based on the CRT + provider::EnvironmentVariableCredentialsProvider::new() +} + impl ProvideCredentials for Credentials { fn credentials(&self) -> Result { Ok(self.clone()) } } + +pub fn set_provider(config: &mut PropertyBag, provider: Arc) { + config.insert(provider); +} diff --git a/aws/rust-runtime/aws-auth/src/provider.rs b/aws/rust-runtime/aws-auth/src/provider.rs index 1cb088acb..22ae7d416 100644 --- a/aws/rust-runtime/aws-auth/src/provider.rs +++ b/aws/rust-runtime/aws-auth/src/provider.rs @@ -18,7 +18,7 @@ impl EnvironmentVariableCredentialsProvider { } /// Create a EnvironmentVariable provider from a HashMap for testing - fn for_map(env: HashMap) -> Self { + pub fn for_map(env: HashMap) -> Self { EnvironmentVariableCredentialsProvider { env: Box::new(move |key: &str| { env.get(key) diff --git a/aws/sdk-codegen/build.gradle.kts b/aws/sdk-codegen/build.gradle.kts index 7c7673f1a..ccbdced8c 100644 --- a/aws/sdk-codegen/build.gradle.kts +++ b/aws/sdk-codegen/build.gradle.kts @@ -23,6 +23,7 @@ dependencies { implementation("software.amazon.smithy:smithy-aws-protocol-tests:$smithyVersion") implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion") implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion") + testImplementation("org.junit.jupiter:junit-jupiter:5.6.1") } tasks.compileKotlin { diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt index 9a1628e78..e7147f391 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt @@ -5,7 +5,7 @@ package software.amazon.smithy.rustsdk -import software.amazon.smithy.rust.codegen.smithy.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization 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 new file mode 100644 index 000000000..2bf713649 --- /dev/null +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt @@ -0,0 +1,99 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +package software.amazon.smithy.rustsdk + +import software.amazon.smithy.model.shapes.OperationShape +import software.amazon.smithy.rust.codegen.rustlang.CargoDependency +import software.amazon.smithy.rust.codegen.rustlang.Local +import software.amazon.smithy.rust.codegen.rustlang.Writable +import software.amazon.smithy.rust.codegen.rustlang.docs +import software.amazon.smithy.rust.codegen.rustlang.rust +import software.amazon.smithy.rust.codegen.rustlang.writable +import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig +import software.amazon.smithy.rust.codegen.smithy.RuntimeType +import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.smithy.generators.OperationCustomization +import software.amazon.smithy.rust.codegen.smithy.generators.OperationSection +import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig +import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization +import software.amazon.smithy.rust.codegen.smithy.generators.config.ServiceConfig + +class CredentialsProviderDecorator : RustCodegenDecorator { + override val name: String = "CredentialsProvider" + override val order: Byte = 0 + + override fun configCustomizations( + protocolConfig: ProtocolConfig, + baseCustomizations: List + ): List { + return baseCustomizations + CredentialProviderConfig(protocolConfig.runtimeConfig) + } + + override fun operationCustomizations( + protocolConfig: ProtocolConfig, + operation: OperationShape, + baseCustomizations: List + ): List { + return baseCustomizations + CredentialsProviderFeature(protocolConfig.runtimeConfig) + } +} + +/** + * Add a `.credentials_provider` field and builder to the `Config` for a given service + */ +class CredentialProviderConfig(runtimeConfig: RuntimeConfig) : ConfigCustomization() { + private val credentialsProvider = credentialsProvider(runtimeConfig) + private val defaultProvider = defaultProvider(runtimeConfig) + override fun section(section: ServiceConfig) = writable { + when (section) { + is ServiceConfig.ConfigStruct -> rust( + """pub(crate) credentials_provider: std::sync::Arc,""", + credentialsProvider + ) + is ServiceConfig.ConfigImpl -> emptySection + is ServiceConfig.BuilderStruct -> + rust("credentials_provider: Option>,", credentialsProvider) + ServiceConfig.BuilderImpl -> { + docs("""Set the credentials provider for this service""") + rust( + """ + pub fn credentials_provider(mut self, credentials_provider: impl #T + 'static) -> Self { + self.credentials_provider = Some(std::sync::Arc::new(credentials_provider)); + self + } + """, + credentialsProvider + + ) + } + ServiceConfig.BuilderBuild -> rust( + "credentials_provider: self.credentials_provider.unwrap_or_else(|| std::sync::Arc::new(#T())),", + defaultProvider + ) + } + } +} + +class CredentialsProviderFeature(private val runtimeConfig: RuntimeConfig) : OperationCustomization() { + override fun section(section: OperationSection): Writable { + return when (section) { + is OperationSection.Feature -> writable { + rust( + """ + #T(&mut ${section.request}.config_mut(), ${section.config}.credentials_provider.clone()); + """, + setProvider(runtimeConfig) + ) + } + else -> emptySection + } + } +} + +fun awsAuth(runtimeConfig: RuntimeConfig) = CargoDependency("aws-auth", Local(runtimeConfig.relativePath)) +fun credentialsProvider(runtimeConfig: RuntimeConfig) = RuntimeType("ProvideCredentials", awsAuth(runtimeConfig), "aws_auth") +fun defaultProvider(runtimeConfig: RuntimeConfig) = RuntimeType("default_provider", awsAuth(runtimeConfig), "aws_auth") +fun setProvider(runtimeConfig: RuntimeConfig) = RuntimeType("set_provider", awsAuth(runtimeConfig), "aws_auth") diff --git a/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.RustCodegenDecorator b/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.RustCodegenDecorator deleted file mode 100644 index 1ceb403ca..000000000 --- a/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.RustCodegenDecorator +++ /dev/null @@ -1 +0,0 @@ -software.amazon.smithy.rustsdk.AwsCodegenDecorator diff --git a/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator b/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator new file mode 100644 index 000000000..5ac05de97 --- /dev/null +++ b/aws/sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator @@ -0,0 +1,2 @@ +software.amazon.smithy.rustsdk.AwsCodegenDecorator +software.amazon.smithy.rustsdk.CredentialsProviderDecorator diff --git a/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/CredentialProviderConfigTest.kt b/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/CredentialProviderConfigTest.kt new file mode 100644 index 000000000..4a34e6f94 --- /dev/null +++ b/aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/CredentialProviderConfigTest.kt @@ -0,0 +1,17 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +package software.amazon.smithy.rustsdk + +import org.junit.jupiter.api.Test +import software.amazon.smithy.rust.codegen.testutil.TestRuntimeConfig +import software.amazon.smithy.rust.codegen.testutil.validateConfigCustomizations + +internal class CredentialProviderConfigTest { + @Test + fun `generates a valid config`() { + validateConfigCustomizations(CredentialProviderConfig(TestRuntimeConfig)) + } +} diff --git a/aws/sdk/build.gradle.kts b/aws/sdk/build.gradle.kts index bf359a7a4..693a1a6e3 100644 --- a/aws/sdk/build.gradle.kts +++ b/aws/sdk/build.gradle.kts @@ -22,7 +22,7 @@ val sdkOutputDir = buildDir.resolve("aws-sdk") val awsServices = discoverServices() // TODO: smithy-http should be removed val runtimeModules = listOf("smithy-types", "smithy-http") -val awsModules = listOf("auth") +val awsModules = listOf("aws-auth", "aws-endpoint", "aws-types") buildscript { val smithyVersion: String by project @@ -111,11 +111,19 @@ tasks.register("relocateAwsRuntime") { } exclude("**/target") exclude("**/Cargo.lock") - // filter { line -> line.replace("../../rust-runtime/", "") } + filter { line -> rewritePathDependency(line) } into(sdkOutputDir) outputs.upToDateWhen { false } } +/** + * The aws/rust-runtime crates depend on local versions of the Smithy core runtime enabling local compilation. However, + * those paths need to be replaced in the final build. We should probably fix this with some symlinking. + */ +fun rewritePathDependency(line: String): String { + return line.replace("../../rust-runtime/", "") +} + tasks.register("relocateRuntime") { from("$rootDir/rust-runtime") { runtimeModules.forEach { @@ -128,7 +136,7 @@ tasks.register("relocateRuntime") { } fun generateCargoWorkspace(services: List): String { - val modules = services.map(AwsService::module) + runtimeModules + val modules = services.map(AwsService::module) + runtimeModules + awsModules return """ [workspace] members = [ @@ -144,7 +152,7 @@ task("generateCargoWorkspace") { } task("finalizeSdk") { - finalizedBy("relocateServices", "relocateRuntime", "generateCargoWorkspace") + finalizedBy("relocateServices", "relocateRuntime", "relocateAwsRuntime", "generateCargoWorkspace") } tasks["smithyBuildJar"].dependsOn("generateSmithyBuild") diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/CargoDependency.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/CargoDependency.kt index 531248245..a0de96b48 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/CargoDependency.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/CargoDependency.kt @@ -91,7 +91,7 @@ class InlineDependency( fun instant8601() = forRustFile("instant_iso8601", CargoDependency.Serde) fun idempotencyToken() = - forRustFile("idempotency_token", CargoDependency.Rand) + forRustFile("idempotency_token", CargoDependency.FastRand) fun blobSerde(runtimeConfig: RuntimeConfig) = forRustFile( "blob_serde", @@ -157,7 +157,7 @@ data class CargoDependency( } companion object { - val Rand: CargoDependency = CargoDependency("rand", CratesIo("0.7")) + val FastRand = CargoDependency("fastrand", CratesIo("1")) val Http: CargoDependency = CargoDependency("http", CratesIo("0.2")) fun SmithyTypes(runtimeConfig: RuntimeConfig) = CargoDependency("${runtimeConfig.cratePrefix}-types", Local(runtimeConfig.relativePath)) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt index 46e411254..e61a889d0 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/CodegenVisitor.kt @@ -17,6 +17,7 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape import software.amazon.smithy.model.traits.EnumTrait import software.amazon.smithy.rust.codegen.rustlang.RustWriter +import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.EnumGenerator import software.amazon.smithy.rust.codegen.smithy.generators.HttpProtocolGenerator import software.amazon.smithy.rust.codegen.smithy.generators.ModelBuilderGenerator @@ -25,7 +26,6 @@ import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolGeneratorFa import software.amazon.smithy.rust.codegen.smithy.generators.ServiceGenerator import software.amazon.smithy.rust.codegen.smithy.generators.StructureGenerator import software.amazon.smithy.rust.codegen.smithy.generators.UnionGenerator -import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.smithy.generators.implBlock import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolLoader import software.amazon.smithy.rust.codegen.smithy.traits.SyntheticInputTrait @@ -34,7 +34,7 @@ import software.amazon.smithy.rust.codegen.util.CommandFailed import software.amazon.smithy.rust.codegen.util.runCommand import java.util.logging.Logger -class CodegenVisitor(context: PluginContext, codegenDecorator: RustCodegenDecorator) : ShapeVisitor.Default() { +class CodegenVisitor(context: PluginContext, private val codegenDecorator: RustCodegenDecorator) : ShapeVisitor.Default() { private val logger = Logger.getLogger(javaClass.name) private val settings = RustSettings.from(context.model, context.settings) @@ -46,7 +46,6 @@ class CodegenVisitor(context: PluginContext, codegenDecorator: RustCodegenDecora private val protocolConfig: ProtocolConfig private val protocolGenerator: ProtocolGeneratorFactory private val httpGenerator: HttpProtocolGenerator - private val configCustomizations: List init { val symbolVisitorConfig = @@ -66,14 +65,10 @@ class CodegenVisitor(context: PluginContext, codegenDecorator: RustCodegenDecora RustWriter.Factory ) httpGenerator = protocolGenerator.buildProtocolGenerator(protocolConfig) - configCustomizations = codegenDecorator.configCustomizations(protocolConfig, listOf()) } private fun baselineTransform(model: Model) = RecursiveShapeBoxer.transform(model) - private fun CodegenWriterDelegator.includedModules(): List = - this.writers.values.mapNotNull { it.module() } - fun execute() { logger.info("generating Rust client...") val service = settings.getService(model) @@ -120,6 +115,6 @@ class CodegenVisitor(context: PluginContext, codegenDecorator: RustCodegenDecora } override fun serviceShape(shape: ServiceShape) { - ServiceGenerator(writers, httpGenerator, protocolGenerator.support(), protocolConfig, configCustomizations).render() + ServiceGenerator(writers, httpGenerator, protocolGenerator.support(), protocolConfig, codegenDecorator).render() } } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt index 4a5bfcd7f..03a1bc7cc 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenPlugin.kt @@ -9,6 +9,7 @@ import software.amazon.smithy.build.PluginContext import software.amazon.smithy.build.SmithyBuildPlugin import software.amazon.smithy.model.Model import software.amazon.smithy.rust.codegen.rustlang.RustReservedWordSymbolProvider +import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecorator class RustCodegenPlugin : SmithyBuildPlugin { override fun getName(): String = "rust-codegen" diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/NamedSectionGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NamedSectionGenerator.kt similarity index 96% rename from codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/NamedSectionGenerator.kt rename to codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NamedSectionGenerator.kt index 4e8653a30..bea7c5689 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/NamedSectionGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/NamedSectionGenerator.kt @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0. */ -package software.amazon.smithy.rust.codegen.smithy.generators.config +package software.amazon.smithy.rust.codegen.smithy.customize import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.writable diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenDecorator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt similarity index 71% rename from codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenDecorator.kt rename to codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt index be679d003..e461d1c47 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RustCodegenDecorator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/customize/RustCodegenDecorator.kt @@ -3,10 +3,13 @@ * SPDX-License-Identifier: Apache-2.0. */ -package software.amazon.smithy.rust.codegen.smithy +package software.amazon.smithy.rust.codegen.smithy.customize import software.amazon.smithy.build.PluginContext +import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider +import software.amazon.smithy.rust.codegen.smithy.generators.OperationCustomization import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolMap @@ -24,24 +27,36 @@ interface RustCodegenDecorator { /** * The name of this [RustCodegenDecorator], used for logging and debug information */ - abstract val name: String + val name: String /** * Enable a deterministic ordering to be applied, with the lowest numbered integrations being applied first */ - abstract val order: Byte + val order: Byte + fun configCustomizations( protocolConfig: ProtocolConfig, baseCustomizations: List ): List = baseCustomizations + fun operationCustomizations( + protocolConfig: ProtocolConfig, + operation: OperationShape, + baseCustomizations: List + ): List = baseCustomizations + fun protocols(serviceId: ShapeId, currentProtocols: ProtocolMap): ProtocolMap = currentProtocols fun symbolProvider(baseProvider: RustSymbolProvider): RustSymbolProvider = baseProvider } +/** + * [CombinedCodegenDecorator] merges the results of multiple decorators into a single decorator. + * + * This makes the actual concrete codegen simpler by not needing to deal with multiple separate decorators. + */ class CombinedCodegenDecorator(decorators: List) : RustCodegenDecorator { - val orderedDecorators = decorators.sortedBy { it.order } + private val orderedDecorators = decorators.sortedBy { it.order } override val name: String get() = "MetaDecorator" override val order: Byte @@ -56,6 +71,16 @@ class CombinedCodegenDecorator(decorators: List) : RustCod } } + override fun operationCustomizations( + protocolConfig: ProtocolConfig, + operation: OperationShape, + baseCustomizations: List + ): List { + return orderedDecorators.foldRight(baseCustomizations) { decorator: RustCodegenDecorator, customizations -> + decorator.operationCustomizations(protocolConfig, operation, customizations) + } + } + override fun protocols(serviceId: ShapeId, currentProtocols: ProtocolMap): ProtocolMap { return orderedDecorators.foldRight(currentProtocols) { decorator, protocolMap -> decorator.protocols(serviceId, protocolMap) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/BuilderGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/BuilderGenerator.kt index 8ca39aafa..28dc24131 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/BuilderGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/BuilderGenerator.kt @@ -62,7 +62,8 @@ class ModelBuilderGenerator( class OperationInputBuilderGenerator( model: Model, private val symbolProvider: RustSymbolProvider, - private val shape: OperationShape + private val shape: OperationShape, + private val features: List, ) : BuilderGenerator(model, symbolProvider, shape.inputShape(model)) { override fun buildFn(implBlockWriter: RustWriter) { val fallibleBuilder = StructureGenerator.fallibleBuilder(shape.inputShape(model), symbolProvider) @@ -86,6 +87,7 @@ class OperationInputBuilderGenerator( """, operationModule, sdkBody ) + features.forEach { it.section(OperationSection.Feature("request", "_config"))(this) } rust( """ #T::new( diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt index 988bfd35d..caf467784 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGenerator.kt @@ -20,7 +20,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider -import software.amazon.smithy.rust.codegen.smithy.generators.config.Section +import software.amazon.smithy.rust.codegen.smithy.customize.Section /** * For a given Operation ([this]), return the symbol referring to the unified error? This can be used diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolGenerator.kt index d9aaf5d0f..7bdc7778a 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolGenerator.kt @@ -47,10 +47,10 @@ abstract class HttpProtocolGenerator( ) { private val symbolProvider = protocolConfig.symbolProvider private val model = protocolConfig.model - fun renderOperation(operationWriter: RustWriter, inputWriter: RustWriter, operationShape: OperationShape) { + fun renderOperation(operationWriter: RustWriter, inputWriter: RustWriter, operationShape: OperationShape, customizations: List) { val inputShape = operationShape.inputShape(model) val inputSymbol = symbolProvider.toSymbol(inputShape) - val builderGenerator = OperationInputBuilderGenerator(model, symbolProvider, operationShape) + val builderGenerator = OperationInputBuilderGenerator(model, symbolProvider, operationShape, customizations) builderGenerator.render(inputWriter) // impl OperationInputShape { ... } inputWriter.implBlock(inputShape, symbolProvider) { @@ -100,6 +100,8 @@ abstract class HttpProtocolGenerator( rustBlock("pub fn new(input: #T) -> Self", inputSymbol) { write("Self { input }") } + + customizations.forEach { customization -> customization.section(OperationSection.ImplBlock)(this) } } traitImplementations(operationWriter, operationShape) } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/OperationCustomization.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/OperationCustomization.kt new file mode 100644 index 000000000..cc951e16c --- /dev/null +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/OperationCustomization.kt @@ -0,0 +1,24 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +package software.amazon.smithy.rust.codegen.smithy.generators + +import software.amazon.smithy.rust.codegen.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.smithy.customize.Section + +sealed class OperationSection(name: String) : Section(name) { + /** Write custom code into the `impl` block of this operation */ + object ImplBlock : OperationSection("ImplBlock") + + /** Write custom code into the block that builds an operation + * + * [request]: Name of the variable holding the `smithy_http::Request` + * [config]: Name of the variable holding the service config. + * + * */ + data class Feature(val request: String, val config: String) : OperationSection("Feature") +} + +typealias OperationCustomization = NamedSectionGenerator diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ServiceGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ServiceGenerator.kt index 34c2452ae..5c26275f2 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ServiceGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ServiceGenerator.kt @@ -10,7 +10,7 @@ import software.amazon.smithy.model.knowledge.TopDownIndex import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.rust.codegen.rustlang.RustWriter -import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization +import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.generators.config.ServiceConfigGenerator import software.amazon.smithy.rust.codegen.smithy.traits.SyntheticInputTrait import software.amazon.smithy.rust.codegen.smithy.traits.SyntheticOutputTrait @@ -21,7 +21,7 @@ class ServiceGenerator( private val protocolGenerator: HttpProtocolGenerator, private val protocolSupport: ProtocolSupport, private val config: ProtocolConfig, - private val extraCustomizations: List, + private val decorator: RustCodegenDecorator, ) { private val index = TopDownIndex.of(config.model) @@ -30,7 +30,12 @@ class ServiceGenerator( operations.map { operation -> writers.useShapeWriter(operation) { operationWriter -> writers.useShapeWriter(operation.inputShape(config.model)) { inputWriter -> - protocolGenerator.renderOperation(operationWriter, inputWriter, operation) + protocolGenerator.renderOperation( + operationWriter, + inputWriter, + operation, + decorator.operationCustomizations(config, operation, listOf()) + ) HttpProtocolTestGenerator(config, protocolSupport, operation, operationWriter).render() } } @@ -42,7 +47,10 @@ class ServiceGenerator( renderBodies(operations) writers.useFileWriter("src/config.rs", "crate::config") { writer -> - ServiceConfigGenerator.withBaseBehavior(config, extraCustomizations = extraCustomizations).render(writer) + ServiceConfigGenerator.withBaseBehavior( + config, + extraCustomizations = decorator.configCustomizations(config, listOf()) + ).render(writer) } writers.useFileWriter("src/lib.rs", "crate::lib") { it.write("pub use config::Config;") diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyProviderConfig.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomization.kt similarity index 92% rename from codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyProviderConfig.kt rename to codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomization.kt index 74a658bff..c3d7d198d 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyProviderConfig.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomization.kt @@ -9,11 +9,12 @@ import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.writable import software.amazon.smithy.rust.codegen.smithy.RuntimeType +import software.amazon.smithy.rust.codegen.smithy.customize.NamedSectionGenerator /** * Add a `token_provider` field to Service config. See below for the resulting generated code. */ -class IdempotencyProviderConfig : NamedSectionGenerator() { +class IdempotencyTokenProviderCustomization : NamedSectionGenerator() { override fun section(section: ServiceConfig): Writable { return when (section) { is ServiceConfig.ConfigStruct -> writable { @@ -21,7 +22,7 @@ class IdempotencyProviderConfig : NamedSectionGenerator() { } ServiceConfig.ConfigImpl -> emptySection ServiceConfig.BuilderStruct -> writable { - rust("token_provider: Option>", RuntimeType.IdempotencyToken) + rust("token_provider: Option>,", RuntimeType.IdempotencyToken) } ServiceConfig.BuilderImpl -> writable { rust( diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGenerator.kt index b47cbe46a..91699a1ae 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGenerator.kt @@ -13,6 +13,8 @@ import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.rustlang.raw import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.rustlang.rustTemplate +import software.amazon.smithy.rust.codegen.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.smithy.customize.Section import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig /** @@ -94,7 +96,7 @@ class ServiceConfigGenerator(private val customizations: List): ServiceConfigGenerator { val baseFeatures = mutableListOf() if (protocolConfig.serviceShape.needsIdempotencyToken(protocolConfig.model)) { - baseFeatures.add(IdempotencyProviderConfig()) + baseFeatures.add(IdempotencyTokenProviderCustomization()) } return ServiceConfigGenerator(baseFeatures + extraCustomizations) } diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/testutil/Rust.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/Rust.kt similarity index 98% rename from codegen/src/test/kotlin/software/amazon/smithy/rust/testutil/Rust.kt rename to codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/Rust.kt index ed48be734..c9dad472c 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/testutil/Rust.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/Rust.kt @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0. */ -package software.amazon.smithy.rust.testutil +package software.amazon.smithy.rust.codegen.testutil import com.moandjiezana.toml.TomlWriter import org.intellij.lang.annotations.Language @@ -86,7 +86,7 @@ object TestWorkspace { } } - fun testProject(symbolProvider: RustSymbolProvider?): TestWriterDelegator { + fun testProject(symbolProvider: RustSymbolProvider? = null): TestWriterDelegator { val subprojectDir = subproject() val symbolProvider = symbolProvider ?: object : RustSymbolProvider { override fun config(): SymbolVisitorConfig { diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestConfigCustomization.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestConfigCustomization.kt new file mode 100644 index 000000000..bc0c129ea --- /dev/null +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestConfigCustomization.kt @@ -0,0 +1,60 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +package software.amazon.smithy.rust.codegen.testutil + +import software.amazon.smithy.rust.codegen.rustlang.Writable +import software.amazon.smithy.rust.codegen.rustlang.rust +import software.amazon.smithy.rust.codegen.rustlang.writable +import software.amazon.smithy.rust.codegen.smithy.generators.config.ConfigCustomization +import software.amazon.smithy.rust.codegen.smithy.generators.config.ServiceConfig +import software.amazon.smithy.rust.codegen.smithy.generators.config.ServiceConfigGenerator + +fun stubCustomization(name: String): ConfigCustomization { + return object : ConfigCustomization() { + override fun section(section: ServiceConfig): Writable = writable { + when (section) { + ServiceConfig.ConfigStruct -> rust("$name: u64,") + ServiceConfig.ConfigImpl -> emptySection + ServiceConfig.BuilderStruct -> rust("$name: Option,") + ServiceConfig.BuilderImpl -> rust( + """ + pub fn $name(mut self, $name: u64) -> Self { + self.$name = Some($name); + self + } + """ + ) + ServiceConfig.BuilderBuild -> rust( + """ + $name: self.$name.unwrap_or(123), + """ + ) + } + } + } +} + +/** Basic validation of [ConfigCustomization]s + * + * This test is not comprehensive, but it ensures that your customization generates Rust code that compiles and correctly + * composes with other customizations. + * */ +fun validateConfigCustomizations(vararg customization: ConfigCustomization) { + val customizations = listOf(stubCustomization("a")) + customization.toList() + stubCustomization("b") + val generator = ServiceConfigGenerator(customizations = customizations.toList()) + val project = TestWorkspace.testProject() + project.useFileWriter("src/config.rs", "crate::config") { + generator.render(it) + it.unitTest( + """ + fn assert_send_sync() {} + assert_send_sync::(); + + """ + ) + } + project.compileAndTest() +} diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/testutil/TestHelpers.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt similarity index 97% rename from codegen/src/test/kotlin/software/amazon/smithy/rust/testutil/TestHelpers.kt rename to codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt index 13b424b50..4ac6c99b8 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/testutil/TestHelpers.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/testutil/TestHelpers.kt @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0. */ -package software.amazon.smithy.rust.testutil +package software.amazon.smithy.rust.codegen.testutil import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.StructureShape diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/SymbolBuilderTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/SymbolBuilderTest.kt index f3e0e08d3..2a3d87447 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/SymbolBuilderTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/SymbolBuilderTest.kt @@ -33,8 +33,8 @@ import software.amazon.smithy.rust.codegen.smithy.Models import software.amazon.smithy.rust.codegen.smithy.Operations import software.amazon.smithy.rust.codegen.smithy.isOptional import software.amazon.smithy.rust.codegen.smithy.rustType -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.testSymbolProvider +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider class SymbolBuilderTest { private fun Symbol.referenceClosure(): List { diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/EnumGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/EnumGeneratorTest.kt index bb0fc8c27..18fbf8f6d 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/EnumGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/EnumGeneratorTest.kt @@ -13,10 +13,10 @@ import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.traits.EnumTrait import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.smithy.generators.EnumGenerator +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider class EnumGeneratorTest { @Test @@ -52,7 +52,7 @@ class EnumGeneratorTest { assert_eq!(InstanceType::from("other"), InstanceType::Unknown("other".to_owned())); // round trip unknown variants: assert_eq!(InstanceType::from("other").as_str(), "other"); - """.trimIndent() + """ ) writer.toString() shouldContain "#[non_exhaustive]" diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/HttpTraitBindingGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/HttpTraitBindingGeneratorTest.kt index fa4d32925..fac5fd13c 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/HttpTraitBindingGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/HttpTraitBindingGeneratorTest.kt @@ -17,12 +17,12 @@ import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.generators.HttpTraitBindingGenerator import software.amazon.smithy.rust.codegen.smithy.generators.uriFormatString import software.amazon.smithy.rust.codegen.smithy.transformers.OperationNormalizer +import software.amazon.smithy.rust.codegen.testutil.TestRuntimeConfig +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.renderWithModelBuilder +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.dq -import software.amazon.smithy.rust.testutil.TestRuntimeConfig -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.renderWithModelBuilder -import software.amazon.smithy.rust.testutil.testSymbolProvider class HttpTraitBindingGeneratorTest { private val baseModel = """ diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/StructureGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/StructureGeneratorTest.kt index d9244154f..bc0158f88 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/StructureGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/StructureGeneratorTest.kt @@ -17,12 +17,12 @@ import software.amazon.smithy.rust.codegen.rustlang.docs import software.amazon.smithy.rust.codegen.rustlang.raw import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.smithy.generators.StructureGenerator +import software.amazon.smithy.rust.codegen.testutil.TestWorkspace +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider +import software.amazon.smithy.rust.codegen.testutil.unitTest import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.TestWorkspace -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider -import software.amazon.smithy.rust.testutil.unitTest class StructureGeneratorTest { companion object { diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/UnionGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/UnionGeneratorTest.kt index 2f3ef3911..db015838a 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/UnionGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/generators/UnionGeneratorTest.kt @@ -10,10 +10,10 @@ import org.junit.jupiter.api.Test import software.amazon.smithy.codegen.core.SymbolProvider import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.smithy.generators.UnionGenerator +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider class UnionGeneratorTest { @Test diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/rustlang/InlineDependencyTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/rustlang/InlineDependencyTest.kt index 6969d5e62..ac87b059a 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/rustlang/InlineDependencyTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/rustlang/InlineDependencyTest.kt @@ -8,7 +8,7 @@ package software.amazon.smithy.rust.codegen.rustlang import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import org.junit.jupiter.api.Test -import software.amazon.smithy.rust.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.compileAndTest internal class InlineDependencyTest { fun makeDep(name: String) = InlineDependency(name, "module") { @@ -29,7 +29,7 @@ internal class InlineDependencyTest { fun `locate dependencies from the inlineable module`() { val dep = InlineDependency.idempotencyToken() val testWriter = RustWriter.root() - testWriter.addDependency(CargoDependency.Rand) + testWriter.addDependency(CargoDependency.FastRand) testWriter.withModule(dep.module) { dep.renderer(this) } diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGeneratorTest.kt index ef5a93d98..456c713ae 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CombinedErrorGeneratorTest.kt @@ -8,11 +8,11 @@ package software.amazon.smithy.rust.codegen.smithy.generators import org.junit.jupiter.api.Test import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.rust.codegen.rustlang.RustWriter +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.renderWithModelBuilder +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.renderWithModelBuilder -import software.amazon.smithy.rust.testutil.testSymbolProvider internal class CombinedErrorGeneratorTest { @Test diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolTestGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolTestGeneratorTest.kt index 294812c65..4043f2211 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolTestGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/HttpProtocolTestGeneratorTest.kt @@ -16,14 +16,14 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.smithy.CodegenVisitor import software.amazon.smithy.rust.codegen.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.smithy.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolMap import software.amazon.smithy.rust.codegen.smithy.transformers.OperationNormalizer +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.generatePluginContext import software.amazon.smithy.rust.codegen.util.CommandFailed import software.amazon.smithy.rust.codegen.util.dq import software.amazon.smithy.rust.codegen.util.runCommand -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.generatePluginContext import java.nio.file.Path class HttpProtocolTestGeneratorTest { diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/InstantiatorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/InstantiatorTest.kt index 8bc6c8931..887a355ef 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/InstantiatorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/InstantiatorTest.kt @@ -18,13 +18,13 @@ import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.rustlang.withBlock import software.amazon.smithy.rust.codegen.smithy.transformers.RecursiveShapeBoxer +import software.amazon.smithy.rust.codegen.testutil.TestRuntimeConfig +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.renderWithModelBuilder +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.dq import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.TestRuntimeConfig -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.renderWithModelBuilder -import software.amazon.smithy.rust.testutil.testSymbolProvider class InstantiatorTest { private val model = """ diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ModelBuilderGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ModelBuilderGeneratorTest.kt index 0b8f7ce63..10b78350f 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ModelBuilderGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ModelBuilderGeneratorTest.kt @@ -16,8 +16,8 @@ import software.amazon.smithy.rust.codegen.smithy.Default import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider import software.amazon.smithy.rust.codegen.smithy.SymbolVisitorConfig import software.amazon.smithy.rust.codegen.smithy.setDefault -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider internal class ModelBuilderGeneratorTest { private val model = StructureGeneratorTest.model diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomizationTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomizationTest.kt new file mode 100644 index 000000000..a4e0bb635 --- /dev/null +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/IdempotencyTokenProviderCustomizationTest.kt @@ -0,0 +1,16 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +package software.amazon.smithy.rust.codegen.smithy.generators.config + +import org.junit.jupiter.api.Test +import software.amazon.smithy.rust.codegen.testutil.validateConfigCustomizations + +class IdempotencyTokenProviderCustomizationTest { + @Test + fun `generates a valid config`() { + validateConfigCustomizations(IdempotencyTokenProviderCustomization()) + } +} diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGeneratorTest.kt index 6fd6c3267..cc6d9e4d4 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/config/ServiceConfigGeneratorTest.kt @@ -11,12 +11,13 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.rust.codegen.rustlang.Writable import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.writable +import software.amazon.smithy.rust.codegen.smithy.customize.NamedSectionGenerator +import software.amazon.smithy.rust.codegen.testutil.TestWorkspace +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider +import software.amazon.smithy.rust.codegen.testutil.unitTest import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.TestWorkspace -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider -import software.amazon.smithy.rust.testutil.unitTest internal class ServiceConfigGeneratorTest { @Test diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/CustomSerializerGeneratorTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/CustomSerializerGeneratorTest.kt index bbc1229d0..a7e90ab65 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/CustomSerializerGeneratorTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/CustomSerializerGeneratorTest.kt @@ -16,11 +16,11 @@ import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.smithy.RuntimeType import software.amazon.smithy.rust.codegen.smithy.WrappingSymbolProvider import software.amazon.smithy.rust.codegen.smithy.makeRustBoxed +import software.amazon.smithy.rust.codegen.testutil.TestWorkspace +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.TestWorkspace -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider internal class CustomSerializerGeneratorTest { private val model = """ diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/OperationNormalizerTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/OperationNormalizerTest.kt index d7a068561..d983a4b78 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/OperationNormalizerTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/OperationNormalizerTest.kt @@ -14,10 +14,10 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.rust.codegen.smithy.traits.InputBodyTrait import software.amazon.smithy.rust.codegen.smithy.traits.SyntheticInputTrait import software.amazon.smithy.rust.codegen.smithy.traits.SyntheticOutputTrait +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.lookup import software.amazon.smithy.rust.codegen.util.orNull -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.testSymbolProvider internal class OperationNormalizerTest { diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapeBoxerTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapeBoxerTest.kt index c352c5c3e..5879fcfaa 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapeBoxerTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapeBoxerTest.kt @@ -9,8 +9,8 @@ import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.rust.codegen.smithy.RustBoxTrait +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.asSmithyModel import kotlin.streams.toList internal class RecursiveShapeBoxerTest { diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapesIntegrationTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapesIntegrationTest.kt index 508cc1b85..9ccff1e89 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapesIntegrationTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/transformers/RecursiveShapesIntegrationTest.kt @@ -13,11 +13,11 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.smithy.generators.StructureGenerator import software.amazon.smithy.rust.codegen.smithy.generators.UnionGenerator +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.CommandFailed import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.testSymbolProvider class RecursiveShapesIntegrationTest { @Test diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt index 469e0231f..6dfb6945f 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt @@ -20,14 +20,14 @@ import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.rustlang.docs import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.smithy.RuntimeType +import software.amazon.smithy.rust.codegen.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.testutil.compileAndRun +import software.amazon.smithy.rust.codegen.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.testutil.shouldCompile +import software.amazon.smithy.rust.codegen.testutil.shouldParseAsRust +import software.amazon.smithy.rust.codegen.testutil.testSymbolProvider import software.amazon.smithy.rust.codegen.util.lookup -import software.amazon.smithy.rust.testutil.asSmithyModel -import software.amazon.smithy.rust.testutil.compileAndRun -import software.amazon.smithy.rust.testutil.compileAndTest -import software.amazon.smithy.rust.testutil.shouldCompile import software.amazon.smithy.rust.testutil.shouldMatchResource -import software.amazon.smithy.rust.testutil.shouldParseAsRust -import software.amazon.smithy.rust.testutil.testSymbolProvider class RustWriterTest { @Test diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/UseDeclarationsTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/UseDeclarationsTest.kt index 504716a86..efe8b4ad3 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/UseDeclarationsTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/UseDeclarationsTest.kt @@ -8,7 +8,7 @@ package software.amazon.smithy.rust.lang import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import software.amazon.smithy.rust.codegen.rustlang.UseDeclarations -import software.amazon.smithy.rust.testutil.shouldCompile +import software.amazon.smithy.rust.codegen.testutil.shouldCompile class UseDeclarationsTest { private fun useDecl() = UseDeclarations("test") diff --git a/rust-runtime/inlineable/Cargo.toml b/rust-runtime/inlineable/Cargo.toml index c832d2f23..5ba64d76c 100644 --- a/rust-runtime/inlineable/Cargo.toml +++ b/rust-runtime/inlineable/Cargo.toml @@ -14,7 +14,7 @@ are to allow this crate to be compilable and testable in isolation, no client co "http" = "0.2.1" "smithy-types" = { version = "0.0.1", path = "../smithy-types" } "smithy-http" = { version = "0.0.1", path = "../smithy-http"} -"rand" = "0.7" +"fastrand" = "1" [dev-dependencies] proptest = "0.10.1" diff --git a/rust-runtime/inlineable/src/idempotency_token.rs b/rust-runtime/inlineable/src/idempotency_token.rs index 6bf689046..f8f50d651 100644 --- a/rust-runtime/inlineable/src/idempotency_token.rs +++ b/rust-runtime/inlineable/src/idempotency_token.rs @@ -30,20 +30,17 @@ pub(crate) fn uuid_v4(input: u128) -> String { out } -pub trait ProvideIdempotencyToken { +pub trait ProvideIdempotencyToken: Send + Sync { fn token(&self) -> String; } pub fn default_provider() -> impl ProvideIdempotencyToken { - Mutex::new(rand::thread_rng()) + Mutex::new(fastrand::Rng::new()) } -impl ProvideIdempotencyToken for Mutex -where - T: rand::Rng, -{ +impl ProvideIdempotencyToken for Mutex { fn token(&self) -> String { - let input: u128 = self.lock().unwrap().gen(); + let input: u128 = self.lock().unwrap().u128(..); uuid_v4(input) } } diff --git a/rust-runtime/inlineable/src/lib.rs b/rust-runtime/inlineable/src/lib.rs index 22bc9a9c8..0d821b110 100644 --- a/rust-runtime/inlineable/src/lib.rs +++ b/rust-runtime/inlineable/src/lib.rs @@ -19,11 +19,13 @@ mod instant_iso8601; #[cfg(test)] mod test { use crate::doc_json::SerDoc; + use crate::idempotency_token; use crate::idempotency_token::uuid_v4; use proptest::prelude::*; use proptest::std_facade::HashMap; use smithy_types::Document; use smithy_types::Number; + use std::sync::Mutex; #[test] fn nan_floats_serialize_null() { @@ -47,6 +49,20 @@ mod test { ); } + #[test] + fn default_token_generator_smoke_test() { + // smoke test to make sure the default token generator produces a token-like object + use crate::idempotency_token::ProvideIdempotencyToken; + assert_eq!(idempotency_token::default_provider().token().len(), 36); + } + + #[test] + fn token_generator() { + let provider = Mutex::new(fastrand::Rng::with_seed(123)); + use crate::idempotency_token::ProvideIdempotencyToken; + assert_eq!(provider.token(), "b4021a03-ae07-4db5-fc1b-38bf919691f8"); + } + fn assert_valid(uuid: String) { assert_eq!(uuid.len(), 36); let bytes = uuid.as_bytes(); -- GitLab