Unverified Commit acffa7df authored by 82marbag's avatar 82marbag Committed by GitHub
Browse files

Re-export runtime types in server generated crates (#1909)



* Re-export runtime types in server generated crates

Re-export commonly used types from aws-smithy-http-server so that customers don't have
to depend on that crate.

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>
parent f5c56b66
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package software.amazon.smithy.rust.codegen.server.smithy.generators

import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType

class ServerRuntimeTypesReExportsGenerator(
    codegenContext: CodegenContext,
) {
    private val runtimeConfig = codegenContext.runtimeConfig
    private val codegenScope = arrayOf(
        "Router" to ServerRuntimeType.router(runtimeConfig),
        "SmithyHttpServer" to ServerCargoDependency.smithyHttpServer(runtimeConfig).toType(),
    )

    fun render(writer: RustWriter) {
        writer.rustTemplate(
            """
            pub mod body {
                pub use #{SmithyHttpServer}::body::BoxBody;
            }
            pub mod operation {
                pub use #{SmithyHttpServer}::operation::OperationShape;
                pub use #{SmithyHttpServer}::operation::Operation;
            }
            pub mod plugin {
                pub use #{SmithyHttpServer}::plugin::Plugin;
                pub use #{SmithyHttpServer}::plugin::PluginPipeline;
                pub use #{SmithyHttpServer}::plugin::PluginStack;
            }
            pub mod request {
                pub use #{SmithyHttpServer}::request::FromParts;
            }
            pub mod response {
                pub use #{SmithyHttpServer}::response::IntoResponse;
            }
            pub mod routing {
                pub use #{SmithyHttpServer}::routing::IntoMakeService;
                pub use #{SmithyHttpServer}::routing::IntoMakeServiceWithConnectInfo;
                pub use #{SmithyHttpServer}::routing::Router;

                ##[cfg(feature = "aws-lambda")]
                pub use #{SmithyHttpServer}::routing::LambdaHandler;
            }

            pub use #{SmithyHttpServer}::instrumentation;
            pub use #{SmithyHttpServer}::proto;

            pub use #{SmithyHttpServer}::Extension;
            """,
            *codegenScope,
        )
    }
}
+16 −0
Original line number Diff line number Diff line
@@ -296,6 +296,17 @@ open class ServerServiceGenerator(
        }

        renderExtras(operations)

        rustCrate.withModule(
            RustModule.public(
                "server",
                """
                Contains the types that are re-exported from the `aws-smithy-http-server` create.
                """,
            ),
        ) {
            renderServerReExports(this)
        }
    }

    // Render any extra section needed by subclasses of `ServerServiceGenerator`.
@@ -315,4 +326,9 @@ open class ServerServiceGenerator(
    private fun renderOperationRegistry(writer: RustWriter, operations: List<OperationShape>) {
        ServerOperationRegistryGenerator(codegenContext, protocol, operations).render(writer)
    }

    // Render `server` crate, re-exporting types.
    private fun renderServerReExports(writer: RustWriter) {
        ServerRuntimeTypesReExportsGenerator(codegenContext).render(writer)
    }
}