summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-03 15:48:13 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-03 15:51:22 +0100
commitcffab4d76d6358dde9b708691d96852d39f9e977 (patch)
treeb00ae9ed3fb4ca742e017f173dd69db000729c8d /src/main/java/org/zwobble
parent7311b7638de76ba68f197f891248b426e656e730 (diff)
Add subsections for Rust items
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java124
1 files changed, 67 insertions, 57 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 b732390..6cd5860 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
@@ -73,6 +73,70 @@ public class RustWriter implements AutoCloseable {
}
}
+ // === Functions ===
+
+ private void writeFunction(RustFunction function) throws IOException {
+ this.writer.write("pub fn ");
+ this.writeIdentifier(function.name());
+ this.writer.write("(");
+
+ writeWithSeparator(
+ function.params(),
+ param -> {
+ this.writeIdentifier(param.name());
+ this.writer.write(": ");
+ this.writeType(param.type());
+ },
+ () -> {
+ this.writer.write(", ");
+ }
+ );
+
+ this.writer.write(")");
+
+ if (function.returnType().isPresent()) {
+ this.writer.write(" ");
+ this.writer.write("-> ");
+ this.writeType(function.returnType().get());
+ }
+
+ if (function.body().isPresent()) {
+ this.writer.write(" ");
+ this.writeBlockExpression(function.body().get());
+ } else {
+ this.writer.write(";");
+ }
+ }
+
+ // === Structs ===
+
+ private void writeStructStruct(RustStructStruct structStruct) throws IOException {
+ this.writeDocComment(structStruct.docComment());
+ this.writer.write("pub struct ");
+ this.writeIdentifier(structStruct.name());
+ this.writer.write(" {");
+
+ if (!structStruct.fields().isEmpty()) {
+ this.writer.indent();
+
+ for (var field : structStruct.fields()) {
+ this.writer.newLine();
+ this.writer.write("pub ");
+ this.writeIdentifier(field.name());
+ this.writer.write(": ");
+ this.writeType(field.type());
+ this.writer.write(",");
+ }
+
+ this.writer.dedent();
+ }
+
+ this.writer.newLine();
+ this.writer.write("}");
+ }
+
+ // === Enumerations ===
+
private void writeEnum(RustEnum enum_) throws IOException {
this.writeDocComment(enum_.docComment());
this.writer.write("pub enum ");
@@ -112,38 +176,7 @@ public class RustWriter implements AutoCloseable {
this.writer.write("}");
}
- private void writeFunction(RustFunction function) throws IOException {
- this.writer.write("pub fn ");
- this.writeIdentifier(function.name());
- this.writer.write("(");
-
- writeWithSeparator(
- function.params(),
- param -> {
- this.writeIdentifier(param.name());
- this.writer.write(": ");
- this.writeType(param.type());
- },
- () -> {
- this.writer.write(", ");
- }
- );
-
- this.writer.write(")");
-
- if (function.returnType().isPresent()) {
- this.writer.write(" ");
- this.writer.write("-> ");
- this.writeType(function.returnType().get());
- }
-
- if (function.body().isPresent()) {
- this.writer.write(" ");
- this.writeBlockExpression(function.body().get());
- } else {
- this.writer.write(";");
- }
- }
+ // === Implementations ===
private void writeInherentImpl(RustInherentImpl inherentImpl) throws IOException {
this.writer.write("impl ");
@@ -173,6 +206,8 @@ public class RustWriter implements AutoCloseable {
this.writer.write("}");
}
+ // === Associated items ===
+
void writeAssociatedItem(RustAssociatedItem item) throws IOException {
switch (item) {
case RustFunction function -> {
@@ -181,31 +216,6 @@ public class RustWriter implements AutoCloseable {
}
}
- private void writeStructStruct(RustStructStruct structStruct) throws IOException {
- this.writeDocComment(structStruct.docComment());
- this.writer.write("pub struct ");
- this.writeIdentifier(structStruct.name());
- this.writer.write(" {");
-
- if (!structStruct.fields().isEmpty()) {
- this.writer.indent();
-
- for (var field : structStruct.fields()) {
- this.writer.newLine();
- this.writer.write("pub ");
- this.writeIdentifier(field.name());
- this.writer.write(": ");
- this.writeType(field.type());
- this.writer.write(",");
- }
-
- this.writer.dedent();
- }
-
- this.writer.newLine();
- this.writer.write("}");
- }
-
// == Statements ==
void writeStatement(RustStatement statement) throws IOException {