From 066af00eebe662e5a589a4e274e1613201b36982 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 2 Jul 2026 09:57:35 +0100 Subject: Support translating sums into Rust types --- .../generators/rusttypes/RustTypesGenerator.java | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/main') 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 99151c4..e681a3e 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 @@ -92,7 +92,7 @@ public class RustTypesGenerator implements Generator { } case TypedSumDefinitionNode sumDefinition -> { - yield List.of(); + yield generateSumDefinition(sumDefinition, context); } }; } @@ -115,6 +115,26 @@ public class RustTypesGenerator implements Generator { return List.of(rustStruct); } + private List generateSumDefinition( + TypedSumDefinitionNode sumDefinition, + Context context + ) { + var rustEnumName = generateTypeName(sumDefinition.name()); + + var rustVariants = sumDefinition.variants().stream() + .map(variant -> new RustEnumVariant( + generateVariantName(variant.type().value()), + new RustEnumVariantTuple(List.of( + new RustTupleField(generateRustTypeExpression(variant.type(), context)) + )) + )) + .toList(); + + var rustEnum = new RustEnum(rustEnumName, rustVariants, sumDefinition.docComment()); + + return List.of(rustEnum); + } + private RustType generateRustTypeExpression( TypedTypeLevelExpressionNode type, Context context @@ -196,6 +216,15 @@ public class RustTypesGenerator implements Generator { return new RustIdentifier(Casing.lowerCamelCaseToSnakeCase(part)); } + private RustIdentifier generateVariantName(Type type) { + if (type instanceof StructType structType) { + return generateTypeName(structType.name()); + } else { + throw new UnsupportedOperationException("TODO"); + } + + } + private static class Context { private final NamespaceName currentNamespaceName; -- cgit v1.2.3