aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src/generator.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <Kijewski@users.noreply.github.com>2022-01-28 10:48:46 +0100
committerLibravatar GitHub <noreply@github.com>2022-01-28 10:48:46 +0100
commit85ad2e6ba3a205e9b648431318aac0e75c027a82 (patch)
tree5e186e2b9d4efcc97666a9d58410dfda238f5735 /askama_shared/src/generator.rs
parentcb351fe6b1dda644a4ec023dc850cdfe83732503 (diff)
downloadaskama-85ad2e6ba3a205e9b648431318aac0e75c027a82.tar.gz
askama-85ad2e6ba3a205e9b648431318aac0e75c027a82.tar.bz2
askama-85ad2e6ba3a205e9b648431318aac0e75c027a82.zip
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.
Diffstat (limited to 'askama_shared/src/generator.rs')
-rw-r--r--askama_shared/src/generator.rs12
1 files changed, 12 insertions, 0 deletions
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<DisplayWrap, CompileError> {
+ 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("!(");