diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-14 19:39:31 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-14 19:39:31 +0100 |
| commit | 6cb75452ad193e1e40a092478ab527513a7e2bda (patch) | |
| tree | 8ca3317162200d3bc6524a1f0f4c7b84e5064b04 /src/main | |
| parent | 514f2c0437a5ccbf44a12b894e6a42a6c780b49d (diff) | |
Add attributes to Rust structs
Diffstat (limited to 'src/main')
4 files changed, 75 insertions, 11 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java index aab7284..28926a3 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java @@ -123,7 +123,7 @@ public class RustTypesGenerator implements Generator { )) .toList(); - var rustStruct = new RustStructStruct(rustStructName, rustFields, structDefinition.docComment()); + var rustStruct = new RustStructStruct(List.of(), rustStructName, rustFields, structDefinition.docComment()); return List.of(rustStruct); } 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 3d85056..581d08a 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 @@ -118,6 +118,7 @@ public class RustWriter implements AutoCloseable { private void writeStructStruct(RustStructStruct structStruct) throws IOException { this.writeDocComment(structStruct.docComment()); + this.writerOuterAttributes(structStruct.attributes()); this.writer.write("pub struct "); this.writeIdentifier(structStruct.name()); this.writer.write(" {"); @@ -248,6 +249,21 @@ public class RustWriter implements AutoCloseable { } } + // == Attributes == + + private void writerOuterAttributes(List<RustOuterAttribute> attributes) throws IOException { + for (var attribute : attributes) { + writeOuterAttribute(attribute); + } + } + + private void writeOuterAttribute(RustOuterAttribute attribute) throws IOException { + this.writer.write("#["); + this.writeTypePath(attribute.path()); + this.writer.write("]"); + this.writer.newLine(); + } + // == Statements == void writeStatement(RustStatement statement) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttribute.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttribute.java new file mode 100644 index 0000000..862685a --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustOuterAttribute.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.RustOuterAttribute imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute imports + +public record RustOuterAttribute(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath path) { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath path) { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute(path); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder withPath(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath path) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder(path); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder withPath(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath.Builder path) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder(path.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStruct.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStruct.java index 4eba76a..0bb57b7 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStruct.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructStruct.java @@ -5,42 +5,58 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct imports -public record RustStructStruct(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField> fields, org.zwobble.hobgoblin.compiler.ast.DocComment docComment) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem { +public record RustStructStruct(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute> attributes, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField> fields, org.zwobble.hobgoblin.compiler.ast.DocComment docComment) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment()); } - public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField> fields, org.zwobble.hobgoblin.compiler.ast.DocComment docComment) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem.Builder { + public record Builder(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute> attributes, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField> fields, org.zwobble.hobgoblin.compiler.ast.DocComment docComment) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem.Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct build() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct(name, fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct(attributes, name, fields, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder withAttributes(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute> attributes) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder addAttribute(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute attribute) { + var attributes = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute>(this.attributes); + attributes.add(attribute); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder addAttribute(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute.Builder attribute) { + var attributes = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustOuterAttribute>(this.attributes); + attributes.add(attribute.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(name, fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.Builder name) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(name.build(), fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name.build(), fields, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder withFields(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField> fields) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(name, fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder addField(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField field) { var fields = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField>(this.fields); fields.add(field); - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(name, fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder addField(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField.Builder field) { var fields = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructField>(this.fields); fields.add(field.build()); - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(name, fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(name, fields, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder(attributes, name, fields, docComment); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructStruct.Builder body |
