From 0392bcac47325873d479e1eeefc88b4b9c2b7950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Mon, 20 Feb 2023 11:30:55 +0100 Subject: [PATCH 1/2] Replace `toml_edit` with `basic-toml` --- askama_derive/Cargo.toml | 4 ++-- askama_derive/src/config.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 15dd36bfa..c13e7cafb 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -14,7 +14,7 @@ rust-version = "1.58" proc-macro = true [features] -config = ["serde", "toml_edit"] +config = ["serde", "basic-toml"] humansize = [] markdown = [] urlencode = [] @@ -38,4 +38,4 @@ proc-macro2 = "1" quote = "1" serde = { version = "1.0", optional = true, features = ["derive"] } syn = "1" -toml_edit = { version = "0.19", optional = true, features = ["serde"] } +basic-toml = { version = "0.1.1", optional = true } diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs index a07574498..eec20ed92 100644 --- a/askama_derive/src/config.rs +++ b/askama_derive/src/config.rs @@ -192,7 +192,7 @@ struct RawConfig { impl RawConfig { #[cfg(feature = "config")] fn from_toml_str(s: &str) -> std::result::Result { - toml_edit::de::from_str(s) + basic_toml::from_str(s) .map_err(|e| format!("invalid TOML in {CONFIG_FILE_NAME}: {e}").into()) } From 747958b743d356b1b9650def5df4650384dd8e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Mon, 20 Feb 2023 11:46:41 +0100 Subject: [PATCH 2/2] Revert "derive: Make Config `'static`" --- askama_derive/src/config.rs | 96 ++++++++++++++++--------------- askama_derive/src/heritage.rs | 2 +- askama_derive/src/input.rs | 8 +-- askama_derive/src/parser/mod.rs | 29 +++++----- askama_derive/src/parser/node.rs | 4 +- askama_derive/src/parser/tests.rs | 4 +- 6 files changed, 74 insertions(+), 69 deletions(-) diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs index eec20ed92..98e51ce0c 100644 --- a/askama_derive/src/config.rs +++ b/askama_derive/src/config.rs @@ -9,16 +9,16 @@ use serde::Deserialize; use crate::CompileError; #[derive(Debug)] -pub(crate) struct Config { +pub(crate) struct Config<'a> { pub(crate) dirs: Vec, - pub(crate) syntaxes: BTreeMap, - pub(crate) default_syntax: String, + pub(crate) syntaxes: BTreeMap>, + pub(crate) default_syntax: &'a str, pub(crate) escapers: Vec<(HashSet, String)>, pub(crate) whitespace: WhitespaceHandling, } -impl Config { - pub(crate) fn new(s: &str) -> std::result::Result { +impl Config<'_> { + pub(crate) fn new(s: &str) -> std::result::Result, CompileError> { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let default_dirs = vec![root.join("templates")]; @@ -40,19 +40,19 @@ impl Config { dirs.map_or(default_dirs, |v| { v.into_iter().map(|dir| root.join(dir)).collect() }), - default_syntax.unwrap_or_else(|| DEFAULT_SYNTAX_NAME.to_owned()), + default_syntax.unwrap_or(DEFAULT_SYNTAX_NAME), whitespace, ), None => ( default_dirs, - DEFAULT_SYNTAX_NAME.to_owned(), + DEFAULT_SYNTAX_NAME, WhitespaceHandling::default(), ), }; if let Some(raw_syntaxes) = raw.syntax { for raw_s in raw_syntaxes { - let name = raw_s.name.clone(); + let name = raw_s.name; if syntaxes .insert(name.to_string(), Syntax::try_from(raw_s)?) @@ -63,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()); } @@ -121,33 +121,33 @@ impl Config { } #[derive(Debug)] -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, +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, } -impl Default for Syntax { +impl Default for Syntax<'static> { fn default() -> Self { Self { - block_start: "{%".to_owned(), - block_end: "%}".to_owned(), - expr_start: "{{".to_owned(), - expr_end: "}}".to_owned(), - comment_start: "{#".to_owned(), - comment_end: "#}".to_owned(), + block_start: "{%", + block_end: "%}", + expr_start: "{{", + expr_end: "}}", + comment_start: "{#", + comment_end: "#}", } } } -impl TryFrom for Syntax { +impl<'a> TryFrom> for Syntax<'a> { type Error = CompileError; - fn try_from(raw: RawSyntax) -> std::result::Result { - let default = Self::default(); + fn try_from(raw: RawSyntax<'a>) -> std::result::Result { + let default = Syntax::default(); let syntax = Self { block_start: raw.block_start.unwrap_or(default.block_start), block_end: raw.block_end.unwrap_or(default.block_end), @@ -183,21 +183,22 @@ impl TryFrom for Syntax { #[cfg_attr(feature = "serde", derive(Deserialize))] #[derive(Default)] -struct RawConfig { - general: Option, - syntax: Option>, - escaper: Option>, +struct RawConfig<'a> { + #[cfg_attr(feature = "serde", serde(borrow))] + general: Option>, + syntax: Option>>, + escaper: Option>>, } -impl RawConfig { +impl RawConfig<'_> { #[cfg(feature = "config")] - fn from_toml_str(s: &str) -> std::result::Result { + fn from_toml_str(s: &str) -> std::result::Result, CompileError> { basic_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 { + fn from_toml_str(_: &str) -> std::result::Result, CompileError> { Err("TOML support not available".into()) } } @@ -223,28 +224,29 @@ impl Default for WhitespaceHandling { } #[cfg_attr(feature = "serde", derive(Deserialize))] -struct General { - dirs: Option>, - default_syntax: Option, +struct General<'a> { + #[cfg_attr(feature = "serde", serde(borrow))] + dirs: Option>, + default_syntax: Option<&'a str>, #[cfg_attr(feature = "serde", serde(default))] whitespace: WhitespaceHandling, } #[cfg_attr(feature = "serde", derive(Deserialize))] -struct RawSyntax { - name: String, - block_start: Option, - block_end: Option, - expr_start: Option, - expr_end: Option, - comment_start: Option, - comment_end: Option, +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>, } #[cfg_attr(feature = "serde", derive(Deserialize))] -struct RawEscaper { - path: String, - extensions: Vec, +struct RawEscaper<'a> { + path: &'a str, + extensions: Vec<&'a str>, } pub(crate) fn read_config_file( diff --git a/askama_derive/src/heritage.rs b/askama_derive/src/heritage.rs index ec1997e4e..dbb2b1fa0 100644 --- a/askama_derive/src/heritage.rs +++ b/askama_derive/src/heritage.rs @@ -44,7 +44,7 @@ pub(crate) struct Context<'a> { impl Context<'_> { pub(crate) fn new<'n>( - config: &Config, + config: &Config<'_>, path: &Path, nodes: &'n [Node<'n>], ) -> Result, CompileError> { diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index b8a0da487..47d51bd89 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -9,8 +9,8 @@ use mime::Mime; pub(crate) struct TemplateInput<'a> { pub(crate) ast: &'a syn::DeriveInput, - pub(crate) config: &'a Config, - pub(crate) syntax: &'a Syntax, + pub(crate) config: &'a Config<'a>, + pub(crate) syntax: &'a Syntax<'a>, pub(crate) source: Source, pub(crate) print: Print, pub(crate) escaper: &'a str, @@ -25,7 +25,7 @@ impl TemplateInput<'_> { /// `template()` attribute list fields. pub(crate) fn new<'n>( ast: &'n syn::DeriveInput, - config: &'n Config, + config: &'n Config<'_>, args: TemplateArgs, ) -> Result, CompileError> { let TemplateArgs { @@ -51,7 +51,7 @@ impl TemplateInput<'_> { // Validate syntax let syntax = syntax.map_or_else( - || Ok(config.syntaxes.get(&config.default_syntax).unwrap()), + || Ok(config.syntaxes.get(config.default_syntax).unwrap()), |s| { config .syntaxes diff --git a/askama_derive/src/parser/mod.rs b/askama_derive/src/parser/mod.rs index d345a81b2..79b178ef8 100644 --- a/askama_derive/src/parser/mod.rs +++ b/askama_derive/src/parser/mod.rs @@ -22,12 +22,12 @@ mod node; mod tests; struct State<'a> { - syntax: &'a Syntax, + syntax: &'a Syntax<'a>, loop_depth: Cell, } -impl State<'_> { - fn new(syntax: &Syntax) -> State<'_> { +impl<'a> State<'a> { + fn new(syntax: &'a Syntax<'a>) -> State<'a> { State { syntax, loop_depth: Cell::new(0), @@ -58,7 +58,10 @@ impl From for Whitespace { } } -pub(crate) fn parse<'a>(src: &'a str, syntax: &'a Syntax) -> Result>, CompileError> { +pub(crate) fn parse<'a>( + src: &'a str, + syntax: &'a Syntax<'_>, +) -> Result>, CompileError> { match Node::parse(src, &State::new(syntax)) { Ok((left, res)) => { if !left.is_empty() { @@ -271,9 +274,9 @@ fn path(i: &str) -> IResult<&str, Vec<&str>> { fn take_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> { let p_start = alt(( - tag(s.syntax.block_start.as_str()), - tag(s.syntax.comment_start.as_str()), - tag(s.syntax.expr_start.as_str()), + tag(s.syntax.block_start), + tag(s.syntax.comment_start), + tag(s.syntax.expr_start), )); let (i, _) = not(eof)(i)?; @@ -290,25 +293,25 @@ fn take_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> { } fn tag_block_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.block_start.as_str())(i) + tag(s.syntax.block_start)(i) } fn tag_block_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.block_end.as_str())(i) + tag(s.syntax.block_end)(i) } fn tag_comment_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.comment_start.as_str())(i) + tag(s.syntax.comment_start)(i) } fn tag_comment_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.comment_end.as_str())(i) + tag(s.syntax.comment_end)(i) } fn tag_expr_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.expr_start.as_str())(i) + tag(s.syntax.expr_start)(i) } fn tag_expr_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.expr_end.as_str())(i) + tag(s.syntax.expr_end)(i) } diff --git a/askama_derive/src/parser/node.rs b/askama_derive/src/parser/node.rs index 3bd30d82a..9f8b37ba0 100644 --- a/askama_derive/src/parser/node.rs +++ b/askama_derive/src/parser/node.rs @@ -520,8 +520,8 @@ fn block_node<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> { fn block_comment_body<'a>(mut i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { let mut level = 0; loop { - let (end, tail) = take_until(s.syntax.comment_end.as_str())(i)?; - match take_until::<_, _, Error<_>>(s.syntax.comment_start.as_str())(i) { + let (end, tail) = take_until(s.syntax.comment_end)(i)?; + match take_until::<_, _, Error<_>>(s.syntax.comment_start)(i) { Ok((start, _)) if start.as_ptr() < end.as_ptr() => { level += 1; i = &start[2..]; diff --git a/askama_derive/src/parser/tests.rs b/askama_derive/src/parser/tests.rs index 045bf28ae..91bb09ba8 100644 --- a/askama_derive/src/parser/tests.rs +++ b/askama_derive/src/parser/tests.rs @@ -224,8 +224,8 @@ fn test_parse_root_path() { #[test] fn change_delimiters_parse_filter() { let syntax = Syntax { - expr_start: "{=".to_owned(), - expr_end: "=}".to_owned(), + expr_start: "{=", + expr_end: "=}", ..Syntax::default() };