diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-05 19:54:15 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-05 19:54:15 +0200 |
commit | cf360ade77d53c3eb14061dd8a5631a56e0457e2 (patch) | |
tree | 3e502b3520684dc30af09c210246e4a006c1c6f6 /askama_shared | |
parent | 41a41579acbdfb3f85455edb65ff82d9ee24bf1b (diff) | |
download | askama-cf360ade77d53c3eb14061dd8a5631a56e0457e2.tar.gz askama-cf360ade77d53c3eb14061dd8a5631a56e0457e2.tar.bz2 askama-cf360ade77d53c3eb14061dd8a5631a56e0457e2.zip |
Create new TemplateInput abstraction
Diffstat (limited to 'askama_shared')
-rw-r--r-- | askama_shared/src/lib.rs | 40 |
1 files 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 { |