aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive
diff options
context:
space:
mode:
authorLibravatar yossyJ <28825627+yossyJ@users.noreply.github.com>2019-01-04 23:12:44 +0900
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2019-01-04 16:58:09 +0100
commitbef88f1696b828caf72695561c2270456f35894d (patch)
treefb83e8f9fe1b05a49c18c43adef96a366dc18f6e /askama_derive
parent39e08325c0e0fba75384579427d1c6f7eca8fbc9 (diff)
downloadaskama-bef88f1696b828caf72695561c2270456f35894d.tar.gz
askama-bef88f1696b828caf72695561c2270456f35894d.tar.bz2
askama-bef88f1696b828caf72695561c2270456f35894d.zip
Add support for tuple
Diffstat (limited to '')
-rw-r--r--askama_derive/src/generator.rs44
-rw-r--r--askama_derive/src/parser.rs24
2 files changed, 55 insertions, 13 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 95a7d17..67ec2c2 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -468,11 +468,7 @@ impl<'a> Generator<'a> {
self.write_buf_writable(buf);
buf.write("for (_loop_index, ");
- let targets = self.visit_target(var);
- for name in &targets {
- self.locals.insert(name);
- buf.write(name);
- }
+ self.visit_target(buf, var);
match iter {
Expr::Range(_, _, _) => buf.writeln(&format!(") in ({}).enumerate() {{", expr_code)),
_ => buf.writeln(&format!(") in (&{}).into_iter().enumerate() {{", expr_code)),
@@ -576,6 +572,15 @@ impl<'a> Generator<'a> {
self.locals.insert(name);
buf.write(name);
}
+ Target::Tuple(ref targets) => {
+ buf.write("(");
+ for name in targets {
+ self.locals.insert(name);
+ buf.write(name);
+ buf.write(",");
+ }
+ buf.write(")");
+ }
}
buf.writeln(";");
}
@@ -593,6 +598,15 @@ impl<'a> Generator<'a> {
}
buf.write(name);
}
+ Target::Tuple(ref targets) => {
+ buf.write("let (");
+ for name in targets {
+ self.locals.insert(name);
+ buf.write(name);
+ buf.write(",");
+ }
+ buf.write(")");
+ }
}
buf.writeln(&format!(" = {};", &expr_buf.buf));
}
@@ -1024,13 +1038,21 @@ impl<'a> Generator<'a> {
DisplayWrap::Unwrapped
}
- fn visit_target_single<'t>(&mut self, name: &'t str) -> Vec<&'t str> {
- vec![name]
- }
-
- fn visit_target<'t>(&mut self, target: &'t Target) -> Vec<&'t str> {
+ fn visit_target(&mut self, buf: &mut Buffer, target: &'a Target) {
match *target {
- Target::Name(s) => self.visit_target_single(s),
+ Target::Name(name) => {
+ self.locals.insert(name);
+ buf.write(name);
+ }
+ Target::Tuple(ref targets) => {
+ buf.write("(");
+ for name in targets {
+ self.locals.insert(name);
+ buf.write(name);
+ buf.write(",");
+ }
+ buf.write(")");
+ }
}
}
diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs
index 81935f6..c5e610e 100644
--- a/askama_derive/src/parser.rs
+++ b/askama_derive/src/parser.rs
@@ -42,6 +42,7 @@ pub enum MatchParameter<'a> {
#[derive(Debug)]
pub enum Target<'a> {
Name(&'a str),
+ Tuple(Vec<&'a str>),
}
#[derive(Clone, Copy, Debug)]
@@ -275,6 +276,25 @@ named!(target_single<Input, Target>, map!(identifier,
|s| Target::Name(s)
));
+named!(target_tuple<Input, Target>, do_parse!(
+ tag!("(") >>
+ args: opt!(do_parse!(
+ arg0: ws!(identifier) >>
+ args: many0!(do_parse!(
+ tag!(",") >>
+ argn: ws!(identifier) >>
+ (argn)
+ )) >>
+ ({
+ let mut res = vec![arg0];
+ res.extend(args);
+ res
+ })
+ )) >>
+ tag!(")") >>
+ (Target::Tuple(args.unwrap_or_default()))
+));
+
named!(variant_name<Input, MatchVariant>, map!(identifier,
|s| MatchVariant::Name(s)
));
@@ -703,7 +723,7 @@ named_args!(block_match<'a>(s: &'a Syntax<'a>) <Input<'a>, Node<'a>>, do_parse!(
named!(block_let<Input, Node>, do_parse!(
pws: opt!(tag!("-")) >>
ws!(tag!("let")) >>
- var: ws!(target_single) >>
+ var: ws!(alt!(target_single | target_tuple)) >>
val: opt!(do_parse!(
ws!(tag!("=")) >>
val: ws!(expr_any) >>
@@ -720,7 +740,7 @@ named!(block_let<Input, Node>, do_parse!(
named_args!(block_for<'a>(s: &'a Syntax<'a>) <Input<'a>, Node<'a>>, do_parse!(
pws1: opt!(tag!("-")) >>
ws!(tag!("for")) >>
- var: ws!(target_single) >>
+ var: ws!(alt!(target_single | target_tuple)) >>
ws!(tag!("in")) >>
iter: ws!(expr_any) >>
nws1: opt!(tag!("-")) >>