|
|
@@ -1,5 +1,6 @@
|
|
1
|
1
|
use clap;
|
|
2
|
2
|
use directories::ProjectDirs;
|
|
|
3
|
+use session::Session;
|
|
3
|
4
|
use std::fmt::Debug;
|
|
4
|
5
|
use std::fs::File;
|
|
5
|
6
|
use std::io::{Read, Write};
|
|
|
@@ -24,6 +25,7 @@ pub struct Config {
|
|
24
|
25
|
fps: f64,
|
|
25
|
26
|
use_network: bool,
|
|
26
|
27
|
async: bool,
|
|
|
28
|
+ open_last_session: bool,
|
|
27
|
29
|
}
|
|
28
|
30
|
|
|
29
|
31
|
impl Config {
|
|
|
@@ -72,27 +74,6 @@ impl Config {
|
|
72
|
74
|
}
|
|
73
|
75
|
}
|
|
74
|
76
|
|
|
75
|
|
- fn create_config_file<P: AsRef<Path> + Debug>(dir_path: P, file_path: P, contents: &[u8]) -> Result<(), String> {
|
|
76
|
|
- if !dir_path.as_ref().is_dir() {
|
|
77
|
|
- if let Err(err) = ::std::fs::create_dir_all(&dir_path) {
|
|
78
|
|
- return Err(format!("failed to create config directory ({:?}): {}",
|
|
79
|
|
- dir_path,
|
|
80
|
|
- err
|
|
81
|
|
- ));
|
|
82
|
|
- }
|
|
83
|
|
- }
|
|
84
|
|
-
|
|
85
|
|
- let mut file = File::create(&file_path)
|
|
86
|
|
- .map_err(|err| format!("failed to create config file {:?}: {}", &file_path, err))?;
|
|
87
|
|
-
|
|
88
|
|
- file.write_all(contents)
|
|
89
|
|
- .map_err(|err| format!(
|
|
90
|
|
- "failed to write contents to config file {:?}: {}",
|
|
91
|
|
- &file_path,
|
|
92
|
|
- err
|
|
93
|
|
- ))
|
|
94
|
|
- }
|
|
95
|
|
-
|
|
96
|
77
|
fn find_or_create() -> Result<Config, String> {
|
|
97
|
78
|
let config_dir = PROJ_DIRS.config_dir();
|
|
98
|
79
|
let config_file = {
|
|
|
@@ -108,7 +89,7 @@ impl Config {
|
|
108
|
89
|
} else {
|
|
109
|
90
|
// try to write a default config file
|
|
110
|
91
|
|
|
111
|
|
- match Config::create_config_file(
|
|
|
92
|
+ match create_config_file(
|
|
112
|
93
|
config_dir,
|
|
113
|
94
|
&config_file,
|
|
114
|
95
|
DEFAULT_CONFIG.as_bytes()
|
|
|
@@ -136,7 +117,7 @@ impl Config {
|
|
136
|
117
|
} else {
|
|
137
|
118
|
// try to write a default config file
|
|
138
|
119
|
|
|
139
|
|
- match Config::create_config_file(
|
|
|
120
|
+ match create_config_file(
|
|
140
|
121
|
config_dir,
|
|
141
|
122
|
&sources_file,
|
|
142
|
123
|
DEFAULT_TILE_SOURCES.as_bytes()
|
|
|
@@ -220,6 +201,14 @@ impl Config {
|
|
220
|
201
|
}
|
|
221
|
202
|
};
|
|
222
|
203
|
|
|
|
204
|
+ let open_last_session = {
|
|
|
205
|
+ match table.get("open_last_session") {
|
|
|
206
|
+ Some(&Value::Boolean(x)) => x,
|
|
|
207
|
+ Some(_) => return Err("open_last_session has to be a boolean.".to_string()),
|
|
|
208
|
+ None => false,
|
|
|
209
|
+ }
|
|
|
210
|
+ };
|
|
|
211
|
+
|
|
223
|
212
|
Ok(
|
|
224
|
213
|
Config {
|
|
225
|
214
|
tile_cache_dir,
|
|
|
@@ -229,6 +218,7 @@ impl Config {
|
|
229
|
218
|
fps,
|
|
230
|
219
|
use_network,
|
|
231
|
220
|
async,
|
|
|
221
|
+ open_last_session,
|
|
232
|
222
|
}
|
|
233
|
223
|
)
|
|
234
|
224
|
},
|
|
|
@@ -361,8 +351,56 @@ impl Config {
|
|
361
|
351
|
pub fn async(&self) -> bool {
|
|
362
|
352
|
self.async
|
|
363
|
353
|
}
|
|
|
354
|
+
|
|
|
355
|
+ pub fn open_last_session(&self) -> bool {
|
|
|
356
|
+ self.open_last_session
|
|
|
357
|
+ }
|
|
|
358
|
+}
|
|
|
359
|
+
|
|
|
360
|
+fn create_config_file<P: AsRef<Path> + Debug>(dir_path: P, file_path: P, contents: &[u8]) -> Result<(), String> {
|
|
|
361
|
+ if !dir_path.as_ref().is_dir() {
|
|
|
362
|
+ if let Err(err) = ::std::fs::create_dir_all(&dir_path) {
|
|
|
363
|
+ return Err(format!("failed to create config directory ({:?}): {}",
|
|
|
364
|
+ dir_path,
|
|
|
365
|
+ err
|
|
|
366
|
+ ));
|
|
|
367
|
+ }
|
|
|
368
|
+ }
|
|
|
369
|
+
|
|
|
370
|
+ let mut file = File::create(&file_path)
|
|
|
371
|
+ .map_err(|err| format!("failed to create config file {:?}: {}", &file_path, err))?;
|
|
|
372
|
+
|
|
|
373
|
+ file.write_all(contents)
|
|
|
374
|
+ .map_err(|err| format!(
|
|
|
375
|
+ "failed to write contents to config file {:?}: {}",
|
|
|
376
|
+ &file_path,
|
|
|
377
|
+ err
|
|
|
378
|
+ ))
|
|
|
379
|
+}
|
|
|
380
|
+
|
|
|
381
|
+pub fn read_last_session() -> Result<Session, String> {
|
|
|
382
|
+ let session_path = {
|
|
|
383
|
+ let config_dir = PROJ_DIRS.config_dir();
|
|
|
384
|
+ let mut path = PathBuf::from(config_dir);
|
|
|
385
|
+ path.push("last_session.toml");
|
|
|
386
|
+ path
|
|
|
387
|
+ };
|
|
|
388
|
+ Session::from_toml_file(session_path)
|
|
364
|
389
|
}
|
|
365
|
390
|
|
|
|
391
|
+pub fn save_session(session: &Session) -> Result<(), String>
|
|
|
392
|
+{
|
|
|
393
|
+ let config_dir = PROJ_DIRS.config_dir();
|
|
|
394
|
+ let session_path = {
|
|
|
395
|
+ let mut path = PathBuf::from(config_dir);
|
|
|
396
|
+ path.push("last_session.toml");
|
|
|
397
|
+ path
|
|
|
398
|
+ };
|
|
|
399
|
+ let contents = session.to_toml_string();
|
|
|
400
|
+ create_config_file(config_dir, &session_path, contents.as_bytes())
|
|
|
401
|
+}
|
|
|
402
|
+
|
|
|
403
|
+
|
|
366
|
404
|
#[cfg(test)]
|
|
367
|
405
|
mod tests {
|
|
368
|
406
|
use config::*;
|