diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-04-23 13:36:26 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-04-23 13:40:39 +0200 |
commit | 4633dd989c58e7707f681c0ffd651c8b5336d5a9 (patch) | |
tree | f96eaa4fb09932c9e8e6be142802270a6b3b1f6c /askama_shared | |
parent | 641940ef3805589e92b4520d4944ed66afc875fb (diff) | |
download | askama-4633dd989c58e7707f681c0ffd651c8b5336d5a9.tar.gz askama-4633dd989c58e7707f681c0ffd651c8b5336d5a9.tar.bz2 askama-4633dd989c58e7707f681c0ffd651c8b5336d5a9.zip |
Fix taking references of function arguments (fixes #311)
Diffstat (limited to 'askama_shared')
-rw-r--r-- | askama_shared/src/generator.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index d904524..fe02b8a 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -984,13 +984,13 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { if name == "escape" || name == "safe" || name == "e" || name == "json" { buf.write(&format!( - "::askama::filters::{}({}, &", + "::askama::filters::{}({}, ", name, self.input.escaper )); } else if filters::BUILT_IN_FILTERS.contains(&name) { - buf.write(&format!("::askama::filters::{}(&", name)); + buf.write(&format!("::askama::filters::{}(", name)); } else { - buf.write(&format!("filters::{}(&", name)); + buf.write(&format!("filters::{}(", name)); } self._visit_args(buf, args); @@ -1004,7 +1004,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { fn _visit_format_filter(&mut self, buf: &mut Buffer, args: &[Expr]) { buf.write("format!("); - self._visit_args(buf, args); + if let Some(Expr::StrLit(v)) = args.first() { + self.visit_str_lit(buf, v); + if args.len() > 1 { + buf.write(", "); + } + } else { + panic!("invalid expression type for format filter"); + } + self._visit_args(buf, &args[1..]); buf.write(")"); } @@ -1024,9 +1032,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } fn _visit_args(&mut self, buf: &mut Buffer, args: &[Expr]) { + if args.is_empty() { + return; + } + for (i, arg) in args.iter().enumerate() { if i > 0 { buf.write(", &"); + } else { + buf.write("&"); } let scoped = match *arg { @@ -1172,7 +1186,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } buf.write(part); } - buf.write("(&"); + buf.write("("); self._visit_args(buf, args); buf.write(")"); DisplayWrap::Unwrapped @@ -1196,7 +1210,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.write("self."); buf.write(s); } - buf.write(")(&"); + buf.write(")("); self._visit_args(buf, args); buf.write(")"); DisplayWrap::Unwrapped |