forked from smithy-lang/smithy-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RustReservedWords.kt
195 lines (176 loc) · 6.87 KB
/
RustReservedWords.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
/*
* 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.ReservedWordSymbolProvider
import software.amazon.smithy.codegen.core.ReservedWords
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.EnumShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.Shape
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.rust.codegen.core.smithy.RustSymbolProvider
import software.amazon.smithy.rust.codegen.core.smithy.WrappingSymbolProvider
import software.amazon.smithy.rust.codegen.core.smithy.generators.UnionGenerator
import software.amazon.smithy.rust.codegen.core.smithy.renamedFrom
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.letIf
class RustReservedWordSymbolProvider(private val base: RustSymbolProvider) : WrappingSymbolProvider(base) {
private val internal =
ReservedWordSymbolProvider.builder().symbolProvider(base)
.nameReservedWords(RustReservedWords)
.memberReservedWords(RustReservedWords)
.build()
override fun toMemberName(shape: MemberShape): String {
val baseName = super.toMemberName(shape)
val reservedWordReplacedName = internal.toMemberName(shape)
val container = model.expectShape(shape.container)
return when {
container is StructureShape -> when (baseName) {
"build" -> "build_value"
"builder" -> "builder_value"
"default" -> "default_value"
"send" -> "send_value"
// To avoid conflicts with the `make_operation` and `presigned` functions on generated inputs
"make_operation" -> "make_operation_value"
"presigned" -> "presigned_value"
"customize" -> "customize_value"
// To avoid conflicts with the error metadata `meta` field
"meta" -> "meta_value"
else -> reservedWordReplacedName
}
container is UnionShape -> when (baseName) {
// Unions contain an `Unknown` variant. This exists to support parsing data returned from the server
// that represent union variants that have been added since this SDK was generated.
UnionGenerator.UnknownVariantName -> "${UnionGenerator.UnknownVariantName}Value"
"${UnionGenerator.UnknownVariantName}Value" -> "${UnionGenerator.UnknownVariantName}Value_"
else -> reservedWordReplacedName
}
container is EnumShape || container.hasTrait<EnumTrait>() -> when (baseName) {
// Unknown is used as the name of the variant containing unexpected values
"Unknown" -> "UnknownValue"
// Real models won't end in `_` so it's safe to stop here
"UnknownValue" -> "UnknownValue_"
else -> reservedWordReplacedName
}
else -> error("unexpected container: $container")
}
}
/**
* Convert shape to a Symbol
*
* If this symbol provider renamed the symbol, a `renamedFrom` field will be set on the symbol, enabling
* code generators to generate special docs.
*/
override fun toSymbol(shape: Shape): Symbol {
// Sanity check that the symbol provider stack is set up correctly
check(super.toSymbol(shape).renamedFrom() == null) {
"RustReservedWordSymbolProvider should only run once"
}
var renamedSymbol = internal.toSymbol(shape)
return when (shape) {
is MemberShape -> {
val container = model.expectShape(shape.container)
val containerIsEnum = container is EnumShape || container.hasTrait<EnumTrait>()
if (container !is StructureShape && container !is UnionShape && !containerIsEnum) {
return base.toSymbol(shape)
}
val previousName = base.toMemberName(shape)
val escapedName = this.toMemberName(shape)
// if the names don't match and it isn't a simple escaping with `r#`, record a rename
renamedSymbol.toBuilder().name(escapedName)
.letIf(escapedName != previousName && !escapedName.contains("r#")) {
it.renamedFrom(previousName)
}.build()
}
else -> renamedSymbol
}
}
}
enum class EscapeFor {
TypeName,
ModuleName,
}
object RustReservedWords : ReservedWords {
private val RustKeywords = setOf(
"as",
"break",
"const",
"continue",
"crate",
"else",
"enum",
"extern",
"false",
"fn",
"for",
"if",
"impl",
"in",
"let",
"loop",
"match",
"mod",
"move",
"mut",
"pub",
"ref",
"return",
"self",
"Self",
"static",
"struct",
"super",
"trait",
"true",
"type",
"unsafe",
"use",
"where",
"while",
"async",
"await",
"dyn",
"abstract",
"become",
"box",
"do",
"final",
"macro",
"override",
"priv",
"typeof",
"unsized",
"virtual",
"yield",
"try",
)
// Some things can't be used as a raw identifier, so we can't use the normal escaping strategy
// https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094/4
private val keywordEscapingMap = mapOf(
"crate" to "crate_",
"super" to "super_",
"self" to "self_",
"Self" to "SelfValue",
// Real models won't end in `_` so it's safe to stop here
"SelfValue" to "SelfValue_",
)
override fun escape(word: String): String = doEscape(word, EscapeFor.TypeName)
private fun doEscape(word: String, escapeFor: EscapeFor = EscapeFor.TypeName): String =
when (val mapped = keywordEscapingMap[word]) {
null -> when (escapeFor) {
EscapeFor.TypeName -> "r##$word"
EscapeFor.ModuleName -> "${word}_"
}
else -> mapped
}
fun escapeIfNeeded(word: String, escapeFor: EscapeFor = EscapeFor.TypeName): String =
when (isReserved(word)) {
true -> doEscape(word, escapeFor)
else -> word
}
override fun isReserved(word: String): Boolean = RustKeywords.contains(word) || keywordEscapingMap.contains(word)
}