diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt index a0ad38f04f..28256ec15d 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenContext.kt @@ -5,6 +5,7 @@ package software.amazon.smithy.rust.codegen.server.smithy +import software.amazon.smithy.codegen.core.Symbol import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.ShapeId @@ -31,6 +32,7 @@ data class ServerCodegenContext( val constrainedShapeSymbolProvider: RustSymbolProvider, val constraintViolationSymbolProvider: ConstraintViolationSymbolProvider, val pubCrateConstrainedShapeSymbolProvider: PubCrateConstrainedShapeSymbolProvider, + val typesToInit: MutableSet = setOf().toMutableSet(), ) : CodegenContext( model, symbolProvider, serviceShape, protocol, settings, CodegenTarget.SERVER, ) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedStringGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedStringGenerator.kt index be1df7fa8c..1b6ed8f080 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedStringGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedStringGenerator.kt @@ -87,6 +87,8 @@ class ConstrainedStringGenerator( if (constrainedTypeVisibility == Visibility.PUBCRATE) { Attribute.AllowUnused.render(writer) } + codegenContext.typesToInit.add(symbol) + writer.rust( """ impl $name { diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt index e408248483..eddfb31b89 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt @@ -13,9 +13,9 @@ import software.amazon.smithy.rust.codegen.core.rustlang.RustModule import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter import software.amazon.smithy.rust.codegen.core.rustlang.Visibility import software.amazon.smithy.rust.codegen.core.rustlang.rust -import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RustCrate import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport +import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolTestGenerator @@ -31,7 +31,7 @@ open class ServerServiceGenerator( private val protocolGenerator: ServerProtocolGenerator, private val protocolSupport: ProtocolSupport, val protocol: ServerProtocol, - private val codegenContext: CodegenContext, + private val codegenContext: ServerCodegenContext, ) { private val index = TopDownIndex.of(codegenContext.model) protected val operations = index.getContainedOperations(codegenContext.serviceShape).sortedBy { it.id } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt index ca782f4da6..6d34bb58a8 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt @@ -16,14 +16,14 @@ 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.CodegenContext import software.amazon.smithy.rust.codegen.core.util.toPascalCase import software.amazon.smithy.rust.codegen.core.util.toSnakeCase import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency +import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol class ServerServiceGeneratorV2( - private val codegenContext: CodegenContext, + private val codegenContext: ServerCodegenContext, private val protocol: ServerProtocol, ) { private val runtimeConfig = codegenContext.runtimeConfig @@ -39,8 +39,11 @@ class ServerServiceGeneratorV2( ) private val model = codegenContext.model private val symbolProvider = codegenContext.symbolProvider - val crateName = codegenContext.settings.moduleName.toSnakeCase() + private val crateName = codegenContext.settings.moduleName.toSnakeCase() + private val typeInitializations = codegenContext.typesToInit + .map { typeSymbol -> writable { rust("$typeSymbol::compile_regex();") } } + .join("\n") private val service = codegenContext.serviceShape private val serviceName = service.id.name.toPascalCase() private val builderName = "${serviceName}Builder" @@ -203,6 +206,7 @@ class ServerServiceGeneratorV2( /// unspecified route requested. pub fn build(self) -> Result<$serviceName<#{SmithyHttpServer}::routing::Route<$builderBodyGenericTypeName>>, MissingOperationsError> { + #{TypeInitializations:W} let router = { use #{SmithyHttpServer}::operation::OperationShape; let mut $missingOperationsVariableName = std::collections::HashMap::new(); @@ -221,6 +225,7 @@ class ServerServiceGeneratorV2( } """, "Router" to protocol.routerType(), + "TypeInitializations" to typeInitializations, "NullabilityChecks" to nullabilityChecks, "RoutesArrayElements" to routesArrayElements, "SmithyHttpServer" to smithyHttpServer,