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
//! Main functions doing actual work.
//!
//! Use `create_window()` to set up a window of correct size, then set up a `conrod::Ui` on it like you'd do normally.
//! Then create an instance of `Widgets` and call `update()` on it each update event, then check for states that need manual
//! handling from usercode and act accordingly.


use toml::{Parser, Value, decode, encode_str};
use rustc_serialize::{Encodable, Decodable};
use std::io::{Read, Write};
use self::super::Error;
use std::path::Path;
use std::fs::File;

mod style;
mod leader;
mod window;
mod widgets;
mod game_state;

pub mod state;

pub use self::leader::Leader;
pub use self::widgets::Widgets;
pub use self::style::set_button_style;
pub use self::game_state::{Difficulty, GameState};
pub use self::window::{create_window, window_size};


fn read_toml_file<T: Decodable>(p: &Path, desc: &'static str) -> Result<T, Error> {
    let mut buf = String::new();
    try!(try!(File::open(p).map_err(|_| {
            Error::Io {
                desc: desc,
                op: "open",
            }
        }))
        .read_to_string(&mut buf)
        .map_err(|_| {
            Error::Io {
                desc: desc,
                op: "read",
            }
        }));

    let mut parser = Parser::new(&buf);
    let parsed = parser.parse().and_then(|t| decode(Value::Table(t)));
    parsed.ok_or_else(move || {
        Error::FileParsingFailed {
            desc: desc,
            errors: parser.errors
                .iter()
                .map(|e| {
                    let (line, col) = parser.to_linecol(e.lo);
                    format!("error: {}:{}: {}", line, col, e.desc)
                })
                .collect(),
        }
    })
}

fn write_toml_file<T: Encodable>(to_write: &T, p: &Path, desc: &'static str) -> Result<(), Error> {
    try!(File::create(p).map_err(|_| {
            Error::Io {
                desc: desc,
                op: "create",
            }
        }))
        .write_all(encode_str(to_write).as_bytes())
        .map_err(|_| {
            Error::Io {
                desc: desc,
                op: "write",
            }
        })
}