Skip to content

Commit

Permalink
Clean up an old hack
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Feb 22, 2023
1 parent 3c0d3d9 commit 4a4f425
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,10 @@ class RustReservedWordSymbolProvider(private val base: RustSymbolProvider) : Wra
// that represent union variants that have been added since this SDK was generated.
UnionGenerator.UnknownVariantName -> "${UnionGenerator.UnknownVariantName}Value"
"${UnionGenerator.UnknownVariantName}Value" -> "${UnionGenerator.UnknownVariantName}Value_"
// Self cannot 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
"Self" -> "SelfValue"
// Real models won't end in `_` so it's safe to stop here
"SelfValue" -> "SelfValue_"
else -> reservedWordReplacedName
}

container is EnumShape || container.hasTrait<EnumTrait>() -> when (baseName) {
// Self cannot 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
"Self" -> "SelfValue"
// Real models won't end in `_` so it's safe to stop here
"SelfValue" -> "SelfValue_"
// 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
Expand Down Expand Up @@ -168,17 +158,26 @@ object RustReservedWords : ReservedWords {
"try",
)

private val cantBeRaw = setOf("self", "crate", "super")
// 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 = when {
cantBeRaw.contains(word) -> "${word}_"
else -> "r##$word"
override fun escape(word: String): String = when (val mapped = keywordEscapingMap[word]) {
null -> "r##$word"
else -> mapped
}

fun escapeIfNeeded(word: String): String = when (isReserved(word)) {
true -> escape(word)
else -> word
}

override fun isReserved(word: String): Boolean = RustKeywords.contains(word)
override fun isReserved(word: String): Boolean = RustKeywords.contains(word) || keywordEscapingMap.contains(word)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class RustReservedWordSymbolProviderTest {
""".asSmithyModel()
val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model))
val symbol = provider.toSymbol(model.lookup("test#Self"))
symbol.name shouldBe "r##Self"
symbol.name shouldBe "SelfValue"
}

@Test
Expand Down

0 comments on commit 4a4f425

Please sign in to comment.