Skip to content

Commit 726cdfe

Browse files
committed
Move AppName and PKG_VERSION
1 parent 52b07c2 commit 726cdfe

File tree

10 files changed

+89
-52
lines changed

10 files changed

+89
-52
lines changed

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

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import software.amazon.smithy.aws.traits.ServiceTrait
99
import software.amazon.smithy.model.shapes.OperationShape
1010
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
1111
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
12+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedConfigModule
13+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedMetaModule
1214
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
1315
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
1416
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
1517
import software.amazon.smithy.rust.codegen.core.rustlang.rust
1618
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1719
import software.amazon.smithy.rust.codegen.core.rustlang.writable
1820
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
21+
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
22+
import software.amazon.smithy.rust.codegen.core.smithy.customizations.CrateVersionCustomization
1923
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization
2024
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization
2125
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection
2226
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization
23-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
24-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
2527
import software.amazon.smithy.rust.codegen.core.util.dq
2628
import software.amazon.smithy.rust.codegen.core.util.expectTrait
2729

@@ -39,21 +41,12 @@ class UserAgentDecorator : ClientCodegenDecorator {
3941
return baseCustomizations + AppNameCustomization(codegenContext.runtimeConfig)
4042
}
4143

42-
override fun libRsCustomizations(
43-
codegenContext: ClientCodegenContext,
44-
baseCustomizations: List<LibRsCustomization>,
45-
): List<LibRsCustomization> {
46-
// We are generating an AWS SDK, the service needs to have the AWS service trait
47-
val serviceTrait = codegenContext.serviceShape.expectTrait<ServiceTrait>()
48-
return baseCustomizations + ApiVersionAndPubUse(codegenContext.runtimeConfig, serviceTrait)
49-
}
50-
5144
override fun operationCustomizations(
5245
codegenContext: ClientCodegenContext,
5346
operation: OperationShape,
5447
baseCustomizations: List<OperationCustomization>,
5548
): List<OperationCustomization> {
56-
return baseCustomizations + UserAgentFeature(codegenContext.runtimeConfig)
49+
return baseCustomizations + UserAgentFeature(codegenContext)
5750
}
5851

