diff options
author | Minijackson <minijackson@riseup.net> | 2021-11-07 23:09:34 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2021-11-07 23:09:34 +0100 |
commit | 517cabe8ec54d0bf5f5f9cc9089d76a1fad7bb6a (patch) | |
tree | 3140cf7b16727f4752633f4e6f5d098f23889af0 /src/config.rs | |
download | pandoc-docbook-517cabe8ec54d0bf5f5f9cc9089d76a1fad7bb6a.tar.gz pandoc-docbook-517cabe8ec54d0bf5f5f9cc9089d76a1fad7bb6a.zip |
initial commit with PoC
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..53922b0 --- /dev/null +++ b/src/config.rs | |||
@@ -0,0 +1,59 @@ | |||
1 | use log::debug; | ||
2 | use serde::{Deserialize, Serialize}; | ||
3 | |||
4 | #[derive(Debug, Clone, Deserialize, Serialize)] | ||
5 | pub struct Config { | ||
6 | #[serde(default)] | ||
7 | pub book: BookConfig, | ||
8 | #[serde(default)] | ||
9 | pub build: BuildConfig, | ||
10 | } | ||
11 | |||
12 | #[derive(Debug, Clone, Deserialize, Serialize)] | ||
13 | pub struct BookConfig { | ||
14 | #[serde(default = "default_summary")] | ||
15 | pub summary: String, | ||
16 | } | ||
17 | |||
18 | impl Default for BookConfig { | ||
19 | fn default() -> Self { | ||
20 | Self { | ||
21 | summary: default_summary(), | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | |||
26 | fn default_summary() -> String { | ||
27 | "src/_summary.md".to_string() | ||
28 | } | ||
29 | |||
30 | #[derive(Debug, Clone, Deserialize, Serialize)] | ||
31 | pub struct BuildConfig { | ||
32 | #[serde(default = "default_build_dir")] | ||
33 | pub build_dir: String, | ||
34 | } | ||
35 | |||
36 | impl Default for BuildConfig { | ||
37 | fn default() -> Self { | ||
38 | Self { | ||
39 | build_dir: default_build_dir(), | ||
40 | } | ||
41 | } | ||
42 | } | ||
43 | |||
44 | fn default_build_dir() -> String { | ||
45 | "pdbook".to_string() | ||
46 | } | ||
47 | |||
48 | impl Config { | ||
49 | pub fn new(config_file: &str) -> Result<Self, config::ConfigError> { | ||
50 | let mut s = config::Config::default(); | ||
51 | |||
52 | debug!("Parsing config file: {}", config_file); | ||
53 | s.merge(config::File::with_name(config_file))?; | ||
54 | debug!("Parsing config from environment"); | ||
55 | s.merge(config::Environment::with_prefix("PANDOC_DOCBOOK").separator("_"))?; | ||
56 | |||
57 | s.try_into() | ||
58 | } | ||
59 | } | ||