diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-02 09:57:35 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-02 09:57:35 +0100 |
| commit | 066af00eebe662e5a589a4e274e1613201b36982 (patch) | |
| tree | 985cf23c7288ca11c51611b744fd5dbde2031fa7 /src/main | |
| parent | 3d6034f51eba69de538ce2b7ccdd59654870db1d (diff) | |
Support translating sums into Rust types
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java | 31 |
1 files changed, 30 insertions, 1 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 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<RustItem> 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> 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; |
