From 584f7157ff58d1dd55ee53b1bcd78b3357fbaac1 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Wed, 12 Jan 2022 16:09:24 +0100 Subject: Make sure '#[template(…)]' is given exactly once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- askama_shared/src/input.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'askama_shared') diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs index a8ad025..f1b2dc6 100644 --- a/askama_shared/src/input.rs +++ b/askama_shared/src/input.rs @@ -28,23 +28,29 @@ impl TemplateInput<'_> { ast: &'n syn::DeriveInput, config: &'n Config<'_>, ) -> Result, CompileError> { - // Check that an attribute called `template()` exists and that it is + // Check that an attribute called `template()` exists once and that it is // the proper type (list). - let template = ast - .attrs - .iter() - .find_map(|attr| { - attr.path.is_ident("template").then(|| { - attr.parse_meta() - .map_err(|e| format!("unable to parse attribute: {}", e).into()) - }) - }) - .unwrap_or(Err(CompileError::Static("no attribute 'template' found")))?; - - let template_args = match template { - syn::Meta::List(inner) => inner, - _ => return Err("attribute 'template' has incorrect type".into()), - }; + let mut template_args = None; + for attr in &ast.attrs { + if attr.path.is_ident("template") { + if template_args.is_some() { + return Err(CompileError::Static("duplicated 'template' attribute")); + } + let template = attr.parse_meta().map_err(|e| { + CompileError::String(format!("unable to parse attribute: {}", e)) + })?; + match template { + syn::Meta::List(inner) => template_args = Some(inner), + _ => { + return Err(CompileError::Static( + "attribute 'template' has incorrect type", + )); + } + } + } + } + let template_args = + template_args.ok_or(CompileError::Static("no attribute 'template' found"))?; // Loop over the meta attributes and find everything that we // understand. Raise panics if something is not right. -- cgit