bookdata/layout/
config.rs

1use std::fs::read_to_string;
2
3use anyhow::Result;
4use log::*;
5use serde::Deserialize;
6
7use super::path::BDPath;
8
9const CFG_PATH: BDPath<'static> = BDPath::new("config.yaml");
10
11#[derive(Debug, Deserialize, Clone)]
12pub struct DSConfig {
13    pub enabled: bool,
14}
15
16#[derive(Debug, Deserialize, Clone)]
17pub struct GRConfig {
18    pub enabled: bool,
19}
20
21#[derive(Debug, Deserialize, Clone)]
22pub struct Config {
23    pub bx: DSConfig,
24    pub az2014: DSConfig,
25    pub az2018: DSConfig,
26    pub goodreads: GRConfig,
27}
28
29impl Config {
30    pub fn ds_enabled(&self, name: &str) -> bool {
31        let (name, _qual) = if let Some((n, q)) = name.split_once("-") {
32            (n, Some(q))
33        } else {
34            (name, None)
35        };
36        match name {
37            "loc" | "LOC" => true,
38            "openlib" | "openlibrary" | "OL" => true,
39            "goodreads" | "GR" => self.goodreads.enabled,
40            "az2014" | "AZ14" => self.az2014.enabled,
41            "az2018" | "AZ18" => self.az2018.enabled,
42            "bx" | "BX" => self.bx.enabled,
43            _ => panic!("unsupported data set {}", name),
44        }
45    }
46}
47
48/// Load the configuration for this project.
49pub fn load_config() -> Result<Config> {
50    let path = CFG_PATH.resolve()?;
51    debug!("reading configuration {}", path.display());
52    let f = read_to_string(&path)?;
53    let cfg: Config = serde_yaml::from_str(&f)?;
54    Ok(cfg)
55}