From 85ad2e6ba3a205e9b648431318aac0e75c027a82 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Fri, 28 Jan 2022 10:48:46 +0100 Subject: Implement error propagation expression: `?` (#590) This change allows using the operator `?` in askama expressions. It works like the same operator in Rust: if a `Result` is `Ok`, it is unwrapped. If it is an error, then the `render()` method fails with this error value. --- askama_shared/src/generator.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'askama_shared/src/generator.rs') diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index fabe277..de2fdcb 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -1060,9 +1060,21 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { Expr::Group(ref inner) => self.visit_group(buf, inner)?, Expr::Call(ref obj, ref args) => self.visit_call(buf, obj, args)?, Expr::RustMacro(name, args) => self.visit_rust_macro(buf, name, args), + Expr::Try(ref expr) => self.visit_try(buf, expr.as_ref())?, }) } + fn visit_try( + &mut self, + buf: &mut Buffer, + expr: &Expr<'_>, + ) -> Result { + buf.write("::core::result::Result::map_err("); + self.visit_expr(buf, expr)?; + buf.write(", |err| ::askama::shared::Error::Custom(::core::convert::Into::into(err)))?"); + Ok(DisplayWrap::Unwrapped) + } + fn visit_rust_macro(&mut self, buf: &mut Buffer, name: &str, args: &str) -> DisplayWrap { buf.write(name); buf.write("!("); -- cgit