Unverified Commit 934129cf authored by Harry Barber's avatar Harry Barber Committed by GitHub
Browse files

Consolidate `proto` and `protocols` modules into single `protocol` module (#2780)

## Motivation and Context

We have two modules `proto` and `protocols` in `aws_smithy_http_server`,
these can be consolidated.
parent 472adcfb
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ object ServerRuntimeType {
        ServerCargoDependency.smithyHttpServer(runtimeConfig).toType().resolve("routing::Router")

    fun protocol(name: String, path: String, runtimeConfig: RuntimeConfig) =
        ServerCargoDependency.smithyHttpServer(runtimeConfig).toType().resolve("proto::$path::$name")
        ServerCargoDependency.smithyHttpServer(runtimeConfig).toType().resolve("protocol::$path::$name")

    fun protocol(runtimeConfig: RuntimeConfig) = protocol("Protocol", "", runtimeConfig)
}
+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ class ServerRuntimeTypesReExportsGenerator(
            }

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

            pub use #{SmithyHttpServer}::Extension;
            """,
+9 −9
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerRestJso
import software.amazon.smithy.rust.codegen.server.smithy.targetCanReachConstrainedShape

interface ServerProtocol : Protocol {
    /** The path such that `aws_smithy_http_server::proto::$path` points to the protocol's module. */
    /** The path such that `aws_smithy_http_server::protocol::$path` points to the protocol's module. */
    val protocolModulePath: String

    /** Returns the Rust marker struct enjoying `OperationShape`. */
@@ -79,17 +79,17 @@ interface ServerProtocol : Protocol {
    /** The protocol-specific `RequestRejection` type. **/
    fun requestRejection(runtimeConfig: RuntimeConfig): RuntimeType =
        ServerCargoDependency.smithyHttpServer(runtimeConfig)
            .toType().resolve("proto::$protocolModulePath::rejection::RequestRejection")
            .toType().resolve("protocol::$protocolModulePath::rejection::RequestRejection")

    /** The protocol-specific `ResponseRejection` type. **/
    fun responseRejection(runtimeConfig: RuntimeConfig): RuntimeType =
        ServerCargoDependency.smithyHttpServer(runtimeConfig)
            .toType().resolve("proto::$protocolModulePath::rejection::ResponseRejection")
            .toType().resolve("protocol::$protocolModulePath::rejection::ResponseRejection")

    /** The protocol-specific `RuntimeError` type. **/
    fun runtimeError(runtimeConfig: RuntimeConfig): RuntimeType =
        ServerCargoDependency.smithyHttpServer(runtimeConfig)
            .toType().resolve("proto::$protocolModulePath::runtime_error::RuntimeError")
            .toType().resolve("protocol::$protocolModulePath::runtime_error::RuntimeError")
}

fun returnSymbolToParseFn(codegenContext: ServerCodegenContext): (Shape) -> ReturnSymbolToParse {
@@ -145,7 +145,7 @@ class ServerAwsJsonProtocol(
    }

    override fun routerType() = ServerCargoDependency.smithyHttpServer(runtimeConfig).toType()
        .resolve("proto::aws_json::router::AwsJsonRouter")
        .resolve("protocol::aws_json::router::AwsJsonRouter")

    /**
     * Returns the operation name as required by the awsJson1.x protocols.
@@ -170,18 +170,18 @@ class ServerAwsJsonProtocol(

    override fun requestRejection(runtimeConfig: RuntimeConfig): RuntimeType =
        ServerCargoDependency.smithyHttpServer(runtimeConfig)
            .toType().resolve("proto::aws_json::rejection::RequestRejection")
            .toType().resolve("protocol::aws_json::rejection::RequestRejection")
    override fun responseRejection(runtimeConfig: RuntimeConfig): RuntimeType =
        ServerCargoDependency.smithyHttpServer(runtimeConfig)
            .toType().resolve("proto::aws_json::rejection::ResponseRejection")
            .toType().resolve("protocol::aws_json::rejection::ResponseRejection")
    override fun runtimeError(runtimeConfig: RuntimeConfig): RuntimeType =
        ServerCargoDependency.smithyHttpServer(runtimeConfig)
            .toType().resolve("proto::aws_json::runtime_error::RuntimeError")
            .toType().resolve("protocol::aws_json::runtime_error::RuntimeError")
}

private fun restRouterType(runtimeConfig: RuntimeConfig) =
    ServerCargoDependency.smithyHttpServer(runtimeConfig).toType()
        .resolve("proto::rest::router::RestRouter")
        .resolve("protocol::rest::router::RestRouter")

class ServerRestJsonProtocol(
    private val serverCodegenContext: ServerCodegenContext,
+4 −4
Original line number Diff line number Diff line
@@ -210,7 +210,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
            httpBindingResolver.responseContentType(operationShape)?.also { contentType ->
                rustTemplate(
                    """
                    if !#{SmithyHttpServer}::protocols::accept_header_classifier(request.headers(), &$staticContentType) {
                    if !#{SmithyHttpServer}::protocol::accept_header_classifier(request.headers(), &$staticContentType) {
                        return Err(#{RequestRejection}::NotAcceptable);
                    }
                    """,
@@ -246,7 +246,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
                            ?.let { "Some(${it.dq()})" } ?: "None"
                        rustTemplate(
                            """
                            #{SmithyHttpServer}::protocols::content_type_header_classifier(request.headers(), $expectedRequestContentType)?;
                            #{SmithyHttpServer}::protocol::content_type_header_classifier(request.headers(), $expectedRequestContentType)?;
                            """,
                            *codegenScope,
                        )
@@ -700,7 +700,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
            if (protocol is RestJson) {
                rustTemplate(
                    """
                    #{SmithyHttpServer}::protocols::content_type_header_classifier(&parts.headers, Some("application/json"))?;
                    #{SmithyHttpServer}::protocol::content_type_header_classifier(&parts.headers, Some("application/json"))?;
                    """,
                    *codegenScope,
                )
@@ -742,7 +742,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
            conditionalBlock("if body.is_empty() {", "}", conditional = parser != null) {
                rustTemplate(
                    """
                    #{SmithyHttpServer}::protocols::content_type_header_empty_body_no_modeled_input(&parts.headers)?;
                    #{SmithyHttpServer}::protocol::content_type_header_empty_body_no_modeled_input(&parts.headers)?;
                    """,
                    *codegenScope,
                )
+2 −2
Original line number Diff line number Diff line
@@ -265,7 +265,7 @@ Note that both traits are parameterized by `Protocol`. These [protocols](https:/

```rust
# extern crate aws_smithy_http_server;
# use aws_smithy_http_server::proto::{
# use aws_smithy_http_server::protocol::{
#   aws_json_10::AwsJson1_0 as _,
#   aws_json_11::AwsJson1_1 as _,
#   rest_json_1::RestJson1 as _,
@@ -615,7 +615,7 @@ The final outcome, an instance of `PokemonService`, looks roughly like this:

```rust
# extern crate aws_smithy_http_server;
# use aws_smithy_http_server::{routing::RoutingService, proto::rest_json_1::{router::RestRouter, RestJson1}};
# use aws_smithy_http_server::{routing::RoutingService, protocol::rest_json_1::{router::RestRouter, RestJson1}};
/// The Pokémon Service allows you to retrieve information about Pokémon species.
#[derive(Clone)]
pub struct PokemonService<S> {
Loading