diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-08 19:46:12 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-08 19:46:12 +0100 |
commit | 330e2ef471e07dbb242bf402001f69e5c0cd2701 (patch) | |
tree | ab6921092983343126356c071b4ec32c3168cde0 /askama_derive | |
parent | 4e34adcbbee5a1bc592692521bb1de5b24977931 (diff) | |
download | askama-330e2ef471e07dbb242bf402001f69e5c0cd2701.tar.gz askama-330e2ef471e07dbb242bf402001f69e5c0cd2701.tar.bz2 askama-330e2ef471e07dbb242bf402001f69e5c0cd2701.zip |
Simplify code to find template path
Diffstat (limited to 'askama_derive')
-rw-r--r-- | askama_derive/src/lib.rs | 33 |
1 files changed, 10 insertions, 23 deletions
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 59d1ae1..244c43b 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -4,32 +4,19 @@ extern crate syn; use proc_macro::TokenStream; -fn get_path_from_attrs(attrs: &Vec<syn::Attribute>) -> String { - for attr in attrs { - if attr.name() == "template" { - match attr.value { - syn::MetaItem::List(_, ref inner) => { - match inner[0] { - syn::NestedMetaItem::MetaItem(ref item) => { - match item { - &syn::MetaItem::NameValue(ref key, ref val) => { - assert_eq!(key.as_ref(), "path"); - match val { - &syn::Lit::Str(ref s, _) => { return s.clone(); }, - _ => panic!("template path must be a string"), - } - }, - _ => panic!("template annotation must contain key/value pair"), - } - }, - _ => panic!("template annotation must contain item"), - } - }, - _ => panic!("template annotation must be of List type"), +fn get_path_from_attrs(attrs: &[syn::Attribute]) -> String { + let attr = attrs.iter().find(|a| a.name() == "template").unwrap(); + if let syn::MetaItem::List(_, ref inner) = attr.value { + if let syn::NestedMetaItem::MetaItem(ref item) = inner[0] { + if let &syn::MetaItem::NameValue(ref key, ref val) = item { + assert_eq!(key.as_ref(), "path"); + if let &syn::Lit::Str(ref s, _) = val { + return s.clone(); + } } } } - panic!("template annotation not found"); + panic!("template path not found in struct attributes"); } #[proc_macro_derive(Template, attributes(template))] |