aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--askama_shared/src/generator.rs25
-rw-r--r--testing/templates/match-literal-num.html6
-rw-r--r--testing/tests/matches.rs27
3 files changed, 57 insertions, 1 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index 0c8b1e2..701de02 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -161,6 +161,7 @@ impl<'a> Generator<'a> {
self.write_header(state, "::askama::Template", &[]);
self.writeln("fn render_into(&self, writer: &mut ::std::fmt::Write) -> \
::askama::Result<()> {");
+ self.writeln("#[allow(unused_imports)] use ::std::ops::Deref as HiddenDerefTrait;");
self.handle(state, state.nodes, AstLevel::Top);
self.flush_ws(&WS(false, false));
self.writeln("Ok(())");
@@ -199,6 +200,7 @@ impl<'a> Generator<'a> {
"fn render_trait_into(&self, timpl: &{}, writer: &mut ::std::fmt::Write) \
-> ::askama::Result<()> {{",
state.trait_name));
+ self.writeln("#[allow(unused_imports)] use ::std::ops::Deref as HiddenDerefTrait;");
if let Some(nodes) = nodes {
self.handle(state, nodes, AstLevel::Top);
@@ -439,14 +441,18 @@ impl<'a> Generator<'a> {
}
self.write("match ");
+ self.write("(&");
self.visit_expr(expr);
+ self.write(").deref()");
self.writeln(" {");
for arm in arms {
let &(ref ws, ref variant, ref params, ref body) = arm;
self.locals.push();
match *variant {
- Some(ref param) => { self.visit_match_param(param); },
+ Some(ref param) => {
+ self.visit_match_variant(param);
+ },
None => self.write("_"),
};
if params.len() > 0 {
@@ -629,11 +635,28 @@ impl<'a> Generator<'a> {
}
}
+ fn visit_match_variant(&mut self, param: &MatchParameter) -> DisplayWrap {
+ match *param {
+ MatchParameter::StrLit(s) => self.visit_str_lit(s),
+ MatchParameter::NumLit(s) => {
+ // Variants need to be references until match-modes land
+ self.write("&");
+ self.visit_num_lit(s)
+ },
+ MatchParameter::Name(s) => {
+ self.write("&");
+ self.write(s);
+ DisplayWrap::Unwrapped
+ }
+ }
+ }
+
fn visit_match_param(&mut self, param: &MatchParameter) -> DisplayWrap {
match *param {
MatchParameter::NumLit(s) => self.visit_num_lit(s),
MatchParameter::StrLit(s) => self.visit_str_lit(s),
MatchParameter::Name(s) => {
+ self.write("ref ");
self.write(s);
DisplayWrap::Unwrapped
}
diff --git a/testing/templates/match-literal-num.html b/testing/templates/match-literal-num.html
new file mode 100644
index 0000000..4864116
--- /dev/null
+++ b/testing/templates/match-literal-num.html
@@ -0,0 +1,6 @@
+{% match item %}
+{% when 42 %}
+Found answer to everything
+{% else %}
+Else found {{item}}
+{% endmatch %}
diff --git a/testing/tests/matches.rs b/testing/tests/matches.rs
index 4f159ab..b8a6c98 100644
--- a/testing/tests/matches.rs
+++ b/testing/tests/matches.rs
@@ -9,6 +9,13 @@ struct MatchOptTemplate<'a> {
item: Option<&'a str>,
}
+#[derive(Template)]
+#[template(path = "match-opt.html")]
+struct MatchOptRefTemplate<'a> {
+ item: &'a Option<&'a str>,
+}
+
+
#[test]
fn test_match_option() {
let s = MatchOptTemplate { item: Some("foo") };
@@ -21,6 +28,11 @@ fn test_match_option() {
assert_eq!(s.render().unwrap(), "\n\nNot Found\n");
}
+#[test]
+fn test_match_ref_deref() {
+ let s = MatchOptRefTemplate { item: &Some("foo") };
+ assert_eq!(s.render().unwrap(), "\n\nFound literal foo\n");
+}
#[derive(Template)]
#[template(path = "match-literal.html")]
@@ -36,3 +48,18 @@ fn test_match_literal() {
let s = MatchLitTemplate { item: "qux" };
assert_eq!(s.render().unwrap(), "\n\nElse found qux\n");
}
+
+#[derive(Template)]
+#[template(path = "match-literal-num.html")]
+struct MatchLitNumTemplate {
+ item: u32,
+}
+
+#[test]
+fn test_match_literal_num() {
+ let s = MatchLitNumTemplate { item: 42 };
+ assert_eq!(s.render().unwrap(), "\n\nFound answer to everything\n");
+
+ let s = MatchLitNumTemplate { item: 23 };
+ assert_eq!(s.render().unwrap(), "\n\nElse found 23\n");
+}