pub mod data; pub mod transient_0; #[cfg(test)] mod test { use std::io::Cursor; use super::data::{InnerStruct, OuterStruct, StructWithBool, StructWithInt32, StructWithInt64, StructWithString}; #[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() { let value = StructWithInt32 { a: 10, b: 25 }; assert_round_trip_encoding( value, super::data::transient_0::encode_struct_with_int_32, super::data::transient_0::decode_struct_with_int_32, ); } #[test] fn struct_with_int_64() { let value = StructWithInt64 { a: 10, b: 25 }; assert_round_trip_encoding( value, super::data::transient_0::encode_struct_with_int_64, super::data::transient_0::decode_struct_with_int_64, ); } #[test] fn struct_with_string() { let value = StructWithString { a: "abc".to_string(), b: "def".to_string() }; assert_round_trip_encoding( value, super::data::transient_0::encode_struct_with_string, super::data::transient_0::decode_struct_with_string, ); } #[test] fn nested_struct() { let value = OuterStruct { inner: InnerStruct { a: 10, b: 25 }, c: 42 }; assert_round_trip_encoding( value, super::data::transient_0::encode_outer_struct, super::data::transient_0::decode_outer_struct, ); } fn assert_round_trip_encoding( value: T, encode: impl Fn(&T, &mut Cursor>) -> std::io::Result<()>, decode: impl Fn(&mut Cursor>) -> std::io::Result, ) { let mut bytes = Cursor::new(Vec::new()); encode(&value, &mut bytes).unwrap(); bytes.set_position(0); let decoded_value = decode(&mut bytes).unwrap(); assert_eq!(value, decoded_value); } }