Skip to content

Commit 2c262f9

Browse files
serde decorator
1 parent a85cef6 commit 2c262f9

File tree

2 files changed

+129
-99
lines changed

2 files changed

+129
-99
lines changed

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,105 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package software.amazon.smithy.rust.codegen.client.smithy
6+
package software.amazon.smithy.rust.codegen.client.smithy
77

8-
import software.amazon.smithy.build.PluginContext
9-
import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider
10-
import software.amazon.smithy.model.Model
11-
import software.amazon.smithy.model.shapes.ServiceShape
12-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ApiKeyAuthDecorator
13-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ClientCustomizations
14-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpAuthDecorator
15-
import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpConnectorConfigDecorator
16-
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
17-
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator
18-
import software.amazon.smithy.rust.codegen.client.smithy.customize.NoOpEventStreamSigningDecorator
19-
import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations
20-
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointParamsDecorator
21-
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointsDecorator
22-
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator
23-
import software.amazon.smithy.rust.codegen.client.testutil.ClientDecoratableBuildPlugin
24-
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.NonExhaustive
25-
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider
26-
import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider
27-
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
28-
import software.amazon.smithy.rust.codegen.core.smithy.EventStreamSymbolProvider
29-
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProviderConfig
30-
import software.amazon.smithy.rust.codegen.core.smithy.StreamingShapeMetadataProvider
31-
import software.amazon.smithy.rust.codegen.core.smithy.StreamingShapeSymbolProvider
32-
import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor
33-
import java.util.logging.Level
34-
import java.util.logging.Logger
35-
36-
/**
37-
* Rust Client Codegen Plugin
38-
*
39-
* This is the entrypoint for code generation, triggered by the smithy-build plugin.
40-
* `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which
41-
* enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models.
42-
*/
43-
class RustClientCodegenPlugin : ClientDecoratableBuildPlugin() {
44-
override fun getName(): String = "rust-client-codegen"
45-
46-
override fun executeWithDecorator(
47-
context: PluginContext,
48-
vararg decorator: ClientCodegenDecorator,
49-
) {
50-
// Suppress extremely noisy logs about reserved words
51-
Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF
52-
// Discover `RustCodegenDecorators` on the classpath. `RustCodegenDecorator` returns different types of
53-
// customizations. A customization is a function of:
54-
// - location (e.g. the mutate section of an operation)
55-
// - context (e.g. the of the operation)
56-
// - writer: The active RustWriter at the given location
57-
val codegenDecorator =
58-
CombinedClientCodegenDecorator.fromClasspath(
59-
context,
60-
ClientCustomizations(),
61-
RequiredCustomizations(),
62-
FluentClientDecorator(),
63-
EndpointsDecorator(),
64-
EndpointParamsDecorator(),
65-
NoOpEventStreamSigningDecorator(),
66-
ApiKeyAuthDecorator(),
67-
HttpAuthDecorator(),
68-
HttpConnectorConfigDecorator(),
69-
*decorator,
70-
)
71-
72-
// ClientCodegenVisitor is the main driver of code generation that traverses the model and generates code
73-
ClientCodegenVisitor(context, codegenDecorator).execute()
74-
}
75-
76-
companion object {
77-
/**
78-
* When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider
79-
*
80-
* The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered
81-
* with other symbol providers, documented inline, to handle the full scope of Smithy types.
82-
*/
83-
fun baseSymbolProvider(
84-
settings: ClientRustSettings,
85-
model: Model,
86-
serviceShape: ServiceShape,
87-
rustSymbolProviderConfig: RustSymbolProviderConfig,
88-
codegenDecorator: ClientCodegenDecorator,
89-
) =
90-
SymbolVisitor(settings, model, serviceShape = serviceShape, config = rustSymbolProviderConfig)
91-
// Generate different types for EventStream shapes (e.g. transcribe streaming)
92-
.let { EventStreamSymbolProvider(rustSymbolProviderConfig.runtimeConfig, it, CodegenTarget.CLIENT) }
93-
// Generate `ByteStream` instead of `Blob` for streaming binary shapes (e.g. S3 GetObject)
94-
.let { StreamingShapeSymbolProvider(it) }
95-
// Add Rust attributes (like `#[derive(PartialEq)]`) to generated shapes
96-
.let { BaseSymbolMetadataProvider(it, additionalAttributes = listOf(NonExhaustive)) }
97-
// Streaming shapes need different derives (e.g. they cannot derive `PartialEq`)
98-
.let { StreamingShapeMetadataProvider(it) }
99-
// Rename shapes that clash with Rust reserved words & and other SDK specific features e.g. `send()` cannot
100-
// be the name of an operation input
101-
.let { RustReservedWordSymbolProvider(it, ClientReservedWords) }
102-
// Allows decorators to inject a custom symbol provider
103-
.let { codegenDecorator.symbolProvider(it) }
104-
}
105-
}
8+
import software.amazon.smithy.build.PluginContext
9+
import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider
10+
import software.amazon.smithy.model.Model
11+
import software.amazon.smithy.model.shapes.ServiceShape
12+
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ApiKeyAuthDecorator
13+
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ClientCustomizations
14+
import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpAuthDecorator
15+
import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpConnectorConfigDecorator
16+
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
17+
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator
18+
import software.amazon.smithy.rust.codegen.client.smithy.customize.NoOpEventStreamSigningDecorator
19+
import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations
20+
import software.amazon.smithy.rust.codegen.client.smithy.customize.SerdeDecorator
21+
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointParamsDecorator
22+
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointsDecorator
23+
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator
24+
import software.amazon.smithy.rust.codegen.client.testutil.ClientDecoratableBuildPlugin
25+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.NonExhaustive
26+
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider
27+
import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider
28+
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
29+
import software.amazon.smithy.rust.codegen.core.smithy.EventStreamSymbolProvider
30+
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProviderConfig
31+
import software.amazon.smithy.rust.codegen.core.smithy.StreamingShapeMetadataProvider
32+
import software.amazon.smithy.rust.codegen.core.smithy.StreamingShapeSymbolProvider
33+
import software.amazon.smithy.rust.codegen.core.smithy.SymbolVisitor
34+
import java.util.logging.Level
35+
import java.util.logging.Logger
36+
37+
/**
38+
* Rust Client Codegen Plugin
39+
*
40+
* This is the entrypoint for code generation, triggered by the smithy-build plugin.
41+
* `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which
42+
* enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models.
43+
*/
44+
class RustClientCodegenPlugin : ClientDecoratableBuildPlugin() {
45+
override fun getName(): String = "rust-client-codegen"
46+
47+
override fun executeWithDecorator(
48+
context: PluginContext,
49+
vararg decorator: ClientCodegenDecorator,
50+
) {
51+
// Suppress extremely noisy logs about reserved words
52+
Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF
53+
// Discover `RustCodegenDecorators` on the classpath. `RustCodegenDecorator` returns different types of
54+
// customizations. A customization is a function of:
55+
// - location (e.g. the mutate section of an operation)
56+
// - context (e.g. the of the operation)
57+
// - writer: The active RustWriter at the given location
58+
val codegenDecorator =
59+
CombinedClientCodegenDecorator.fromClasspath(
60+
context,
61+
SerdeDecorator(),
62+
ClientCustomizations(),
63+
RequiredCustomizations(),
64+
FluentClientDecorator(),
65+
EndpointsDecorator(),
66+
EndpointParamsDecorator(),
67+
NoOpEventStreamSigningDecorator(),
68+
ApiKeyAuthDecorator(),
69+
HttpAuthDecorator(),
70+
HttpConnectorConfigDecorator(),
71+
*decorator,
72+
)
73+
74+
// ClientCodegenVisitor is the main driver of code generation that traverses the model and generates code
75+
ClientCodegenVisitor(context, codegenDecorator).execute()
76+
}
77+
78+
companion object {
79+
/**
80+
* When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider
81+
*
82+
* The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered
83+
* with other symbol providers, documented inline, to handle the full scope of Smithy types.
84+
*/
85+
fun baseSymbolProvider(
86+
settings: ClientRustSettings,
87+
model: Model,
88+
serviceShape: ServiceShape,
89+
rustSymbolProviderConfig: RustSymbolProviderConfig,
90+
codegenDecorator: ClientCodegenDecorator,
91+
) =
92+
SymbolVisitor(settings, model, serviceShape = serviceShape, config = rustSymbolProviderConfig)
93+
// Generate different types for EventStream shapes (e.g. transcribe streaming)
94+
.let { EventStreamSymbolProvider(rustSymbolProviderConfig.runtimeConfig, it, CodegenTarget.CLIENT) }
95+
// Generate `ByteStream` instead of `Blob` for streaming binary shapes (e.g. S3 GetObject)
96+
.let { StreamingShapeSymbolProvider(it) }
97+
// Add Rust attributes (like `#[derive(PartialEq)]`) to generated shapes
98+
.let { BaseSymbolMetadataProvider(it, additionalAttributes = listOf(NonExhaustive)) }
99+
// Streaming shapes need different derives (e.g. they cannot derive `PartialEq`)
100+
.let { StreamingShapeMetadataProvider(it) }
101+
// Rename shapes that clash with Rust reserved words & and other SDK specific features e.g. `send()` cannot
102+
// be the name of an operation input
103+
.let { RustReservedWordSymbolProvider(it, ClientReservedWords) }
104+
// Allows decorators to inject a custom symbol provider
105+
.let { codegenDecorator.symbolProvider(it) }
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.client.smithy.customize
7+
8+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
9+
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
10+
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
11+
12+
/**
13+
* This class,
14+
* - Adds serde as a dependency
15+
*
16+
*/
17+
class SerdeDecorator : ClientCodegenDecorator {
18+
override val name: String = "SerdeDecorator"
19+
override val order: Byte = -1
20+
21+
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
22+
fun _feature(feature_name: String, crate_name: String): Feature {
23+
return Feature(feature_name, false, listOf(crate_name + "/" + feature_name))
24+
}
25+
rustCrate.mergeFeature(_feature("serde-serialize", "aws-smithy-types"))
26+
rustCrate.mergeFeature(_feature("serde-deserialize", "aws-smithy-types"))
27+
}
28+
}

0 commit comments

Comments
 (0)