From 6cb75452ad193e1e40a092478ab527513a7e2bda Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 14 Jul 2026 19:39:31 +0100 Subject: Add attributes to Rust structs --- .../compiler/output/lang/rust/RustWriterTests.java | 19 +++++++++++ .../lang/rust/ast/RustOuterAttributeMatcher.java | 39 ++++++++++++++++++++++ .../lang/rust/ast/RustStructStructMatcher.java | 6 ++++ 3 files changed, 64 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttributeMatcher.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 33fa490..b591058 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 @@ -109,6 +109,25 @@ public class RustWriterTests { }""")); } + @Test + public void structWithAttributes() throws IOException { + var rust = RustStructStruct.arbitrary() + .withName(new RustIdentifier("Rectangle")) + .withAttributes(List.of( + new RustOuterAttribute(RustTypePath.of("a", "b", "C")), + new RustOuterAttribute(RustTypePath.of("Debug")) + )) + .build(); + + var string = write(writer -> writer.writeItem(rust)); + + assertThat(string, equalTo(""" + #[a::b::C] + #[Debug] + pub struct Rectangle { + }""")); + } + @Test public void structWithFields() throws IOException { var rust = RustStructStruct.arbitrary() diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttributeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttributeMatcher.java new file mode 100644 index 0000000..6e1e930 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttributeMatcher.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.RustOuterAttributeMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher imports + +public class RustOuterAttributeMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher isRustOuterAttribute() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustOuterAttributeMatcher(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.RustOuterAttribute.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher withPath(org.zwobble.precisely.Matcher path) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("path", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute::path, path)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttributeMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStructMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStructMatcher.java index 0301dbb..4c4aa6a 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStructMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStructMatcher.java @@ -28,6 +28,12 @@ public class RustStructStructMatcher implements org.zwobble.precisely.Matcher> attributes) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("attributes", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct::attributes, attributes)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStructMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStructMatcher 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.RustStructStruct::name, name)); -- cgit v1.2.3