diff options
3 files changed, 114 insertions, 33 deletions
diff --git a/examples/10-transient-0/output/rust/src/gen/transient_0.rs b/examples/10-transient-0/output/rust/src/gen/transient_0.rs index 024ad03..68aa0eb 100644 --- a/examples/10-transient-0/output/rust/src/gen/transient_0.rs +++ b/examples/10-transient-0/output/rust/src/gen/transient_0.rs @@ -1,12 +1,28 @@ // Generated by hobgoblin. pub fn encode_bool(value: &::core::primitive::bool, writer: &mut impl std::io::Write) -> ::std::io::Result::<()> { - todo!(); + if *value { + writer.write_all(&1u8.to_le_bytes())?; + } else { + writer.write_all(&0u8.to_le_bytes())?; + }; ::std::io::Result::Ok(()) } pub fn decode_bool(reader: &mut impl std::io::Read) -> ::std::io::Result::<::core::primitive::bool> { - std::io::Result::Ok(todo!()) + let int_value = { + let bytes = { + let mut bytes = [0; 1]; + reader.read_exact(&mut bytes)?; + bytes + }; + u8::from_le_bytes(bytes) + }; + std::io::Result::Ok(if int_value == 0 { + false + } else { + true + }) } pub fn encode_int_32(value: &::core::primitive::i32, writer: &mut impl std::io::Write) -> ::std::io::Result::<()> { diff --git a/examples/10-transient-0/output/rust/src/lib.rs b/examples/10-transient-0/output/rust/src/lib.rs index 80fa64f..75a7b47 100644 --- a/examples/10-transient-0/output/rust/src/lib.rs +++ b/examples/10-transient-0/output/rust/src/lib.rs @@ -4,7 +4,18 @@ pub mod transient_0; #[cfg(test)] mod test { use std::io::Cursor; - use super::data::{InnerStruct, OuterStruct, StructWithInt32, StructWithInt64}; + use super::data::{InnerStruct, OuterStruct, StructWithBool, StructWithInt32, StructWithInt64}; + + #[test] + fn struct_with_bool() { + let value = StructWithBool { a: true, b: false }; + + assert_round_trip_encoding( + value, + super::data::transient_0::encode_struct_with_bool, + super::data::transient_0::decode_struct_with_bool, + ); + } #[test] fn struct_with_int_32() { 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 d96130b..5639ca2 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 @@ -84,17 +84,60 @@ public class RustTransient0Generator implements Generator { return generateEncodeFunction( NativeTypes.BOOL, List.of( - new RustExpressionStatement(generateTodo()) + new RustExpressionStatement(new RustIfExpression( + new RustPrefixExpression( + RustPrefixOperator.DEREFERENCE, + RustPath.of(VALUE_NAME) + ), + new RustBlockExpression( + List.of( + generateWriterWriteInt( + new RustIntegerLiteral(1, Optional.of(RustIdentifier.of("u8"))) + ) + ), + Optional.empty() + ), + new RustBlockExpression( + List.of( + generateWriterWriteInt( + new RustIntegerLiteral(0, Optional.of(RustIdentifier.of("u8"))) + ) + ), + Optional.empty() + ) + )) ) ); } private RustItem generateDecodeBoolFunction() { + var intValue = RustIdentifier.of("int_value"); + return generateDecodeFunction( NativeTypes.BOOL, new RustBlockExpression( - List.of(), - Optional.of(generateTodo()) + List.of( + new RustLetStatement( + intValue, + false, + generateReaderReadInt(RustPath.of("u8"), 8) + ) + ), + Optional.of(new RustIfExpression( + new RustBinaryExpression( + RustBinaryOperator.EQUAL, + RustPath.of(intValue), + new RustIntegerLiteral(0, Optional.empty()) + ), + new RustBlockExpression( + List.of(), + Optional.of(new RustBoolLiteral(false)) + ), + new RustBlockExpression( + List.of(), + Optional.of(new RustBoolLiteral(true)) + ) + )) ) ); } @@ -119,44 +162,55 @@ public class RustTransient0Generator implements Generator { return generateEncodeFunction( type, List.of( - new RustExpressionStatement( - generateWriterWrite(new RustPrefixExpression( - RustPrefixOperator.BORROW, - new RustCallExpression( - new RustFieldExpression( - RustPath.of(VALUE_NAME), - RustIdentifier.of("to_le_bytes") - ), - List.of() - ) - )) - ) + generateWriterWriteInt(RustPath.of(VALUE_NAME)) ) ); } private RustItem generateDecodeIntFunction(SimpleNativeType type, int bits) { - var bytes = RustIdentifier.of("bytes"); - return generateDecodeFunction( type, - new RustBlockExpression( - List.of( - new RustLetStatement( - bytes, - false, - generateReaderReadExact(bits / 8) - ) - ), - Optional.of(new RustCallExpression( - this.rustGenerator.generateRustTypeExpression(type) - .addSegment(RustPathSegment.of(RustIdentifier.of("from_le_bytes"))), - List.of(RustPath.of(bytes)) - )) + generateReaderReadInt( + this.rustGenerator.generateRustTypeExpression(type), + bits ) ); } + private RustStatement generateWriterWriteInt(RustExpression intExpression) { + return new RustExpressionStatement( + generateWriterWrite(new RustPrefixExpression( + RustPrefixOperator.BORROW, + new RustCallExpression( + new RustFieldExpression( + intExpression, + RustIdentifier.of("to_le_bytes") + ), + List.of() + ) + )) + ); + } + + private RustBlockExpression generateReaderReadInt(RustPath type, int bits) { + var bytes = RustIdentifier.of("bytes"); + + return new RustBlockExpression( + List.of( + new RustLetStatement( + bytes, + false, + generateReaderReadExact(bits / 8) + ) + ), + Optional.of(new RustCallExpression( + type + .addSegment(RustPathSegment.of(RustIdentifier.of("from_le_bytes"))), + List.of(RustPath.of(bytes)) + )) + ); + } + private RustItem generateEncodeStringFunction() { return generateEncodeFunction( NativeTypes.STRING, |
