summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 9b95f9af01a49527a735799234846d169cee9d8d (plain)
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
56
57
58
59
60
61
62
63
use std::collections::HashMap;

use log::debug;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
    #[serde(default)]
    pub book: BookConfig,
    #[serde(default)]
    pub build: BuildConfig,
    #[serde(default)]
    pub pandoc: HashMap<String, serde_json::Value>,
}

impl Config {
    pub fn new(config_file: &str) -> Result<Self, config::ConfigError> {
        let mut s = config::Config::default();

        debug!("Parsing config file: {}", config_file);
        s.merge(config::File::with_name(config_file))?;
        debug!("Parsing config from environment");
        s.merge(config::Environment::with_prefix("PANDOC_DOCBOOK").separator("_"))?;

        s.try_into()
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BookConfig {
    #[serde(default = "default_summary")]
    pub summary: String,
}

impl Default for BookConfig {
    fn default() -> Self {
        Self {
            summary: default_summary(),
        }
    }
}

fn default_summary() -> String {
    "src/_summary.md".to_string()
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BuildConfig {
    #[serde(default = "default_build_dir")]
    pub build_dir: String,
}

impl Default for BuildConfig {
    fn default() -> Self {
        Self {
            build_dir: default_build_dir(),
        }
    }
}

fn default_build_dir() -> String {
    "pdbook".to_string()
}