diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCargoDependency.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCargoDependency.kt new file mode 100644 index 0000000000..8b05a8ed71 --- /dev/null +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCargoDependency.kt @@ -0,0 +1,19 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +package software.amazon.smithy.rust.codegen.server.smithy +import software.amazon.smithy.rust.codegen.rustlang.CargoDependency +import software.amazon.smithy.rust.codegen.rustlang.CratesIo + +/** + * Object used *exclusively* in the runtime of the server, for separation concerns. + * Analogous to the companion object in [CargoDependency]; see its documentation for details. + * For a dependency that is used in the client, or in both the client and the server, use [CargoDependency] directly. + */ +object ServerCargoDependency { + val Axum: CargoDependency = CargoDependency("axum", CratesIo("0.3")) + // TODO Remove this dependency https://github.com/awslabs/smithy-rs/issues/864 + val DeriveBuilder = CargoDependency("derive_builder", CratesIo("0.10")) +} diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRuntimeTypes.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRuntimeTypes.kt new file mode 100644 index 0000000000..7479d37dd5 --- /dev/null +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRuntimeTypes.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.server.smithy + +import software.amazon.smithy.rust.codegen.rustlang.CargoDependency +import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig +import software.amazon.smithy.rust.codegen.smithy.RuntimeType + +/** + * Object used *exclusively* in the runtime of the server, for separation concerns. + * Analogous to the companion object in [RuntimeType]; see its documentation for details. + * For a runtime type that is used in the client, or in both the client and the server, use [RuntimeType] directly. + */ +object ServerRuntimeType { + val DeriveBuilder = RuntimeType("Builder", dependency = ServerCargoDependency.DeriveBuilder, namespace = "derive_builder") + + fun Router(runtimeConfig: RuntimeConfig) = + RuntimeType("Router", CargoDependency.SmithyHttpServer(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_http_server::routing") + fun RequestSpecModule(runtimeConfig: RuntimeConfig) = + RuntimeType("request_spec", CargoDependency.SmithyHttpServer(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_http_server::routing") +} diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/OperationRegistryGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/OperationRegistryGenerator.kt index 998a3adb77..3f0de17592 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/OperationRegistryGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/OperationRegistryGenerator.kt @@ -7,10 +7,10 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.rustlang.* +import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerHttpProtocolGenerator import software.amazon.smithy.rust.codegen.smithy.CodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.smithy.RuntimeType.Companion.RequestSpecModule import software.amazon.smithy.rust.codegen.smithy.generators.error.errorSymbol import software.amazon.smithy.rust.codegen.smithy.protocols.HttpBindingResolver import software.amazon.smithy.rust.codegen.smithy.protocols.HttpTraitHttpBindingResolver @@ -33,13 +33,13 @@ class OperationRegistryGenerator( private val operationNames = operations.map { symbolProvider.toSymbol(it).name.toSnakeCase() } private val runtimeConfig = codegenContext.runtimeConfig private val codegenScope = arrayOf( - "Router" to RuntimeType.Router(runtimeConfig), + "Router" to ServerRuntimeType.Router(runtimeConfig), ) private val httpBindingResolver: HttpBindingResolver = HttpTraitHttpBindingResolver(codegenContext.model, ProtocolContentTypes.consistent("application/json")) fun render(writer: RustWriter) { - Attribute.Derives(setOf(RuntimeType.Debug, RuntimeType.DeriveBuilder)).render(writer) + Attribute.Derives(setOf(RuntimeType.Debug, ServerRuntimeType.DeriveBuilder)).render(writer) Attribute.Custom("builder(pattern = \"owned\")").render(writer) // Generic arguments of the `OperationRegistryBuilder`. val operationsGenericArguments = operations.mapIndexed { i, _ -> "Fun$i, Fut$i"}.joinToString() @@ -98,7 +98,7 @@ class OperationRegistryGenerator( private fun OperationShape.requestSpec(): String { val httpTrait = httpBindingResolver.httpTrait(this) - val namespace = RequestSpecModule(runtimeConfig).fullyQualifiedName() + val namespace = ServerRuntimeType.RequestSpecModule(runtimeConfig).fullyQualifiedName() // TODO Support the `endpoint` trait: https://awslabs.github.io/smithy/1.0/spec/core/endpoint-traits.html#endpoint-trait diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt index 2a4425386a..66aa8cbe22 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt @@ -27,6 +27,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustBlock import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.withBlock +import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerHttpProtocolGenerator import software.amazon.smithy.rust.codegen.smithy.CodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeType @@ -65,7 +66,7 @@ class ServerProtocolTestGenerator( "SmithyHttp" to CargoDependency.SmithyHttp(codegenContext.runtimeConfig).asType(), "Http" to CargoDependency.Http.asType(), "Hyper" to CargoDependency.Hyper.asType(), - "Axum" to CargoDependency.Axum.asType(), + "Axum" to ServerCargoDependency.Axum.asType(), "SmithyHttpServer" to CargoDependency.SmithyHttpServer(codegenContext.runtimeConfig).asType(), ) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt index 811a59dd0c..f46df55073 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt @@ -23,6 +23,7 @@ import software.amazon.smithy.rust.codegen.rustlang.rustBlockTemplate import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.withBlock import software.amazon.smithy.rust.codegen.rustlang.writable +import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency import software.amazon.smithy.rust.codegen.smithy.CodegenContext import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RuntimeType @@ -86,7 +87,7 @@ private class ServerHttpProtocolImplGenerator( private val operationSerModule = RustModule.private("operation_ser") private val codegenScope = arrayOf( - "Axum" to CargoDependency.Axum.asType(), + "Axum" to ServerCargoDependency.Axum.asType(), "DateTime" to RuntimeType.DateTime(runtimeConfig), "HttpBody" to CargoDependency.HttpBody.asType(), "Hyper" to CargoDependency.Hyper.asType(), 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 c1056b9f03..53250f10b1 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 @@ -189,10 +189,8 @@ data class CargoDependency( } companion object { - val Axum: CargoDependency = CargoDependency("axum", CratesIo("0.3")) val Bytes: CargoDependency = CargoDependency("bytes", CratesIo("1")) val BytesUtils: CargoDependency = CargoDependency("bytes-utils", CratesIo("0.1.1")) - val DeriveBuilder = CargoDependency("derive_builder", CratesIo("0.10")) val FastRand: CargoDependency = CargoDependency("fastrand", CratesIo("1")) val Hex: CargoDependency = CargoDependency("hex", CratesIo("0.4.3")) val HttpBody: CargoDependency = CargoDependency("http-body", CratesIo("0.4")) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt index 7326bd7af7..8274d97c01 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt @@ -27,7 +27,6 @@ import java.util.Optional * * This can be configured via the `runtimeConfig.version` field in smithy-build.json */ - data class RuntimeCrateLocation(val path: String?, val version: String?) { init { check(path != null || version != null) { @@ -176,13 +175,6 @@ data class RuntimeType(val name: String?, val dependency: RustDependency?, val n val PartialEq = std.member("cmp::PartialEq") val StdError = RuntimeType("Error", dependency = null, namespace = "std::error") val String = RuntimeType("String", dependency = null, namespace = "std::string") - val DeriveBuilder = RuntimeType("Builder", dependency = CargoDependency.DeriveBuilder, namespace = "derive_builder") - - fun RequestSpecModule(runtimeConfig: RuntimeConfig) = - RuntimeType("request_spec", CargoDependency.SmithyHttpServer(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_http_server::routing") - - fun Router(runtimeConfig: RuntimeConfig) = - RuntimeType("Router", CargoDependency.SmithyHttpServer(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_http_server::routing") fun DateTime(runtimeConfig: RuntimeConfig) = RuntimeType("DateTime", CargoDependency.SmithyTypes(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_types")