From e09cdfaf1f9cca45ed320c3ad97f3502c48ff73d Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 21 Jun 2018 12:13:56 +0200 Subject: Let build_template() own the template source --- askama_derive/src/input.rs | 24 ++++++++---------------- askama_derive/src/lib.rs | 11 +++++++++-- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index c4bb0a3..62c1072 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -9,33 +9,25 @@ pub struct TemplateInput<'a> { pub ast: &'a syn::DeriveInput, pub meta: TemplateMeta, pub path: PathBuf, - pub source: String, } impl<'a> TemplateInput<'a> { pub fn new(ast: &'a syn::DeriveInput) -> TemplateInput<'a> { let meta = TemplateMeta::new(ast); - let (path, source) = match meta.source { - Source::Source(ref s) => { - let path = match meta.ext { - Some(ref v) => PathBuf::from(format!("{}.{}", ast.ident, v)), - None => PathBuf::new(), - }; - (path, s.clone()) - }, - Source::Path(ref s) => { - let path = path::find_template_from_path(s, None); - let src = path::get_template_source(&path); - (path, src) + let path = match meta.source { + Source::Source(_) => match meta.ext { + Some(ref v) => PathBuf::from(format!("{}.{}", ast.ident, v)), + None => PathBuf::new(), }, + Source::Path(ref s) => path::find_template_from_path(s, None), }; - TemplateInput { ast, meta, path, source } + TemplateInput { ast, meta, path } } } // Holds metadata for the template, based on the `template()` attribute. pub struct TemplateMeta { - source: Source, + pub source: Source, pub print: Print, pub escaping: EscapeMode, pub ext: Option, @@ -127,7 +119,7 @@ impl TemplateMeta { } } -enum Source { +pub enum Source { Path(String), Source(String), } diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 0e372ed..a2b4ace 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -11,8 +11,9 @@ mod input; mod generator; mod parser; -use input::Print; +use input::{Print, Source}; use proc_macro::TokenStream; +use shared::path; #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { @@ -33,10 +34,16 @@ pub fn derive_template(input: TokenStream) -> TokenStream { /// value as passed to the `template()` attribute. fn build_template(ast: &syn::DeriveInput) -> String { let input = input::TemplateInput::new(ast); - let nodes = parser::parse(input.source.as_ref()); + let source = match input.meta.source { + Source::Source(ref s) => s.clone(), + Source::Path(_) => path::get_template_source(&input.path) + }; + + let nodes = parser::parse(&source); if input.meta.print == Print::Ast || input.meta.print == Print::All { println!("{:?}", nodes); } + let code = generator::generate(&input, &nodes); if input.meta.print == Print::Code || input.meta.print == Print::All { println!("{}", code); -- cgit