summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-02 17:31:01 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-02 17:31:01 +0100
commit34b45be0454f5a3a48fa2ab5dd14a7700fd9c6ee (patch)
tree19feaa0f923b377a3b0837d565adce5e6a4ffefe /src/main/java/org/zwobble
parent152c1dc711f472d3ae441a94aefd7a8b54811ffb (diff)
Add associated items to Rust inherent impls
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java36
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustAssociatedItem.java18
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.java32
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java28
4 files changed, 108 insertions, 6 deletions
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 7ab09c7..60d7d60 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
@@ -111,14 +111,50 @@ public class RustWriter implements AutoCloseable {
this.writer.write("}");
}
+ private void writeFunction(RustFunction function) throws IOException {
+ this.writer.write("fn ");
+ this.writeIdentifier(function.name());
+ this.writer.write("() {");
+ this.writer.newLine();
+ this.writer.write("}");
+ }
+
private void writeInherentImpl(RustInherentImpl inherentImpl) throws IOException {
this.writer.write("impl ");
this.writeType(inherentImpl.type());
this.writer.write(" {");
+
+ if (!inherentImpl.items().isEmpty()) {
+ this.writer.indent();
+ this.writer.newLine();
+
+ writeWithSeparator(
+ inherentImpl.items(),
+ item -> {
+
+ this.writeAssociatedItem(item);
+ },
+ () -> {
+ this.writer.newLine();
+ this.writer.newLine();
+ }
+ );
+
+ this.writer.dedent();
+ }
+
this.writer.newLine();
this.writer.write("}");
}
+ private void writeAssociatedItem(RustAssociatedItem item) throws IOException {
+ switch (item) {
+ case RustFunction function -> {
+ writeFunction(function);
+ }
+ }
+ }
+
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/RustAssociatedItem.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustAssociatedItem.java
new file mode 100644
index 0000000..7334441
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustAssociatedItem.java
@@ -0,0 +1,18 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.rust.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem imports
+
+public sealed interface RustAssociatedItem permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction {
+ public interface Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem build();
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.java
new file mode 100644
index 0000000..aea16f0
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.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.RustFunction imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction imports
+
+public record RustFunction(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.arbitrary().build());
+ }
+
+ public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction(name);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder(name);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.Builder name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder(name.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction body
+}
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
index 55447bb..161dd5d 100644
--- 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
@@ -5,22 +5,38 @@ 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 record RustInherentImpl(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem> items) 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());
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath.arbitrary().build(), java.util.List.of());
}
- 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 record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem> items) 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);
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl(type, items);
}
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);
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items);
}
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());
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type.build(), items);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder withItems(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem> items) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder addItem(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem item) {
+ var items = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem>(this.items);
+ items.add(item);
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder addItem(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder item) {
+ var items = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem>(this.items);
+ items.add(item.build());
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items);
}
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder body