1
0
dust/src/abstract_tree/insert.rs

45 lines
1.4 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-10-25 20:44:50 +00:00
use crate::{AbstractTree, Expression, Identifier, Map, Result, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Insert {
identifier: Identifier,
expression: Expression,
}
impl AbstractTree for Insert {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
2023-10-22 18:10:16 +00:00
let identifier_node = node.child(2).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)?;
Ok(Insert {
identifier,
expression,
})
}
2023-10-25 20:44:50 +00:00
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let table_name = self.identifier.inner().clone();
let mut table = self.identifier.run(source, context)?.as_table()?.clone();
let new_rows = self.expression.run(source, context)?.into_inner_list()?;
2023-10-26 22:03:59 +00:00
let values = new_rows.items();
2023-10-26 22:03:59 +00:00
table.reserve(values.len());
2023-10-26 22:03:59 +00:00
for row in values.iter() {
let row_values = row.clone().into_inner_list()?;
table.insert(row_values.items().clone())?;
}
2023-10-29 23:31:06 +00:00
context
2023-11-05 18:54:29 +00:00
.variables_mut()?
2023-10-29 23:31:06 +00:00
.insert(table_name, Value::Table(table));
Ok(Value::Empty)
}
}