Skip to content

Commit

Permalink
Split RuntimeType and CargoDependency for server code generation (#…
Browse files Browse the repository at this point in the history
…870)

For clear separation of concerns.
  • Loading branch information
david-perez authored Nov 19, 2021
1 parent 0bc5523 commit 1ee7d65
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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"))
}
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Fun0, Fut0, ..., FunN, FutN>`.
val operationsGenericArguments = operations.mapIndexed { i, _ -> "Fun$i, Fut$i"}.joinToString()
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 1ee7d65

Please sign in to comment.