1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use serde::Deserialize;

#[derive(Deserialize)]
#[serde(default)]
pub struct ImporterConfiguration {
    pub osmconvert: String,
    pub unzip: String,
    pub gunzip: String,
    pub gunzip_args: String,
}

impl Default for ImporterConfiguration {
    fn default() -> ImporterConfiguration {
        ImporterConfiguration {
            osmconvert: String::from("osmconvert"),
            unzip: String::from("unzip"),
            gunzip: String::from("gunzip"),
            gunzip_args: String::from(""),
        }
    }
}

pub fn load_configuration() -> ImporterConfiguration {
    // Safe to assume that {} can be parsed given struct-level Default implementation.
    let default = serde_json::from_str("{}").unwrap();

    match fs_err::read_to_string("importer.json") {
        Ok(contents) => serde_json::from_str(&contents).unwrap_or(default),
        Err(_) => default,
    }
}