Skip to content

Commit 4c76847

Browse files
authored
Enclose missing Rust template parameters in backticks in the exception message (#1492)
* Enclose missing Rust template parameters in backticks in the exception message * appease ktlint * ./gradlew ktlintFormat
1 parent e23790e commit 4c76847

File tree

2 files changed

+23
-3
lines changed
  • codegen-core/src
    • main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang
    • test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang

2 files changed

+23
-3
lines changed

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ private fun transformTemplate(template: String, scope: Array<out Pair<String, An
167167
val templateType = matchResult.groupValues[2].ifEmpty { ":T" }
168168
if (!scope.toMap().keys.contains(keyName)) {
169169
throw CodegenException(
170-
"Rust block template expected `$keyName` but was not present in template.\n hint: Template contains: ${
171-
scope.map { it.first }
172-
}",
170+
"""
171+
Rust block template expected `$keyName` but was not present in template.
172+
Hint: Template contains: ${scope.map { "`${it.first}`" }}
173+
""".trimIndent(),
173174
)
174175
}
175176
"#{${keyName.lowercase()}$templateType}"

codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriterTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import io.kotest.matchers.shouldBe
1010
import io.kotest.matchers.string.shouldContain
1111
import io.kotest.matchers.string.shouldContainOnlyOnce
1212
import org.junit.jupiter.api.Test
13+
import org.junit.jupiter.api.assertThrows
14+
import software.amazon.smithy.codegen.core.CodegenException
1315
import software.amazon.smithy.model.Model
1416
import software.amazon.smithy.model.shapes.SetShape
1517
import software.amazon.smithy.model.shapes.StringShape
@@ -161,6 +163,23 @@ class RustWriterTest {
161163
sut.toString().shouldContain("inner: hello, regular: http::foo")
162164
}
163165

166+
@Test
167+
fun `missing template parameters are enclosed in backticks in the exception message`() {
168+
val sut = RustWriter.forModule("lib")
169+
val exception = assertThrows<CodegenException> {
170+
sut.rustTemplate(
171+
"#{Foo} #{Bar}",
172+
"Foo Bar" to CargoDependency.Http.toType().resolve("foo"),
173+
"Baz" to CargoDependency.Http.toType().resolve("foo"),
174+
)
175+
}
176+
exception.message shouldBe
177+
"""
178+
Rust block template expected `Foo` but was not present in template.
179+
Hint: Template contains: [`Foo Bar`, `Baz`]
180+
""".trimIndent()
181+
}
182+
164183
@Test
165184
fun `can handle file paths properly when determining module`() {
166185
val sut = RustWriter.forModule("src/module_name")

0 commit comments

Comments
 (0)