diff options
-rw-r--r-- | askama/src/lib.rs | 12 | ||||
-rw-r--r-- | askama/src/path.rs | 17 | ||||
-rw-r--r-- | askama_derive/src/lib.rs | 18 |
3 files changed, 24 insertions, 23 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 2b59cf0..99917ed 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -11,8 +11,14 @@ pub trait Template { } } -pub mod filters; -pub mod generator; -pub mod parser; +mod generator; +mod parser; mod path; + +pub mod filters; pub use path::rerun_if_templates_changed; +pub fn build_template(path: &str, ast: &syn::DeriveInput) -> String { + let src = path::get_template_source(path); + let nodes = parser::parse(&src); + generator::generate(ast, path, nodes) +} diff --git a/askama/src/path.rs b/askama/src/path.rs index c09b200..5f500e2 100644 --- a/askama/src/path.rs +++ b/askama/src/path.rs @@ -1,7 +1,18 @@ use std::env; -use std::fs::{self, DirEntry}; -use std::io; -use std::path::Path; +use std::fs::{self, DirEntry, File}; +use std::io::{self, Read}; +use std::path::{Path, PathBuf}; + +pub fn get_template_source(tpl_file: &str) -> String { + let root = env::var("CARGO_MANIFEST_DIR").unwrap(); + let mut path = PathBuf::from(root); + path.push("templates"); + path.push(Path::new(tpl_file)); + let mut f = File::open(path).unwrap(); + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + s +} fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> { if dir.is_dir() { diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index ba5dec4..59d1ae1 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -3,9 +3,6 @@ extern crate proc_macro; extern crate syn; use proc_macro::TokenStream; -use std::fs::File; -use std::io::Read; -use std::path::{Path, PathBuf}; fn get_path_from_attrs(attrs: &Vec<syn::Attribute>) -> String { for attr in attrs { @@ -35,17 +32,6 @@ fn get_path_from_attrs(attrs: &Vec<syn::Attribute>) -> String { panic!("template annotation not found"); } -fn get_template_source(tpl_file: &str) -> String { - let root = ::std::env::var("CARGO_MANIFEST_DIR").unwrap(); - let mut path = PathBuf::from(root); - path.push("templates"); - path.push(Path::new(tpl_file)); - let mut f = File::open(path).unwrap(); - let mut s = String::new(); - f.read_to_string(&mut s).unwrap(); - s -} - #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { let source = input.to_string(); @@ -57,7 +43,5 @@ pub fn derive_template(input: TokenStream) -> TokenStream { }; let path = get_path_from_attrs(&ast.attrs); - let src = get_template_source(&path); - let nodes = askama::parser::parse(&src); - askama::generator::generate(&ast, &path, nodes).parse().unwrap() + askama::build_template(&path, &ast).parse().unwrap() } |