mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 07:20:55 +00:00
Make match ref/deref as needed
Much of this can be yanked out and made simpler when match-modes lands in stable
This commit is contained in:
parent
cc51d201ab
commit
89a90eb46c
@ -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
|
||||
}
|
||||
|
6
testing/templates/match-literal-num.html
Normal file
6
testing/templates/match-literal-num.html
Normal file
@ -0,0 +1,6 @@
|
||||
{% match item %}
|
||||
{% when 42 %}
|
||||
Found answer to everything
|
||||
{% else %}
|
||||
Else found {{item}}
|
||||
{% endmatch %}
|
@ -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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user