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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::fs::read_to_string;

use anyhow::Result;
use log::*;
use serde::Deserialize;

use super::path::BDPath;

const CFG_PATH: BDPath<'static> = BDPath::new("config.yaml");

#[derive(Debug, Deserialize, Clone)]
pub struct DSConfig {
    pub enabled: bool,
}

#[derive(Debug, Deserialize, Clone)]
pub struct GRConfig {
    pub enabled: bool,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
    pub bx: DSConfig,
    pub az2014: DSConfig,
    pub az2018: DSConfig,
    pub goodreads: GRConfig,
}

impl Config {
    pub fn ds_enabled(&self, name: &str) -> bool {
        let (name, _qual) = if let Some((n, q)) = name.split_once("-") {
            (n, Some(q))
        } else {
            (name, None)
        };
        match name {
            "loc" | "LOC" => true,
            "openlib" | "openlibrary" | "OL" => true,
            "goodreads" | "GR" => self.goodreads.enabled,
            "az2014" | "AZ14" => self.az2014.enabled,
            "az2018" | "AZ18" => self.az2018.enabled,
            "bx" | "BX" => self.bx.enabled,
            _ => panic!("unsupported data set {}", name),
        }
    }
}

/// Load the configuration for this project.
pub fn load_config() -> Result<Config> {
    let path = CFG_PATH.resolve()?;
    debug!("reading configuration {}", path.display());
    let f = read_to_string(&path)?;
    let cfg: Config = serde_yaml::from_str(&f)?;
    Ok(cfg)
}