allow including context
https://claude.ai/chat/a6df04ed-85dd-46b9-a3bd-26b5ae3c78f5
This commit is contained in:
85
src/lib.rs
85
src/lib.rs
@@ -1,8 +1,10 @@
|
||||
use std::collections::HashMap;
|
||||
use std::mem;
|
||||
use std::slice;
|
||||
use std::str;
|
||||
|
||||
use cel::{Context, ExecutionError, Program, Value};
|
||||
use cel::{Context, Program, Value};
|
||||
use serde_json;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn allocation(n: usize) -> *mut u8 {
|
||||
@@ -21,9 +23,9 @@ pub unsafe extern "C" fn evaluate(s: *const u8) -> *mut u8 {
|
||||
|
||||
fn evaluate_buffers(input: &[u8]) -> Vec<u8> {
|
||||
let contents = str::from_utf8(input).unwrap();
|
||||
let result = evaluate_cel(contents);
|
||||
let result = evaluate_cel_with_context(contents);
|
||||
let success = result.is_ok();
|
||||
let message = result.unwrap_or_else(|e| e.to_string());
|
||||
let message = result.unwrap_or_else(|e| e);
|
||||
let len = message.len();
|
||||
let mut buffer = Vec::with_capacity(len + 8);
|
||||
buffer.push(if success { 1 } else { 0 });
|
||||
@@ -33,10 +35,75 @@ fn evaluate_buffers(input: &[u8]) -> Vec<u8> {
|
||||
buffer
|
||||
}
|
||||
|
||||
fn evaluate_cel(content: &str) -> Result<String, ExecutionError> {
|
||||
let program = Program::compile(content).unwrap();
|
||||
let mut context = Context::default();
|
||||
// context.add_function("add", |a: i64, b: i64| a + b);
|
||||
let value = program.execute(&context)?;
|
||||
Ok(format!("{:?}", value))
|
||||
fn json_to_cel_value(json_value: &serde_json::Value) -> Value {
|
||||
match json_value {
|
||||
serde_json::Value::Null => Value::Null,
|
||||
serde_json::Value::Bool(b) => Value::Bool(*b),
|
||||
serde_json::Value::Number(n) => {
|
||||
if let Some(i) = n.as_i64() {
|
||||
Value::Int(i)
|
||||
} else if let Some(f) = n.as_f64() {
|
||||
Value::Float(f)
|
||||
} else {
|
||||
Value::Null
|
||||
}
|
||||
}
|
||||
serde_json::Value::String(s) => Value::String(s.clone().into()),
|
||||
serde_json::Value::Array(arr) => {
|
||||
let cel_vec: Vec<Value> = arr.iter().map(json_to_cel_value).collect();
|
||||
Value::List(cel_vec.into())
|
||||
}
|
||||
serde_json::Value::Object(obj) => {
|
||||
let mut cel_map = HashMap::new();
|
||||
for (key, value) in obj.iter() {
|
||||
cel_map.insert(key.clone(), json_to_cel_value(value));
|
||||
}
|
||||
Value::Map(cel_map.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn evaluate_cel_with_context(content: &str) -> Result<String, String> {
|
||||
// Split the input to get expression and context
|
||||
let parts: Vec<&str> = content.split("\n---CONTEXT---\n").collect();
|
||||
let expression = parts[0].trim();
|
||||
let context_json = if parts.len() > 1 {
|
||||
parts[1].trim()
|
||||
} else {
|
||||
"{}"
|
||||
};
|
||||
|
||||
// Parse the CEL expression
|
||||
let program = match Program::compile(expression) {
|
||||
Ok(prog) => prog,
|
||||
Err(e) => return Err(format!("Compilation error: {}", e)),
|
||||
};
|
||||
|
||||
// Create context and add JSON values
|
||||
let mut context = Context::default();
|
||||
|
||||
// Parse JSON context if provided and not empty
|
||||
if !context_json.is_empty() && context_json != "{}" {
|
||||
match serde_json::from_str::<serde_json::Value>(context_json) {
|
||||
Ok(json_value) => {
|
||||
if let serde_json::Value::Object(obj) = json_value {
|
||||
for (key, value) in obj.iter() {
|
||||
let cel_value = json_to_cel_value(value);
|
||||
context
|
||||
.add_variable(key, cel_value)
|
||||
.map_err(|e| format!("Context error: {}", e))?;
|
||||
}
|
||||
} else {
|
||||
return Err("JSON context must be an object".to_string());
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(format!("JSON parsing error: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
// Execute the program
|
||||
match program.execute(&context) {
|
||||
Ok(value) => Ok(format!("{:?}", value)),
|
||||
Err(e) => Err(format!("Execution error: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user