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

Support dependency features (#23)

* Support dependency features

* Fix typo
parent c7d4c031
Loading
Loading
Loading
Loading
+28 −8
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.lang
import software.amazon.smithy.codegen.core.SymbolDependency
import software.amazon.smithy.codegen.core.SymbolDependencyContainer
import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.util.dq

sealed class DependencyScope
object Dev : DependencyScope()
@@ -15,16 +16,21 @@ object Compile : DependencyScope()

sealed class DependencyLocation
data class CratesIo(val version: String) : DependencyLocation()
data class Local(val path: String? = null) : DependencyLocation()
data class Local(val basePath: String) : DependencyLocation()

data class RustDependency(
    val name: String,
    val location: DependencyLocation,
    val scope: DependencyScope = Compile
    val scope: DependencyScope = Compile,
    val features: List<String> = listOf()
) : SymbolDependencyContainer {
    override fun getDependencies(): List<SymbolDependency> {
        return listOf(
            SymbolDependency.builder().packageName(name).version(this.version()).putProperty(PropKey, this).build()
            SymbolDependency
                .builder()
                .packageName(name).version(version())
                // We rely on retrieving the structured dependency from the symbol later
                .putProperty(PropertyKey, this).build()
        )
    }

@@ -34,11 +40,25 @@ data class RustDependency(
    }

    override fun toString(): String {
        return when (location) {
            is CratesIo -> """$name = "${location.version}""""
            is Local -> """$name = { path = "${location.path}/$name" }"""
        val attribs = mutableListOf<String>()
        with(location) {
            attribs.add(
                when (this) {
                    is CratesIo -> """version = ${version.dq()}"""
                    is Local -> {
                        val fullPath = "$basePath/$name"
                        """path = ${fullPath.dq()}"""
                    }
                }
            )
        }
        with(features) {
            if (!isEmpty()) {
                attribs.add("features = [${joinToString(",") { it.dq() }}]")
            }
        }
        return "$name = { ${attribs.joinToString(",")} }"
    }

    companion object {
        val Http: RustDependency = RustDependency("http", CratesIo("0.2"))
@@ -53,9 +73,9 @@ data class RustDependency(
            "protocol-test-helpers", Local(runtimeConfig.relativePath), scope = Dev
        )

        private val PropKey = "rustdep"
        private val PropertyKey = "rustdep"

        fun fromSymbolDependency(symbolDependency: SymbolDependency) =
            symbolDependency.getProperty(PropKey, RustDependency::class.java).get()
            symbolDependency.getProperty(PropertyKey, RustDependency::class.java).get()
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ data class RuntimeConfig(val cratePrefix: String = "smithy", val relativePath: S
        fun fromNode(node: Optional<ObjectNode>): RuntimeConfig {
            return if (node.isPresent) {
                RuntimeConfig(
                    node.get().getStringMemberOrDefault("createPrefix", "smithy"),
                    node.get().getStringMemberOrDefault("cratePrefix", "smithy"),
                    File(node.get().getStringMemberOrDefault("relativePath", "../")).absolutePath
                )
            } else {