diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-10 14:51:42 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-10 14:51:42 +0100 |
commit | e40057d6eda27617bd9e1593f34ab9354a7dae4d (patch) | |
tree | 8508767dbc5cc3fe94a28f6a364375c5f2010dfe /askama_shared/src | |
parent | 3dd29b9f15720338d8e1face240d86dd1b6ed50e (diff) | |
download | askama-e40057d6eda27617bd9e1593f34ab9354a7dae4d.tar.gz askama-e40057d6eda27617bd9e1593f34ab9354a7dae4d.tar.bz2 askama-e40057d6eda27617bd9e1593f34ab9354a7dae4d.zip |
Simplify code to read configuration
Diffstat (limited to 'askama_shared/src')
-rw-r--r-- | askama_shared/src/path.rs | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index 6468b91..437c594 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -1,5 +1,5 @@ use std::env; -use std::fs::File; +use std::fs::{self, File}; use std::io::Read; use std::path::{Path, PathBuf}; @@ -65,19 +65,21 @@ impl Config { fn from_file() -> Config { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let filename = root.join(CONFIG_FILE_NAME); - RawConfig::from_file(&filename).map_or_else( - || Config { - dirs: vec![root.join("templates")], - }, - |config| Config { - dirs: config - .dirs - .unwrap_or_else(|| Vec::new()) - .into_iter() - .map(|directory| root.join(directory)) - .collect(), - }, - ) + + 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) + } else { + default + }; + + Config { dirs } } } @@ -86,21 +88,6 @@ struct RawConfig { dirs: Option<Vec<String>>, } -impl RawConfig { - fn from_file(filename: &PathBuf) -> Option<RawConfig> { - if filename.exists() { - let mut contents = String::new(); - File::open(filename) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); - Some(toml::from_str(&contents).unwrap()) - } else { - None - } - } -} - static CONFIG_FILE_NAME: &str = "askama.toml"; #[cfg(test)] |