Unverified Commit 7f4152cc authored by ysaito1001's avatar ysaito1001 Committed by GitHub
Browse files

Add more client re-exports (#2437)

* Add more client re-exports

This commit adds more client re-exports proposed in
https://github.com/awslabs/smithy-rs/issues/1759

.
Specifically, it re-exports the following types:
- `aws_smithy_http::body::SdkBody`
- `aws_smithy_http::byte_stream::error::Error`
- `aws_smithy_http::operation::{Request, Response}`

* Update CHANGELOG.next.toml

---------

Co-authored-by: default avatarYuki Saito <awsaito@amazon.com>
parent f474294b
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -251,3 +251,15 @@ message = "Fix `FilterByOperationName` plugin. This previous caused services wit
references = ["smithy-rs#2441"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "server" }
author = "hlbarber"

[[aws-sdk-rust]]
message = "Add more client re-exports. Specifically, it re-exports `aws_smithy_http::body::SdkBody`, `aws_smithy_http::byte_stream::error::Error`, and `aws_smithy_http::operation::{Request, Response}`."
references = ["smithy-rs#2437", "aws-sdk-rust#600"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "ysaito1001"

[[smithy-rs]]
message = "Add more client re-exports. Specifically, it re-exports `aws_smithy_http::body::SdkBody`, `aws_smithy_http::byte_stream::error::Error`, and `aws_smithy_http::operation::{Request, Response}`."
references = ["smithy-rs#2437", "aws-sdk-rust#600"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" }
author = "ysaito1001"
+4 −0
Original line number Diff line number Diff line
@@ -33,10 +33,14 @@ class CustomizableOperationGenerator(
            rustTemplate(
                """
                pub use #{Operation};
                pub use #{Request};
                pub use #{Response};
                pub use #{ClassifyRetry};
                pub use #{RetryKind};
                """,
                "Operation" to smithyHttp.resolve("operation::Operation"),
                "Request" to smithyHttp.resolve("operation::Request"),
                "Response" to smithyHttp.resolve("operation::Response"),
                "ClassifyRetry" to smithyHttp.resolve("retry::ClassifyRetry"),
                "RetryKind" to smithyTypes.resolve("retry::RetryKind"),
            )
+21 −7
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import software.amazon.smithy.rust.codegen.core.util.letIf
private data class PubUseType(
    val type: RuntimeType,
    val shouldExport: (Model) -> Boolean,
    val alias: String? = null,
)

/** Returns true if the model has normal streaming operations (excluding event streams) */
@@ -48,7 +49,10 @@ private fun hasBlobs(model: Model): Boolean = structUnionMembersMatchPredicate(m
private fun hasDateTimes(model: Model): Boolean = structUnionMembersMatchPredicate(model, Shape::isTimestampShape)

/** Returns a list of types that should be re-exported for the given model */
internal fun pubUseTypes(codegenContext: CodegenContext, model: Model): List<RuntimeType> {
internal fun pubUseTypes(codegenContext: CodegenContext, model: Model): List<RuntimeType> =
    pubUseTypesThatShouldBeExported(codegenContext, model).map { it.type }

private fun pubUseTypesThatShouldBeExported(codegenContext: CodegenContext, model: Model): List<PubUseType> {
    val runtimeConfig = codegenContext.runtimeConfig
    return (
        listOf(
@@ -58,16 +62,25 @@ internal fun pubUseTypes(codegenContext: CodegenContext, model: Model): List<Run
            listOf(
                PubUseType(http.resolve("byte_stream::ByteStream"), ::hasStreamingOperations),
                PubUseType(http.resolve("byte_stream::AggregatedBytes"), ::hasStreamingOperations),
                PubUseType(http.resolve("byte_stream::error::Error"), ::hasStreamingOperations, "ByteStreamError"),
                PubUseType(http.resolve("body::SdkBody"), ::hasStreamingOperations),
            )
        }
        ).filter { pubUseType -> pubUseType.shouldExport(model) }.map { it.type }
        ).filter { pubUseType -> pubUseType.shouldExport(model) }
}

/** Adds re-export statements for Smithy primitives */
fun pubUseSmithyPrimitives(codegenContext: CodegenContext, model: Model): Writable = writable {
    val types = pubUseTypes(codegenContext, model)
    val types = pubUseTypesThatShouldBeExported(codegenContext, model)
    if (types.isNotEmpty()) {
        types.forEach { type -> rust("pub use #T;", type) }
        types.forEach {
            val useStatement = if (it.alias == null) {
                "pub use #T;"
            } else {
                "pub use #T as ${it.alias};"
            }
            rust(useStatement, it.type)
        }
    }
}

@@ -77,14 +90,15 @@ fun pubUseSmithyErrorTypes(codegenContext: CodegenContext): Writable = writable
    val reexports = listOf(
        listOf(
            RuntimeType.smithyHttp(runtimeConfig).let { http ->
                PubUseType(http.resolve("result::SdkError")) { true }
                PubUseType(http.resolve("result::SdkError"), { _ -> true })
            },
        ),
        RuntimeType.smithyTypes(runtimeConfig).let { types ->
            listOf(PubUseType(types.resolve("error::display::DisplayErrorContext")) { true })
            listOf(PubUseType(types.resolve("error::display::DisplayErrorContext"), { _ -> true }))
                // Only re-export `ProvideErrorMetadata` for clients
                .letIf(codegenContext.target == CodegenTarget.CLIENT) { list ->
                    list + listOf(PubUseType(types.resolve("error::metadata::ProvideErrorMetadata")) { true })
                    list +
                        listOf(PubUseType(types.resolve("error::metadata::ProvideErrorMetadata"), { _ -> true }))
                }
        },
    ).flatten()