-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroundtrip.rs
76 lines (62 loc) · 1.76 KB
/
roundtrip.rs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
fs::{self, File},
io::{self, Write},
};
fn main() -> io::Result<()> {
let save = Save {
greeting: "Hello world!".to_string(),
keybinds: BTreeMap::from_iter(vec![
(Action::Up, 'W'),
(Action::Down, 'S'),
(Action::Left, 'A'),
(Action::Right, 'D'),
]),
difficulty_factor: 4.3,
respawn_point: (1107.148717794, 1249.045772398),
inventory: vec![
Item::Water,
Item::CannedFood,
Item::IdCard(101),
Item::RocketLauncher {
damage: 250,
explosion_radius: 60.0,
},
],
};
File::create("examples/roundtrip.keon")?
.write_all(keon::to_string_pretty(&save).expect("serialization failed").as_bytes())?;
let s = fs::read_to_string("examples/roundtrip.keon")?;
println!("{}", s);
let save_back = keon::from_str(&s).expect("deserialization failed");
assert_eq!(save, save_back);
println!(
"\nCompare to JSON: \n\n{}",
serde_json::to_string_pretty(&save).unwrap()
);
Ok(())
}
//------------------------------------------------------------------------------
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Save {
greeting: String,
keybinds: BTreeMap<Action, char>,
difficulty_factor: f32,
respawn_point: (f32, f32),
inventory: Vec<Item>,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
enum Action {
Up,
Down,
Left,
Right,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Item {
Water,
CannedFood,
IdCard(u32),
RocketLauncher { damage: i32, explosion_radius: f32 },
}