use linkrs_core::Value;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct RowExpressionContext {
row: Vec<Value>,
col_name_index: HashMap<String, usize>,
variables: HashMap<String, Value>,
}
impl RowExpressionContext {
pub fn new(row: Vec<Value>, col_names: Vec<String>) -> Self {
let col_name_index: HashMap<String, usize> = col_names
.into_iter()
.enumerate()
.map(|(i, name)| (name, i))
.collect();
Self {
row,
col_name_index,
variables: HashMap::new(),
}
}
pub fn from_dataset(row: &[Value], col_names: &[String]) -> Self {
Self::new(row.to_vec(), col_names.to_vec())
}
pub fn get_value_by_name(&self, name: &str) -> Option<&Value> {
let result = self
.col_name_index
.get(name)
.and_then(|&idx| self.row.get(idx));
result
}
}
impl crate::executor::expression::evaluator::traits::ExpressionContext for RowExpressionContext {
fn get_variable(&self, name: &str) -> Option<Value> {
if let Some(value) = self.variables.get(name) {
return Some(value.clone());
}
if let Some(value) = self.get_value_by_name(name) {
return Some(value.clone());
}
None
}
fn set_variable(&mut self, name: String, value: Value) {
self.variables.insert(name, value);
}
fn get_function(
&self,
name: &str,
) -> Option<crate::executor::expression::functions::OwnedFunctionRef> {
let _ = name;
None
}
}