use crate::ast::{BinaryOp, UnaryOp, Expr};
use crate::error::Result;
use crate::value::JsonnetValue;
use crate::eval::Context;
pub trait OpHandler {
fn eval_binary_op(&mut self, context: &mut Context, left: JsonnetValue, op: BinaryOp, right: JsonnetValue) -> Result<JsonnetValue>;
fn eval_unary_op(&mut self, context: &mut Context, op: UnaryOp, operand: JsonnetValue) -> Result<JsonnetValue>;
}
pub trait FuncallHandler {
fn call_function(&mut self, context: &mut Context, func: JsonnetValue, args: Vec<JsonnetValue>) -> Result<JsonnetValue>;
fn is_builtin_function(&self, name: &str) -> bool;
fn call_builtin_function(&mut self, context: &mut Context, name: &str, args: Vec<JsonnetValue>) -> Result<JsonnetValue>;
fn is_external_function(&self, name: &str) -> bool;
fn call_external_function(&mut self, context: &mut Context, name: &str, args: Vec<JsonnetValue>) -> Result<JsonnetValue>;
}
pub trait ComprehensionHandler {
fn eval_list_comprehension(&mut self, context: &mut Context, expr: &Expr, var: &str, collection: &Expr, condition: Option<&Expr>) -> Result<JsonnetValue>;
fn eval_dict_comprehension(&mut self, context: &mut Context, key_expr: &Expr, value_expr: &Expr, var: &str, collection: &Expr, condition: Option<&Expr>) -> Result<JsonnetValue>;
}
pub struct DefaultOpHandler;
impl OpHandler for DefaultOpHandler {
fn eval_binary_op(&mut self, _context: &mut Context, left: JsonnetValue, op: BinaryOp, right: JsonnetValue) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error(format!("Binary operation {:?} not implemented", op)))
}
fn eval_unary_op(&mut self, _context: &mut Context, op: UnaryOp, _operand: JsonnetValue) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error(format!("Unary operation {:?} not implemented", op)))
}
}
pub struct DefaultFuncallHandler;
impl FuncallHandler for DefaultFuncallHandler {
fn call_function(&mut self, _context: &mut Context, _func: JsonnetValue, _args: Vec<JsonnetValue>) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error("Function call not implemented"))
}
fn is_builtin_function(&self, name: &str) -> bool {
name.starts_with("std.")
}
fn call_builtin_function(&mut self, _context: &mut Context, name: &str, args: Vec<JsonnetValue>) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error(format!("Builtin function {} not implemented", name)))
}
fn is_external_function(&self, name: &str) -> bool {
name.starts_with("ai.") ||
name.starts_with("tool.") ||
name.starts_with("memory.") ||
name.starts_with("agent.") ||
name.starts_with("chain.")
}
fn call_external_function(&mut self, _context: &mut Context, name: &str, _args: Vec<JsonnetValue>) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error(format!("External function {} not implemented", name)))
}
}
pub struct DefaultComprehensionHandler;
impl ComprehensionHandler for DefaultComprehensionHandler {
fn eval_list_comprehension(&mut self, _context: &mut Context, _expr: &Expr, _var: &str, _collection: &Expr, _condition: Option<&Expr>) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error("List comprehension not implemented"))
}
fn eval_dict_comprehension(&mut self, _context: &mut Context, _key_expr: &Expr, _value_expr: &Expr, _var: &str, _collection: &Expr, _condition: Option<&Expr>) -> Result<JsonnetValue> {
Err(crate::error::JsonnetError::runtime_error("Dict comprehension not implemented"))
}
}