-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new service builder codegen (#1693)
* Add `ServerProtocol` interface to allow for server side protocol specific methods. * Make public the structs merged in #1679. * Add `ServerOperationGenerator`, which generates a ZST and implements `OperationShape` on it. * Add `ServerServiceGeneratorV2`, which generates the service newtype around a router and a service builder. * Add `hidden` argument to `RustModule` which allows modules to be marked with `#[doc(hidden)]`. * Add `BuildModifier` trait to provide a common interface for extending service builders. * Add `Upgradable` trait to simplifying bounds when upgrading from an `Operation` to a HTTP service. * Add `FromRequest`, `FromParts`, and `IntoResponse` implementations. * Make `RoutingService` accept general body types `B` for the inner services `http::Response<B>`. * Use new service builder in protocol tests.
- Loading branch information
Showing
21 changed files
with
979 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.model.shapes.OperationShape | ||
import software.amazon.smithy.rust.codegen.client.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.client.rustlang.Writable | ||
import software.amazon.smithy.rust.codegen.client.rustlang.asType | ||
import software.amazon.smithy.rust.codegen.client.rustlang.documentShape | ||
import software.amazon.smithy.rust.codegen.client.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.client.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.client.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext | ||
import software.amazon.smithy.rust.codegen.client.util.toPascalCase | ||
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency | ||
|
||
class ServerOperationGenerator( | ||
coreCodegenContext: CoreCodegenContext, | ||
private val operation: OperationShape, | ||
) { | ||
private val runtimeConfig = coreCodegenContext.runtimeConfig | ||
private val codegenScope = | ||
arrayOf( | ||
"SmithyHttpServer" to | ||
ServerCargoDependency.SmithyHttpServer(runtimeConfig).asType(), | ||
) | ||
private val symbolProvider = coreCodegenContext.symbolProvider | ||
private val model = coreCodegenContext.model | ||
|
||
private val operationName = symbolProvider.toSymbol(operation).name.toPascalCase() | ||
private val operationId = operation.id | ||
|
||
/** Returns `std::convert::Infallible` if the model provides no errors. */ | ||
private fun operationError(): Writable = writable { | ||
if (operation.errors.isEmpty()) { | ||
rust("std::convert::Infallible") | ||
} else { | ||
rust("crate::error::${operationName}Error") | ||
} | ||
} | ||
|
||
fun render(writer: RustWriter) { | ||
writer.documentShape(operation, model) | ||
|
||
writer.rustTemplate( | ||
""" | ||
pub struct $operationName; | ||
impl #{SmithyHttpServer}::operation::OperationShape for $operationName { | ||
const NAME: &'static str = "${operationId.toString().replace("#", "##")}"; | ||
type Input = crate::input::${operationName}Input; | ||
type Output = crate::output::${operationName}Output; | ||
type Error = #{Error:W}; | ||
} | ||
""", | ||
"Error" to operationError(), | ||
*codegenScope, | ||
) | ||
// Adds newline to end of render | ||
writer.rust("") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.