Skip to content

Commit a40dbfc

Browse files
authored
Reorganize credentials, endpoints, region, and op customization (#2393)
* Move `Endpoint` re-export into `config` * Move `Credentials` and `Region` * Move `customize` into `crate::client` * Clean up `RustModule`
1 parent 86bddca commit a40dbfc

File tree

11 files changed

+74
-112
lines changed

11 files changed

+74
-112
lines changed

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsFluentClientDecorator.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import software.amazon.smithy.codegen.core.Symbol
99
import software.amazon.smithy.model.shapes.ShapeId
1010
import software.amazon.smithy.model.traits.TitleTrait
1111
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
12+
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
1213
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
13-
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.CustomizableOperationGenerator
14+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedCustomizeModule
1415
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientCustomization
1516
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerator
1617
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerics
@@ -102,10 +103,10 @@ class AwsFluentClientDecorator : ClientCodegenDecorator {
102103
),
103104
retryClassifier = AwsRuntimeType.awsHttp(runtimeConfig).resolve("retry::AwsResponseRetryClassifier"),
104105
).render(rustCrate)
105-
rustCrate.withModule(CustomizableOperationGenerator.CustomizeModule) {
106+
rustCrate.withModule(codegenContext.featureGatedCustomizeModule()) {
106107
renderCustomizableOperationSendMethod(runtimeConfig, generics, this)
107108
}
108-
rustCrate.withModule(FluentClientGenerator.clientModule) {
109+
rustCrate.withModule(ClientRustModule.client) {
109110
AwsFluentClientExtensions(types).render(this)
110111
}
111112
val awsSmithyClient = "aws-smithy-client"

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/BaseRequestIdDecorator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ package software.amazon.smithy.rustsdk
77

88
import software.amazon.smithy.model.shapes.OperationShape
99
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
10+
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
1011
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
1112
import software.amazon.smithy.rust.codegen.client.smithy.generators.error.ErrorCustomization
1213
import software.amazon.smithy.rust.codegen.client.smithy.generators.error.ErrorSection
13-
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
1414
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
1515
import software.amazon.smithy.rust.codegen.core.rustlang.rust
1616
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
@@ -66,7 +66,7 @@ abstract class BaseRequestIdDecorator : ClientCodegenDecorator {
6666
): List<BuilderCustomization> = baseCustomizations + listOf(RequestIdBuilderCustomization())
6767

6868
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
69-
rustCrate.withModule(RustModule.Types) {
69+
rustCrate.withModule(ClientRustModule.Types) {
7070
// Re-export RequestId in generated crate
7171
rust("pub use #T;", accessorTrait(codegenContext))
7272
}

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ package software.amazon.smithy.rustsdk
88
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
99
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
1010
import software.amazon.smithy.rust.codegen.client.smithy.customize.TestUtilFeature
11+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedConfigModule
1112
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
1213
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
13-
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
1414
import software.amazon.smithy.rust.codegen.core.rustlang.rust
1515
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1616
import software.amazon.smithy.rust.codegen.core.rustlang.writable
@@ -19,8 +19,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
1919
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
2020
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
2121
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
22-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
23-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
2422

2523
class CredentialsProviderDecorator : ClientCodegenDecorator {
2624
override val name: String = "CredentialsProvider"
@@ -33,13 +31,6 @@ class CredentialsProviderDecorator : ClientCodegenDecorator {
3331
return baseCustomizations + CredentialProviderConfig(codegenContext.runtimeConfig)
3432
}
3533

36-
override fun libRsCustomizations(
37-
codegenContext: ClientCodegenContext,
38-
baseCustomizations: List<LibRsCustomization>,
39-
): List<LibRsCustomization> {
40-
return baseCustomizations + PubUseCredentials(codegenContext.runtimeConfig)
41-
}
42-
4334
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> =
4435
listOf(
4536
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
@@ -49,6 +40,13 @@ class CredentialsProviderDecorator : ClientCodegenDecorator {
4940

5041
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
5142
rustCrate.mergeFeature(TestUtilFeature.copy(deps = listOf("aws-credential-types/test-util")))
43+
44+
rustCrate.withModule(codegenContext.featureGatedConfigModule()) {
45+
rust(
46+
"pub use #T::Credentials;",
47+
AwsRuntimeType.awsCredentialTypes(codegenContext.runtimeConfig),
48+
)
49+
}
5250
}
5351
}
5452

@@ -95,20 +93,5 @@ class CredentialProviderConfig(runtimeConfig: RuntimeConfig) : ConfigCustomizati
9593
}
9694
}
9795

98-
class PubUseCredentials(private val runtimeConfig: RuntimeConfig) : LibRsCustomization() {
99-
override fun section(section: LibRsSection): Writable {
100-
return when (section) {
101-
is LibRsSection.Body -> writable {
102-
rust(
103-
"pub use #T::Credentials;",
104-
AwsRuntimeType.awsCredentialTypes(runtimeConfig),
105-
)
106-
}
107-
108-
else -> emptySection
109-
}
110-
}
111-
}
112-
11396
fun defaultProvider() =
11497
RuntimeType.forInlineDependency(InlineAwsDependency.forRustFile("no_credentials")).resolve("NoCredentials")

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/RegionDecorator.kt

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter
1212
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
1313
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
1414
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization
15+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedConfigModule
1516
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
1617
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
1718
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
@@ -20,12 +21,11 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
2021
import software.amazon.smithy.rust.codegen.core.rustlang.writable
2122
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
2223
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
24+
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
2325
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
2426
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization
2527
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection
2628
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
27-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
28-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
2929
import software.amazon.smithy.rust.codegen.core.util.dq
3030
import software.amazon.smithy.rust.codegen.core.util.extendIf
3131
import software.amazon.smithy.rust.codegen.core.util.thenSingletonListOf
@@ -101,13 +101,6 @@ class RegionDecorator : ClientCodegenDecorator {
101101
return baseCustomizations.extendIf(usesRegion(codegenContext)) { RegionConfigPlugin() }
102102
}
103103

104-
override fun libRsCustomizations(
105-
codegenContext: ClientCodegenContext,
106-
baseCustomizations: List<LibRsCustomization>,
107-
): List<LibRsCustomization> {
108-
return baseCustomizations.extendIf(usesRegion(codegenContext)) { PubUseRegion(codegenContext.runtimeConfig) }
109-
}
110-
111104
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> {
112105
return usesRegion(codegenContext).thenSingletonListOf {
113106
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section ->
@@ -121,6 +114,14 @@ class RegionDecorator : ClientCodegenDecorator {
121114
}
122115
}
123116

117+
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
118+
if (usesRegion(codegenContext)) {
119+
rustCrate.withModule(codegenContext.featureGatedConfigModule()) {
120+
rust("pub use #T::Region;", region(codegenContext.runtimeConfig))
121+
}
122+
}
123+
}
124+
124125
override fun endpointCustomizations(codegenContext: ClientCodegenContext): List<EndpointCustomization> {
125126
if (!usesRegion(codegenContext)) {
126127
return listOf()
@@ -221,19 +222,4 @@ class RegionConfigPlugin : OperationCustomization() {
221222
}
222223
}
223224

224-
class PubUseRegion(private val runtimeConfig: RuntimeConfig) : LibRsCustomization() {
225-
override fun section(section: LibRsSection): Writable {
226-
return when (section) {
227-
is LibRsSection.Body -> writable {
228-
rust(
229-
"pub use #T::Region;",
230-
region(runtimeConfig),
231-
)
232-
}
233-
234-
else -> emptySection
235-
}
236-
}
237-
}
238-
239225
fun region(runtimeConfig: RuntimeConfig) = AwsRuntimeType.awsTypes(runtimeConfig).resolve("region")

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/endpoints/AwsEndpointDecorator.kt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
1818
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
1919
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointTypesGenerator
2020
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointsModule
21+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedConfigModule
2122
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
2223
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
2324
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
@@ -27,12 +28,9 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust
2728
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
2829
import software.amazon.smithy.rust.codegen.core.rustlang.writable
2930
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
30-
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
3131
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
3232
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
3333
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
34-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
35-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
3634
import software.amazon.smithy.rust.codegen.core.util.extendIf
3735
import software.amazon.smithy.rust.codegen.core.util.letIf
3836
import software.amazon.smithy.rust.codegen.core.util.thenSingletonListOf
@@ -91,14 +89,14 @@ class AwsEndpointDecorator : ClientCodegenDecorator {
9189
}
9290
}
9391

94-
override fun libRsCustomizations(
95-
codegenContext: ClientCodegenContext,
96-
baseCustomizations: List<LibRsCustomization>,
97-
): List<LibRsCustomization> {
98-
return baseCustomizations + PubUseEndpoint(codegenContext.runtimeConfig)
99-
}
100-
10192
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
93+
rustCrate.withModule(codegenContext.featureGatedConfigModule()) {
94+
rust(
95+
"pub use #T::endpoint::Endpoint;",
96+
CargoDependency.smithyHttp(codegenContext.runtimeConfig).toType(),
97+
)
98+
}
99+
102100
val epTypes = EndpointTypesGenerator.fromContext(codegenContext)
103101
if (epTypes.defaultResolver() == null) {
104102
throw CodegenException(
@@ -257,21 +255,6 @@ class AwsEndpointDecorator : ClientCodegenDecorator {
257255
}
258256
}
259257
}
260-
261-
class PubUseEndpoint(private val runtimeConfig: RuntimeConfig) : LibRsCustomization() {
262-
override fun section(section: LibRsSection): Writable {
263-
return when (section) {
264-
is LibRsSection.Body -> writable {
265-
rust(
266-
"pub use #T::endpoint::Endpoint;",
267-
CargoDependency.smithyHttp(runtimeConfig).toType(),
268-
)
269-
}
270-
271-
else -> emptySection
272-
}
273-
}
274-
}
275258
}
276259

277260
fun ClientCodegenContext.isRegionalized() = getBuiltIn(Builtins.REGION) != null

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import software.amazon.smithy.rust.codegen.core.util.toSnakeCase
2727
* Modules for code generated client crates.
2828
*/
2929
object ClientRustModule {
30+
/** crate */
31+
val root = RustModule.LibRs
32+
3033
/** crate::client */
3134
val client = Client.self
3235

@@ -118,3 +121,19 @@ object OldModuleSchemeClientModuleProvider : ModuleProvider {
118121
eventStream: UnionShape,
119122
): RustModule.LeafModule = ClientRustModule.Error
120123
}
124+
125+
// TODO(CrateReorganization): Remove when cleaning up `enableNewCrateOrganizationScheme`
126+
fun ClientCodegenContext.featureGatedConfigModule() = when (settings.codegenConfig.enableNewCrateOrganizationScheme) {
127+
true -> ClientRustModule.Config
128+
else -> ClientRustModule.root
129+
}
130+
131+
// TODO(CrateReorganization): Remove when cleaning up `enableNewCrateOrganizationScheme`
132+
fun ClientCodegenContext.featureGatedCustomizeModule() = when (settings.codegenConfig.enableNewCrateOrganizationScheme) {
133+
true -> ClientRustModule.Client.customize
134+
else -> RustModule.public(
135+
"customize",
136+
"Operation customization and supporting types",
137+
parent = ClientRustModule.Operation,
138+
)
139+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package software.amazon.smithy.rust.codegen.client.smithy.generators
77

88
import software.amazon.smithy.model.shapes.StringShape
9+
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
910
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
1011
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
1112
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
@@ -86,7 +87,7 @@ data class InfallibleEnumType(
8687
}
8788

8889
private fun unknownVariantValue(context: EnumGeneratorContext): RuntimeType {
89-
return RuntimeType.forInlineFun(UnknownVariantValue, RustModule.Types) {
90+
return RuntimeType.forInlineFun(UnknownVariantValue, ClientRustModule.Types) {
9091
docs(
9192
"""
9293
Opaque struct used as inner data for the `Unknown` variant defined in enums in
@@ -167,4 +168,4 @@ data class InfallibleEnumType(
167168
}
168169

169170
class ClientEnumGenerator(codegenContext: CodegenContext, shape: StringShape) :
170-
EnumGenerator(codegenContext.model, codegenContext.symbolProvider, shape, InfallibleEnumType(RustModule.Types))
171+
EnumGenerator(codegenContext.model, codegenContext.symbolProvider, shape, InfallibleEnumType(ClientRustModule.Types))

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
package software.amazon.smithy.rust.codegen.client.smithy.generators.client
77

8+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
9+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedCustomizeModule
810
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
911
import software.amazon.smithy.rust.codegen.core.rustlang.GenericTypeArg
1012
import software.amazon.smithy.rust.codegen.core.rustlang.RustGenerics
11-
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
1213
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
13-
import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
1414
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
15-
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
1615
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
1716
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
1817

@@ -21,20 +20,16 @@ import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
2120
* fluent client builders.
2221
*/
2322
class CustomizableOperationGenerator(
24-
private val runtimeConfig: RuntimeConfig,
23+
private val codegenContext: ClientCodegenContext,
2524
private val generics: FluentClientGenerics,
26-
private val includeFluentClient: Boolean,
2725
) {
28-
29-
companion object {
30-
val CustomizeModule = RustModule.public("customize", "Operation customization and supporting types", parent = RustModule.operation(Visibility.PUBLIC))
31-
}
32-
26+
private val includeFluentClient = codegenContext.settings.codegenConfig.includeFluentClient
27+
private val runtimeConfig = codegenContext.runtimeConfig
3328
private val smithyHttp = CargoDependency.smithyHttp(runtimeConfig).toType()
3429
private val smithyTypes = CargoDependency.smithyTypes(runtimeConfig).toType()
3530

3631
fun render(crate: RustCrate) {
37-
crate.withModule(CustomizeModule) {
32+
crate.withModule(codegenContext.featureGatedCustomizeModule()) {
3833
rustTemplate(
3934
"""
4035
pub use #{Operation};
@@ -67,6 +62,7 @@ class CustomizableOperationGenerator(
6762
"handle_generics_bounds" to handleGenerics.bounds(),
6863
"operation_generics_decl" to operationGenerics.declaration(),
6964
"combined_generics_decl" to combinedGenerics.declaration(),
65+
"customize_module" to codegenContext.featureGatedCustomizeModule(),
7066
)
7167

7268
writer.rustTemplate(
@@ -81,7 +77,7 @@ class CustomizableOperationGenerator(
8177
8278
/// A wrapper type for [`Operation`](aws_smithy_http::operation::Operation)s that allows for
8379
/// customization of the operation before it is sent. A `CustomizableOperation` may be sent
84-
/// by calling its [`.send()`][crate::operation::customize::CustomizableOperation::send] method.
80+
/// by calling its [`.send()`][#{customize_module}::CustomizableOperation::send] method.
8581
##[derive(Debug)]
8682
pub struct CustomizableOperation#{combined_generics_decl:W} {
8783
pub(crate) handle: Arc<Handle#{handle_generics_decl:W}>,

0 commit comments

Comments
 (0)