2023-10-26 22:03:59 +00:00
|
|
|
use rayon::prelude::*;
|
2023-10-17 20:40:07 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-10-22 19:24:10 +00:00
|
|
|
use tree_sitter::Node;
|
2023-10-17 20:40:07 +00:00
|
|
|
|
2023-10-26 22:03:59 +00:00
|
|
|
use crate::{AbstractTree, Expression, Identifier, Item, List, Map, Result, Value};
|
2023-10-17 20:40:07 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct Transform {
|
|
|
|
identifier: Identifier,
|
|
|
|
expression: Expression,
|
|
|
|
item: Item,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for Transform {
|
2023-10-22 19:24:10 +00:00
|
|
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
2023-10-17 20:40:07 +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)?;
|
|
|
|
|
|
|
|
let item_node = node.child(5).unwrap();
|
|
|
|
let item = Item::from_syntax_node(source, item_node)?;
|
|
|
|
|
|
|
|
Ok(Transform {
|
|
|
|
identifier,
|
|
|
|
expression,
|
|
|
|
item,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
2023-10-26 22:03:59 +00:00
|
|
|
let expression_run = self.expression.run(source, context)?;
|
|
|
|
let values = expression_run.as_list()?.items();
|
2023-10-17 20:40:07 +00:00
|
|
|
let key = self.identifier.inner();
|
2023-10-27 02:35:59 +00:00
|
|
|
let new_list = values
|
|
|
|
.par_iter()
|
|
|
|
.map(|value| {
|
|
|
|
let mut iter_context = Map::clone_from(context);
|
2023-10-26 22:03:59 +00:00
|
|
|
|
2023-10-27 02:35:59 +00:00
|
|
|
iter_context.set_value(key.clone(), value.clone()).unwrap();
|
2023-10-26 22:03:59 +00:00
|
|
|
|
2023-10-27 02:35:59 +00:00
|
|
|
let item_run = self.item.run(source, &mut iter_context);
|
2023-10-26 22:03:59 +00:00
|
|
|
|
|
|
|
match item_run {
|
2023-10-27 02:35:59 +00:00
|
|
|
Ok(value) => value,
|
|
|
|
Err(_) => Value::Empty,
|
2023-10-26 22:03:59 +00:00
|
|
|
}
|
2023-10-27 02:35:59 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2023-10-17 20:40:07 +00:00
|
|
|
|
2023-10-27 02:35:59 +00:00
|
|
|
Ok(Value::List(List::with_items(new_list)))
|
2023-10-17 20:40:07 +00:00
|
|
|
}
|
|
|
|
}
|