From e40057d6eda27617bd9e1593f34ab9354a7dae4d Mon Sep 17 00:00:00 2001
From: Dirkjan Ochtman <dirkjan@ochtman.nl>
Date: Tue, 10 Jul 2018 14:51:42 +0100
Subject: Simplify code to read configuration

---
 askama_shared/src/path.rs | 45 ++++++++++++++++-----------------------------
 1 file changed, 16 insertions(+), 29 deletions(-)

(limited to 'askama_shared')

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)]
-- 
cgit