dust/src/abstract_tree/for.rs

77 lines
2.4 KiB
Rust
Raw Normal View History

2023-10-28 14:28:43 +00:00
use rayon::prelude::*;
2023-10-17 18:06:02 +00:00
use serde::{Deserialize, Serialize};
2023-10-22 19:24:10 +00:00
use tree_sitter::Node;
2023-10-17 18:06:02 +00:00
2023-10-31 17:04:22 +00:00
use crate::{AbstractTree, Block, Error, Expression, Identifier, Map, Result, Value};
2023-10-17 18:06:02 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct For {
2023-10-28 14:28:43 +00:00
is_async: bool,
2023-11-04 10:02:27 +00:00
item_id: Identifier,
collection: Expression,
block: Block,
2023-10-17 18:06:02 +00:00
}
impl AbstractTree for For {
2023-10-22 19:24:10 +00:00
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
2023-10-28 14:28:43 +00:00
let for_node = node.child(0).unwrap();
let is_async = match for_node.kind() {
"for" => false,
2023-11-04 10:02:27 +00:00
"async for" => true,
2023-10-28 14:28:43 +00:00
_ => {
return Err(Error::UnexpectedSyntaxNode {
2023-11-04 10:02:27 +00:00
expected: "for or async for",
2023-10-28 14:28:43 +00:00
actual: for_node.kind(),
location: for_node.start_position(),
relevant_source: source[for_node.byte_range()].to_string(),
})
}
};
2023-10-17 18:06:02 +00:00
let identifier_node = node.child(1).unwrap();
let identifier = Identifier::from_syntax_node(source, identifier_node)?;
let expression_node = node.child(3).unwrap();
let expression = Expression::from_syntax_node(source, expression_node)?;
2023-11-03 22:04:45 +00:00
let item_node = node.child(4).unwrap();
2023-10-31 17:04:22 +00:00
let item = Block::from_syntax_node(source, item_node)?;
2023-10-17 18:06:02 +00:00
Ok(For {
2023-10-28 14:28:43 +00:00
is_async,
2023-11-04 10:02:27 +00:00
item_id: identifier,
collection: expression,
block: item,
2023-10-17 18:06:02 +00:00
})
}
2023-10-25 20:44:50 +00:00
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
2023-11-04 10:02:27 +00:00
let expression_run = self.collection.run(source, context)?;
2023-10-26 22:03:59 +00:00
let values = expression_run.as_list()?.items();
2023-11-04 10:02:27 +00:00
let key = self.item_id.inner();
2023-11-05 18:54:29 +00:00
let loop_context = Map::clone_from(context)?;
2023-10-23 19:25:22 +00:00
2023-10-28 14:28:43 +00:00
if self.is_async {
values.par_iter().try_for_each(|value| {
2023-11-04 10:02:27 +00:00
let mut iter_context = loop_context.clone();
2023-10-17 18:06:02 +00:00
2023-10-29 23:31:06 +00:00
iter_context
2023-11-05 18:54:29 +00:00
.variables_mut()?
2023-10-29 23:31:06 +00:00
.insert(key.clone(), value.clone());
2023-10-23 19:25:22 +00:00
2023-11-04 10:02:27 +00:00
self.block.run(source, &mut iter_context).map(|_value| ())
2023-10-28 14:28:43 +00:00
})?;
2023-10-23 19:25:22 +00:00
} else {
2023-11-05 18:54:29 +00:00
let mut variables = loop_context.variables_mut()?;
2023-10-28 14:28:43 +00:00
for value in values.iter() {
2023-11-05 18:54:29 +00:00
variables.insert(key.clone(), value.clone());
2023-10-28 14:28:43 +00:00
2023-11-05 18:54:29 +00:00
self.block.run(source, &mut loop_context.clone())?;
2023-10-28 14:28:43 +00:00
}
2023-10-17 18:06:02 +00:00
}
Ok(Value::Empty)
}
}