From f9f4f57221e4488f3ecf26752e6c53d60167a5af Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 11 Jul 2018 22:38:07 +0100 Subject: Move get_template_source() into askama_derive --- askama_derive/src/lib.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'askama_derive/src/lib.rs') diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index c36da67..1fd7570 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -14,9 +14,10 @@ mod parser; use input::{Print, Source, TemplateInput}; use parser::{Expr, Macro, Node}; use proc_macro::TokenStream; -use shared::{path, Config}; +use shared::Config; use std::collections::HashMap; +use std::fs; use std::path::{Path, PathBuf}; #[proc_macro_derive(Template, attributes(template))] @@ -36,7 +37,7 @@ fn build_template(ast: &syn::DeriveInput) -> String { let input = TemplateInput::new(ast); let source: String = match input.source { Source::Source(ref s) => s.clone(), - Source::Path(_) => path::get_template_source(&input.path), + Source::Path(_) => get_template_source(&input.path), }; let mut sources = HashMap::new(); @@ -70,12 +71,12 @@ fn find_used_templates(input: &TemplateInput, map: &mut HashMap match n { Node::Extends(Expr::StrLit(extends)) => { let extends = input.config.find_template(extends, Some(&path)); - let source = path::get_template_source(&extends); + let source = get_template_source(&extends); check.push((extends, source)); } Node::Import(_, import, _) => { let import = input.config.find_template(import, Some(&path)); - let source = path::get_template_source(&import); + let source = get_template_source(&import); check.push((import, source)); } _ => {} @@ -158,3 +159,30 @@ impl<'a> Context<'a> { } } } + +fn get_template_source(tpl_path: &Path) -> String { + match fs::read_to_string(tpl_path) { + Err(_) => panic!( + "unable to open template file '{}'", + tpl_path.to_str().unwrap() + ), + Ok(mut source) => { + if source.ends_with('\n') { + let _ = source.pop(); + } + source + } + } +} + +#[cfg(test)] +mod tests { + use super::get_template_source; + use Config; + + #[test] + fn get_source() { + let path = Config::new().find_template("b.html", None); + assert_eq!(get_template_source(&path), "bar"); + } +} -- cgit