Loading codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/lang/CargoDependency.kt +1 −1 Original line number Diff line number Diff line Loading @@ -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()) } } Loading codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/lang/RustTypes.kt +3 −3 Original line number Diff line number Diff line Loading @@ -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(")]") } Loading @@ -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("]") Loading codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/lang/RustWriter.kt +15 −5 Original line number Diff line number Diff line Loading @@ -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) } Loading @@ -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 Loading @@ -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", " ") Loading @@ -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, Loading Loading @@ -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)" } } Loading Loading @@ -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 Loading codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/EnumGenerator.kt +2 −2 Original line number Diff line number Diff line Loading @@ -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())") } Loading Loading @@ -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 -> Loading codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ErrorGenerator.kt +2 −2 Original line number Diff line number Diff line Loading @@ -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()})?;") Loading @@ -57,6 +57,6 @@ class ErrorGenerator( } } writer.write("impl \$T for ${symbol.name} {}", StdError) writer.write("impl #T for ${symbol.name} {}", StdError) } } Loading
codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/lang/CargoDependency.kt +1 −1 Original line number Diff line number Diff line Loading @@ -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()) } } Loading
codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/lang/RustTypes.kt +3 −3 Original line number Diff line number Diff line Loading @@ -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(")]") } Loading @@ -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("]") Loading
codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/lang/RustWriter.kt +15 −5 Original line number Diff line number Diff line Loading @@ -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) } Loading @@ -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 Loading @@ -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", " ") Loading @@ -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, Loading Loading @@ -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)" } } Loading Loading @@ -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 Loading
codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/EnumGenerator.kt +2 −2 Original line number Diff line number Diff line Loading @@ -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())") } Loading Loading @@ -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 -> Loading
codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/ErrorGenerator.kt +2 −2 Original line number Diff line number Diff line Loading @@ -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()})?;") Loading @@ -57,6 +57,6 @@ class ErrorGenerator( } } writer.write("impl \$T for ${symbol.name} {}", StdError) writer.write("impl #T for ${symbol.name} {}", StdError) } }