From bf60176622a9f1842e7515cb3f8738ca6263705e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Jul 2026 21:48:09 +0100 Subject: Return single statement from generateEncode() --- .../rusttransient0/RustTransient0Generator.java | 205 ++++++++++----------- 1 file changed, 97 insertions(+), 108 deletions(-) (limited to 'src/main/java') 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 fd7bd34..165880f 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 @@ -218,20 +218,17 @@ public class RustTransient0Generator implements Generator { return generateEncodeFunction( NativeTypes.STRING, List.of( - new RustExpressionStatement(new RustBlockExpression( - generateEncode( - new RustPrefixExpression( - RustPrefixOperator.BORROW, - tryIntoOrUnwrap(methodCall( - RustPath.of(VALUE_NAME), - RustIdentifier.of("len"), - List.of() - )) - ), - NativeTypes.INT_64 + generateEncode( + new RustPrefixExpression( + RustPrefixOperator.BORROW, + tryIntoOrUnwrap(methodCall( + RustPath.of(VALUE_NAME), + RustIdentifier.of("len"), + List.of() + )) ), - Optional.empty() - )), + NativeTypes.INT_64 + ), new RustExpressionStatement(generateWriterWrite( methodCall( RustPath.of(VALUE_NAME), @@ -298,26 +295,30 @@ public class RustTransient0Generator implements Generator { ) { return switch (statement) { case TypedEnumDefinitionNode enumDefinition -> { + var ordinalExpression = new RustMatchExpression( + RustPath.of(VALUE_NAME), + IntStream.range(0, enumDefinition.variants().size()) + .mapToObj(variantIndex -> { + var variant = enumDefinition.variants().get(variantIndex); + var variantPath = this.rustGenerator.generateRustTypeExpression(enumDefinition.type()) + .addSegment(RustPathSegment.of(this.rustGenerator.generateVariantName(variant.name()))); + + return new RustMatchArm( + new RustPathPattern(variantPath), + new RustIntegerLiteral(variantIndex, Optional.empty()) + ); + }) + .toList() + ); + var encodeFunction = generateEncodeFunction( enumDefinition.type(), - generateEncode(new RustPrefixExpression( - RustPrefixOperator.BORROW, - new RustMatchExpression( - RustPath.of(VALUE_NAME), - IntStream.range(0, enumDefinition.variants().size()) - .mapToObj(variantIndex -> { - var variant = enumDefinition.variants().get(variantIndex); - var variantPath = this.rustGenerator.generateRustTypeExpression(enumDefinition.type()) - .addSegment(RustPathSegment.of(this.rustGenerator.generateVariantName(variant.name()))); - - return new RustMatchArm( - new RustPathPattern(variantPath), - new RustIntegerLiteral(variantIndex, Optional.empty()) - ); - }) - .toList() - ) - ), NativeTypes.INT_32) + List.of( + generateEncode(new RustPrefixExpression( + RustPrefixOperator.BORROW, + ordinalExpression + ), NativeTypes.INT_32) + ) ); var decodeMatchArms = IntStream.range(0, enumDefinition.variants().size()) @@ -369,7 +370,7 @@ public class RustTransient0Generator implements Generator { generateEncodeFunction( structDefinition.type(), structDefinition.fields().stream() - .flatMap(field -> generateEncode( + .map(field -> generateEncode( new RustPrefixExpression( RustPrefixOperator.BORROW, new RustFieldExpression( @@ -378,7 +379,7 @@ public class RustTransient0Generator implements Generator { ) ), field.type().value() - ).stream()) + )) .toList() ), generateDecodeFunction( @@ -422,20 +423,14 @@ public class RustTransient0Generator implements Generator { new RustTupleStructPattern(variantPath, List.of(new RustIdentifierPattern(VALUE_NAME))), new RustBlockExpression( List.of( - new RustExpressionStatement(new RustBlockExpression( - generateEncode( - new RustPrefixExpression( - RustPrefixOperator.BORROW, - new RustIntegerLiteral(variantIndex, Optional.empty()) - ), - NativeTypes.INT_32 + generateEncode( + new RustPrefixExpression( + RustPrefixOperator.BORROW, + new RustIntegerLiteral(variantIndex, Optional.empty()) ), - Optional.empty() - )), - new RustExpressionStatement(new RustBlockExpression( - generateEncode(RustPath.of(VALUE_NAME), variantType), - Optional.empty() - )) + NativeTypes.INT_32 + ), + generateEncode(RustPath.of(VALUE_NAME), variantType) ), Optional.empty() ) @@ -536,7 +531,7 @@ public class RustTransient0Generator implements Generator { ); } - private List generateEncode(RustExpression value, Type type) { + private RustStatement generateEncode(RustExpression value, Type type) { return switch (type) { case ConstructedNativeType constructedNativeType -> { if (constructedNativeType.constructor().equals(NativeTypes.LIST)) { @@ -544,12 +539,12 @@ public class RustTransient0Generator implements Generator { } else if (constructedNativeType.constructor().equals(NativeTypes.OPTION)) { yield generateEncodeOption(value, constructedNativeType.args().getFirst()); } else { - yield List.of(new RustExpressionStatement(generateTodo())); + yield new RustExpressionStatement(generateTodo()); } } case EnumType enumType -> { - yield List.of(new RustExpressionStatement(generateTodo())); + yield new RustExpressionStatement(generateTodo()); } case SimpleNativeType nativeType -> { @@ -561,15 +556,15 @@ public class RustTransient0Generator implements Generator { } case SumType sumType -> { - yield List.of(new RustExpressionStatement(generateTodo())); + yield new RustExpressionStatement(generateTodo()); } case TypeLevelValueType typeLevelValueType -> { - yield List.of(new RustExpressionStatement(generateTodo())); + yield new RustExpressionStatement(generateTodo()); } case TypeParam typeParam -> { - yield List.of(new RustExpressionStatement(generateTodo())); + yield new RustExpressionStatement(generateTodo()); } }; } @@ -612,7 +607,7 @@ public class RustTransient0Generator implements Generator { }; } - private List generateEncode(RustExpression value, NamespaceName typeNamespaceName, Type type) { + private RustStatement generateEncode(RustExpression value, NamespaceName typeNamespaceName, Type type) { var rustEncodeFunctionPathSegments = new ArrayList<>( this.generateTransient0ModuleName(typeNamespaceName) ); @@ -621,12 +616,10 @@ public class RustTransient0Generator implements Generator { rustEncodeFunctionPathSegments ); - return List.of( - new RustExpressionStatement(new RustTryPropagationExpression(new RustCallExpression( - rustEncodeFunctionPath, - List.of(value, RustPath.of(WRITER_NAME)) - ))) - ); + return new RustExpressionStatement(new RustTryPropagationExpression(new RustCallExpression( + rustEncodeFunctionPath, + List.of(value, RustPath.of(WRITER_NAME)) + ))); } private RustExpression generateDecode(NamespaceName typeNamespaceName, Type type) { @@ -644,11 +637,11 @@ public class RustTransient0Generator implements Generator { )); } - private List generateEncodeList(RustExpression value, Type elementType) { + private RustStatement generateEncodeList(RustExpression value, Type elementType) { var element = RustIdentifier.of("element"); - return List.of( - new RustExpressionStatement(new RustBlockExpression( + return new RustExpressionStatement(new RustBlockExpression( + List.of( generateEncode( new RustPrefixExpression( RustPrefixOperator.BORROW, @@ -656,17 +649,19 @@ public class RustTransient0Generator implements Generator { ), NativeTypes.INT_64 ), - Optional.empty() - )), - new RustExpressionStatement(new RustIteratorLoopExpression( - new RustIdentifierPattern(element), - value, - new RustBlockExpression( - generateEncode(RustPath.of(element), elementType), - Optional.empty() - ) - )) - ); + new RustExpressionStatement(new RustIteratorLoopExpression( + new RustIdentifierPattern(element), + value, + new RustBlockExpression( + List.of( + generateEncode(RustPath.of(element), elementType) + ), + Optional.empty() + ) + )) + ), + Optional.empty() + )); } private RustExpression generateDecodeList(Type elementType) { @@ -699,46 +694,40 @@ 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() - )) + private RustStatement generateEncodeOption(RustExpression value, Type elementType) { + // TODO: pattern matching + return new RustExpressionStatement(new RustIfExpression( + methodCall(value, RustIdentifier.of("is_some"), List.of()), + new RustBlockExpression( + List.of( + generateEncode( + new RustPrefixExpression(RustPrefixOperator.BORROW, new RustBoolLiteral(true)), + NativeTypes.BOOL ), - Optional.empty() + generateEncode( + new RustPrefixExpression( + RustPrefixOperator.BORROW, + methodCall( + methodCall(value, RustIdentifier.of("as_ref"), List.of()), + RustIdentifier.of("unwrap"), + List.of() + ) + ), + elementType + ) ), - new RustBlockExpression( + Optional.empty() + ), + new RustBlockExpression( + List.of( generateEncode( new RustPrefixExpression(RustPrefixOperator.BORROW, new RustBoolLiteral(false)), NativeTypes.BOOL - ), - Optional.empty() - ) - )) - ); + ) + ), + Optional.empty() + ) + )); } private RustExpression generateDecodeOption(Type elementType) { -- cgit v1.2.3