diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-12 13:29:52 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-12 13:29:52 +0100 |
commit | e9f89e996fea5ccbf024122b0a816c5029b105ea (patch) | |
tree | 46a104d7a6ed92c90cb00809a36916cc5f55dd37 /askama_shared/src/lib.rs | |
parent | f9f4f57221e4488f3ecf26752e6c53d60167a5af (diff) | |
download | askama-e9f89e996fea5ccbf024122b0a816c5029b105ea.tar.gz askama-e9f89e996fea5ccbf024122b0a816c5029b105ea.tar.bz2 askama-e9f89e996fea5ccbf024122b0a816c5029b105ea.zip |
Enable instantiation of Config from str configuration
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/lib.rs | 28 |
1 files changed, 15 insertions, 13 deletions
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 { |