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

Cleanup module handling & add support for Cargo features (#253)

* Cleanup module handling & add support for Cargo features

* Fix AWS tests

* Set optional in the Cargo toml
parent 90f116c5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ internal class EndpointConfigCustomizationTest {
    @Test
    fun `write an endpoint into the config`() {
        val project = stubConfigProject(EndpointConfigCustomization(TestRuntimeConfig, model.lookup("test#TestService")))
        project.useFileWriter("src/lib.rs", "crate") {
        project.lib {
            it.addDependency(awsTypes(TestRuntimeConfig))
            it.addDependency(CargoDependency.Http)
            it.unitTest(
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ internal class SigV4SigningCustomizationTest {
    @Test
    fun `generates a valid config`() {
        val project = stubConfigProject(SigV4SigningConfig(SigV4Trait.builder().name("test-service").build()))
        project.useFileWriter("src/lib.rs", "crate") {
        project.lib {
            it.unitTest(
                """
            let conf = crate::config::Config::builder().build();
+6 −0
Original line number Diff line number Diff line
@@ -106,6 +106,8 @@ class InlineDependency(
fun CargoDependency.asType(): RuntimeType =
    RuntimeType(null, dependency = this, namespace = this.name.replace("-", "_"))

data class Feature(val name: String, val default: Boolean, val deps: List<String>)

/**
 * A dependency on an internal or external Cargo Crate
 */
@@ -113,6 +115,7 @@ data class CargoDependency(
    override val name: String,
    private val location: DependencyLocation,
    val scope: DependencyScope = DependencyScope.Compile,
    val optional: Boolean = false,
    private val features: List<String> = listOf()
) : RustDependency(name) {

@@ -137,6 +140,9 @@ data class CargoDependency(
                attribs["features"] = this
            }
        }
        if (optional) {
            attribs["optional"] = true
        }
        return attribs
    }

+3 −0
Original line number Diff line number Diff line
@@ -21,5 +21,8 @@ data class RustModule(val name: String, val rustMetadata: RustMetadata) {
            }*/
            return RustModule(name, RustMetadata(public = public))
        }

        val Config = default("config", public = true)
        val Error = default("error", public = true)
    }
}
+30 −21
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
package software.amazon.smithy.rust.codegen.rustlang

import software.amazon.smithy.rust.codegen.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.util.dq

/**
 * A hierarchy of types handled by Smithy codegen
@@ -149,14 +150,15 @@ inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType {
 * Meta information about a Rust construction (field, struct, or enum)
 */
data class RustMetadata(
    val derives: Derives = Derives.Empty,
    val derives: Attribute.Derives = Attribute.Derives.Empty,
    val additionalAttributes: List<Attribute> = listOf(),
    val public: Boolean
) {
    fun withDerives(vararg newDerive: RuntimeType): RustMetadata =
        this.copy(derives = derives.copy(derives = derives.derives + newDerive))

    fun attributes(): List<Attribute> = additionalAttributes + derives
    private fun attributes(): List<Attribute> = additionalAttributes + derives

    fun renderAttributes(writer: RustWriter): RustMetadata {
        attributes().forEach {
            it.render(writer)
@@ -201,7 +203,6 @@ sealed class Attribute {
        val NonExhaustive = Custom("non_exhaustive")
        val AllowUnused = Custom("allow(dead_code)")
    }
}

    data class Derives(val derives: Set<RuntimeType>) : Attribute() {
        override fun render(writer: RustWriter) {
@@ -220,14 +221,22 @@ data class Derives(val derives: Set<RuntimeType>) : Attribute() {
        }
    }

data class Custom(val annot: String, val symbols: List<RuntimeType> = listOf()) : Attribute() {
    data class Custom(val annotation: String, val symbols: List<RuntimeType> = listOf()) : Attribute() {
        override fun render(writer: RustWriter) {
        writer.raw("#[")
        writer.writeInline(annot)
        writer.write("]")

            writer.raw("#[$annotation]")
            symbols.forEach {
                writer.addDependency(it.dependency)
            }
        }
    }

    data class Cfg(val cond: String) : Attribute() {
        override fun render(writer: RustWriter) {
            writer.raw("#[cfg($cond)]")
        }

        companion object {
            fun feature(feature: String) = Cfg("feature = ${feature.dq()}")
        }
    }
}
Loading