瀏覽代碼

Read/Create config file from/in XDG directory

Johannes Hofmann 8 年之前
父節點
當前提交
4a1bcbe16f
共有 5 個文件被更改,包括 45 次插入8 次删除
  1. 7
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 0
    0
      default_config.toml
  4. 35
    7
      src/config.rs
  5. 2
    1
      src/main.rs

+ 7
- 0
Cargo.lock 查看文件

@@ -191,6 +191,7 @@ dependencies = [
191 191
  "reqwest 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
192 192
  "servo-glutin 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)",
193 193
  "toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
194
+ "xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
194 195
 ]
195 196
 
196 197
 [[package]]
@@ -1196,6 +1197,11 @@ dependencies = [
1196 1197
  "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
1197 1198
 ]
1198 1199
 
1200
+[[package]]
1201
+name = "xdg"
1202
+version = "2.1.0"
1203
+source = "registry+https://github.com/rust-lang/crates.io-index"
1204
+
1199 1205
 [[package]]
1200 1206
 name = "xml-rs"
1201 1207
 version = "0.7.0"
@@ -1346,4 +1352,5 @@ dependencies = [
1346 1352
 "checksum winapi-x86_64-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98f12c52b2630cd05d2c3ffd8e008f7f48252c042b4871c72aed9dc733b96668"
1347 1353
 "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
1348 1354
 "checksum x11-dl 2.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "28ec50063128cfdbdfe683b0504a3740e07b779c7c75fa26e941218b5f95e098"
1355
+"checksum xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a66b7c2281ebde13cf4391d70d4c7e5946c3c25e72a7b859ca8f677dcd0b0c61"
1349 1356
 "checksum xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2"

+ 1
- 0
Cargo.toml 查看文件

@@ -11,6 +11,7 @@ osmpbf = "0.1"
11 11
 reqwest = "0.8"
12 12
 servo-glutin = "0.13"
13 13
 toml = "0.4"
14
+xdg = "2.1"
14 15
 
15 16
 [build-dependencies]
16 17
 gl_generator = "0.8"

deltamap.toml → default_config.toml 查看文件


+ 35
- 7
src/config.rs 查看文件

@@ -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
     }

+ 2
- 1
src/main.rs 查看文件

@@ -3,6 +3,7 @@ extern crate image;
3 3
 extern crate linked_hash_map;
4 4
 extern crate reqwest;
5 5
 extern crate toml;
6
+extern crate xdg;
6 7
 
7 8
 
8 9
 #[macro_use]
@@ -154,7 +155,7 @@ fn handle_event(event: &Event, map: &mut MapViewGl, input_state: &mut InputState
154 155
 }
155 156
 
156 157
 fn main() {
157
-    let config = config::Config::from_toml("deltamap.toml").unwrap();
158
+    let config = config::Config::load().unwrap();
158 159
     let mut sources = TileSources::new(config.tile_sources()).unwrap();
159 160
 
160 161
     let mut window = glutin::WindowBuilder::new().build().unwrap();