summaryrefslogtreecommitdiff
path: root/examples/10-transient-0/output/rust/src/lib.rs
blob: 64686749f571654d486411c71fe983a197e8e837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pub mod data;
pub mod transient_0;

#[cfg(test)]
mod test {
    use std::io::Cursor;
    use super::data::{InnerStruct, OuterStruct, StructWithInt32, StructWithInt64};

    #[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 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<T: std::cmp::PartialEq + std::fmt::Debug>(
        value: T,
        encode: impl Fn(&T, &mut Cursor<Vec<u8>>),
        decode: impl Fn(&mut Cursor<Vec<u8>>) -> T,
    ) {
        let mut bytes = Cursor::new(Vec::new());
        encode(&value, &mut bytes);

        bytes.set_position(0);
        let decoded_value = decode(&mut bytes);

        assert_eq!(value, decoded_value);
    }
}