From 0be52b37ae8e3df1efc47a972eb320ec5964be3a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 17 Jul 2026 10:54:22 +0100 Subject: Extract generateEncode/generateDecode for named types --- .../rusttransient0/RustTransient0Generator.java | 105 +++++++++++++++------ 1 file changed, 76 insertions(+), 29 deletions(-) (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 02b0ae9..4cb385b 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 @@ -8,9 +8,7 @@ import org.zwobble.hobgoblin.compiler.output.generators.rust.RustGenerator; import org.zwobble.hobgoblin.compiler.output.generators.rust.RustGeneratorConfig; import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.*; import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo; -import org.zwobble.hobgoblin.compiler.types.NamespaceName; -import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; -import org.zwobble.hobgoblin.compiler.types.Type; +import org.zwobble.hobgoblin.compiler.types.*; import org.zwobble.json5.reader.Json5ObjectReader; import java.io.IOException; @@ -306,23 +304,31 @@ public class RustTransient0Generator implements Generator { private List generateEncode(RustExpression value, Type type) { return switch (type) { + case ConstructedNativeType constructedNativeType -> { + yield List.of(new RustExpressionStatement(generateTodo())); + } + + case EnumType enumType -> { + yield List.of(new RustExpressionStatement(generateTodo())); + } + case SimpleNativeType nativeType -> { - var rustEncodeFunctionPathSegments = new ArrayList<>( - this.generateTransient0ModuleName(nativeType.namespaceName()) - ); - rustEncodeFunctionPathSegments.add(encodeMethodName(nativeType)); - var rustEncodeFunctionPath = RustPath.crate( - rustEncodeFunctionPathSegments - ); + yield generateEncode(value, nativeType.namespaceName(), type); + } - yield List.of( - new RustExpressionStatement(new RustCallExpression( - rustEncodeFunctionPath, - List.of(value, RustPath.of(WRITER_NAME)) - )) - ); + case StructType structType -> { + yield List.of(new RustExpressionStatement(generateTodo())); + } + + case SumType sumType -> { + yield List.of(new RustExpressionStatement(generateTodo())); + } + + case TypeLevelValueType typeLevelValueType -> { + yield List.of(new RustExpressionStatement(generateTodo())); } - default -> { + + case TypeParam typeParam -> { yield List.of(new RustExpressionStatement(generateTodo())); } }; @@ -330,27 +336,68 @@ public class RustTransient0Generator implements Generator { private RustExpression generateDecode(Type type) { return switch (type) { + case ConstructedNativeType constructedNativeType -> { + yield generateTodo(); + } + + case EnumType enumType -> { + yield generateTodo(); + } + case SimpleNativeType nativeType -> { - var rustDecodeFunctionPathSegments = new ArrayList<>( - this.generateTransient0ModuleName(nativeType.namespaceName()) - ); - rustDecodeFunctionPathSegments.add(decodeMethodName(nativeType)); - var rustDecodeFunctionPath = RustPath.crate( - rustDecodeFunctionPathSegments - ); + yield generateDecode(nativeType.namespaceName(), type); + } - yield new RustCallExpression( - rustDecodeFunctionPath, - List.of(RustPath.of(READER_NAME)) - ); + case StructType structType -> { + yield generateTodo(); + } + + case SumType sumType -> { + yield generateTodo(); + } + + case TypeLevelValueType typeLevelValueType -> { + yield generateTodo(); } - default -> { + case TypeParam typeParam -> { yield generateTodo(); } }; } + private List generateEncode(RustExpression value, NamespaceName typeNamespaceName, Type type) { + var rustEncodeFunctionPathSegments = new ArrayList<>( + this.generateTransient0ModuleName(typeNamespaceName) + ); + rustEncodeFunctionPathSegments.add(encodeMethodName(type)); + var rustEncodeFunctionPath = RustPath.crate( + rustEncodeFunctionPathSegments + ); + + return List.of( + new RustExpressionStatement(new RustCallExpression( + rustEncodeFunctionPath, + List.of(value, RustPath.of(WRITER_NAME)) + )) + ); + } + + private RustExpression generateDecode(NamespaceName typeNamespaceName, Type type) { + var rustDecodeFunctionPathSegments = new ArrayList<>( + this.generateTransient0ModuleName(typeNamespaceName) + ); + rustDecodeFunctionPathSegments.add(decodeMethodName(type)); + var rustDecodeFunctionPath = RustPath.crate( + rustDecodeFunctionPathSegments + ); + + return new RustCallExpression( + rustDecodeFunctionPath, + List.of(RustPath.of(READER_NAME)) + ); + } + private RustExpression generateWriterWrite(RustExpression valueToWrite) { return new RustCallExpression( new RustFieldExpression( -- cgit v1.2.3