Inline TemplateMeta into TemplateInput

This commit is contained in:
Dirkjan Ochtman 2018-06-21 12:17:45 +02:00
parent e09cdfaf1f
commit f05a924c49
3 changed files with 25 additions and 28 deletions

View File

@ -589,7 +589,7 @@ impl<'a> Generator<'a> {
use self::DisplayWrap::*;
use super::input::EscapeMode::*;
self.write("writer.write_fmt(format_args!(\"{}\", ");
self.write(match (wrapped, &state.input.meta.escaping) {
self.write(match (wrapped, &state.input.escaping) {
(Wrapped, &Html) |
(Wrapped, &None) |
(Unwrapped, &None) => "askama_expr",

View File

@ -7,34 +7,15 @@ use syn;
pub struct TemplateInput<'a> {
pub ast: &'a syn::DeriveInput,
pub meta: TemplateMeta,
pub source: Source,
pub print: Print,
pub escaping: EscapeMode,
pub ext: Option<String>,
pub path: PathBuf,
}
impl<'a> TemplateInput<'a> {
pub fn new(ast: &'a syn::DeriveInput) -> TemplateInput<'a> {
let meta = TemplateMeta::new(ast);
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 }
}
}
// Holds metadata for the template, based on the `template()` attribute.
pub struct TemplateMeta {
pub source: Source,
pub print: Print,
pub escaping: EscapeMode,
pub ext: Option<String>,
}
impl TemplateMeta {
fn new(ast: &syn::DeriveInput) -> TemplateMeta {
let attr = ast.attrs
.iter()
.find(|a| a.interpret_meta().unwrap().name() == "template");
@ -115,7 +96,23 @@ impl TemplateMeta {
}
},
};
TemplateMeta { source, print, escaping, ext }
let path = match source {
Source::Source(_) => match 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,
source,
print,
escaping,
ext,
path,
}
}
}

View File

@ -34,18 +34,18 @@ 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 source = match input.meta.source {
let source = match input.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 {
if input.print == Print::Ast || input.print == Print::All {
println!("{:?}", nodes);
}
let code = generator::generate(&input, &nodes);
if input.meta.print == Print::Code || input.meta.print == Print::All {
if input.print == Print::Code || input.print == Print::All {
println!("{}", code);
}
code