From 325cb975a35579aaad273fc82c1ecc05a31ebe52 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 10 Jul 2018 15:39:45 +0100 Subject: Simplify find_template_from_path() to use Config directly --- askama_shared/src/path.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'askama_shared/src') diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index 16ac0e5..f6eef40 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -5,10 +5,6 @@ use std::path::{Path, PathBuf}; use toml; -fn search_template_in_dirs<'a>(dirs: &'a [PathBuf], path: &Path) -> Option<&'a PathBuf> { - dirs.iter().find(|dir| dir.join(path).exists()) -} - pub fn get_template_source(tpl_path: &Path) -> String { let mut f = match File::open(tpl_path) { Err(_) => panic!("unable to open template file '{}'", tpl_path.to_str().unwrap()), @@ -24,7 +20,6 @@ pub fn get_template_source(tpl_path: &Path) -> String { } pub fn find_template_from_path(path: &str, start_at: Option<&Path>) -> PathBuf { - let dirs = template_dirs(); if let Some(root) = start_at { let relative = root.with_file_name(path); if relative.exists() { @@ -32,13 +27,15 @@ pub fn find_template_from_path(path: &str, start_at: Option<&Path>) -> PathBuf { } } - let mut rooted = PathBuf::new(); - match search_template_in_dirs(&dirs, &Path::new(path)) { - Some(dir) => rooted.push(&dir), - None => panic!("template {:?} not found in directories {:?}", path, dirs), - }; - rooted.push(path); - rooted + let config = Config::from_file(); + for dir in &config.dirs { + let rooted = dir.join(path); + if rooted.exists() { + return rooted; + } + } + + panic!("template {:?} not found in directories {:?}", path, config.dirs) } pub fn template_dirs() -> Vec { -- cgit