diff options
Diffstat (limited to 'examples/10-transient-0')
| -rw-r--r-- | examples/10-transient-0/output/rust/src/gen/data/transient_0.rs | 40 | ||||
| -rw-r--r-- | examples/10-transient-0/output/rust/src/lib.rs | 30 |
2 files changed, 65 insertions, 5 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 f8e9be0..1d65042 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 @@ -109,14 +109,46 @@ pub fn decode_struct_with_list(reader: &mut impl std::io::Read) -> ::std::io::Re } pub fn encode_struct_with_option(value: &crate::data::StructWithOption, writer: &mut impl std::io::Write) -> ::std::io::Result::<()> { - todo!(); - todo!(); + if (&value.a).is_some() { + { + crate::transient_0::encode_bool(&true, writer)?; + }; + { + crate::transient_0::encode_int_64(&((&value.a).as_ref()).unwrap(), writer)?; + }; + } else { + crate::transient_0::encode_bool(&false, writer)?; + }; + if (&value.b).is_some() { + { + crate::transient_0::encode_bool(&true, writer)?; + }; + { + crate::data::transient_0::encode_inner_struct(&((&value.b).as_ref()).unwrap(), writer)?; + }; + } else { + crate::transient_0::encode_bool(&false, writer)?; + }; ::std::io::Result::Ok(()) } pub fn decode_struct_with_option(reader: &mut impl std::io::Read) -> ::std::io::Result::<crate::data::StructWithOption> { - let a = todo!(); - let b = todo!(); + let a = { + let is_some = crate::transient_0::decode_bool(reader)?; + if is_some { + ::std::option::Option::Some(crate::transient_0::decode_int_64(reader)?) + } else { + ::std::option::Option::None + } + }; + let b = { + let is_some = crate::transient_0::decode_bool(reader)?; + if is_some { + ::std::option::Option::Some(crate::data::transient_0::decode_inner_struct(reader)?) + } else { + ::std::option::Option::None + } + }; std::io::Result::Ok(crate::data::StructWithOption { a: a, b: b }) } diff --git a/examples/10-transient-0/output/rust/src/lib.rs b/examples/10-transient-0/output/rust/src/lib.rs index a06f73e..3de2597 100644 --- a/examples/10-transient-0/output/rust/src/lib.rs +++ b/examples/10-transient-0/output/rust/src/lib.rs @@ -4,7 +4,7 @@ pub mod transient_0; #[cfg(test)] mod test { use std::io::Cursor; - use super::data::{InnerStruct, OuterStruct, StructWithBool, StructWithInt32, StructWithInt64, StructWithList, StructWithString}; + use super::data::{InnerStruct, OuterStruct, StructWithBool, StructWithInt32, StructWithInt64, StructWithList, StructWithOption, StructWithString}; #[test] fn struct_with_bool() { @@ -79,6 +79,34 @@ mod test { ); } + #[test] + fn struct_with_option_none() { + let value = StructWithOption { + a: None, + b: None, + }; + + assert_round_trip_encoding( + value, + super::data::transient_0::encode_struct_with_option, + super::data::transient_0::decode_struct_with_option, + ); + } + + #[test] + fn struct_with_option_some() { + let value = StructWithOption { + a: Some(10), + b: Some(InnerStruct { a: 42, b: 47 }), + }; + + assert_round_trip_encoding( + value, + super::data::transient_0::encode_struct_with_option, + super::data::transient_0::decode_struct_with_option, + ); + } + fn assert_round_trip_encoding<T: std::cmp::PartialEq + std::fmt::Debug>( value: T, encode: impl Fn(&T, &mut Cursor<Vec<u8>>) -> std::io::Result<()>, |
