From 7eed843944a247a99e48bdb6f9dc3aac17d983c5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 18 Jul 2026 19:32:20 +0100 Subject: Support options in rust-transient-0 --- .../rusttransient0/RustTransient0Generator.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'src/main') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java index a17ae6b..7122e54 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java @@ -405,6 +405,8 @@ public class RustTransient0Generator implements Generator { case ConstructedNativeType constructedNativeType -> { if (constructedNativeType.constructor().equals(NativeTypes.LIST)) { yield generateEncodeList(value, constructedNativeType.args().getFirst()); + } else if (constructedNativeType.constructor().equals(NativeTypes.OPTION)) { + yield generateEncodeOption(value, constructedNativeType.args().getFirst()); } else { yield List.of(new RustExpressionStatement(generateTodo())); } @@ -441,6 +443,8 @@ public class RustTransient0Generator implements Generator { case ConstructedNativeType constructedNativeType -> { if (constructedNativeType.constructor().equals(NativeTypes.LIST)) { yield generateDecodeList(constructedNativeType.args().getFirst()); + } else if (constructedNativeType.constructor().equals(NativeTypes.OPTION)) { + yield generateDecodeOption(constructedNativeType.args().getFirst()); } else { yield generateTodo(); } @@ -560,6 +564,76 @@ public class RustTransient0Generator implements Generator { ); } + private List generateEncodeOption(RustExpression value, Type elementType) { + return List.of( + // TODO: pattern matching + new RustExpressionStatement(new RustIfExpression( + methodCall(value, RustIdentifier.of("is_some"), List.of()), + new RustBlockExpression( + List.of( + new RustExpressionStatement(new RustBlockExpression( + generateEncode( + new RustPrefixExpression(RustPrefixOperator.BORROW, new RustBoolLiteral(true)), + NativeTypes.BOOL + ), + Optional.empty() + )), + new RustExpressionStatement(new RustBlockExpression( + generateEncode( + new RustPrefixExpression( + RustPrefixOperator.BORROW, + methodCall( + methodCall(value, RustIdentifier.of("as_ref"), List.of()), + RustIdentifier.of("unwrap"), + List.of() + ) + ), + elementType + ), + Optional.empty() + )) + ), + Optional.empty() + ), + new RustBlockExpression( + generateEncode( + new RustPrefixExpression(RustPrefixOperator.BORROW, new RustBoolLiteral(false)), + NativeTypes.BOOL + ), + Optional.empty() + ) + )) + ); + } + + private RustExpression generateDecodeOption(Type elementType) { + var isSome = RustIdentifier.of("is_some"); + + return new RustBlockExpression( + List.of( + new RustLetStatement(isSome, false, generateDecode(NativeTypes.BOOL)) + ), + Optional.of(new RustIfExpression( + RustPath.of(isSome), + new RustBlockExpression( + List.of(), + Optional.of( + new RustCallExpression( + RustPath.global("std", "option", "Option", "Some"), + List.of(generateDecode(elementType)) + ) + ) + ), + new RustBlockExpression( + List.of(), + Optional.of( + RustPath.global("std", "option", "Option", "None") + ) + ) + )) + ); + } + private RustExpression generateWriterWrite(RustExpression valueToWrite) { return new RustTryPropagationExpression(new RustCallExpression( new RustFieldExpression( -- cgit v1.2.3