5952
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> {
@@ -65,44 +58,54 @@ class UserAgentDecorator : ClientCodegenDecorator {
6558
}
6659

6760
/**
68-
* Adds a static `API_METADATA` variable to the crate root containing the serviceId & the version of the crate for this individual service
61+
* Adds a static `API_METADATA` variable to the crate `config` containing the serviceId & the version of the crate for this individual service
6962
*/
70-
private class ApiVersionAndPubUse(private val runtimeConfig: RuntimeConfig, serviceTrait: ServiceTrait) :
71-
LibRsCustomization() {
72-
private val serviceId = serviceTrait.sdkId.lowercase().replace(" ", "")
73-
override fun section(section: LibRsSection): Writable = when (section) {
74-
is LibRsSection.Body -> writable {
75-
// PKG_VERSION comes from CrateVersionGenerator
76-
rust(
77-
"static API_METADATA: #1T::ApiMetadata = #1T::ApiMetadata::new(${serviceId.dq()}, PKG_VERSION);",
78-
AwsRuntimeType.awsHttp(runtimeConfig).resolve("user_agent"),
79-
)
63+
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
64+
val runtimeConfig = codegenContext.runtimeConfig
8065

81-
// Re-export the app name so that it can be specified in config programmatically without an explicit dependency
82-
rustTemplate(
83-
"pub use #{AppName};",
84-
"AppName" to AwsRuntimeType.awsTypes(runtimeConfig).resolve("app_name::AppName"),
85-
)
86-
}
66+
// We are generating an AWS SDK, the service needs to have the AWS service trait
67+
val serviceTrait = codegenContext.serviceShape.expectTrait<ServiceTrait>()
68+
val serviceId = serviceTrait.sdkId.lowercase().replace(" ", "")
69+
70+
rustCrate.withModule(codegenContext.featureGatedMetaModule()) {
71+
rustTemplate(
72+
"""
73+
pub(crate) static API_METADATA: #{user_agent}::ApiMetadata =
74+
#{user_agent}::ApiMetadata::new(${serviceId.dq()}, #{PKG_VERSION});
75+
""",
76+
"user_agent" to AwsRuntimeType.awsHttp(runtimeConfig).resolve("user_agent"),
77+
"PKG_VERSION" to CrateVersionCustomization.pkgVersion(codegenContext.featureGatedMetaModule()),
78+
)
79+
}
8780

88-
else -> emptySection
81+
rustCrate.withModule(codegenContext.featureGatedConfigModule()) {
82+
// Re-export the app name so that it can be specified in config programmatically without an explicit dependency
83+
rustTemplate(
84+
"pub use #{AppName};",
85+
"AppName" to AwsRuntimeType.awsTypes(runtimeConfig).resolve("app_name::AppName"),
86+
)
8987
}
9088
}
9189

92-
private class UserAgentFeature(private val runtimeConfig: RuntimeConfig) : OperationCustomization() {
90+
private class UserAgentFeature(
91+
private val codegenContext: ClientCodegenContext,
92+
) : OperationCustomization() {
93+
private val runtimeConfig = codegenContext.runtimeConfig
94+
9395
override fun section(section: OperationSection): Writable = when (section) {
9496
is OperationSection.MutateRequest -> writable {
9597
rustTemplate(
9698
"""
9799
let mut user_agent = #{ua_module}::AwsUserAgent::new_from_environment(
98100
#{Env}::real(),
99-
crate::API_METADATA.clone(),
101+
#{meta}::API_METADATA.clone(),
100102
);
101103
if let Some(app_name) = _config.app_name() {
102104
user_agent = user_agent.with_app_name(app_name.clone());
103105
}
104106
${section.request}.properties_mut().insert(user_agent);
105107
""",
108+
"meta" to codegenContext.featureGatedMetaModule(),
106109
"ua_module" to AwsRuntimeType.awsHttp(runtimeConfig).resolve("user_agent"),
107110
"Env" to AwsRuntimeType.awsTypes(runtimeConfig).resolve("os_shim_internal::Env"),
108111
)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import software.amazon.smithy.rust.codegen.core.util.hasTrait
2020
* Modules for code generated client crates.
2121
*/
2222
object ClientRustModule {
23+
/** crate */
24+
val root = RustModule.LibRs
25+
2326
/** crate::client */
2427
val client = Client.self
2528
object Client {
@@ -33,6 +36,7 @@ object ClientRustModule {
3336
val Config = RustModule.public("config", documentation = "Configuration for the service.")
3437
val Error = RustModule.public("error", documentation = "All error types that operations can return. Documentation on these types is copied from the model.")
3538
val Operation = RustModule.public("operation", documentation = "All operations that this crate can perform.")
39+
val Meta = RustModule.public("meta", documentation = "Information about this crate.")
3640
val Model = RustModule.public("model", documentation = "Data structures used by operation inputs/outputs. Documentation on these types is copied from the model.")
3741
val Input = RustModule.public("input", documentation = "Input structures for operations. Documentation on these types is copied from the model.")
3842
val Output = RustModule.public("output", documentation = "Output structures for operations. Documentation on these types is copied from the model.")
@@ -57,3 +61,15 @@ object ClientModuleProvider : ModuleProvider {
5761
override fun moduleForEventStreamError(eventStream: UnionShape): RustModule.LeafModule =
5862
ClientRustModule.Error
5963
}
64+
65+
// TODO(CrateReorganization): Remove when cleaning up `enableNewCrateOrganizationScheme`
66+
fun ClientCodegenContext.featureGatedConfigModule() = when (settings.codegenConfig.enableNewCrateOrganizationScheme) {
67+
true -> ClientRustModule.Config
68+
else -> ClientRustModule.root
69+
}
70+
71+
// TODO(CrateReorganization): Remove when cleaning up `enableNewCrateOrganizationScheme`
72+
fun ClientCodegenContext.featureGatedMetaModule() = when (settings.codegenConfig.enableNewCrateOrganizationScheme) {
73+
true -> ClientRustModule.Meta
74+
else -> ClientRustModule.root
75+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpVers
1414
import software.amazon.smithy.rust.codegen.client.smithy.customizations.IdempotencyTokenGenerator
1515
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyConfigCustomization
1616
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyReExportCustomization
17+
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedMetaModule
1718
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
1819
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
1920
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
@@ -55,7 +56,7 @@ class RequiredCustomizations : ClientCodegenDecorator {
5556
codegenContext: ClientCodegenContext,
5657
baseCustomizations: List<LibRsCustomization>,
5758
): List<LibRsCustomization> =
58-
baseCustomizations + CrateVersionCustomization() + AllowLintsCustomization()
59+
baseCustomizations + AllowLintsCustomization()
5960

6061
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
6162
// Add rt-tokio feature for `ByteStream::from_path`
@@ -69,5 +70,11 @@ class RequiredCustomizations : ClientCodegenDecorator {
6970
rustCrate.withModule(ClientRustModule.Types) {
7071
pubUseSmithyTypes(codegenContext, codegenContext.model)(this)
7172
}
73+
74+
codegenContext.featureGatedMetaModule().also { metaModule ->
75+
rustCrate.withModule(metaModule) {
76+
CrateVersionCustomization.extras(rustCrate, metaModule)
77+
}
78+
}
7279
}
7380
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.generators.protocol
88
import software.amazon.smithy.aws.traits.ServiceTrait
99
import software.amazon.smithy.model.shapes.BlobShape
1010
import software.amazon.smithy.model.shapes.OperationShape
11+
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
1112
import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RequestBindingGenerator
1213
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
1314
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
@@ -54,7 +55,7 @@ open class MakeOperationGenerator(
5455
?: codegenContext.serviceShape.id.getName(codegenContext.serviceShape)
5556

5657
private val codegenScope = arrayOf(
57-
"config" to RuntimeType.Config,
58+
"config" to ClientRustModule.Config,
5859
"header_util" to RuntimeType.smithyHttp(runtimeConfig).resolve("header"),
5960
"http" to RuntimeType.Http,
6061
"HttpRequestBuilder" to RuntimeType.HttpRequestBuilder,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import software.amazon.smithy.protocoltests.traits.HttpRequestTestsTrait
1919
import software.amazon.smithy.protocoltests.traits.HttpResponseTestCase
2020
import software.amazon.smithy.protocoltests.traits.HttpResponseTestsTrait
2121
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
22+
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
2223
import software.amazon.smithy.rust.codegen.client.smithy.generators.clientInstantiator
2324
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
2425
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.allow
@@ -168,12 +169,12 @@ class ProtocolTestGenerator(
168169
} ?: writable { }
169170
rustTemplate(
170171
"""
171-
let builder = #{Config}::Config::builder().with_test_defaults().endpoint_resolver("https://example.com");
172+
let builder = #{config}::Config::builder().with_test_defaults().endpoint_resolver("https://example.com");
172173
#{customParams}
173174
let config = builder.build();
174175
175176
""",
176-
"Config" to RuntimeType.Config,
177+
"config" to ClientRustModule.Config,
177178
"customParams" to customParams,
178179
)
179180
writeInline("let input =")

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,10 @@ class RustWriter private constructor(
705705
t.fullyQualifiedName()
706706
}
707707

708+
is RustModule -> {
709+
t.fullyQualifiedPath()
710+
}
711+
708712
is Symbol -> {
709713
addDepsRecursively(t)
710714
t.rustType().render(fullyQualified = true)

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ data class RuntimeType(val path: String, val dependency: RustDependency? = null)
241241
val Tracing = CargoDependency.Tracing.toType()
242242

243243
// codegen types
244-
val Config = RuntimeType("crate::config")
245244
val ConstrainedTrait = RuntimeType("crate::constrained::Constrained", InlineDependency.constrained())
246245
val MaybeConstrained = RuntimeType("crate::constrained::MaybeConstrained", InlineDependency.constrained())
247246

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/customizations/CrateVersionCustomization.kt

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

66
package software.amazon.smithy.rust.codegen.core.smithy.customizations
77

8+
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
89
import software.amazon.smithy.rust.codegen.core.rustlang.rust
9-
import software.amazon.smithy.rust.codegen.core.rustlang.writable
10-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
11-
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
10+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
11+
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
1212

1313
/**
1414
* Add `PGK_VERSION` const in lib.rs to enable knowing the version of the current module
1515
*/
16-
class CrateVersionCustomization : LibRsCustomization() {
17-
override fun section(section: LibRsSection) =
18-
writable {
19-
if (section is LibRsSection.Body) {
20-
rust(
21-
"""
22-
/// Crate version number.
23-
pub static PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
24-
""",
25-
)
26-
}
16+
object CrateVersionCustomization {
17+
fun pkgVersion(module: RustModule): RuntimeType = RuntimeType(module.fullyQualifiedPath() + "::PKG_VERSION")
18+
19+
fun extras(rustCrate: RustCrate, module: RustModule) =
20+
rustCrate.withModule(module) {
21+
rust(
22+
"""
23+
/// Crate version number.
24+
pub static PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
25+
""",
26+
)
2727
}
2828
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustModule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import software.amazon.smithy.rust.codegen.core.smithy.traits.SyntheticOutputTra
1717
import software.amazon.smithy.rust.codegen.core.util.hasTrait
1818

1919
object ServerRustModule {
20+
val root = RustModule.LibRs
21+
2022
val Error = RustModule.public("error", documentation = "All error types that operations can return. Documentation on these types is copied from the model.")
2123
val Operation = RustModule.public("operation", documentation = "All operations that this crate can perform.")
2224
val Model = RustModule.public("model", documentation = "Data structures used by operation inputs/outputs. Documentation on these types is copied from the model.")

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/ServerRequiredCustomizations.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ServerRequiredCustomizations : ServerCodegenDecorator {
3030
codegenContext: ServerCodegenContext,
3131
baseCustomizations: List<LibRsCustomization>,
3232
): List<LibRsCustomization> =
33-
baseCustomizations + CrateVersionCustomization() + AllowLintsCustomization()
33+
baseCustomizations + AllowLintsCustomization()
3434

3535
override fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) {
3636
// Add rt-tokio feature for `ByteStream::from_path`
@@ -39,5 +39,9 @@ class ServerRequiredCustomizations : ServerCodegenDecorator {
3939
rustCrate.withModule(ServerRustModule.Types) {
4040
pubUseSmithyTypes(codegenContext, codegenContext.model)(this)
4141
}
42+
43+
rustCrate.withModule(ServerRustModule.root) {
44+
CrateVersionCustomization.extras(rustCrate, ServerRustModule.root)
45+
}
4246
}
4347
}

0 commit comments

Comments
 (0)