summaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java111
1 files changed, 108 insertions, 3 deletions
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 5639ca2..f5e1f25 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
@@ -215,17 +215,68 @@ public class RustTransient0Generator implements Generator {
return generateEncodeFunction(
NativeTypes.STRING,
List.of(
- new RustExpressionStatement(generateTodo())
+ new RustExpressionStatement(new RustBlockExpression(
+ generateEncode(
+ new RustPrefixExpression(
+ RustPrefixOperator.BORROW,
+ methodCall(
+ methodCall(
+ methodCall(
+ RustPath.of(VALUE_NAME),
+ RustIdentifier.of("len"),
+ List.of()
+ ),
+ RustIdentifier.of("try_into"),
+ List.of()
+ ),
+ // TODO: remove unwrap
+ RustIdentifier.of("unwrap"),
+ List.of()
+ )
+ ),
+ NativeTypes.INT_64
+ ),
+ Optional.empty()
+ )),
+ new RustExpressionStatement(generateWriterWrite(
+ methodCall(
+ RustPath.of(VALUE_NAME),
+ RustIdentifier.of("as_bytes"),
+ List.of()
+ )
+ ))
)
);
}
private RustItem generateDecodeStringFunction() {
+ var len = RustIdentifier.of("len");
+ var bytes = RustIdentifier.of("bytes");
+
return generateDecodeFunction(
NativeTypes.STRING,
new RustBlockExpression(
- List.of(),
- Optional.of(generateTodo())
+ List.of(
+ new RustLetStatement(
+ len,
+ false,
+ generateDecode(NativeTypes.INT_64)
+ ),
+ new RustLetStatement(
+ bytes,
+ false,
+ generateReaderReadExact(RustPath.of(len))
+ )
+ ),
+ Optional.of(methodCall(
+ new RustCallExpression(
+ RustPath.global("std", "string", "String", "from_utf8"),
+ List.of(RustPath.of(bytes))
+ ),
+ // TODO: remove unwrap
+ RustIdentifier.of("unwrap"),
+ List.of()
+ ))
)
);
}
@@ -494,6 +545,46 @@ public class RustTransient0Generator implements Generator {
);
}
+ private RustExpression generateReaderReadExact(RustExpression length) {
+ var bytes = RustIdentifier.of("bytes");
+
+ // TODO: extract into let
+ var lengthUsize = methodCall(
+ methodCall(
+ length,
+ RustIdentifier.of("try_into"),
+ List.of()
+ ),
+ // TODO: remove unwrap
+ RustIdentifier.of("unwrap"),
+ List.of()
+ );
+
+ return new RustBlockExpression(
+ List.of(
+ new RustLetStatement(
+ bytes,
+ true,
+ // TODO: use uninit
+ new RustVecRepeatExpression(new RustIntegerLiteral(0, Optional.of(RustIdentifier.of("u8"))), lengthUsize)
+ ),
+ new RustExpressionStatement(new RustTryPropagationExpression(new RustCallExpression(
+ new RustFieldExpression(
+ RustPath.of(READER_NAME),
+ RustIdentifier.of("read_exact")
+ ),
+ List.of(
+ new RustPrefixExpression(
+ RustPrefixOperator.BORROW_MUTABLE,
+ RustPath.of(bytes)
+ )
+ )
+ )))
+ ),
+ Optional.of(RustPath.of(bytes))
+ );
+ }
+
private RustExpression generateTodo() {
return new RustCallExpression(
RustPath.of("todo!"),
@@ -508,4 +599,18 @@ public class RustTransient0Generator implements Generator {
private RustIdentifier decodeMethodName(Type type) {
return this.rustGenerator.generateFieldName("decode" + type.name());
}
+
+ private RustExpression methodCall(
+ RustExpression receiver,
+ RustIdentifier methodName,
+ List<RustExpression> args
+ ) {
+ return new RustCallExpression(
+ new RustFieldExpression(
+ receiver,
+ methodName
+ ),
+ args
+ );
+ }
}