|
|
@@ -1,9 +1,12 @@
|
|
1
|
1
|
use std::error::Error;
|
|
2
|
2
|
use std::fs::File;
|
|
3
|
|
-use std::io::Read;
|
|
|
3
|
+use std::io::{Read, Write};
|
|
4
|
4
|
use std::path::{Path, PathBuf};
|
|
5
|
5
|
use tile_source::TileSource;
|
|
6
|
6
|
use toml::Value;
|
|
|
7
|
+use xdg;
|
|
|
8
|
+
|
|
|
9
|
+static DEFAULT_CONFIG: &'static str = include_str!("../default_config.toml");
|
|
7
|
10
|
|
|
8
|
11
|
|
|
9
|
12
|
pub struct Config {
|
|
|
@@ -12,13 +15,29 @@ pub struct Config {
|
|
12
|
15
|
}
|
|
13
|
16
|
|
|
14
|
17
|
impl Config {
|
|
15
|
|
- pub fn from_toml<P: AsRef<Path>>(path: P) -> Result<Config, String> {
|
|
16
|
|
- let mut file = File::open(path).map_err(|e| e.description().to_string())?;
|
|
|
18
|
+ pub fn load() -> Result<Config, String> {
|
|
|
19
|
+ if let Ok(xdg_dirs) = xdg::BaseDirectories::with_prefix("deltamap") {
|
|
|
20
|
+ if let Some(config_path) = xdg_dirs.find_config_file("config.toml") {
|
|
|
21
|
+ println!("Load config from path {:?}", config_path);
|
|
17
|
22
|
|
|
18
|
|
- let mut content = String::new();
|
|
19
|
|
- file.read_to_string(&mut content).map_err(|e| e.description().to_string())?;
|
|
|
23
|
+ Config::from_toml_file(config_path)
|
|
|
24
|
+ } else {
|
|
|
25
|
+ if let Ok(path) = xdg_dirs.place_config_file("config.toml") {
|
|
|
26
|
+ if let Ok(mut file) = File::create(&path) {
|
|
|
27
|
+ println!("write default config {:?}", &path);
|
|
|
28
|
+ file.write_all(DEFAULT_CONFIG.as_bytes());
|
|
|
29
|
+ }
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ Config::from_toml_str(DEFAULT_CONFIG)
|
|
|
33
|
+ }
|
|
|
34
|
+ } else {
|
|
|
35
|
+ Config::from_toml_str(DEFAULT_CONFIG)
|
|
|
36
|
+ }
|
|
|
37
|
+ }
|
|
20
|
38
|
|
|
21
|
|
- match content.parse::<Value>() {
|
|
|
39
|
+ pub fn from_toml_str(toml_str: &str) -> Result<Config, String> {
|
|
|
40
|
+ match toml_str.parse::<Value>() {
|
|
22
|
41
|
Ok(Value::Table(ref table)) => {
|
|
23
|
42
|
let tile_cache_dir = table.get("tile_cache_dir")
|
|
24
|
43
|
.ok_or_else(|| "missing \"tile_cache_dir\" entry".to_string())?
|
|
|
@@ -39,7 +58,7 @@ impl Config {
|
|
39
|
58
|
.ok_or_else(|| "max_zoom has to be an integer".to_string())
|
|
40
|
59
|
.and_then(|m| {
|
|
41
|
60
|
if m <= 0 || m > 30 {
|
|
42
|
|
- Err(format!("max_zoom = {} is out of bounds. Has to be in interval [1, 30].", m))
|
|
|
61
|
+ Err(format!("max_zoom = {} is out of bounds, has to be in interval [1, 30]", m))
|
|
43
|
62
|
} else {
|
|
44
|
63
|
Ok(m)
|
|
45
|
64
|
}
|
|
|
@@ -86,6 +105,15 @@ impl Config {
|
|
86
|
105
|
}
|
|
87
|
106
|
}
|
|
88
|
107
|
|
|
|
108
|
+ pub fn from_toml_file<P: AsRef<Path>>(path: P) -> Result<Config, String> {
|
|
|
109
|
+ let mut file = File::open(path).map_err(|e| e.description().to_string())?;
|
|
|
110
|
+
|
|
|
111
|
+ let mut content = String::new();
|
|
|
112
|
+ file.read_to_string(&mut content).map_err(|e| e.description().to_string())?;
|
|
|
113
|
+
|
|
|
114
|
+ Config::from_toml_str(&content)
|
|
|
115
|
+ }
|
|
|
116
|
+
|
|
89
|
117
|
pub fn tile_sources(&self) -> &[(String, TileSource)] {
|
|
90
|
118
|
&self.sources
|
|
91
|
119
|
}
|