Преглед на файлове

Add config key pbf_file

Johannes Hofmann преди 7 години
родител
ревизия
2f257afb43
променени са 1 файла, в които са добавени 29 реда и са изтрити 6 реда
  1. 29
    6
      src/config.rs

+ 29
- 6
src/config.rs Целия файл

@@ -117,7 +117,7 @@ impl Config {
117 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,7 +157,7 @@ impl Config {
157 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 161
         match toml_str.parse::<Value>() {
162 162
             Ok(Value::Table(ref table)) => {
163 163
                 let tile_cache_dir = {
@@ -172,6 +172,29 @@ impl Config {
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 198
                 let fps = {
176 199
                     match table.get("fps") {
177 200
                         Some(&Value::Float(fps)) => fps,
@@ -201,7 +224,7 @@ impl Config {
201 224
                     Config {
202 225
                         tile_cache_dir,
203 226
                         sources: vec![],
204
-                        pbf_path: None,
227
+                        pbf_path,
205 228
                         search_pattern: None,
206 229
                         fps,
207 230
                         use_network,
@@ -215,12 +238,12 @@ impl Config {
215 238
     }
216 239
 
217 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 243
         let mut content = String::new();
221 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 249
     fn add_tile_sources_from_str(&mut self, toml_str: &str) -> Result<(), String> {
@@ -346,7 +369,7 @@ mod tests {
346 369
 
347 370
     #[test]
348 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 373
         config.add_tile_sources_from_str(DEFAULT_TILE_SOURCES).unwrap();
351 374
     }
352 375
 }