diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-06 20:03:22 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-06 20:03:47 +0200 |
commit | 60036152a0ab731a90649d153a3fc9c944ac6e8d (patch) | |
tree | 4065bb64e0bd884c6470dce759160d422b790b33 /askama_shared | |
parent | ddcc0453ba8803ba8f15eb1de030cd5f96731ed5 (diff) | |
download | askama-60036152a0ab731a90649d153a3fc9c944ac6e8d.tar.gz askama-60036152a0ab731a90649d153a3fc9c944ac6e8d.tar.bz2 askama-60036152a0ab731a90649d153a3fc9c944ac6e8d.zip |
Allow setting an extension with source attributes
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/input.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs index 5fc064b..f0baed8 100644 --- a/askama_shared/src/input.rs +++ b/askama_shared/src/input.rs @@ -17,7 +17,13 @@ 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(s) => (PathBuf::new(), Cow::Borrowed(s)), + Source::Source(s) => { + let path = match meta.ext { + Some(v) => PathBuf::from(format!("_.{}", v)), + None => PathBuf::new(), + }; + (path, Cow::Borrowed(s)) + }, Source::Path(s) => { let path = path::find_template_from_path(&s, None); let src = path::get_template_source(&path); @@ -33,6 +39,7 @@ pub struct TemplateMeta<'a> { source: Source<'a>, pub print: Print, pub escaping: EscapeMode, + pub ext: Option<&'a str>, } impl<'a> TemplateMeta<'a> { @@ -48,6 +55,7 @@ impl<'a> TemplateMeta<'a> { let mut source = None; let mut print = Print::None; let mut escaping = EscapeMode::Html; + let mut ext = None; if let syn::MetaItem::List(_, ref inner) = attr.value { for nm_item in inner { if let syn::NestedMetaItem::MetaItem(ref item) = *nm_item { @@ -79,6 +87,11 @@ impl<'a> TemplateMeta<'a> { } else { panic!("escape value must be string literal"); }, + "ext" => if let syn::Lit::Str(ref s, _) = *val { + ext = Some((s.as_ref() as &str).into()); + } else { + panic!("ext value must be string literal"); + }, _ => { panic!("unsupported annotation key found") } } } @@ -87,7 +100,14 @@ impl<'a> TemplateMeta<'a> { } match source { - Some(s) => TemplateMeta { source: s, print, escaping }, + Some(s) => { + if let Source::Path(_) = s { + if ext.is_some() { + panic!("'ext' attribute cannot be used with 'path' attribute"); + } + } + TemplateMeta { source: s, print, escaping, ext } + }, None => panic!("template path or source not found in struct attributes"), } } |