diff --git a/README b/README.md similarity index 61% rename from README rename to README.md index 5c850a4..39397da 100644 --- a/README +++ b/README.md @@ -19,13 +19,8 @@ To build and use this: 4. Use the API: ```js - // Method 1: Get structured result - const result = evaluate_cel("name + ' is ' + string(age)", {name: "Alice", age: 30}); - console.log(result.success, result.result, result.error); - - // Method 2: Get string result or error try { - const result = evaluate_cel_string("name + ' is ' + string(age)", {name: "Alice", age: 30}); + const result = evaluate_cel("name + ' is ' + string(age)", {name: "Alice", age: 30}); console.log(result); } catch (error) { console.error(error); diff --git a/index.html b/index.html index 3485bef..49da250 100644 --- a/index.html +++ b/index.html @@ -113,8 +113,8 @@

CEL-Rust WASM-bindgen Demo

- This demo uses wasm-bindgen to provide a clean JavaScript API - for CEL evaluation. + This demo uses wasm-bindgen to provide a JavaScript API for + CEL evaluation using cel-rust.

@@ -136,14 +136,9 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
-
- - -
-
-
Click "Evaluate" to see results...
+
WASM module not loaded yet...
@@ -162,9 +157,9 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive

Math Operations

Expression: (price * quantity) * (1 + tax)Expression: (price * double(quantity)) * (1.0 + tax) Context: {"price": 10.50, "quantity": 3, "tax": 0.08}
@@ -172,9 +167,10 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive

List Operations

Expression: scores.filter(s, s > 90).size() + ' high scores'Expression: string(scores.filter(s, s > 90).size()) + ' high + scores' Context: {"scores": [85, 92, 78, 96, 88]}
@@ -206,10 +202,7 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
diff --git a/src/lib.rs b/src/lib.rs index cd92cf4..1dc3d55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,15 +93,14 @@ fn cel_value_to_serializable(cel_val: &Value) -> SerializableValue { Value::Float(f) => SerializableValue::Float(*f), Value::String(s) => SerializableValue::String(s.to_string()), Value::List(_list) => { - // For now, convert to string representation - // We can improve this later when we figure out the exact CEL API - SerializableValue::Other(format!("{:?}", cel_val)) - } - Value::Map(_map) => { - // For now, convert to string representation - // We can improve this later when we figure out the exact CEL API - SerializableValue::Other(format!("{:?}", cel_val)) + SerializableValue::List(_list.iter().map(cel_value_to_serializable).collect()) } + Value::Map(_map) => SerializableValue::Map( + _map.map + .iter() + .map(|(k, v)| (k.to_string(), cel_value_to_serializable(v))) + .collect(), + ), _ => SerializableValue::Other(format!("{:?}", cel_val)), } } @@ -122,15 +121,6 @@ pub fn evaluate_cel(expression: &str, context: &JsValue) -> Result Result { - match evaluate_cel_internal(expression, context) { - Ok(cel_value) => Ok(format!("{:?}", cel_value)), - Err(e) => Err(JsValue::from_str(&e)), - } -} - fn evaluate_cel_internal(expression: &str, context: &JsValue) -> Result { // Compile the CEL expression let program = Program::compile(expression).map_err(|e| format!("Compilation error: {}", e))?;