Unverified Commit 9c7307af authored by Zelda Hessler's avatar Zelda Hessler Committed by GitHub
Browse files

Refactor attribute macro codegen (#2126)

* refactor: rust Attribute codegen

* update: post-refactor usage of Attribute in codegen
add: more attribute tests
update: don't render attributes missing required args

* fix: Python Server codegen broken by Attribute refactor

* fix: Python Server codegen broken by extra `:W`

* remove: TODOs

* update: codegen code to use new attribute syntax

* fix: RustMetadata creation in ConstrainedBlobGenerator
parent b894ff28
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.endpoint.memberName
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rustName
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.symbol
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.derive
import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
@@ -27,10 +28,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.stripOuter
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.Clone
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.Debug
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.Default
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.PartialEq
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
import software.amazon.smithy.rust.codegen.core.smithy.makeOptional
import software.amazon.smithy.rust.codegen.core.smithy.mapRustType
@@ -175,7 +172,7 @@ internal class EndpointParamsGenerator(private val parameters: Parameters) {
        // Ensure that fields can be added in the future
        Attribute.NonExhaustive.render(writer)
        // Automatically implement standard Rust functionality
        Attribute.Derives(setOf(Debug, PartialEq, Clone)).render(writer)
        Attribute(derive(RuntimeType.Debug, RuntimeType.PartialEq, RuntimeType.Clone)).render(writer)
        // Generate the struct block:
        /*
            pub struct Params {
@@ -238,7 +235,7 @@ internal class EndpointParamsGenerator(private val parameters: Parameters) {

    private fun generateEndpointParamsBuilder(rustWriter: RustWriter) {
        rustWriter.docs("Builder for [`Params`]")
        Attribute.Derives(setOf(Debug, Default, PartialEq, Clone)).render(rustWriter)
        Attribute(derive(RuntimeType.Debug, RuntimeType.Default, RuntimeType.PartialEq, RuntimeType.Clone)).render(rustWriter)
        rustWriter.rustBlock("pub struct ParamsBuilder") {
            parameters.toList().forEach { parameter ->
                val name = parameter.memberName()
@@ -255,7 +252,7 @@ internal class EndpointParamsGenerator(private val parameters: Parameters) {
                "ParamsError" to paramsError(),
            ) {
                val params = writable {
                    Attribute.Custom("allow(clippy::unnecessary_lazy_evaluations)").render(this)
                    Attribute.AllowClippyUnnecessaryLazyEvaluations.render(this)
                    rustBlockTemplate("#{Params}", "Params" to paramsStruct()) {
                        parameters.toList().forEach { parameter ->
                            rust("${parameter.memberName()}: self.${parameter.memberName()}")
+3 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.Expre
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.Ownership
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rustName
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.allow
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.comment
@@ -202,7 +203,7 @@ internal class EndpointResolverGenerator(stdlib: List<CustomRuntimeFunction>, ru
        fnsUsed: List<CustomRuntimeFunction>,
    ): RuntimeType {
        return RuntimeType.forInlineFun("resolve_endpoint", EndpointsImpl) {
            allowLintsForResolver.map { Attribute.Custom("allow($it)") }.map { it.render(this) }
            Attribute(allow(allowLintsForResolver)).render(this)
            rustTemplate(
                """
                pub(super) fn resolve_endpoint($ParamsName: &#{Params}, $DiagnosticCollector: &mut #{DiagnosticCollector}, #{additional_args}) -> #{endpoint}::Result {
@@ -233,7 +234,7 @@ internal class EndpointResolverGenerator(stdlib: List<CustomRuntimeFunction>, ru
        }
        if (!isExhaustive(rules.last())) {
            // it's hard to figure out if these are always needed or not
            Attribute.Custom("allow(unreachable_code)").render(this)
            Attribute.AllowUnreachableCode.render(this)
            rustTemplate(
                """return Err(#{EndpointError}::message(format!("No rules matched these parameters. This is a bug. {:?}", $ParamsName)));""",
                *codegenScope,
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ class TemplateGenerator(

    override fun visitDynamicElement(expr: Expression) = writable {
        // we don't need to own the argument to push_str
        Attribute.Custom("allow(clippy::needless_borrow)").render(this)
        Attribute.AllowClippyNeedlessBorrow.render(this)
        rust("out.push_str(&#W);", exprGenerator(expr, Ownership.Borrowed))
    }

+5 −2
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@ import software.amazon.smithy.model.traits.DocumentationTrait
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.generators.PaginatorGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.isPaginated
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.derive
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWords
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
@@ -230,7 +232,8 @@ class FluentClientGenerator(
                val operationSymbol = symbolProvider.toSymbol(operation)
                val input = operation.inputShape(model)
                val baseDerives = symbolProvider.toSymbol(input).expectRustMetadata().derives
                val derives = baseDerives.derives.intersect(setOf(RuntimeType.Clone)) + RuntimeType.Debug
                // Filter out any derive that isn't Clone. Then add a Debug derive
                val derives = baseDerives.filter { it == RuntimeType.Clone } + RuntimeType.Debug
                rust(
                    """
                    /// Fluent builder constructing a request to `${operationSymbol.name}`.
@@ -240,7 +243,7 @@ class FluentClientGenerator(

                documentShape(operation, model, autoSuppressMissingDocs = false)
                deprecatedShape(operation)
                baseDerives.copy(derives = derives).render(this)
                Attribute(derive(derives.toSet())).render(this)
                rustTemplate(
                    """
                    pub struct ${operationSymbol.name}#{generics:W} {
+1 −1
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@ class RequestBindingGenerator(
        uriBase(implBlockWriter)
        val addHeadersFn = httpBindingGenerator.generateAddHeadersFn(operationShape)
        val hasQuery = uriQuery(implBlockWriter)
        Attribute.Custom("allow(clippy::unnecessary_wraps)").render(implBlockWriter)
        Attribute.AllowClippyUnnecessaryWraps.render(implBlockWriter)
        implBlockWriter.rustBlockTemplate(
            """
            fn update_http_builder(
Loading