From e5f4a367a810d70a6f2cbe048ca1fd78549f1d28 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 14 Jul 2026 21:00:45 +0100 Subject: Add struct expressions to Rust AST --- .../compiler/output/lang/rust/RustWriterTests.java | 34 ++++++++++++++++ .../lang/rust/ast/RustStructExprFieldMatcher.java | 45 ++++++++++++++++++++++ .../lang/rust/ast/RustStructExpressionMatcher.java | 45 ++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.java (limited to 'src/test/java/org/zwobble') 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 21f8833..67d0a42 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 @@ -480,6 +480,40 @@ public class RustWriterTests { assertThat(string, equalTo("x.y")); } + // === Struct expressions === + + @Test + public void structExpressionWithNoFields() throws IOException { + var rust = RustStructExpression.arbitrary() + .withStructPath(RustPathInExpression.of("A")) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("A {}")); + } + + @Test + public void structExpressionWithFields() throws IOException { + var rust = RustStructExpression.arbitrary() + .withStructPath(RustPathInExpression.of("X")) + .addFieldValue( + RustStructExprField.arbitrary() + .withName(RustIdentifier.of("a")) + .withValue(RustPathInExpression.of("b")) + ) + .addFieldValue( + RustStructExprField.arbitrary() + .withName(RustIdentifier.of("c")) + .withValue(RustPathInExpression.of("d")) + ) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("X { a: b, c: d }")); + } + // == Types == @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.java new file mode 100644 index 0000000..2c8ac77 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher imports + +public class RustStructExprFieldMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher isRustStructExprField() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustStructExprFieldMatcher(java.util.List> 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 toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher withName(org.zwobble.precisely.Matcher name) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField::name, name)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher withValue(org.zwobble.precisely.Matcher value) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField::value, value)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.java new file mode 100644 index 0000000..5597d00 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher imports + +public class RustStructExpressionMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher isRustStructExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustStructExpressionMatcher(java.util.List> 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 toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher withStructPath(org.zwobble.precisely.Matcher structPath) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("structPath", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression::structPath, structPath)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher withFieldValues(org.zwobble.precisely.Matcher> fieldValues) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("fieldValues", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression::fieldValues, fieldValues)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher body +} -- cgit v1.2.3