From e9f89e996fea5ccbf024122b0a816c5029b105ea Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 12 Jul 2018 13:29:52 +0100 Subject: Enable instantiation of Config from str configuration --- askama_shared/src/lib.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'askama_shared') diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 312057c..e9b368e 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -26,21 +26,23 @@ impl Config { pub fn new() -> Config { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let filename = root.join(CONFIG_FILE_NAME); - - let default = vec![root.join("templates")]; - let dirs = if filename.exists() { - let config_str = fs::read_to_string(&filename) - .expect(&format!("unable to read {}", filename.to_str().unwrap())); - let raw: RawConfig = toml::from_str(&config_str) - .expect(&format!("invalid TOML in {}", filename.to_str().unwrap())); - raw.dirs - .map(|dirs| dirs.into_iter().map(|dir| root.join(dir)).collect()) - .unwrap_or_else(|| default) + if filename.exists() { + Self::from_str(&fs::read_to_string(&filename) + .expect(&format!("unable to read {}", filename.to_str().unwrap()))) } else { - default - }; + Self::from_str("") + } + } - Config { dirs } + pub fn from_str(s: &str) -> Self { + let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + 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); + Self { dirs } } pub fn find_template(&self, path: &str, start_at: Option<&Path>) -> PathBuf { -- cgit