Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate operation and operation_shape modules #2781

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,9 @@ open class ServerCodegenVisitor(
ServerOperationErrorGenerator(model, codegenContext.symbolProvider, shape).render(this)
}

// Generate operation shapes.
rustCrate.withModule(ServerRustModule.OperationShape) {
ServerOperationGenerator(shape, codegenContext).render(this)
}

// Generate operations ser/de.
// Generate operations ser/de and operation shapes.
rustCrate.withModule(ServerRustModule.Operation) {
ServerOperationGenerator(shape, codegenContext).render(this)
protocolGenerator.renderOperation(this, shape)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ object ServerRustModule {

val Error = RustModule.public("error")
val Operation = RustModule.public("operation")
val OperationShape = RustModule.public("operation_shape")
val Model = RustModule.public("model")
val Input = RustModule.public("input")
val Output = RustModule.public("output")
Expand All @@ -54,8 +53,7 @@ class ServerModuleDocProvider(private val codegenContext: ServerCodegenContext)
val strDoc: (String) -> Writable = { str -> writable { docs(escape(str)) } }
return when (module) {
ServerRustModule.Error -> strDoc("All error types that operations can return. Documentation on these types is copied from the model.")
ServerRustModule.Operation -> strDoc("All operations that this crate can perform.")
ServerRustModule.OperationShape -> operationShapeModuleDoc()
ServerRustModule.Operation -> operationShapeModuleDoc()
ServerRustModule.Model -> strDoc("Data structures used by operation inputs/outputs. Documentation on these types is copied from the model.")
ServerRustModule.Input -> strDoc("Input structures for operations. Documentation on these types is copied from the model.")
ServerRustModule.Output -> strDoc("Output structures for operations. Documentation on these types is copied from the model.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class ScopeMacroGenerator(
/// ## impl<P, Op, S> Plugin<P, Op, S> for MockPlugin { type Service = u32; fn apply(&self, svc: S) -> u32 { 3 } }
/// ## let scoped_a = Scoped::new::<ScopeA>(MockPlugin);
/// ## let scoped_b = Scoped::new::<ScopeB>(MockPlugin);
/// ## let a = Plugin::<(), $crateName::operation_shape::$firstOperationName, u64>::apply(&scoped_a, 6);
/// ## let b = Plugin::<(), $crateName::operation_shape::$firstOperationName, u64>::apply(&scoped_b, 6);
/// ## let a = Plugin::<(), $crateName::operation::$firstOperationName, u64>::apply(&scoped_a, 6);
/// ## let b = Plugin::<(), $crateName::operation::$firstOperationName, u64>::apply(&scoped_b, 6);
/// ## assert_eq!(a, 3_u32);
/// ## assert_eq!(b, 6_u64);
/// ```
Expand Down Expand Up @@ -142,7 +142,7 @@ class ScopeMacroGenerator(
includes: [$($ include:ident),*]
}
) => {
use $ crate::operation_shape::*;
use $ crate::operation::*;
#{SmithyHttpServer}::scope! {
$(##[$ attrs])*
$ vis struct $ name {
Expand All @@ -158,7 +158,7 @@ class ScopeMacroGenerator(
excludes: [$($ exclude:ident),*]
}
) => {
use $ crate::operation_shape::*;
use $ crate::operation::*;

#{SmithyHttpServer}::scope! {
$(##[$ attrs])*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ServerServiceGenerator(
val handlerFixed = docHandler.docFixedSignature()
rustTemplate(
"""
/// Sets the [`$structName`](crate::operation_shape::$structName) operation.
/// Sets the [`$structName`](crate::operation::$structName) operation.
///
/// This should be an async function satisfying the [`Handler`](#{SmithyHttpServer}::operation::Handler) trait.
/// See the [operation module documentation](#{SmithyHttpServer}::operation) for more information.
Expand All @@ -133,26 +133,26 @@ class ServerServiceGenerator(
///
pub fn $fieldName<HandlerType, HandlerExtractors, UpgradeExtractors>(self, handler: HandlerType) -> Self
where
HandlerType: #{SmithyHttpServer}::operation::Handler<crate::operation_shape::$structName, HandlerExtractors>,
HandlerType: #{SmithyHttpServer}::operation::Handler<crate::operation::$structName, HandlerExtractors>,

ModelPlugin: #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
#{SmithyHttpServer}::operation::IntoService<crate::operation_shape::$structName, HandlerType>
crate::operation::$structName,
#{SmithyHttpServer}::operation::IntoService<crate::operation::$structName, HandlerType>
>,
#{SmithyHttpServer}::operation::UpgradePlugin::<UpgradeExtractors>: #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
crate::operation::$structName,
ModelPlugin::Service
>,
HttpPlugin: #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
crate::operation::$structName,
<
#{SmithyHttpServer}::operation::UpgradePlugin::<UpgradeExtractors>
as #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
crate::operation::$structName,
ModelPlugin::Service
>
>::Service
Expand All @@ -164,14 +164,14 @@ class ServerServiceGenerator(
{
use #{SmithyHttpServer}::operation::OperationShapeExt;
use #{SmithyHttpServer}::plugin::Plugin;
let svc = crate::operation_shape::$structName::from_handler(handler);
let svc = crate::operation::$structName::from_handler(handler);
let svc = self.model_plugin.apply(svc);
let svc = #{SmithyHttpServer}::operation::UpgradePlugin::<UpgradeExtractors>::new().apply(svc);
let svc = self.http_plugin.apply(svc);
self.${fieldName}_custom(svc)
}

/// Sets the [`$structName`](crate::operation_shape::$structName) operation.
/// Sets the [`$structName`](crate::operation::$structName) operation.
///
/// This should be an async function satisfying the [`Handler`](#{SmithyHttpServer}::operation::Handler) trait.
/// See the [operation module documentation](#{SmithyHttpServer}::operation) for more information.
Expand All @@ -196,26 +196,26 @@ class ServerServiceGenerator(
///
pub fn ${fieldName}_service<S, ServiceExtractors, UpgradeExtractors>(self, service: S) -> Self
where
S: #{SmithyHttpServer}::operation::OperationService<crate::operation_shape::$structName, ServiceExtractors>,
S: #{SmithyHttpServer}::operation::OperationService<crate::operation::$structName, ServiceExtractors>,

ModelPlugin: #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
#{SmithyHttpServer}::operation::Normalize<crate::operation_shape::$structName, S>
crate::operation::$structName,
#{SmithyHttpServer}::operation::Normalize<crate::operation::$structName, S>
>,
#{SmithyHttpServer}::operation::UpgradePlugin::<UpgradeExtractors>: #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
crate::operation::$structName,
ModelPlugin::Service
>,
HttpPlugin: #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
crate::operation::$structName,
<
#{SmithyHttpServer}::operation::UpgradePlugin::<UpgradeExtractors>
as #{SmithyHttpServer}::plugin::Plugin<
#{Protocol},
crate::operation_shape::$structName,
crate::operation::$structName,
ModelPlugin::Service
>
>::Service
Expand All @@ -227,14 +227,14 @@ class ServerServiceGenerator(
{
use #{SmithyHttpServer}::operation::OperationShapeExt;
use #{SmithyHttpServer}::plugin::Plugin;
let svc = crate::operation_shape::$structName::from_service(service);
let svc = crate::operation::$structName::from_service(service);
let svc = self.model_plugin.apply(svc);
let svc = #{SmithyHttpServer}::operation::UpgradePlugin::<UpgradeExtractors>::new().apply(svc);
let svc = self.http_plugin.apply(svc);
self.${fieldName}_custom(svc)
}

/// Sets the [`$structName`](crate::operation_shape::$structName) to a custom [`Service`](tower::Service).
/// Sets the [`$structName`](crate::operation::$structName) to a custom [`Service`](tower::Service).
/// not constrained by the Smithy contract.
fn ${fieldName}_custom<S>(mut self, svc: S) -> Self
where
Expand Down Expand Up @@ -268,7 +268,7 @@ class ServerServiceGenerator(
rust(
"""
if self.$fieldName.is_none() {
$missingOperationsVariableName.insert(crate::operation_shape::$operationZstTypeName::ID, ".$fieldName()");
$missingOperationsVariableName.insert(crate::operation::$operationZstTypeName::ID, ".$fieldName()");
}
""",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
private val protocolFunctions = ProtocolFunctions(codegenContext)

private val codegenScope = arrayOf(
*RuntimeType.preludeScope,
"AsyncTrait" to ServerCargoDependency.AsyncTrait.toType(),
"Cow" to RuntimeType.Cow,
"DateTime" to RuntimeType.dateTime(runtimeConfig),
Expand Down Expand Up @@ -264,12 +265,12 @@ class ServerHttpBoundProtocolTraitImplGenerator(
/// A [`Future`](std::future::Future) aggregating the body bytes of a [`Request`] and constructing the
/// [`${inputSymbol.name}`](#{I}) using modelled bindings.
pub struct $inputFuture {
inner: std::pin::Pin<Box<dyn std::future::Future<Output = Result<#{I}, #{RuntimeError}>> + Send>>
inner: std::pin::Pin<Box<dyn std::future::Future<Output = #{Result}<#{I}, #{RuntimeError}>> + Send>>
}
}

impl std::future::Future for $inputFuture {
type Output = Result<#{I}, #{RuntimeError}>;
type Output = #{Result}<#{I}, #{RuntimeError}>;

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
let this = self.project();
Expand Down Expand Up @@ -378,7 +379,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
"""
pub async fn $fnName<B>(
##[allow(unused_variables)] request: #{http}::Request<B>
) -> std::result::Result<
) -> #{Result}<
#{I},
#{RequestRejection}
>
Expand Down Expand Up @@ -415,7 +416,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
"""
pub fn $fnName(
##[allow(unused_variables)] output: #{O}
) -> std::result::Result<
) -> #{Result}<
#{SmithyHttpServer}::response::Response,
#{ResponseRejection}
>
Expand All @@ -438,7 +439,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
return protocolFunctions.serializeFn(operationShape, fnNameSuffix = "http_error") { fnName ->
Attribute.AllowClippyUnnecessaryWraps.render(this)
rustBlockTemplate(
"pub fn $fnName(error: &#{E}) -> std::result::Result<#{SmithyHttpServer}::response::Response, #{ResponseRejection}>",
"pub fn $fnName(error: &#{E}) -> #{Result}<#{SmithyHttpServer}::response::Response, #{ResponseRejection}>",
*codegenScope,
"E" to errorSymbol,
) {
Expand Down Expand Up @@ -1146,7 +1147,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
val output = unconstrainedShapeSymbolProvider.toSymbol(binding.member)
return protocolFunctions.deserializeFn(binding.member) { fnName ->
rustBlockTemplate(
"pub fn $fnName(value: &str) -> std::result::Result<#{O}, #{RequestRejection}>",
"pub fn $fnName(value: &str) -> #{Result}<#{O}, #{RequestRejection}>",
*codegenScope,
"O" to output,
) {
Expand Down
6 changes: 3 additions & 3 deletions design/src/server/anatomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Smithy Rust will use this model to produce the following API:
```rust
# extern crate pokemon_service_server_sdk;
# extern crate aws_smithy_http_server;
# use pokemon_service_server_sdk::{input::*, output::*, error::*, operation_shape::*, PokemonService};
# use pokemon_service_server_sdk::{input::*, output::*, error::*, operation::*, PokemonService};
// A handler for the `GetPokemonSpecies` operation (the `PokemonSpecies` resource).
async fn get_pokemon_species(input: GetPokemonSpeciesInput) -> Result<GetPokemonSpeciesOutput, GetPokemonSpeciesError> {
todo!()
Expand Down Expand Up @@ -171,7 +171,7 @@ use pokemon_service_server_sdk::{
input::GetPokemonSpeciesInput,
output::GetPokemonSpeciesOutput,
error::GetPokemonSpeciesError,
operation_shape::GetPokemonSpecies
operation::GetPokemonSpecies
};
use aws_smithy_http_server::operation::OperationShapeExt;

Expand All @@ -192,7 +192,7 @@ use pokemon_service_server_sdk::{
input::GetPokemonSpeciesInput,
output::GetPokemonSpeciesOutput,
error::GetPokemonSpeciesError,
operation_shape::GetPokemonSpecies
operation::GetPokemonSpecies
};
use aws_smithy_http_server::operation::OperationShapeExt;
use std::task::{Context, Poll};
Expand Down
2 changes: 1 addition & 1 deletion design/src/server/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This is enabled via the `instrument` method provided by the `aws_smithy_http_ser
```rust,no_run
# extern crate aws_smithy_http_server;
# extern crate pokemon_service_server_sdk;
# use pokemon_service_server_sdk::{operation_shape::GetPokemonSpecies, input::*, output::*, error::*};
# use pokemon_service_server_sdk::{operation::GetPokemonSpecies, input::*, output::*, error::*};
# let handler = |req: GetPokemonSpeciesInput| async { Result::<GetPokemonSpeciesOutput, GetPokemonSpeciesError>::Ok(todo!()) };
use aws_smithy_http_server::{
instrumentation::InstrumentExt,
Expand Down
6 changes: 3 additions & 3 deletions design/src/server/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ A "HTTP layer" can be applied to specific operations.
# extern crate aws_smithy_http_server;
# use tower::{util::service_fn, Layer};
# use std::time::Duration;
# use pokemon_service_server_sdk::{operation_shape::GetPokemonSpecies, input::*, output::*, error::*};
# use pokemon_service_server_sdk::{operation::GetPokemonSpecies, input::*, output::*, error::*};
# use aws_smithy_http_server::{operation::OperationShapeExt, plugin::*, operation::*};
# let handler = |req: GetPokemonSpeciesInput| async { Result::<GetPokemonSpeciesOutput, GetPokemonSpeciesError>::Ok(todo!()) };
# struct LoggingLayer;
Expand Down Expand Up @@ -247,7 +247,7 @@ A "model layer" can be applied to specific operations.
# extern crate pokemon_service_server_sdk;
# extern crate aws_smithy_http_server;
# use tower::{util::service_fn, Layer};
# use pokemon_service_server_sdk::{operation_shape::GetPokemonSpecies, input::*, output::*, error::*};
# use pokemon_service_server_sdk::{operation::GetPokemonSpecies, input::*, output::*, error::*};
# let handler = |req: GetPokemonSpeciesInput| async { Result::<GetPokemonSpeciesOutput, GetPokemonSpeciesError>::Ok(todo!()) };
# use aws_smithy_http_server::{operation::*, plugin::*};
# struct BufferLayer;
Expand Down Expand Up @@ -406,7 +406,7 @@ This allows for:
# impl<P, Op, S> Plugin<P, Op, S> for PrintPlugin { type Service = S; fn apply(&self, svc: S) -> Self::Service { svc }}
# trait PrintExt<EP> { fn print(self) -> PluginPipeline<PluginStack<PrintPlugin, EP>>; }
# impl<EP> PrintExt<EP> for PluginPipeline<EP> { fn print(self) -> PluginPipeline<PluginStack<PrintPlugin, EP>> { self.push(PrintPlugin) }}
# use pokemon_service_server_sdk::{operation_shape::GetPokemonSpecies, input::*, output::*, error::*};
# use pokemon_service_server_sdk::{operation::GetPokemonSpecies, input::*, output::*, error::*};
# let handler = |req: GetPokemonSpeciesInput| async { Result::<GetPokemonSpeciesOutput, GetPokemonSpeciesError>::Ok(todo!()) };
use aws_smithy_http_server::plugin::{IdentityPlugin, PluginPipeline};
use pokemon_service_server_sdk::PokemonService;
Expand Down