diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2022-01-12 16:09:24 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2022-01-13 15:10:09 +0100 |
commit | 584f7157ff58d1dd55ee53b1bcd78b3357fbaac1 (patch) | |
tree | 4596b712dfb4b2dcbc9a15b33da3878b2e7a444d /askama_shared/src | |
parent | 18d5bb7b810645b4bb0a8c669dfc41a0cd4933f9 (diff) | |
download | askama-584f7157ff58d1dd55ee53b1bcd78b3357fbaac1.tar.gz askama-584f7157ff58d1dd55ee53b1bcd78b3357fbaac1.tar.bz2 askama-584f7157ff58d1dd55ee53b1bcd78b3357fbaac1.zip |
Make sure '#[template(…)]' is given exactly once
Diffstat (limited to 'askama_shared/src')
-rw-r--r-- | askama_shared/src/input.rs | 38 |
1 files changed, 22 insertions, 16 deletions
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<TemplateInput<'n>, 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. |