aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/path.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-27 22:10:42 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-27 22:10:42 +0200
commit9f3b590206e3dfe33b7129b1c8ff010f60318cf2 (patch)
tree5411864d92ec220d9d4db8d9e40cda6df9b2b16f /askama_derive/src/path.rs
parentaeac47cee0e14b9fa38c01082876667f0ec8d874 (diff)
downloadaskama-9f3b590206e3dfe33b7129b1c8ff010f60318cf2.tar.gz
askama-9f3b590206e3dfe33b7129b1c8ff010f60318cf2.tar.bz2
askama-9f3b590206e3dfe33b7129b1c8ff010f60318cf2.zip
Move most of the code into new askama_shared crate
This makes it possible to share code between askama and askama_derive.
Diffstat (limited to 'askama_derive/src/path.rs')
-rw-r--r--askama_derive/src/path.rs86
1 files changed, 0 insertions, 86 deletions
diff --git a/askama_derive/src/path.rs b/askama_derive/src/path.rs
deleted file mode 100644
index 3c04965..0000000
--- a/askama_derive/src/path.rs
+++ /dev/null
@@ -1,86 +0,0 @@
-use std::env;
-use std::fs::File;
-use std::io::Read;
-use std::path::{Path, PathBuf};
-
-pub fn get_template_source(tpl_path: &Path) -> String {
- let mut path = template_dir();
- path.push(tpl_path);
- let mut f = match File::open(&path) {
- Err(_) => {
- let msg = format!("unable to open template file '{}'",
- &path.to_str().unwrap());
- panic!(msg);
- },
- Ok(f) => f,
- };
- let mut s = String::new();
- f.read_to_string(&mut s).unwrap();
- if s.ends_with('\n') {
- let _ = s.pop();
- }
- s
-}
-
-pub fn find_template_from_path<'a>(path: &str, start_at: Option<&Path>) -> PathBuf {
- let root = template_dir();
- if let Some(rel) = start_at {
- let mut fs_rel_path = root.clone();
- fs_rel_path.push(rel);
- fs_rel_path = fs_rel_path.with_file_name(path);
- if fs_rel_path.exists() {
- return fs_rel_path.strip_prefix(&root).unwrap().to_owned();
- }
- }
-
- let mut fs_abs_path = root.clone();
- let path = Path::new(path);
- fs_abs_path.push(Path::new(path));
- if fs_abs_path.exists() {
- path.to_owned()
- } else {
- panic!(format!("template '{:?}' not found", path.to_str()));
- }
-}
-
-// Duplicated in askama
-fn template_dir() -> PathBuf {
- let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
- path.push("templates");
- path
-}
-
-#[cfg(test)]
-mod tests {
- use super::{find_template_from_path, get_template_source};
- use super::Path;
-
- #[test]
- fn get_source() {
- assert_eq!(get_template_source(Path::new("sub/b.html")), "bar");
- }
-
- #[test]
- fn find_absolute() {
- let path = find_template_from_path("sub/b.html", Some(Path::new("a.html")));
- assert_eq!(path, Path::new("sub/b.html"));
- }
-
- #[test]
- #[should_panic]
- fn find_relative_nonexistent() {
- find_template_from_path("b.html", Some(Path::new("a.html")));
- }
-
- #[test]
- fn find_relative() {
- let path = find_template_from_path("c.html", Some(Path::new("sub/b.html")));
- assert_eq!(path, Path::new("sub/c.html"));
- }
-
- #[test]
- fn find_relative_sub() {
- let path = find_template_from_path("sub1/d.html", Some(Path::new("sub/b.html")));
- assert_eq!(path, Path::new("sub/sub1/d.html"));
- }
-}