From cf360ade77d53c3eb14061dd8a5631a56e0457e2 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 5 Sep 2017 19:54:15 +0200 Subject: Create new TemplateInput abstraction --- askama_shared/src/lib.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 0444a18..f38a6df 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -31,26 +31,40 @@ use std::path::PathBuf; /// the parse tree and/or generated source according to the `print` key's /// value as passed to the `template()` attribute. pub fn build_template(ast: &syn::DeriveInput) -> String { - let meta = TemplateMeta::new(ast); - let (path, src) = match meta.source { - Source::Source(s) => (PathBuf::new(), Cow::Borrowed(s)), - Source::Path(s) => { - let path = path::find_template_from_path(&s, None); - let src = path::get_template_source(&path); - (path, Cow::Owned(src)) - }, - }; - let nodes = parser::parse(src.as_ref()); - if meta.print == Print::Ast || meta.print == Print::All { + let data = TemplateInput::new(ast); + let nodes = parser::parse(data.source.as_ref()); + if data.meta.print == Print::Ast || data.meta.print == Print::All { println!("{:?}", nodes); } - let code = generator::generate(ast, &path, &nodes); - if meta.print == Print::Code || meta.print == Print::All { + let code = generator::generate(data.ast, &data.path, &nodes); + if data.meta.print == Print::Code || data.meta.print == Print::All { println!("{}", code); } code } +struct TemplateInput<'a> { + pub ast: &'a syn::DeriveInput, + pub meta: TemplateMeta<'a>, + pub path: PathBuf, + pub source: Cow<'a, str>, +} + +impl<'a> TemplateInput<'a> { + fn new(ast: &'a syn::DeriveInput) -> TemplateInput<'a> { + let meta = TemplateMeta::new(ast); + let (path, source) = match meta.source { + Source::Source(s) => (PathBuf::new(), Cow::Borrowed(s)), + Source::Path(s) => { + let path = path::find_template_from_path(&s, None); + let src = path::get_template_source(&path); + (path, Cow::Owned(src)) + }, + }; + TemplateInput { ast, meta, path, source } + } +} + mod errors { error_chain! { foreign_links { -- cgit