Browse Source

Read/Create config file from/in XDG directory

Johannes Hofmann 8 years ago
parent
commit
4a1bcbe16f
5 changed files with 45 additions and 8 deletions
  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 View File

191
  "reqwest 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
191
  "reqwest 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
192
  "servo-glutin 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)",
192
  "servo-glutin 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)",
193
  "toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
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
 [[package]]
197
 [[package]]
1196
  "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
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
 [[package]]
1205
 [[package]]
1200
 name = "xml-rs"
1206
 name = "xml-rs"
1201
 version = "0.7.0"
1207
 version = "0.7.0"
1346
 "checksum winapi-x86_64-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98f12c52b2630cd05d2c3ffd8e008f7f48252c042b4871c72aed9dc733b96668"
1352
 "checksum winapi-x86_64-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98f12c52b2630cd05d2c3ffd8e008f7f48252c042b4871c72aed9dc733b96668"
1347
 "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
1353
 "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
1348
 "checksum x11-dl 2.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "28ec50063128cfdbdfe683b0504a3740e07b779c7c75fa26e941218b5f95e098"
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
 "checksum xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2"
1356
 "checksum xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2"

+ 1
- 0
Cargo.toml View File

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

deltamap.toml → default_config.toml View File


+ 35
- 7
src/config.rs View File

1
 use std::error::Error;
1
 use std::error::Error;
2
 use std::fs::File;
2
 use std::fs::File;
3
-use std::io::Read;
3
+use std::io::{Read, Write};
4
 use std::path::{Path, PathBuf};
4
 use std::path::{Path, PathBuf};
5
 use tile_source::TileSource;
5
 use tile_source::TileSource;
6
 use toml::Value;
6
 use toml::Value;
7
+use xdg;
8
+
9
+static DEFAULT_CONFIG: &'static str = include_str!("../default_config.toml");
7
 
10
 
8
 
11
 
9
 pub struct Config {
12
 pub struct Config {
12
 }
15
 }
13
 
16
 
14
 impl Config {
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
             Ok(Value::Table(ref table)) => {
41
             Ok(Value::Table(ref table)) => {
23
                 let tile_cache_dir = table.get("tile_cache_dir")
42
                 let tile_cache_dir = table.get("tile_cache_dir")
24
                     .ok_or_else(|| "missing \"tile_cache_dir\" entry".to_string())?
43
                     .ok_or_else(|| "missing \"tile_cache_dir\" entry".to_string())?
39
                         .ok_or_else(|| "max_zoom has to be an integer".to_string())
58
                         .ok_or_else(|| "max_zoom has to be an integer".to_string())
40
                         .and_then(|m| {
59
                         .and_then(|m| {
41
                             if m <= 0 || m > 30 {
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
                             } else {
62
                             } else {
44
                                 Ok(m)
63
                                 Ok(m)
45
                             }
64
                             }
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
     pub fn tile_sources(&self) -> &[(String, TileSource)] {
117
     pub fn tile_sources(&self) -> &[(String, TileSource)] {
90
         &self.sources
118
         &self.sources
91
     }
119
     }

+ 2
- 1
src/main.rs View File

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