forked from TheNeikos/rustbreak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
61 lines (52 loc) · 1.36 KB
/
config.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
// This just reads an example configuration.
// If it doesn't find one, it uses your default configuration
//
// You can create one by writing this file to `/tmp/config.ron`:
// ```
// ---
// user_path: /tmp/nope
// allow_overwrite: true
// ```
//
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate lazy_static;
use rustbreak::deser::Ron;
use rustbreak::FileDatabase;
use std::default::Default;
use std::path::PathBuf;
type DB = FileDatabase<Config, Ron>;
lazy_static! {
static ref CONFIG: DB = {
let db = FileDatabase::load_from_path_or_default("/tmp/config.ron")
.expect("Create database from path");
db.load().expect("Config to load");
db
};
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct Config {
user_path: PathBuf,
allow_overwrite: bool,
}
impl Default for Config {
fn default() -> Config {
Config {
user_path: PathBuf::from("/tmp"),
allow_overwrite: false,
}
}
}
fn main() {
let _conf: Config = CONFIG
.read(|conf| conf.clone())
.expect("Reading configuration");
let (user_path, allow_overwrite) = CONFIG
.read(|conf| (conf.user_path.clone(), conf.allow_overwrite.clone()))
.expect("Read config");
println!(
"The current configuration is: {:?} and {}",
user_path, allow_overwrite
);
}