Skip to content

Commit e00a6d6

Browse files
authored
Merge pull request #151 from epage/for
fix(for): Re-enable support for object.access
2 parents 36ba3b2 + cc9998b commit e00a6d6

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/tags/for_block.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use value::Value;
1515

1616
#[derive(Clone, Debug)]
1717
enum Range {
18-
Array(String),
18+
Array(Argument),
1919
Counted(Argument, Argument),
2020
}
2121

@@ -30,10 +30,15 @@ struct For {
3030
reversed: bool,
3131
}
3232

33-
fn get_array(context: &Context, array_id: &str) -> Result<Vec<Value>> {
34-
match context.get_val(array_id) {
35-
Some(&Value::Array(ref x)) => Ok(x.clone()),
36-
x => Err(Error::Render(format!("Tried to iterate over {:?}, which is not supported.", x))),
33+
fn get_array(context: &Context, array_id: &Argument) -> Result<Vec<Value>> {
34+
let array = array_id.evaluate(context)?;
35+
match array {
36+
Value::Array(x) => Ok(x),
37+
x => {
38+
Err(Error::Render(format!("Tried to iterate over {:?} ({}), which is not supported.",
39+
x,
40+
array_id)))
41+
}
3742
}
3843
}
3944

@@ -49,7 +54,7 @@ fn token_as_int(arg: &Argument, context: &Context) -> Result<isize> {
4954
impl Renderable for For {
5055
fn render(&self, context: &mut Context) -> Result<Option<String>> {
5156
let mut range = match self.range {
52-
Range::Array(ref array_id) => try!(get_array(context, array_id)),
57+
Range::Array(ref array_id) => get_array(context, array_id)?,
5358

5459
Range::Counted(ref start_arg, ref stop_arg) => {
5560
let start = token_as_int(start_arg, context)?;
@@ -150,21 +155,28 @@ pub fn for_block(_tag_name: &str,
150155

151156
expect(&mut args, &Token::Identifier("in".to_owned()))?;
152157

153-
let range = match args.next() {
154-
Some(&Token::Identifier(ref x)) => Range::Array(x.clone()),
155-
Some(&Token::OpenRound) => {
156-
// this might be a range, let's try and see
157-
let start = range_end_point(&mut args)?.to_arg()?;
158+
let range = if let Some(token) = args.next() {
159+
match token {
160+
&Token::Identifier(_) => {
161+
let arg = token.to_arg()?;
162+
Range::Array(arg)
163+
}
164+
&Token::OpenRound => {
165+
// this might be a range, let's try and see
166+
let start = range_end_point(&mut args)?.to_arg()?;
158167

159-
expect(&mut args, &Token::DotDot)?;
168+
expect(&mut args, &Token::DotDot)?;
160169

161-
let stop = range_end_point(&mut args)?.to_arg()?;
170+
let stop = range_end_point(&mut args)?.to_arg()?;
162171

163-
expect(&mut args, &Token::CloseRound)?;
172+
expect(&mut args, &Token::CloseRound)?;
164173

165-
Range::Counted(start, stop)
174+
Range::Counted(start, stop)
175+
}
176+
x => return Error::parser("Identifier or (", Some(x)),
166177
}
167-
x => return Error::parser("Identifier or (", x),
178+
} else {
179+
return Error::parser("Identifier or (", None);
168180
};
169181

170182
// now we get to check for parameters...

0 commit comments

Comments
 (0)