1
1
/*
2
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
5
6
- package software.amazon.smithy.rust.codegen.client.smithy
6
+ package software.amazon.smithy.rust.codegen.client.smithy
7
7
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
- }
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
+ }
0 commit comments