aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2018-06-21 15:47:53 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2018-06-21 15:47:53 +0200
commit532e252270697af2737153d27dd13f978ef856df (patch)
tree65294266aef43dab403c02d6401998eb5c86c8f2
parent959f5e1333effbbf52a1e2e91a7f31ee303e5cbe (diff)
downloadaskama-532e252270697af2737153d27dd13f978ef856df.tar.gz
askama-532e252270697af2737153d27dd13f978ef856df.tar.bz2
askama-532e252270697af2737153d27dd13f978ef856df.zip
Store parent field type in TemplateInput
-rw-r--r--askama_derive/src/generator.rs21
-rw-r--r--askama_derive/src/input.rs15
2 files changed, 19 insertions, 17 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 5d16f2c..6c6e1e7 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -61,9 +61,10 @@ impl<'a> Generator<'a> {
if ctx.extends.is_none() {
self.define_trait(ctx);
} else {
- let parent_type = get_parent_type(self.input.ast)
- .expect("expected field '_parent' in extending template struct");
- self.deref_to_parent(parent_type);
+ match self.input.parent {
+ Some(ty) => self.deref_to_parent(ty),
+ None => panic!("expected field '_parent' in extending template struct"),
+ }
}
let trait_nodes = if ctx.extends.is_none() {
@@ -930,20 +931,6 @@ where
}
}
-fn get_parent_type(ast: &syn::DeriveInput) -> Option<&syn::Type> {
- match ast.data {
- syn::Data::Struct(syn::DataStruct {
- fields: syn::Fields::Named(ref fields),
- ..
- }) => fields.named.iter().filter_map(|f| {
- f.ident
- .as_ref()
- .and_then(|name| if name == "_parent" { Some(&f.ty) } else { None })
- }),
- _ => panic!("derive(Template) only works for struct items"),
- }.next()
-}
-
#[derive(Clone, PartialEq)]
enum AstLevel {
Top,
diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs
index d835763..afa4226 100644
--- a/askama_derive/src/input.rs
+++ b/askama_derive/src/input.rs
@@ -10,6 +10,7 @@ pub struct TemplateInput<'a> {
pub print: Print,
pub escaping: EscapeMode,
pub ext: Option<String>,
+ pub parent: Option<&'a syn::Type>,
pub path: PathBuf,
}
@@ -83,6 +84,7 @@ impl<'a> TemplateInput<'a> {
}
_ => {}
}
+
let escaping = match escaping {
Some(m) => m,
None => {
@@ -101,6 +103,18 @@ impl<'a> TemplateInput<'a> {
}
};
+ let parent = match ast.data {
+ syn::Data::Struct(syn::DataStruct {
+ fields: syn::Fields::Named(ref fields),
+ ..
+ }) => fields.named.iter().filter_map(|f| {
+ f.ident
+ .as_ref()
+ .and_then(|name| if name == "_parent" { Some(&f.ty) } else { None })
+ }),
+ _ => panic!("derive(Template) only works for struct items"),
+ }.next();
+
let path = match source {
Source::Source(_) => match ext {
Some(ref v) => PathBuf::from(format!("{}.{}", ast.ident, v)),
@@ -115,6 +129,7 @@ impl<'a> TemplateInput<'a> {
print,
escaping,
ext,
+ parent,
path,
}
}