浏览代码

Add config key pbf_file

Johannes Hofmann 7 年前
父节点
当前提交
2f257afb43
共有 1 个文件被更改,包括 29 次插入6 次删除
  1. 29
    6
      src/config.rs

+ 29
- 6
src/config.rs 查看文件

117
                 Ok(()) => info!("create default config file {:?}", config_file),
117
                 Ok(()) => info!("create default config file {:?}", config_file),
118
             }
118
             }
119
 
119
 
120
-            Config::from_toml_str(DEFAULT_CONFIG)
120
+            Config::from_toml_str::<&str>(DEFAULT_CONFIG, None)
121
         }
121
         }
122
     }
122
     }
123
 
123
 
157
         path
157
         path
158
     }
158
     }
159
 
159
 
160
-    fn from_toml_str(toml_str: &str) -> Result<Config, String> {
160
+    fn from_toml_str<P: AsRef<Path>>(toml_str: &str, config_path: Option<P>) -> Result<Config, String> {
161
         match toml_str.parse::<Value>() {
161
         match toml_str.parse::<Value>() {
162
             Ok(Value::Table(ref table)) => {
162
             Ok(Value::Table(ref table)) => {
163
                 let tile_cache_dir = {
163
                 let tile_cache_dir = {
172
                     }
172
                     }
173
                 };
173
                 };
174
 
174
 
175
+                let pbf_path = {
176
+                    match table.get("pbf_file") {
177
+                        Some(&Value::String(ref pbf_file)) => {
178
+                            match config_path {
179
+                                Some(config_path) => {
180
+                                    let p = config_path.as_ref().parent()
181
+                                        .ok_or_else(|| "root path is not a valid config file.")?;
182
+                                    let mut p = PathBuf::from(p);
183
+                                    p.push(pbf_file);
184
+                                    p = p.canonicalize().
185
+                                        map_err(|e| format!("pbf_file ({:?}): {}", p, e))?;
186
+                                    Some(p)
187
+                                },
188
+                                None => Some(PathBuf::from(pbf_file)),
189
+                            }
190
+                        },
191
+                        Some(_) => {
192
+                            return Err("pbf_file has to be a string.".to_string());
193
+                        },
194
+                        None => None,
195
+                    }
196
+                };
197
+
175
                 let fps = {
198
                 let fps = {
176
                     match table.get("fps") {
199
                     match table.get("fps") {
177
                         Some(&Value::Float(fps)) => fps,
200
                         Some(&Value::Float(fps)) => fps,
201
                     Config {
224
                     Config {
202
                         tile_cache_dir,
225
                         tile_cache_dir,
203
                         sources: vec![],
226
                         sources: vec![],
204
-                        pbf_path: None,
227
+                        pbf_path,
205
                         search_pattern: None,
228
                         search_pattern: None,
206
                         fps,
229
                         fps,
207
                         use_network,
230
                         use_network,
215
     }
238
     }
216
 
239
 
217
     fn from_toml_file<P: AsRef<Path>>(path: P) -> Result<Config, String> {
240
     fn from_toml_file<P: AsRef<Path>>(path: P) -> Result<Config, String> {
218
-        let mut file = File::open(path).map_err(|e| format!("{}", e))?;
241
+        let mut file = File::open(&path).map_err(|e| format!("{}", e))?;
219
 
242
 
220
         let mut content = String::new();
243
         let mut content = String::new();
221
         file.read_to_string(&mut content).map_err(|e| format!("{}", e))?;
244
         file.read_to_string(&mut content).map_err(|e| format!("{}", e))?;
222
 
245
 
223
-        Config::from_toml_str(&content)
246
+        Config::from_toml_str(&content, Some(path))
224
     }
247
     }
225
 
248
 
226
     fn add_tile_sources_from_str(&mut self, toml_str: &str) -> Result<(), String> {
249
     fn add_tile_sources_from_str(&mut self, toml_str: &str) -> Result<(), String> {
346
 
369
 
347
     #[test]
370
     #[test]
348
     fn default_config() {
371
     fn default_config() {
349
-        let mut config = Config::from_toml_str(DEFAULT_CONFIG).unwrap();
372
+        let mut config = Config::from_toml_str::<&str>(DEFAULT_CONFIG, None).unwrap();
350
         config.add_tile_sources_from_str(DEFAULT_TILE_SOURCES).unwrap();
373
         config.add_tile_sources_from_str(DEFAULT_TILE_SOURCES).unwrap();
351
     }
374
     }
352
 }
375
 }