forked from smithy-lang/smithy-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CargoDependency.kt
260 lines (229 loc) · 11.7 KB
/
CargoDependency.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.rustlang
import software.amazon.smithy.codegen.core.SymbolDependency
import software.amazon.smithy.codegen.core.SymbolDependencyContainer
import software.amazon.smithy.rust.codegen.core.smithy.ConstrainedModule
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.util.dq
import java.nio.file.Path
sealed class DependencyScope {
object Dev : DependencyScope()
object Compile : DependencyScope()
}
sealed class DependencyLocation
data class CratesIo(val version: String) : DependencyLocation()
data class Local(val basePath: String, val version: String? = null) : DependencyLocation()
sealed class RustDependency(open val name: String) : SymbolDependencyContainer {
abstract fun version(): String
open fun dependencies(): List<RustDependency> = listOf()
override fun getDependencies(): List<SymbolDependency> {
return listOf(
SymbolDependency
.builder()
.packageName(name).version(version())
// We rely on retrieving the structured dependency from the symbol later
.putProperty(PropertyKey, this).build(),
) + dependencies().flatMap { it.dependencies }
}
companion object {
private const val PropertyKey = "rustdep"
fun fromSymbolDependency(symbolDependency: SymbolDependency) =
symbolDependency.getProperty(PropertyKey, RustDependency::class.java).get()
}
}
/**
* A dependency on a snippet of code
*
* InlineDependency should not be instantiated directly, rather, it should be constructed with
* [software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.forInlineFun]
*
* InlineDependencies are created as private modules within the main crate. This is useful for any code that
* doesn't need to exist in a shared crate, but must still be generated exactly once during codegen.
*
* CodegenVisitor de-duplicates inline dependencies by (module, name) during code generation.
*/
class InlineDependency(
name: String,
val module: RustModule,
private val extraDependencies: List<RustDependency> = listOf(),
val renderer: Writable,
) : RustDependency(name) {
override fun version(): String {
// just need a version that won't crash
return renderer.hashCode().toString()
}
override fun dependencies(): List<RustDependency> {
return extraDependencies
}
fun key() = "${module.fullyQualifiedPath()}::$name"
companion object {
fun forRustFile(
module: RustModule.LeafModule,
resourcePath: String,
vararg additionalDependencies: RustDependency,
): InlineDependency {
// The inline crate is loaded as a dependency on the runtime classpath
val rustFile = this::class.java.getResource(resourcePath)
check(rustFile != null) { "Rust file $resourcePath was missing from the resource bundle!" }
return InlineDependency(module.name, module, additionalDependencies.toList()) {
raw(rustFile.readText())
}
}
private fun forInlineableRustFile(name: String, vararg additionalDependencies: RustDependency) =
forRustFile(RustModule.private(name), "/inlineable/src/$name.rs", *additionalDependencies)
fun jsonErrors(runtimeConfig: RuntimeConfig) =
forInlineableRustFile(
"json_errors",
CargoDependency.smithyJson(runtimeConfig),
CargoDependency.Bytes,
CargoDependency.Http,
)
fun idempotencyToken() =
forInlineableRustFile("idempotency_token", CargoDependency.FastRand)
fun ec2QueryErrors(runtimeConfig: RuntimeConfig): InlineDependency =
forInlineableRustFile("ec2_query_errors", CargoDependency.smithyXml(runtimeConfig))
fun wrappedXmlErrors(runtimeConfig: RuntimeConfig): InlineDependency =
forInlineableRustFile("rest_xml_wrapped_errors", CargoDependency.smithyXml(runtimeConfig))
fun unwrappedXmlErrors(runtimeConfig: RuntimeConfig): InlineDependency =
forInlineableRustFile("rest_xml_unwrapped_errors", CargoDependency.smithyXml(runtimeConfig))
fun constrained(): InlineDependency =
InlineDependency.forRustFile(ConstrainedModule, "/inlineable/src/constrained.rs")
}
}
fun InlineDependency.toType() = RuntimeType(module.fullyQualifiedPath(), this)
data class Feature(val name: String, val default: Boolean, val deps: List<String>)
/**
* A dependency on an internal or external Cargo Crate
*/
data class CargoDependency(
override val name: String,
private val location: DependencyLocation,
val scope: DependencyScope = DependencyScope.Compile,
val optional: Boolean = false,
val features: Set<String> = emptySet(),
val rustName: String = name.replace("-", "_"),
) : RustDependency(name) {
val key: Triple<String, DependencyLocation, DependencyScope> get() = Triple(name, location, scope)
fun withFeature(feature: String): CargoDependency {
return copy(features = features.toMutableSet().apply { add(feature) })
}
fun toDevDependency() = copy(scope = DependencyScope.Dev)
override fun version(): String = when (location) {
is CratesIo -> location.version
is Local -> "local"
}
fun toMap(): Map<String, Any> {
val attribs = mutableMapOf<String, Any>()
with(location) {
when (this) {
is CratesIo -> attribs["version"] = version
is Local -> {
val fullPath = Path.of("$basePath/$name")
attribs["path"] = fullPath.normalize().toString()
version?.also { attribs["version"] = version }
}
}
}
with(features) {
if (!isEmpty()) {
attribs["features"] = this
}
}
if (optional) {
attribs["optional"] = true
}
return attribs
}
override fun toString(): String {
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(",")} }"
}
fun toType(): RuntimeType {
return RuntimeType(rustName, this)
}
companion object {
val OnceCell: CargoDependency = CargoDependency("once_cell", CratesIo("1.16"))
val Url: CargoDependency = CargoDependency("url", CratesIo("2.3.1"))
val Bytes: CargoDependency = CargoDependency("bytes", CratesIo("1.0.0"))
val BytesUtils: CargoDependency = CargoDependency("bytes-utils", CratesIo("0.1.0"))
val FastRand: CargoDependency = CargoDependency("fastrand", CratesIo("1.8.0"))
val Hex: CargoDependency = CargoDependency("hex", CratesIo("0.4.3"))
val Http: CargoDependency = CargoDependency("http", CratesIo("0.2.0"))
val HttpBody: CargoDependency = CargoDependency("http-body", CratesIo("0.4.4"))
val Hyper: CargoDependency = CargoDependency("hyper", CratesIo("0.14.12"))
val HyperWithStream: CargoDependency = Hyper.withFeature("stream")
val LazyStatic: CargoDependency = CargoDependency("lazy_static", CratesIo("1.4.0"))
val Md5: CargoDependency = CargoDependency("md-5", CratesIo("0.10.0"), rustName = "md5")
val PercentEncoding: CargoDependency = CargoDependency("percent-encoding", CratesIo("2.0.0"))
val Regex: CargoDependency = CargoDependency("regex", CratesIo("1.5.5"))
val Ring: CargoDependency = CargoDependency("ring", CratesIo("0.16.0"))
val TokioStream: CargoDependency = CargoDependency("tokio-stream", CratesIo("0.1.7"))
val Tower: CargoDependency = CargoDependency("tower", CratesIo("0.4"))
val Tracing: CargoDependency = CargoDependency("tracing", CratesIo("0.1"))
// Test-only dependencies
val AsyncStd: CargoDependency = CargoDependency("async-std", CratesIo("1.12.0"), DependencyScope.Dev)
val AsyncStream: CargoDependency = CargoDependency("async-stream", CratesIo("0.3.0"), DependencyScope.Dev)
val Criterion: CargoDependency = CargoDependency("criterion", CratesIo("0.4.0"), DependencyScope.Dev)
val FuturesCore: CargoDependency = CargoDependency("futures-core", CratesIo("0.3.25"), DependencyScope.Dev)
val FuturesUtil: CargoDependency = CargoDependency("futures-util", CratesIo("0.3.25"), DependencyScope.Dev)
val HdrHistogram: CargoDependency = CargoDependency("hdrhistogram", CratesIo("7.5.2"), DependencyScope.Dev)
val Hound: CargoDependency = CargoDependency("hound", CratesIo("3.4.0"), DependencyScope.Dev)
val PrettyAssertions: CargoDependency =
CargoDependency("pretty_assertions", CratesIo("1.3.0"), DependencyScope.Dev)
val SerdeJson: CargoDependency = CargoDependency("serde_json", CratesIo("1.0.0"), DependencyScope.Dev)
val Smol: CargoDependency = CargoDependency("smol", CratesIo("1.2.0"), DependencyScope.Dev)
val TempFile: CargoDependency = CargoDependency("tempfile", CratesIo("3.2.0"), DependencyScope.Dev)
val Tokio: CargoDependency =
CargoDependency(
"tokio",
CratesIo("1.8.4"),
DependencyScope.Dev,
features = setOf("macros", "test-util", "rt-multi-thread"),
)
val TracingAppender: CargoDependency = CargoDependency(
"tracing-appender",
CratesIo("0.2.2"),
DependencyScope.Dev,
)
val TracingSubscriber: CargoDependency = CargoDependency(
"tracing-subscriber",
CratesIo("0.3.16"),
DependencyScope.Dev,
features = setOf("env-filter", "json"),
)
fun smithyAsync(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-async")
fun smithyChecksums(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-checksums")
fun smithyClient(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-client")
fun smithyClientTestUtil(runtimeConfig: RuntimeConfig) =
smithyClient(runtimeConfig).toDevDependency().withFeature("test-util")
fun smithyEventStream(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-eventstream")
fun smithyHttp(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-http")
fun smithyHttpTower(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-http-tower")
fun smithyJson(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-json")
fun smithyProtocolTestHelpers(runtimeConfig: RuntimeConfig) =
runtimeConfig.smithyRuntimeCrate("smithy-protocol-test", scope = DependencyScope.Dev)
fun smithyQuery(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-query")
fun smithyTypes(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-types")
fun smithyXml(runtimeConfig: RuntimeConfig) = runtimeConfig.smithyRuntimeCrate("smithy-xml")
}
}