Unverified Commit 41b938df authored by david-perez's avatar david-perez Committed by GitHub
Browse files

Squash assorted Clippy and Rust doc warnings in codegen integration tests (#3684)

This commit gets rid of some Clippy and Rust doc warnings we produce in
generated code, which are surfaced in our codegen integration tests. The
aim is that eventually we will be able to deny Clippy and Rust doc
warnings in #3678 (despite us baking in `RUSTDOCFLAGS="-D warnings"` and
`RUSTFLAGS="-D warnings"` in our CI Docker image, we curently clear
these in codegen integration tests). Note that denying compiler warnings
is separately tracked in #3194.

The commit contains fixes for the following:

- Unconditionally referring to the `crate::types` module in Rust docs
when
  the Smithy model is such that it is not generated.
- Broken intra-crate doc links for client documentation.
- Incorrectly escaping Rust reserved keywords when referring to
  operation names in Rust docs.
- An unnecessary semi-colon when rendering additional client
  plugins.
- Not escaping brackets in text when rendering Rust docs containing
  HTML, that are interpreted as broken links.
- A broken link to unit variants of unions.
- Using `TryFrom` instead of `From` when infallibly converting from
  `&str` to `String` when deserializing an `@httpPayload` in the server.
- Using a redundant closure with `unwrap_or_else` when falling back to a
  `@default` boolean or number value for a `document` shape, instead of
  `unwrap_or`.
- Not opting into `clippy::enum_variant_names` when rendering the
  `Operation` enum in the server (since we render the operation names
  that were modeled as-is).

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent b7f12a67
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -7,10 +7,13 @@ package software.amazon.smithy.rust.codegen.client.smithy

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.EnumShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.model.traits.ErrorTrait
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDocs
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientGenerator
@@ -195,8 +198,9 @@ object ClientModuleProvider : ModuleProvider {
    override fun moduleForShape(
        context: ModuleProviderContext,
        shape: Shape,
    ): RustModule.LeafModule =
        when (shape) {
    ): RustModule.LeafModule {
        fun shouldNotBeRendered(): Nothing = PANIC("Shape ${shape.id} should not be rendered in any module")
        return when (shape) {
            is OperationShape -> perOperationModule(context, shape)
            is StructureShape ->
                when {
@@ -206,7 +210,17 @@ object ClientModuleProvider : ModuleProvider {
                    else -> ClientRustModule.types
                }

            else -> ClientRustModule.types
            is UnionShape, is EnumShape -> ClientRustModule.types
            is StringShape -> {
                if (shape.hasTrait<EnumTrait>()) {
                    ClientRustModule.types
                } else {
                    shouldNotBeRendered()
                }
            }

            else -> shouldNotBeRendered()
        }
    }

    override fun moduleForOperationError(
+19 −1
Original line number Diff line number Diff line
@@ -7,9 +7,11 @@ package software.amazon.smithy.rust.codegen.client.smithy.customizations

import software.amazon.smithy.model.traits.TitleTrait
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.containerDocs
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.DirectedWalker
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsCustomization
import software.amazon.smithy.rust.codegen.core.smithy.generators.LibRsSection
import software.amazon.smithy.rust.codegen.core.smithy.generators.ModuleDocSection
@@ -30,6 +32,22 @@ class ClientDocsGenerator(private val codegenContext: ClientCodegenContext) : Li

    private fun crateLayout(): Writable =
        writable {
            val hasTypesModule =
                DirectedWalker(codegenContext.model).walkShapes(codegenContext.serviceShape)
                    .any {
                        try {
                            codegenContext.symbolProvider.moduleForShape(it).name == ClientRustModule.types.name
                        } catch (ex: RuntimeException) {
                            // The shape should not be rendered in any module.
                            false
                        }
                    }
            val typesModuleSentence =
                if (hasTypesModule) {
                    "These structs and enums live in [`types`](crate::types). "
                } else {
                    ""
                }
            val serviceName = codegenContext.serviceShape.getTrait<TitleTrait>()?.value ?: "the service"
            containerDocs(
                """
@@ -40,7 +58,7 @@ class ClientDocsGenerator(private val codegenContext: ClientCodegenContext) : Li
                either a successful output or a [`SdkError`](crate::error::SdkError).

                Some of these API inputs may be structs or enums to provide more complex structured information.
                These structs and enums live in [`types`](crate::types). There are some simpler types for
                ${typesModuleSentence}There are some simpler types for
                representing data such as date times or binary blobs that live in [`primitives`](crate::primitives).

                All types required to configure a client via the [`Config`](crate::Config) struct live
+3 −3
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@ object FluentClientDocs {
                or identity resolver to be configured. The config is used to customize various aspects of the client,
                such as:

                  - [HTTP Connector](crate::config::Builder::http_connector)
                  - [Retry](crate::config::Builder::retry_config)
                  - [The underlying HTTP client](crate::config::Builder::http_client)
                  - [Retries](crate::config::Builder::retry_config)
                  - [Timeouts](crate::config::Builder::timeout_config)
                  - [... and more](crate::config::Builder)

@@ -76,7 +76,7 @@ object FluentClientDocs {
                if (operation != null && member != null) {
                    val operationSymbol = symbolProvider.toSymbol(operation)
                    val memberSymbol = symbolProvider.toSymbol(member)
                    val operationFnName = FluentClientGenerator.clientOperationFnName(operation, symbolProvider)
                    val operationFnName = FluentClientGenerator.clientOperationFnDocsName(operation, symbolProvider)
                    docsTemplate(
                        """
                        ## Using the `Client`
+8 −2
Original line number Diff line number Diff line
@@ -59,7 +59,13 @@ class FluentClientGenerator(
        fun clientOperationFnName(
            operationShape: OperationShape,
            symbolProvider: RustSymbolProvider,
        ): String = RustReservedWords.escapeIfNeeded(symbolProvider.toSymbol(operationShape).name.toSnakeCase())
        ): String = RustReservedWords.escapeIfNeeded(clientOperationFnDocsName(operationShape, symbolProvider))

        /** When using the function name in Rust docs, there's no need to escape Rust reserved words. **/
        fun clientOperationFnDocsName(
            operationShape: OperationShape,
            symbolProvider: RustSymbolProvider,
        ): String = symbolProvider.toSymbol(operationShape).name.toSnakeCase()

        fun clientOperationModuleName(
            operationShape: OperationShape,
@@ -277,7 +283,7 @@ private fun baseClientRuntimePluginsFn(
                        .with_client_plugin(crate::config::ServiceRuntimePlugin::new(config.clone()))
                        .with_client_plugin(#{NoAuthRuntimePlugin}::new());

                    #{additional_client_plugins:W};
                    #{additional_client_plugins:W}

                    for plugin in configured_plugins {
                        plugins = plugins.with_client_plugin(plugin);
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ use smithy.framework#ValidationException

/// A service to test miscellaneous aspects of code generation where protocol
/// selection is not relevant. If you want to test something protocol-specific,
/// add it to a separate `<protocol>-extras.smithy`.
/// add it to a separate `[protocol]-extras.smithy`.
@restJson1
@title("MiscService")
service MiscService {
Loading