From 8be198581864eab72582b0f76c7a1adf72d618af Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 14 Jul 2018 14:42:25 +0100 Subject: Make config file structure more future-proof --- askama_shared/src/lib.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'askama_shared') diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index bff7b57..2b82782 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -39,9 +39,14 @@ impl Config { let default = vec![root.join("templates")]; let raw: RawConfig = toml::from_str(&s).expect(&format!("invalid TOML in {}", CONFIG_FILE_NAME)); - let dirs = raw.dirs - .map(|dirs| dirs.into_iter().map(|dir| root.join(dir)).collect()) - .unwrap_or_else(|| default); + + let dirs = match raw.general { + Some(General { dirs: Some(dirs) }) => { + dirs.into_iter().map(|dir| root.join(dir)).collect() + } + Some(General { dirs: None }) | None => default, + }; + Self { dirs } } @@ -69,6 +74,11 @@ impl Config { #[derive(Deserialize)] struct RawConfig { + general: Option, +} + +#[derive(Deserialize)] +struct General { dirs: Option>, } @@ -92,7 +102,7 @@ mod tests { fn test_config_dirs() { let mut root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); root.push("tpl"); - let config = Config::from_str("dirs = [\"tpl\"]"); + let config = Config::from_str("[general]\ndirs = [\"tpl\"]"); assert_eq!(config.dirs, vec![root]); } -- cgit