From 514a0eefe86174ba535f46f3fb3481c539b79f96 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 2 Jul 2026 09:36:57 +0100 Subject: Reorder rust writer code to match AST definitions --- .../compiler/output/lang/rust/RustWriter.java | 32 ++++++++++++++-------- .../compiler/output/lang/rust/RustWriterTests.java | 24 ++++++++++------ 2 files changed, 37 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index 77ed461..514e3f5 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -37,13 +37,7 @@ public class RustWriter implements AutoCloseable { this.writer.close(); } - private void writeDocComment(DocComment docComment) throws IOException { - for (var line : docComment.lines()) { - this.writer.write("/// "); - this.writer.write(line); - this.writer.write("\n"); - } - } + // == Modules == public void writeModule(RustModule module) throws IOException { writeWithSeparator( @@ -58,10 +52,7 @@ public class RustWriter implements AutoCloseable { this.writer.newLine(); } - void writeIdentifier(RustIdentifier identifier) throws IOException { - // TODO: handle keywords - this.writer.write(identifier.value()); - } + // == Items == void writeItem(RustItem item) throws IOException { switch (item) { @@ -96,6 +87,8 @@ public class RustWriter implements AutoCloseable { this.writer.write("}"); } + // == Types == + void writeType(RustType type) throws IOException { switch (type) { case RustTypePath typePath -> { @@ -133,4 +126,21 @@ public class RustWriter implements AutoCloseable { } } } + + // == Identifiers == + + void writeIdentifier(RustIdentifier identifier) throws IOException { + // TODO: handle keywords + this.writer.write(identifier.value()); + } + + // == Doc comments == + + private void writeDocComment(DocComment docComment) throws IOException { + for (var line : docComment.lines()) { + this.writer.write("/// "); + this.writer.write(line); + this.writer.write("\n"); + } + } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java index 319393e..f4103c2 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java @@ -14,14 +14,7 @@ import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; public class RustWriterTests { - @Test - public void identifierNonKeywordIsWrittenVerbatim() throws IOException { - var rust = new RustIdentifier("rigs"); - - var string = write(writer -> writer.writeIdentifier(rust)); - - assertThat(string, equalTo("rigs")); - } + // == Modules == @Test public void module() throws IOException { @@ -41,6 +34,8 @@ public class RustWriterTests { """)); } + // == Items == + @Test public void emptyStruct() throws IOException { var rust = RustStructStruct.arbitrary().withName(new RustIdentifier("Rectangle")).build(); @@ -86,6 +81,8 @@ public class RustWriterTests { }""")); } + // == Types == + @Test public void typeSegmentsAreSeparatedByDoubleColons() throws IOException { var rust = RustTypePath.of("std", "string", "String"); @@ -114,6 +111,17 @@ public class RustWriterTests { assertThat(string, equalTo("std::collections::HashMap")); } + // == Identifiers == + + @Test + public void identifierNonKeywordIsWrittenVerbatim() throws IOException { + var rust = new RustIdentifier("rigs"); + + var string = write(writer -> writer.writeIdentifier(rust)); + + assertThat(string, equalTo("rigs")); + } + private String write(Write write) throws IOException { return write(write, Optional.empty()); } -- cgit v1.2.3