Unverified Commit 6676583b authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Change default Smithy expression start character to # (#67)

parent 0b6ecac6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ class InlineDependency(name: String, val module: String, val renderer: (RustWrit
            val rustFile = this::class.java.getResource("/inlineable/src/$filename")
            check(rustFile != null)
            return InlineDependency(name, module) { writer ->
                writer.write(rustFile.readText())
                writer.raw(rustFile.readText())
            }
        }

+3 −3
Original line number Diff line number Diff line
@@ -179,9 +179,9 @@ data class Derives(val derives: Set<RuntimeType>) : Attribute() {
        if (derives.isEmpty()) {
            return
        }
        writer.writeInline("#[derive(")
        writer.raw("#[derive(")
        derives.sortedBy { it.name }.forEach { derive ->
            writer.writeInline("\$T, ", derive)
            writer.writeInline("#T, ", derive)
        }
        writer.write(")]")
    }
@@ -193,7 +193,7 @@ data class Derives(val derives: Set<RuntimeType>) : Attribute() {

data class Custom(val annot: String, val symbols: List<RuntimeType> = listOf()) : Attribute() {
    override fun render(writer: RustWriter) {
        writer.writeInline("#[")
        writer.raw("#[")
        writer.writeInline(annot)
        writer.write("]")

+15 −5
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ fun <T : CodeWriter> T.conditionalBlock(
/**
 * Convenience wrapper that tells Intellij that the contents of this block are Rust
 */
fun <T : CodeWriter> T.rust(@Language("Rust", prefix = "fn foo() {", suffix = "}") contents: String, vararg args: Any) {
fun <T : CodeWriter> T.rust(@Language("Rust", prefix = "fn foo(&self) {", suffix = "}") contents: String, vararg args: Any) {
    this.write(contents, *args)
}

@@ -85,7 +85,7 @@ fun <T : CodeWriter> T.documentShape(shape: Shape, model: Model): T {
    val docTrait = shape.getMemberTrait(model, DocumentationTrait::class.java).orNull()

    docTrait?.value?.also {
        this.docs(it)
        this.docs(escape(it))
    }

    return this
@@ -100,12 +100,13 @@ fun <T : CodeWriter> T.documentShape(shape: Shape, model: Model): T {
 *    - Empty newlines are removed
 */
fun <T : CodeWriter> T.docs(text: String, vararg args: Any) {
    pushState("docs")
    pushState()
    setNewlinePrefix("/// ")
    // TODO: Smithy updates should remove the need for a number of these changes
    val cleaned = text.lines()
        // We need to filter out blank lines—an empty line causes the markdown parser to interpret the subsequent
        // docs as a code block because they are indented.
        .filter { !it.isBlank() }
        .filter { it.isNotBlank() }
        .joinToString("\n") {
            // Rustdoc warns on tabs in documentation
            it.trimStart().replace("\t", "  ")
@@ -114,6 +115,14 @@ fun <T : CodeWriter> T.docs(text: String, vararg args: Any) {
    popState()
}

/** Escape the [expressionStart] character to avoid problems during formatting */
fun CodeWriter.escape(text: String): String = text.replace("$expressionStart", "$expressionStart$expressionStart")

/**
 * Write _exactly_ the text as written into the code writer without newlines or formatting
 */
fun CodeWriter.raw(text: String) = writeInline(escape(text))

class RustWriter private constructor(
    private val filename: String,
    val namespace: String,
@@ -141,6 +150,7 @@ class RustWriter private constructor(
    private var n = 0

    init {
        expressionStart = '#'
        if (filename.endsWith(".rs")) {
            require(namespace.startsWith("crate")) { "We can only write into files in the crate (got $namespace)" }
        }
@@ -183,7 +193,7 @@ class RustWriter private constructor(
        moduleWriter(innerWriter)
        rustMetadata.render(this)
        rustBlock("mod $moduleName") {
            write(innerWriter.toString())
            writeWithNoFormatting(innerWriter.toString())
        }
        innerWriter.dependencies.forEach { addDependency(it) }
        return this
+2 −2
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ class EnumGenerator(
            }
        }

        writer.rustBlock("impl <T> \$T<T> for $enumName where T: \$T<str>", RuntimeType.From, RuntimeType.AsRef) {
        writer.rustBlock("impl <T> #T<T> for $enumName where T: #T<str>", RuntimeType.From, RuntimeType.AsRef) {
            writer.rustBlock("fn from(s: T) -> Self") {
                write("$enumName(s.as_ref().to_owned())")
            }
@@ -103,7 +103,7 @@ class EnumGenerator(
    }

    private fun renderFromStr() {
        writer.rustBlock("impl <T> \$T<T> for $enumName where T: \$T<str>", RuntimeType.From, RuntimeType.AsRef) {
        writer.rustBlock("impl <T> #T<T> for $enumName where T: #T<str>", RuntimeType.From, RuntimeType.AsRef) {
            writer.rustBlock("fn from(s: T) -> Self") {
                writer.rustBlock("match s.as_ref()") {
                    sortedMembers.forEach { member ->
+2 −2
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ class ErrorGenerator(
            write("pub fn throttling(&self) -> bool { $throttling }")
        }

        writer.rustBlock("impl \$T for ${symbol.name}", StdFmt("Display")) {
        writer.rustBlock("impl #T for ${symbol.name}", StdFmt("Display")) {
            rustBlock("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result") {
                val message = shape.getMember("message")
                write("write!(f, ${symbol.name.dq()})?;")
@@ -57,6 +57,6 @@ class ErrorGenerator(
            }
        }

        writer.write("impl \$T for ${symbol.name} {}", StdError)
        writer.write("impl #T for ${symbol.name} {}", StdError)
    }
}
Loading