summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java20
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunctionMatcher.java39
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImplMatcher.java6
7 files changed, 173 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
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 ea04006..9d1565c 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
@@ -139,6 +139,26 @@ public class RustWriterTests {
}
@Test
+ public void inherentImplWithAssociatedItems() throws IOException {
+ var rust = RustInherentImpl.arbitrary()
+ .withType(RustTypePath.of("Square"))
+ .addItem(RustFunction.arbitrary().withName(new RustIdentifier("width")))
+ .addItem(RustFunction.arbitrary().withName(new RustIdentifier("height")))
+ .build();
+
+ var string = write(writer -> writer.writeItem(rust));
+
+ assertThat(string, equalTo("""
+ impl Square {
+ fn width() {
+ }
+
+ fn height() {
+ }
+ }"""));
+ }
+
+ @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/RustFunctionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunctionMatcher.java
new file mode 100644
index 0000000..4777b24
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunctionMatcher.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.RustFunctionMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher imports
+
+public class RustFunctionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher isRustFunction() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction>> submatchers;
+
+ private RustFunctionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction>> 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.RustFunction.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> name) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction::name, name));
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunctionMatcher body
+}
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
index 23b23a1..ee4b9f1 100644
--- 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
@@ -34,6 +34,12 @@ public class RustInherentImplMatcher implements org.zwobble.precisely.Matcher<ja
return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher(submatchers);
}
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImplMatcher withItems(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem>> items) {
+ 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("items", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl::items, items));
+ 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
}