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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::io;
use toml;
use rustc_serialize::{Decodable, Encodable};
use std::collections::BTreeMap;
use {GameError, GameResult};
#[derive(RustcDecodable, RustcEncodable, Debug, PartialEq, Default)]
pub struct Conf {
pub version: String,
pub window_title: String,
pub window_icon: String,
pub window_height: u32,
pub window_width: u32,
}
impl Conf {
pub fn new() -> Conf {
Conf {
version: String::from("0.0.0"),
window_title: String::from("An easy, good game"),
window_icon: String::from(""),
window_height: 600,
window_width: 800,
}
}
pub fn from_toml_file<R: io::Read>(file: &mut R) -> GameResult<Conf> {
let mut s = String::new();
try!(file.read_to_string(&mut s));
let mut parser = toml::Parser::new(&s);
let toml = try!(parser.parse()
.ok_or(String::from("Could not parse config file?")));
let config = try!(toml.get("ggez")
.ok_or(String::from("Section [ggez] not in config file")));
let mut decoder = toml::Decoder::new(config.clone());
Conf::decode(&mut decoder).map_err(GameError::from)
}
pub fn to_toml_file<W: io::Write>(&self, file: &mut W) -> GameResult<()> {
let mut e = toml::Encoder::new();
self.encode(&mut e)?;
let mut t = BTreeMap::new();
t.insert("ggez".to_owned(), toml::Value::Table(e.toml));
let toml_t = toml::Value::Table(t);
let toml_str = toml::encode_str(&toml_t);
let toml_bytes = toml_str.as_bytes();
file.write_all(toml_bytes).map_err(GameError::from)
}
}
#[cfg(test)]
mod tests {
use conf;
#[test]
fn encode_round_trip() {
let c1 = conf::Conf::new();
let mut writer = Vec::new();
c1.to_toml_file(&mut writer).unwrap();
let mut reader = writer.as_slice();
let c2 = conf::Conf::from_toml_file(&mut reader).unwrap();
assert_eq!(c1, c2);
}
}