From 2a4d58cbb2033114890415c98a61e730185d1f83 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 6 Oct 2023 12:06:17 +0200 Subject: Reorganize TemplateArgs and TemplateInput structure --- askama_derive/src/input.rs | 42 +++++++++++++++++++++--------------------- askama_derive/src/lib.rs | 7 +++---- 2 files changed, 24 insertions(+), 25 deletions(-) (limited to 'askama_derive') diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index 891dec7..45da7f0 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -6,7 +6,7 @@ use mime::Mime; use quote::ToTokens; use syn::punctuated::Punctuated; -use crate::config::{get_template_source, Config}; +use crate::config::{get_template_source, read_config_file, Config}; use crate::CompileError; use parser::{Node, Parsed, Syntax}; @@ -14,10 +14,10 @@ pub(crate) struct TemplateInput<'a> { pub(crate) ast: &'a syn::DeriveInput, pub(crate) config: &'a Config<'a>, pub(crate) syntax: &'a Syntax<'a>, - pub(crate) source: Source, + pub(crate) source: &'a Source, pub(crate) print: Print, pub(crate) escaper: &'a str, - pub(crate) ext: Option, + pub(crate) ext: Option<&'a str>, pub(crate) mime_type: String, pub(crate) path: PathBuf, } @@ -29,7 +29,7 @@ impl TemplateInput<'_> { pub(crate) fn new<'n>( ast: &'n syn::DeriveInput, config: &'n Config<'_>, - args: TemplateArgs, + args: &'n TemplateArgs, ) -> Result, CompileError> { let TemplateArgs { source, @@ -43,7 +43,9 @@ impl TemplateInput<'_> { // Validate the `source` and `ext` value together, since they are // related. In case `source` was used instead of `path`, the value // of `ext` is merged into a synthetic `path` value here. - let source = source.expect("template path or source not found in attributes"); + let source = source + .as_ref() + .expect("template path or source not found in attributes"); let path = match (&source, &ext) { (Source::Path(path), _) => config.find_template(path, None)?, (&Source::Source(_), Some(ext)) => PathBuf::from(format!("{}.{}", ast.ident, ext)), @@ -53,28 +55,25 @@ impl TemplateInput<'_> { }; // Validate syntax - let syntax = syntax.map_or_else( + let syntax = syntax.as_deref().map_or_else( || Ok(config.syntaxes.get(config.default_syntax).unwrap()), |s| { config .syntaxes - .get(&s) + .get(s) .ok_or_else(|| CompileError::from(format!("attribute syntax {s} not exist"))) }, )?; // Match extension against defined output formats - let escaping = escaping.unwrap_or_else(|| { - path.extension() - .map(|s| s.to_str().unwrap()) - .unwrap_or("") - .to_string() - }); + let escaping = escaping + .as_deref() + .unwrap_or_else(|| path.extension().map(|s| s.to_str().unwrap()).unwrap_or("")); let mut escaper = None; for (extensions, path) in &config.escapers { - if extensions.contains(&escaping) { + if extensions.contains(escaping) { escaper = Some(path); break; } @@ -93,9 +92,9 @@ impl TemplateInput<'_> { config, syntax, source, - print, + print: *print, escaper, - ext, + ext: ext.as_deref(), mime_type, path, }) @@ -148,18 +147,18 @@ impl TemplateInput<'_> { #[inline] pub(crate) fn extension(&self) -> Option<&str> { - ext_default_to_path(self.ext.as_deref(), &self.path) + ext_default_to_path(self.ext, &self.path) } } -#[derive(Default)] +#[derive(Debug, Default)] pub(crate) struct TemplateArgs { pub(crate) source: Option, pub(crate) print: Print, pub(crate) escaping: Option, pub(crate) ext: Option, pub(crate) syntax: Option, - pub(crate) config_path: Option, + pub(crate) config: String, pub(crate) whitespace: Option, } @@ -259,7 +258,7 @@ impl TemplateArgs { } } else if ident == "config" { if let syn::Lit::Str(s) = value.lit { - args.config_path = Some(s.value()) + args.config = read_config_file(Some(&s.value()))?; } else { return Err("config value must be string literal".into()); } @@ -297,12 +296,13 @@ fn extension(path: &Path) -> Option<&str> { } } +#[derive(Debug)] pub(crate) enum Source { Path(String), Source(String), } -#[derive(PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq)] pub(crate) enum Print { All, Ast, diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 3046f9d..a133813 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -10,7 +10,7 @@ use proc_macro2::Span; use parser::ParseError; mod config; -use config::{read_config_file, Config}; +use config::Config; mod generator; use generator::{Generator, MapChain}; mod heritage; @@ -36,9 +36,8 @@ pub fn derive_template(input: TokenStream) -> TokenStream { /// value as passed to the `template()` attribute. 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())?; - let input = TemplateInput::new(ast, &config, template_args)?; + let config = Config::new(&template_args.config, template_args.whitespace.as_ref())?; + let input = TemplateInput::new(ast, &config, &template_args)?; let mut templates = HashMap::new(); input.find_used_templates(&mut templates)?; -- cgit