From 58e02a4fb6f6d58ff68d18f24abcfc8cab53f7be Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 24 Jan 2023 09:21:52 +0100 Subject: derive: Make Config `'static` The configuration is made `'static`, because `toml` and `toml_edit` both needs to implement serde's `DeserializeOwned` by now. We allocate the strings once per template, so it is very unlikely that this change will have any measurable impact, neither in compile time nor RAM usage. --- askama_derive/src/config.rs | 95 ++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 48 deletions(-) (limited to 'askama_derive/src/config.rs') diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs index 8c49217..4dcf1ea 100644 --- a/askama_derive/src/config.rs +++ b/askama_derive/src/config.rs @@ -1,4 +1,5 @@ use std::collections::{BTreeMap, HashSet}; +use std::convert::TryFrom; use std::path::{Path, PathBuf}; use std::{env, fs}; @@ -8,16 +9,16 @@ use serde::Deserialize; use crate::CompileError; #[derive(Debug)] -pub(crate) struct Config<'a> { +pub(crate) struct Config { pub(crate) dirs: Vec, - pub(crate) syntaxes: BTreeMap>, - pub(crate) default_syntax: &'a str, + pub(crate) syntaxes: BTreeMap, + pub(crate) default_syntax: String, pub(crate) escapers: Vec<(HashSet, String)>, pub(crate) whitespace: WhitespaceHandling, } -impl Config<'_> { - pub(crate) fn new(s: &str) -> std::result::Result, CompileError> { +impl Config { + pub(crate) fn new(s: &str) -> std::result::Result { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let default_dirs = vec![root.join("templates")]; @@ -39,19 +40,19 @@ impl Config<'_> { dirs.map_or(default_dirs, |v| { v.into_iter().map(|dir| root.join(dir)).collect() }), - default_syntax.unwrap_or(DEFAULT_SYNTAX_NAME), + default_syntax.unwrap_or_else(|| DEFAULT_SYNTAX_NAME.to_owned()), whitespace, ), None => ( default_dirs, - DEFAULT_SYNTAX_NAME, + DEFAULT_SYNTAX_NAME.to_owned(), WhitespaceHandling::default(), ), }; if let Some(raw_syntaxes) = raw.syntax { for raw_s in raw_syntaxes { - let name = raw_s.name; + let name = raw_s.name.clone(); if syntaxes .insert(name.to_string(), Syntax::try_from(raw_s)?) @@ -62,7 +63,7 @@ impl Config<'_> { } } - if !syntaxes.contains_key(default_syntax) { + if !syntaxes.contains_key(&default_syntax) { return Err(format!("default syntax \"{default_syntax}\" not found").into()); } @@ -120,32 +121,32 @@ impl Config<'_> { } #[derive(Debug)] -pub(crate) struct Syntax<'a> { - pub(crate) block_start: &'a str, - pub(crate) block_end: &'a str, - pub(crate) expr_start: &'a str, - pub(crate) expr_end: &'a str, - pub(crate) comment_start: &'a str, - pub(crate) comment_end: &'a str, +pub(crate) struct Syntax { + pub(crate) block_start: String, + pub(crate) block_end: String, + pub(crate) expr_start: String, + pub(crate) expr_end: String, + pub(crate) comment_start: String, + pub(crate) comment_end: String, } -impl Default for Syntax<'_> { +impl Default for Syntax { fn default() -> Self { Self { - block_start: "{%", - block_end: "%}", - expr_start: "{{", - expr_end: "}}", - comment_start: "{#", - comment_end: "#}", + block_start: "{%".to_owned(), + block_end: "%}".to_owned(), + expr_start: "{{".to_owned(), + expr_end: "}}".to_owned(), + comment_start: "{#".to_owned(), + comment_end: "#}".to_owned(), } } } -impl<'a> TryFrom> for Syntax<'a> { +impl TryFrom for Syntax { type Error = CompileError; - fn try_from(raw: RawSyntax<'a>) -> std::result::Result { + fn try_from(raw: RawSyntax) -> std::result::Result { let default = Self::default(); let syntax = Self { block_start: raw.block_start.unwrap_or(default.block_start), @@ -182,21 +183,20 @@ impl<'a> TryFrom> for Syntax<'a> { #[cfg_attr(feature = "serde", derive(Deserialize))] #[derive(Default)] -struct RawConfig<'d> { - #[cfg_attr(feature = "serde", serde(borrow))] - general: Option>, - syntax: Option>>, - escaper: Option>>, +struct RawConfig { + general: Option, + syntax: Option>, + escaper: Option>, } -impl RawConfig<'_> { +impl RawConfig { #[cfg(feature = "config")] - fn from_toml_str(s: &str) -> std::result::Result, CompileError> { + fn from_toml_str(s: &str) -> std::result::Result { toml::from_str(s).map_err(|e| format!("invalid TOML in {CONFIG_FILE_NAME}: {e}").into()) } #[cfg(not(feature = "config"))] - fn from_toml_str(_: &str) -> std::result::Result, CompileError> { + fn from_toml_str(_: &str) -> std::result::Result { Err("TOML support not available".into()) } } @@ -222,29 +222,28 @@ impl Default for WhitespaceHandling { } #[cfg_attr(feature = "serde", derive(Deserialize))] -struct General<'a> { - #[cfg_attr(feature = "serde", serde(borrow))] - dirs: Option>, - default_syntax: Option<&'a str>, +struct General { + dirs: Option>, + default_syntax: Option, #[cfg_attr(feature = "serde", serde(default))] whitespace: WhitespaceHandling, } #[cfg_attr(feature = "serde", derive(Deserialize))] -struct RawSyntax<'a> { - name: &'a str, - block_start: Option<&'a str>, - block_end: Option<&'a str>, - expr_start: Option<&'a str>, - expr_end: Option<&'a str>, - comment_start: Option<&'a str>, - comment_end: Option<&'a str>, +struct RawSyntax { + name: String, + block_start: Option, + block_end: Option, + expr_start: Option, + expr_end: Option, + comment_start: Option, + comment_end: Option, } #[cfg_attr(feature = "serde", derive(Deserialize))] -struct RawEscaper<'a> { - path: &'a str, - extensions: Vec<&'a str>, +struct RawEscaper { + path: String, + extensions: Vec, } pub(crate) fn read_config_file( -- cgit