diff options
3 files changed, 17 insertions, 24 deletions
diff --git a/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs b/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs index 2ce685d..2caeb3a 100644 --- a/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs +++ b/examples/10-transient-0/output/rust/src/gen/data/transient_0.rs @@ -74,13 +74,13 @@ pub fn decode_inner_struct(reader: &mut impl std::io::Read) -> ::std::io::Result pub fn encode_struct_with_list(value: &crate::data::StructWithList, writer: &mut impl std::io::Write) -> ::std::io::Result::<()> { { - crate::transient_0::encode_int_64(&(((&value.a).len()).try_into()).unwrap(), writer)?; + crate::transient_0::encode_int_64(&(((&value.a).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer)?; for element in &value.a { crate::transient_0::encode_int_64(element, writer)?; }; }; { - crate::transient_0::encode_int_64(&(((&value.b).len()).try_into()).unwrap(), writer)?; + crate::transient_0::encode_int_64(&(((&value.b).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer)?; for element in &value.b { crate::data::transient_0::encode_inner_struct(element, writer)?; }; @@ -91,7 +91,7 @@ pub fn encode_struct_with_list(value: &crate::data::StructWithList, writer: &mut pub fn decode_struct_with_list(reader: &mut impl std::io::Read) -> ::std::io::Result::<crate::data::StructWithList> { let a = { let len = crate::transient_0::decode_int_64(reader)?; - let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).unwrap()); + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); for _ in 0..len { elements.push(crate::transient_0::decode_int_64(reader)?); }; @@ -99,7 +99,7 @@ pub fn decode_struct_with_list(reader: &mut impl std::io::Read) -> ::std::io::Re }; let b = { let len = crate::transient_0::decode_int_64(reader)?; - let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).unwrap()); + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); for _ in 0..len { elements.push(crate::data::transient_0::decode_inner_struct(reader)?); }; 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 e57324a..560fe51 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 @@ -54,7 +54,7 @@ pub fn decode_int_64(reader: &mut impl std::io::Read) -> ::std::io::Result::<::c } pub fn encode_string(value: &::std::string::String, writer: &mut impl std::io::Write) -> ::std::io::Result::<()> { - crate::transient_0::encode_int_64(&((value.len()).try_into()).unwrap(), writer)?; + crate::transient_0::encode_int_64(&((value.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer)?; writer.write_all(value.as_bytes())?; ::std::io::Result::Ok(()) } @@ -62,7 +62,7 @@ pub fn encode_string(value: &::std::string::String, writer: &mut impl std::io::W pub fn decode_string(reader: &mut impl std::io::Read) -> ::std::io::Result::<::std::string::String> { let len = crate::transient_0::decode_int_64(reader)?; let bytes = { - let mut bytes = vec![0u8; (len.try_into()).unwrap()]; + let mut bytes = vec![0u8; (len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?]; reader.read_exact(&mut bytes)?; bytes }; 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 785d20e..700b74f 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 @@ -222,7 +222,7 @@ public class RustTransient0Generator implements Generator { generateEncode( new RustPrefixExpression( RustPrefixOperator.BORROW, - tryIntoOrUnwrap(methodCall( + tryIntoOrInvalidData(methodCall( RustPath.of(VALUE_NAME), RustIdentifier.of("len"), List.of() @@ -676,7 +676,7 @@ public class RustTransient0Generator implements Generator { generateEncode( new RustPrefixExpression( RustPrefixOperator.BORROW, - tryIntoOrUnwrap(methodCall(value, RustIdentifier.of("len"), List.of())) + tryIntoOrInvalidData(methodCall(value, RustIdentifier.of("len"), List.of())) ), NativeTypes.INT_64 ), @@ -704,7 +704,7 @@ public class RustTransient0Generator implements Generator { new RustLetStatement(length, false, generateDecode(NativeTypes.INT_64)), new RustLetStatement(elements, true, new RustCallExpression( RustPath.global("std", "vec", "Vec", "with_capacity"), - List.of(tryIntoOrUnwrap(RustPath.of(length))) + List.of(tryIntoOrInvalidData(RustPath.of(length))) )), new RustExpressionStatement(new RustIteratorLoopExpression( new RustWildcardPattern(), @@ -838,7 +838,7 @@ public class RustTransient0Generator implements Generator { private RustExpression generateReaderReadExact(RustExpression length) { var bytes = RustIdentifier.of("bytes"); - var lengthUsize = tryIntoOrUnwrap(length); + var lengthUsize = tryIntoOrInvalidData(length); return new RustBlockExpression( List.of( @@ -894,20 +894,13 @@ public class RustTransient0Generator implements Generator { ); } - private RustExpression tryIntoOrUnwrap(RustExpression value) { - return unwrap(methodCall( - value, - RustIdentifier.of("try_into"), - List.of() + private RustExpression tryIntoOrInvalidData(RustExpression value) { + return new RustTryPropagationExpression(mapErrToInvalidData( + methodCall( + value, + RustIdentifier.of("try_into"), + List.of() + ) )); } - - private RustExpression unwrap(RustExpression value) { - // TODO: remove unwrap - return methodCall( - value, - RustIdentifier.of("unwrap"), - List.of() - ); - } } |
