From b0ea82d87aa61576d1565f20276d411295b9a19d Mon Sep 17 00:00:00 2001 From: defyrlt Date: Tue, 29 Aug 2017 22:57:44 +0300 Subject: Refactor `visit_filter` to allow easier customization --- askama_shared/src/generator.rs | 32 +++++++++++++++++++++----------- 1 file 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) { -- cgit