diff options
Diffstat (limited to 'src/main/java')
3 files changed, 46 insertions, 54 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 a7d39eb..66a19fe 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 @@ -149,6 +149,8 @@ public class RustTypesGenerator implements Generator { TypedSumDefinitionNode sumDefinition, Context context ) { + var items = new ArrayList<RustItem>(); + var rustEnumName = generateTypeName(sumDefinition.name()); var rustVariants = sumDefinition.variants().stream() @@ -170,61 +172,51 @@ public class RustTypesGenerator implements Generator { .toList(); var rustEnum = new RustEnum(rustEnumName, rustVariants, sumDefinition.docComment()); + items.add(rustEnum); + + for (var variant : sumDefinition.variants()) { + var variantType = generateRustTypeExpression(variant.type(), context); + var variantName = generateVariantName(variant.type().value()); + var innerValueName = new RustIdentifier("value"); + RustExpression innerValue = new RustPathInExpression(List.of( + new RustPathExprSegment( + new RustPathIdentSegmentIdentifier(innerValueName), + Optional.empty() + ) + )); + + if (variant.isBox()) { + innerValue = new RustCallExpression( + RustPathInExpression.global("std", "boxed", "Box", "new"), + List.of(innerValue) + ); + } - var rustCreateVariantFunctions = sumDefinition.variants().stream() - .<RustAssociatedItem>map(variant -> { - var variantType = generateRustTypeExpression(variant.type(), context); - -// if (variant.isBox()) { -// variantType = RustTypePath.global("std", "boxed", "Box") -// .withArgs(List.of(variantType)); -// } -// - var variantName = generateVariantName(variant.type().value()); -// -// return new RustEnumVariant( -// variantName, -// new RustEnumVariantTuple(List.of( -// new RustTupleField(variantType) -// )) -// ); - - var innerValueName = new RustIdentifier("value"); - RustExpression innerValue = new RustPathInExpression(List.of( - new RustPathExprSegment( - new RustPathIdentSegmentIdentifier(innerValueName), - Optional.empty() - ) - )); - - if (variant.isBox()) { - innerValue = new RustCallExpression( - RustPathInExpression.global("std", "boxed", "Box", "new"), - List.of(innerValue) - ); - } - - return new RustFunction( - new RustIdentifier(Casing.upperCamelCaseToSnakeCase(variantName.value())), - List.of( - new RustFunctionParam(innerValueName, variantType) - ), - Optional.of(RustTypePath.selfType()), - Optional.of(new RustBlockExpression( - List.of(), - Optional.of( - new RustCallExpression( - RustPathInExpression.selfType(variantName), - List.of(innerValue) + var fromImpl = new RustTraitImpl( + RustTypePath.global("std", "convert", "From") + .withArgs(List.of(variantType)), + generateRustTypeExpression(sumDefinition.type(), context), + List.of( + new RustFunction( + new RustIdentifier("from"), + List.of(new RustFunctionParam(innerValueName, variantType)), + Optional.of(RustTypePath.selfType()), + Optional.of(new RustBlockExpression( + List.of(), + Optional.of( + new RustCallExpression( + RustPathInExpression.selfType(variantName), + List.of(innerValue) + ) ) - ) - )) - ); - }) - .toList(); - var rustImpl = new RustInherentImpl(RustTypePath.of(rustEnumName), rustCreateVariantFunctions); + )) + ) + ) + ); - return List.of(rustEnum, rustImpl); + items.add(fromImpl); + } + return items; } private RustType generateRustTypeExpression( 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 cc00f82..3d85056 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 @@ -82,7 +82,7 @@ public class RustWriter implements AutoCloseable { // === Functions === private void writeFunction(RustFunction function) throws IOException { - this.writer.write("pub fn "); + this.writer.write("fn "); this.writeIdentifier(function.name()); this.writer.write("("); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java index 797f770..6ce5f7f 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java @@ -90,7 +90,7 @@ public record RustTypePath(java.util.List<org.zwobble.hobgoblin.compiler.output. return new RustTypePath(segments); } - public RustType withArgs(List<RustType> args) { + public RustTypePath withArgs(List<RustType> args) { var segments = new ArrayList<>(this.segments); var lastSegment = segments.removeLast().withArgs(args); segments.add(lastSegment); |
