diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-02 17:24:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-02 17:24:21 +0100 |
| commit | 152c1dc711f472d3ae441a94aefd7a8b54811ffb (patch) | |
| tree | a00bfef908ff493324511243a68204e823b0293a | |
| parent | 6d87397a04e0cd35a5560b50de37c3a3ed550af0 (diff) | |
Support empty inherent impl in Rust AST
6 files changed, 100 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index 8cbe166..929e342 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -11,6 +11,7 @@ struct RustModule { sum RustItem { variant RustEnum; + variant RustInherentImpl; variant RustStructStruct; } @@ -37,6 +38,10 @@ struct RustEnumVariantTuple { field fields: List[RustTupleField]; } +struct RustInherentImpl { + field type: RustType; +} + struct RustStructStruct { field name: RustIdentifier; field fields: List[RustStructField]; 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 59b792c..7ab09c7 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 @@ -62,6 +62,10 @@ public class RustWriter implements AutoCloseable { writeEnum(enum_); } + case RustInherentImpl inherentImpl -> { + writeInherentImpl(inherentImpl); + } + case RustStructStruct structStruct -> { writeStructStruct(structStruct); } @@ -107,6 +111,14 @@ public class RustWriter implements AutoCloseable { this.writer.write("}"); } + private void writeInherentImpl(RustInherentImpl inherentImpl) throws IOException { + this.writer.write("impl "); + this.writeType(inherentImpl.type()); + this.writer.write(" {"); + this.writer.newLine(); + this.writer.write("}"); + } + private void writeStructStruct(RustStructStruct structStruct) throws IOException { this.writeDocComment(structStruct.docComment()); this.writer.write("pub struct "); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java new file mode 100644 index 0000000..55447bb --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl imports + +public record RustInherentImpl(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl(type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType.Builder type) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustItem.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustItem.java index 4471b49..5da65a1 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustItem.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustItem.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem imports -public sealed interface RustItem permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustEnum, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct { +public sealed interface RustItem permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustEnum, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem build(); 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 b5583ab..ea04006 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 @@ -128,6 +128,17 @@ public class RustWriterTests { } @Test + public void emptyInherentImpl() throws IOException { + var rust = RustInherentImpl.arbitrary().withType(RustTypePath.of("Square")).build(); + + var string = write(writer -> writer.writeItem(rust)); + + assertThat(string, equalTo(""" + impl Square { + }""")); + } + + @Test public void emptyStruct() throws IOException { var rust = RustStructStruct.arbitrary().withName(new RustIdentifier("Rectangle")).build(); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImplMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImplMatcher.java new file mode 100644 index 0000000..23b23a1 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImplMatcher.java @@ -0,0 +1,39 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher imports + +public class RustInherentImplMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher isRustInherentImpl() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl>> submatchers; + + private RustInherentImplMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl>> submatchers) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType> type) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl::type, type)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher body +} |
