diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-11 22:30:10 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-11 22:30:10 +0100 |
commit | c8d9a6326d69471145ce6ec7742389a3f9186f1a (patch) | |
tree | c51c0f0c6034525a00fc809d62c9a112006febdd /askama_shared/src | |
parent | 987354946cde37ddd05d5316f7b581c9dd6f0131 (diff) | |
download | askama-c8d9a6326d69471145ce6ec7742389a3f9186f1a.tar.gz askama-c8d9a6326d69471145ce6ec7742389a3f9186f1a.tar.bz2 askama-c8d9a6326d69471145ce6ec7742389a3f9186f1a.zip |
Move find_template_from_path() into Config
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/lib.rs | 70 | ||||
-rw-r--r-- | askama_shared/src/path.rs | 69 |
2 files changed, 73 insertions, 66 deletions
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 2c2a88f..08b243d 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -9,7 +9,7 @@ extern crate toml; use std::env; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; mod error; mod escaping; @@ -43,6 +43,27 @@ impl Config { Config { dirs } } + + pub fn find_template(&self, path: &str, start_at: Option<&Path>) -> PathBuf { + if let Some(root) = start_at { + let relative = root.with_file_name(path); + if relative.exists() { + return relative.to_owned(); + } + } + + for dir in &self.dirs { + let rooted = dir.join(path); + if rooted.exists() { + return rooted; + } + } + + panic!( + "template {:?} not found in directories {:?}", + path, self.dirs + ) + } } #[derive(Deserialize)] @@ -51,3 +72,50 @@ struct RawConfig { } static CONFIG_FILE_NAME: &str = "askama.toml"; + +#[cfg(test)] +mod tests { + use super::Config; + use std::env; + use std::path::{Path, PathBuf}; + + fn assert_eq_rooted(actual: &Path, expected: &str) { + let mut root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + root.push("templates"); + let mut inner = PathBuf::new(); + inner.push(expected); + assert_eq!(actual.strip_prefix(root).unwrap(), inner); + } + + #[test] + fn find_absolute() { + let config = Config::new(); + let root = config.find_template("a.html", None); + let path = config.find_template("sub/b.html", Some(&root)); + assert_eq_rooted(&path, "sub/b.html"); + } + + #[test] + #[should_panic] + fn find_relative_nonexistent() { + let config = Config::new(); + let root = config.find_template("a.html", None); + config.find_template("b.html", Some(&root)); + } + + #[test] + fn find_relative() { + let config = Config::new(); + let root = config.find_template("sub/b.html", None); + let path = config.find_template("c.html", Some(&root)); + assert_eq_rooted(&path, "sub/c.html"); + } + + #[test] + fn find_relative_sub() { + let config = Config::new(); + let root = config.find_template("sub/b.html", None); + let path = config.find_template("sub1/d.html", Some(&root)); + assert_eq_rooted(&path, "sub/sub1/d.html"); + } +} diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index 568437f..16102b8 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -1,7 +1,5 @@ -use super::Config; - use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; pub fn get_template_source(tpl_path: &Path) -> String { match fs::read_to_string(tpl_path) { @@ -18,73 +16,14 @@ pub fn get_template_source(tpl_path: &Path) -> String { } } -pub fn find_template_from_path(path: &str, start_at: Option<&Path>) -> PathBuf { - if let Some(root) = start_at { - let relative = root.with_file_name(path); - if relative.exists() { - return relative.to_owned(); - } - } - - let config = Config::new(); - for dir in &config.dirs { - let rooted = dir.join(path); - if rooted.exists() { - return rooted; - } - } - - panic!( - "template {:?} not found in directories {:?}", - path, config.dirs - ) -} - #[cfg(test)] mod tests { - use super::{find_template_from_path, get_template_source}; - use std::env; - use std::path::{Path, PathBuf}; - - fn assert_eq_rooted(actual: &Path, expected: &str) { - let mut root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); - root.push("templates"); - let mut inner = PathBuf::new(); - inner.push(expected); - assert_eq!(actual.strip_prefix(root).unwrap(), inner); - } + use super::get_template_source; + use Config; #[test] fn get_source() { - let path = find_template_from_path("sub/b.html", None); + let path = Config::new().find_template("sub/b.html", None); assert_eq!(get_template_source(&path), "bar"); } - - #[test] - fn find_absolute() { - let root = find_template_from_path("a.html", None); - let path = find_template_from_path("sub/b.html", Some(&root)); - assert_eq_rooted(&path, "sub/b.html"); - } - - #[test] - #[should_panic] - fn find_relative_nonexistent() { - let root = find_template_from_path("a.html", None); - find_template_from_path("b.html", Some(&root)); - } - - #[test] - fn find_relative() { - let root = find_template_from_path("sub/b.html", None); - let path = find_template_from_path("c.html", Some(&root)); - assert_eq_rooted(&path, "sub/c.html"); - } - - #[test] - fn find_relative_sub() { - let root = find_template_from_path("sub/b.html", None); - let path = find_template_from_path("sub1/d.html", Some(&root)); - assert_eq_rooted(&path, "sub/sub1/d.html"); - } } |