diff options
author | defyrlt <defyrlt@users.noreply.github.com> | 2017-08-29 22:57:44 +0300 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-02 13:34:52 +0200 |
commit | b0ea82d87aa61576d1565f20276d411295b9a19d (patch) | |
tree | cc02a4e9fa3f527b989233d67290bebfe2ededf3 /askama_shared/src | |
parent | 2fe5cfa99862e25ece098c7379d71532c408d388 (diff) | |
download | askama-b0ea82d87aa61576d1565f20276d411295b9a19d.tar.gz askama-b0ea82d87aa61576d1565f20276d411295b9a19d.tar.bz2 askama-b0ea82d87aa61576d1565f20276d411295b9a19d.zip |
Refactor `visit_filter` to allow easier customization
Diffstat (limited to 'askama_shared/src')
-rw-r--r-- | askama_shared/src/generator.rs | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 82f3994..e5ffd08 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -223,25 +223,35 @@ impl<'a> Generator<'a> { self.write(&format!(".{}", attr)); } - fn visit_filter(&mut self, name: &str, args: &[Expr]) { - if name == "format" { - self.write("format!("); - } else if BUILT_IN_FILTERS.contains(&name) { - self.write(&format!("::askama::filters::{}(&", name)); - } else { - self.write(&format!("filters::{}(&", name)); - } - + fn _visit_filter_args(&mut self, args: &[Expr]) { for (i, arg) in args.iter().enumerate() { if i > 0 { self.write(", &"); } self.visit_expr(arg); } + } + + fn _visit_format_filter(&mut self, args: &[Expr]) { + self.write("format!("); + self._visit_filter_args(args); self.write(")"); - if name != "format" { - self.write("?"); + } + + fn visit_filter(&mut self, name: &str, args: &[Expr]) { + if name == "format" { + self._visit_format_filter(args); + return; + } + + if BUILT_IN_FILTERS.contains(&name) { + self.write(&format!("::askama::filters::{}(&", name)); + } else { + self.write(&format!("filters::{}(&", name)); } + + self._visit_filter_args(args); + self.write(")?"); } fn visit_binop(&mut self, op: &str, left: &Expr, right: &Expr) { |