1 #[allow(dead_code, unused_imports)]
2 #[path = "../../monster_test_serialize/mod.rs"]
3 mod monster_test_serialize_generated;
4 pub use monster_test_serialize_generated::my_game;
5
create_serialized_example_with_generated_code(builder: &mut flatbuffers::FlatBufferBuilder)6 fn create_serialized_example_with_generated_code(builder: &mut flatbuffers::FlatBufferBuilder) {
7 let mon = {
8 let s0 = builder.create_string("test1");
9 let s1 = builder.create_string("test2");
10 let fred_name = builder.create_string("Fred");
11
12 // can't inline creation of this Vec3 because we refer to it by reference, so it must live
13 // long enough to be used by MonsterArgs.
14 let pos = my_game::example::Vec3::new(
15 1.0,
16 2.0,
17 3.0,
18 3.0,
19 my_game::example::Color::Green,
20 &my_game::example::Test::new(5i16, 6i8),
21 );
22
23 let args = my_game::example::MonsterArgs {
24 hp: 80,
25 mana: 150,
26 name: Some(builder.create_string("MyMonster")),
27 pos: Some(&pos),
28 test_type: my_game::example::Any::Monster,
29 test: Some(
30 my_game::example::Monster::create(
31 builder,
32 &my_game::example::MonsterArgs {
33 name: Some(fred_name),
34 ..Default::default()
35 },
36 )
37 .as_union_value(),
38 ),
39 inventory: Some(builder.create_vector_direct(&[0u8, 1, 2, 3, 4][..])),
40 test4: Some(builder.create_vector_direct(&[
41 my_game::example::Test::new(10, 20),
42 my_game::example::Test::new(30, 40),
43 ])),
44 testarrayofstring: Some(builder.create_vector(&[s0, s1])),
45 ..Default::default()
46 };
47 my_game::example::Monster::create(builder, &args)
48 };
49 my_game::example::finish_monster_buffer(builder, mon);
50 }
51
main()52 fn main() {
53 // This test is built into its own crate because it has a different set of
54 // dependencies as the normal Rust tests; it requires that the `flatbuffer`
55 // dependency have the "serialize" feature enabled. As this feature may
56 // cause extra code gen and dependencies for those who do not need it, it
57 // is disabled by default.
58 let mut builder = flatbuffers::FlatBufferBuilder::new();
59 create_serialized_example_with_generated_code(&mut builder);
60
61 let data = builder.finished_data();
62 let obj = my_game::example::root_as_monster(&data[..]).unwrap();
63 let value = serde_json::to_value(&obj).unwrap();
64 let o = value.as_object().unwrap();
65
66 let pos = o.get("pos").unwrap().as_object().unwrap();
67 assert_eq!(pos.get("x").unwrap().as_f64().unwrap(), 1.0);
68 assert_eq!(pos.get("y").unwrap().as_f64().unwrap(), 2.0);
69 assert_eq!(pos.get("z").unwrap().as_f64().unwrap(), 3.0);
70
71 let mana = o.get("mana").unwrap();
72 assert_eq!(mana.as_i64().unwrap(), 150);
73
74 let hp = o.get("hp").unwrap();
75 assert_eq!(hp.as_i64().unwrap(), 80);
76
77 let name = o.get("name").unwrap();
78 assert_eq!(name.as_str().unwrap(), "MyMonster");
79
80 let test_type = o.get("test_type").unwrap();
81 assert_eq!(test_type.as_str().unwrap(), "Monster");
82
83 let testarrayofstring = o.get("testarrayofstring").unwrap().as_array().unwrap();
84 let t0 = testarrayofstring[0].as_str().unwrap();
85 assert_eq!(t0, "test1");
86
87 let t1 = testarrayofstring[1].as_str().unwrap();
88 assert_eq!(t1, "test2");
89 }
90