From 145420b68ebe6d310a81c8965f9a9e81aa4dbfe1 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 6 Oct 2023 11:30:37 +0200 Subject: Inline nested derive_template() implementation --- askama_derive/src/generator.rs | 12 +----------- askama_derive/src/lib.rs | 6 +++++- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 6f561f8..cb4b102 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -7,7 +7,6 @@ use parser::node::{ Call, Comment, CondTest, If, Include, Let, Lit, Loop, Match, Target, Whitespace, Ws, }; use parser::{Expr, Node, Parsed}; -use proc_macro::TokenStream; use quote::{quote, ToTokens}; use syn::punctuated::Punctuated; @@ -15,15 +14,6 @@ use std::collections::hash_map::{Entry, HashMap}; use std::path::{Path, PathBuf}; use std::{cmp, hash, mem, str}; -/// The actual implementation for askama_derive::Template -pub(crate) fn derive_template(input: TokenStream) -> TokenStream { - let ast: syn::DeriveInput = syn::parse(input).unwrap(); - match build_template(&ast) { - Ok(source) => source.parse().unwrap(), - Err(e) => e.into_compile_error(), - } -} - /// Takes a `syn::DeriveInput` and generates source code for it /// /// Reads the metadata from the `template()` attribute to get the template @@ -31,7 +21,7 @@ pub(crate) fn derive_template(input: TokenStream) -> TokenStream { /// parsed, and the parse tree is fed to the code generator. Will print /// the parse tree and/or generated source according to the `print` key's /// value as passed to the `template()` attribute. -fn build_template(ast: &syn::DeriveInput) -> Result { +pub(crate) fn build_template(ast: &syn::DeriveInput) -> Result { let template_args = TemplateArgs::new(ast)?; let config_toml = read_config_file(template_args.config_path.as_deref())?; let config = Config::new(&config_toml, template_args.whitespace.as_ref())?; diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 8a737aa..3b80635 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -16,7 +16,11 @@ mod input; #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { - generator::derive_template(input) + let ast = syn::parse::(input).unwrap(); + match generator::build_template(&ast) { + Ok(source) => source.parse().unwrap(), + Err(e) => e.into_compile_error(), + } } #[derive(Debug, Clone)] -- cgit