diff options
Diffstat (limited to 'askama_derive')
-rw-r--r-- | askama_derive/src/config.rs | 96 | ||||
-rw-r--r-- | askama_derive/src/heritage.rs | 2 | ||||
-rw-r--r-- | askama_derive/src/input.rs | 8 | ||||
-rw-r--r-- | askama_derive/src/parser/mod.rs | 29 | ||||
-rw-r--r-- | askama_derive/src/parser/node.rs | 4 | ||||
-rw-r--r-- | 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 eec20ed..98e51ce 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<PathBuf>, - pub(crate) syntaxes: BTreeMap<String, Syntax>, - pub(crate) default_syntax: String, + pub(crate) syntaxes: BTreeMap<String, Syntax<'a>>, + pub(crate) default_syntax: &'a str, pub(crate) escapers: Vec<(HashSet<String>, String)>, pub(crate) whitespace: WhitespaceHandling, } -impl Config { - pub(crate) fn new(s: &str) -> std::result::Result<Config, CompileError> { +impl Config<'_> { + pub(crate) fn new(s: &str) -> std::result::Result<Config<'_>, 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<RawSyntax> for Syntax { +impl<'a> TryFrom<RawSyntax<'a>> for Syntax<'a> { type Error = CompileError; - fn try_from(raw: RawSyntax) -> std::result::Result<Self, Self::Error> { - let default = Self::default(); + fn try_from(raw: RawSyntax<'a>) -> std::result::Result<Self, Self::Error> { + 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<RawSyntax> for Syntax { #[cfg_attr(feature = "serde", derive(Deserialize))] #[derive(Default)] -struct RawConfig { - general: Option<General>, - syntax: Option<Vec<RawSyntax>>, - escaper: Option<Vec<RawEscaper>>, +struct RawConfig<'a> { + #[cfg_attr(feature = "serde", serde(borrow))] + general: Option<General<'a>>, + syntax: Option<Vec<RawSyntax<'a>>>, + escaper: Option<Vec<RawEscaper<'a>>>, } -impl RawConfig { +impl RawConfig<'_> { #[cfg(feature = "config")] - fn from_toml_str(s: &str) -> std::result::Result<RawConfig, CompileError> { + fn from_toml_str(s: &str) -> std::result::Result<RawConfig<'_>, 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<RawConfig, CompileError> { + fn from_toml_str(_: &str) -> std::result::Result<RawConfig<'_>, 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<Vec<String>>, - default_syntax: Option<String>, +struct General<'a> { + #[cfg_attr(feature = "serde", serde(borrow))] + dirs: Option<Vec<&'a str>>, + 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<String>, - block_end: Option<String>, - expr_start: Option<String>, - expr_end: Option<String>, - comment_start: Option<String>, - comment_end: Option<String>, +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<String>, +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 ec1997e..dbb2b1f 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<Context<'n>, CompileError> { diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index b8a0da4..47d51bd 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<TemplateInput<'n>, 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 d345a81..79b178e 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<usize>, } -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<char> for Whitespace { } } -pub(crate) fn parse<'a>(src: &'a str, syntax: &'a Syntax) -> Result<Vec<Node<'a>>, CompileError> { +pub(crate) fn parse<'a>( + src: &'a str, + syntax: &'a Syntax<'_>, +) -> Result<Vec<Node<'a>>, 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 3bd30d8..9f8b37b 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 045bf28..91bb09b 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() }; |