Skip to content

Commit

Permalink
Enclose missing Rust template parameters in backticks in the exceptio…
Browse files Browse the repository at this point in the history
…n message (smithy-lang#1492)

* Enclose missing Rust template parameters in backticks in the exception message

* appease ktlint

* ./gradlew ktlintFormat
  • Loading branch information
david-perez committed Feb 24, 2023
1 parent e23790e commit 4c76847
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Expand Up @@ -167,9 +167,10 @@ private fun transformTemplate(template: String, scope: Array<out Pair<String, An
val templateType = matchResult.groupValues[2].ifEmpty { ":T" }
if (!scope.toMap().keys.contains(keyName)) {
throw CodegenException(
"Rust block template expected `$keyName` but was not present in template.\n hint: Template contains: ${
scope.map { it.first }
}",
"""
Rust block template expected `$keyName` but was not present in template.
Hint: Template contains: ${scope.map { "`${it.first}`" }}
""".trimIndent(),
)
}
"#{${keyName.lowercase()}$templateType}"
Expand Down
Expand Up @@ -10,6 +10,8 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldContainOnlyOnce
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.SetShape
import software.amazon.smithy.model.shapes.StringShape
Expand Down Expand Up @@ -161,6 +163,23 @@ class RustWriterTest {
sut.toString().shouldContain("inner: hello, regular: http::foo")
}

@Test
fun `missing template parameters are enclosed in backticks in the exception message`() {
val sut = RustWriter.forModule("lib")
val exception = assertThrows<CodegenException> {
sut.rustTemplate(
"#{Foo} #{Bar}",
"Foo Bar" to CargoDependency.Http.toType().resolve("foo"),
"Baz" to CargoDependency.Http.toType().resolve("foo"),
)
}
exception.message shouldBe
"""
Rust block template expected `Foo` but was not present in template.
Hint: Template contains: [`Foo Bar`, `Baz`]
""".trimIndent()
}

@Test
fun `can handle file paths properly when determining module`() {
val sut = RustWriter.forModule("src/module_name")
Expand Down

0 comments on commit 4c76847

Please sign in to comment.