Unverified Commit caf52ee6 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Fix docs on union converters (#826)

* Fix docs on union converters

* update changelog

* Fix generation when escaping happens

* clarify docs
parent d2aac628
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ vNext (Month Day, Year)
- `moduleDescription` in `smithy-build.json` settings is now optional
- Upgrade to Smithy 1.12
- `hyper::Error(IncompleteMessage)` will now be retried (smithy-rs#815)
- Fix generated docs on unions. (smithy-rs#826)

v0.27 (October 20th, 2021)
==========================
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ vNext (Month Day, Year)
- :bug: Fix `native-tls` feature in `aws-config` (aws-sdk-rust#265, smithy-rs#803)
- Add example to aws-sig-auth for generating an IAM Token for RDS (smithy-rs#811, aws-sdk-rust#147)
- :bug: `hyper::Error(IncompleteMessage)` will now be retried (smithy-rs#815)
- :bug: Fix generated docs on unions like `dynamodb::AttributeValue`. (smithy-rs#826)

**Breaking Changes**
- `<operation>.make_operation(&config)` is now an `async` function for all operations. Code should be updated to call `.await`. This will only impact users using the low-level API. (smithy-rs#797)
+16 −0
Original line number Diff line number Diff line
@@ -112,6 +112,22 @@ sealed class RustType {
    data class Opaque(override val name: kotlin.String, override val namespace: kotlin.String? = null) : RustType()
}

/**
 * Return the fully qualified name of this type NOT including generic type parameters, references, etc.
 *
 * - To generate something like `std::collections::HashMap`, use this function.
 * - To generate something like `std::collections::HashMap<String, String>`, use [render]
 */
fun RustType.qualifiedName(): String {
    val namespace = this.namespace?.let { "$it::" } ?: ""
    return "$namespace$name"
}

/**
 * Render this type, including references and generic parameters.
 * - To generate something like `std::collections::HashMap<String, String>`, use this function
 * - To generate something like `std::collections::HashMap`, use [qualifiedName]
 */
fun RustType.render(fullyQualified: Boolean = true): String {
    val namespace = if (fullyQualified) {
        this.namespace?.let { "$it::" } ?: ""
+1 −1
Original line number Diff line number Diff line
@@ -397,7 +397,7 @@ class RustWriter private constructor(
    inner class RustDocLinker : BiFunction<Any, String, String> {
        override fun apply(t: Any, u: String): String {
            return when (t) {
                is Symbol -> "[`${t.name}`](${t.fullName})"
                is Symbol -> "[`${t.name}`](${t.rustType().qualifiedName()})"
                else -> throw CodegenException("Invalid type provided to RustDocLinker ($t) expected Symbol")
            }
        }
+4 −4
Original line number Diff line number Diff line
@@ -47,18 +47,18 @@ class UnionGenerator(
        writer.rustBlock("impl ${unionSymbol.name}") {
            sortedMembers.forEach { member ->
                val memberSymbol = symbolProvider.toSymbol(member)
                val funcNamePart = member.memberName.toSnakeCase()
                val variantName = member.memberName.toPascalCase()
                val funcNamePart = member.memberName.toSnakeCase()

                if (sortedMembers.size == 1) {
                    Attribute.Custom("allow(irrefutable_let_patterns)").render(this)
                }
                rust("/// Tries to convert the enum instance into its #D variant.", unionSymbol)
                rust("/// Returns `Err(&Self) if it can't be converted.` ")
                rust("/// Tries to convert the enum instance into [`$variantName`](#T::$variantName), extracting the inner #D.", unionSymbol, memberSymbol)
                rust("/// Returns `Err(&Self)` if it can't be converted.")
                rustBlock("pub fn as_$funcNamePart(&self) -> std::result::Result<&#T, &Self>", memberSymbol) {
                    rust("if let ${unionSymbol.name}::$variantName(val) = &self { Ok(&val) } else { Err(&self) }")
                }
                rust("/// Returns true if the enum instance is the `${unionSymbol.name}` variant.")
                rust("/// Returns true if this is a [`$variantName`](#T::$variantName).", unionSymbol)
                rustBlock("pub fn is_$funcNamePart(&self) -> bool") {
                    rust("self.as_$funcNamePart().is_ok()")
                }