|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+use clap;
|
|
1
|
2
|
use std::fs::File;
|
|
2
|
3
|
use std::io::{Read, Write};
|
|
3
|
4
|
use std::path::{Path, PathBuf};
|
|
|
@@ -13,10 +14,38 @@ pub struct Config {
|
|
13
|
14
|
tile_cache_dir: PathBuf,
|
|
14
|
15
|
sources: Vec<(String, TileSource)>,
|
|
15
|
16
|
fps: f64,
|
|
|
17
|
+ use_network: bool,
|
|
|
18
|
+ async: bool,
|
|
16
|
19
|
}
|
|
17
|
20
|
|
|
18
|
21
|
impl Config {
|
|
19
|
|
- pub fn load() -> Result<Config, String> {
|
|
|
22
|
+ pub fn from_arg_matches<'a>(matches: &clap::ArgMatches<'a>) -> Result<Config, String> {
|
|
|
23
|
+ let mut config = if let Some(config_path) = matches.value_of_os("config") {
|
|
|
24
|
+ Config::from_toml_file(config_path)?
|
|
|
25
|
+ } else {
|
|
|
26
|
+ Config::find_or_create()?
|
|
|
27
|
+ };
|
|
|
28
|
+
|
|
|
29
|
+ config.merge_arg_matches(matches);
|
|
|
30
|
+
|
|
|
31
|
+ Ok(config)
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ fn merge_arg_matches<'a>(&mut self, matches: &clap::ArgMatches<'a>) {
|
|
|
35
|
+ if let Some(Ok(fps)) = matches.value_of("fps").map(|s| s.parse()) {
|
|
|
36
|
+ self.fps = fps;
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ if matches.is_present("offline") {
|
|
|
40
|
+ self.use_network = false;
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ if matches.is_present("sync") {
|
|
|
44
|
+ self.async = false;
|
|
|
45
|
+ }
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ pub fn find_or_create() -> Result<Config, String> {
|
|
20
|
49
|
if let Ok(xdg_dirs) = xdg::BaseDirectories::with_prefix("deltamap") {
|
|
21
|
50
|
if let Some(config_path) = xdg_dirs.find_config_file("config.toml") {
|
|
22
|
51
|
info!("load config from path {:?}", config_path);
|
|
|
@@ -76,6 +105,22 @@ impl Config {
|
|
76
|
105
|
}
|
|
77
|
106
|
};
|
|
78
|
107
|
|
|
|
108
|
+ let use_network = {
|
|
|
109
|
+ match table.get("use_network") {
|
|
|
110
|
+ Some(&Value::Boolean(x)) => x,
|
|
|
111
|
+ Some(_) => return Err("use_network has to be a boolean.".to_string()),
|
|
|
112
|
+ None => true,
|
|
|
113
|
+ }
|
|
|
114
|
+ };
|
|
|
115
|
+
|
|
|
116
|
+ let async = {
|
|
|
117
|
+ match table.get("async") {
|
|
|
118
|
+ Some(&Value::Boolean(x)) => x,
|
|
|
119
|
+ Some(_) => return Err("async has to be a boolean.".to_string()),
|
|
|
120
|
+ None => true,
|
|
|
121
|
+ }
|
|
|
122
|
+ };
|
|
|
123
|
+
|
|
79
|
124
|
let sources_table = table.get("tile_sources")
|
|
80
|
125
|
.ok_or_else(|| "missing \"tile_sources\" table".to_string())?
|
|
81
|
126
|
.as_table()
|
|
|
@@ -149,6 +194,8 @@ impl Config {
|
|
149
|
194
|
tile_cache_dir: tile_cache_dir,
|
|
150
|
195
|
sources: sources_vec,
|
|
151
|
196
|
fps: fps,
|
|
|
197
|
+ use_network: use_network,
|
|
|
198
|
+ async: async,
|
|
152
|
199
|
}
|
|
153
|
200
|
)
|
|
154
|
201
|
},
|
|
|
@@ -173,6 +220,14 @@ impl Config {
|
|
173
|
220
|
pub fn fps(&self) -> f64 {
|
|
174
|
221
|
self.fps
|
|
175
|
222
|
}
|
|
|
223
|
+
|
|
|
224
|
+ pub fn use_network(&self) -> bool {
|
|
|
225
|
+ self.use_network
|
|
|
226
|
+ }
|
|
|
227
|
+
|
|
|
228
|
+ pub fn async(&self) -> bool {
|
|
|
229
|
+ self.async
|
|
|
230
|
+ }
|
|
176
|
231
|
}
|
|
177
|
232
|
|
|
178
|
233
|
#[cfg(test)]
|