cleanup
This commit is contained in:
@@ -19,13 +19,8 @@ To build and use this:
|
|||||||
|
|
||||||
4. Use the API:
|
4. Use the API:
|
||||||
```js
|
```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 {
|
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);
|
console.log(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
76
index.html
76
index.html
@@ -113,8 +113,8 @@
|
|||||||
<body>
|
<body>
|
||||||
<h1>CEL-Rust WASM-bindgen Demo</h1>
|
<h1>CEL-Rust WASM-bindgen Demo</h1>
|
||||||
<p>
|
<p>
|
||||||
This demo uses <code>wasm-bindgen</code> to provide a clean JavaScript API
|
This demo uses <code>wasm-bindgen</code> to provide a JavaScript API for
|
||||||
for CEL evaluation.
|
CEL evaluation using <code>cel-rust</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@@ -136,14 +136,9 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="full-width">
|
|
||||||
<button onclick="evaluateAsJs()">Evaluate as JavaScript Value</button>
|
|
||||||
<button onclick="evaluateAsDebug()">Evaluate & Show Debug</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="full-width">
|
<div class="full-width">
|
||||||
<label>Result:</label>
|
<label>Result:</label>
|
||||||
<div id="result" class="result">Click "Evaluate" to see results...</div>
|
<div id="result" class="result">WASM module not loaded yet...</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="examples">
|
<div class="examples">
|
||||||
@@ -162,9 +157,9 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
<div class="example" onclick="loadExample(this)">
|
<div class="example" onclick="loadExample(this)">
|
||||||
<h4>Math Operations</h4>
|
<h4>Math Operations</h4>
|
||||||
<code
|
<code
|
||||||
data-expression="(price * quantity) * (1 + tax)"
|
data-expression="(price * double(quantity)) * (1.0 + tax)"
|
||||||
data-context='{"price": 10.50, "quantity": 3, "tax": 0.08}'
|
data-context='{"price": 10.50, "quantity": 3, "tax": 0.08}'
|
||||||
>Expression: (price * quantity) * (1 + tax)</code
|
>Expression: (price * double(quantity)) * (1.0 + tax)</code
|
||||||
>
|
>
|
||||||
<code>Context: {"price": 10.50, "quantity": 3, "tax": 0.08}</code>
|
<code>Context: {"price": 10.50, "quantity": 3, "tax": 0.08}</code>
|
||||||
</div>
|
</div>
|
||||||
@@ -172,9 +167,10 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
<div class="example" onclick="loadExample(this)">
|
<div class="example" onclick="loadExample(this)">
|
||||||
<h4>List Operations</h4>
|
<h4>List Operations</h4>
|
||||||
<code
|
<code
|
||||||
data-expression="scores.filter(s, s > 90).size() + ' high scores out of ' + string(scores.size())"
|
data-expression="string(scores.filter(s, s > 90).size()) + ' high scores out of ' + string(scores.size())"
|
||||||
data-context='{"scores": [85, 92, 78, 96, 88]}'
|
data-context='{"scores": [85, 92, 78, 96, 88]}'
|
||||||
>Expression: scores.filter(s, s > 90).size() + ' high scores'</code
|
>Expression: string(scores.filter(s, s > 90).size()) + ' high
|
||||||
|
scores'</code
|
||||||
>
|
>
|
||||||
<code>Context: {"scores": [85, 92, 78, 96, 88]}</code>
|
<code>Context: {"scores": [85, 92, 78, 96, 88]}</code>
|
||||||
</div>
|
</div>
|
||||||
@@ -206,10 +202,7 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import init, {
|
import init, { evaluate_cel } from "./pkg/cel_rust_wasm.js";
|
||||||
evaluate_cel,
|
|
||||||
evaluate_cel_debug,
|
|
||||||
} from "./pkg/cel_rust_wasm.js";
|
|
||||||
|
|
||||||
let wasmModule;
|
let wasmModule;
|
||||||
|
|
||||||
@@ -217,25 +210,22 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
wasmModule = await init();
|
wasmModule = await init();
|
||||||
console.log("WASM module loaded successfully");
|
console.log("WASM module loaded successfully");
|
||||||
|
|
||||||
// Make functions globally available
|
// Make function globally available
|
||||||
window.evaluate_cel = evaluate_cel;
|
window.evaluate_cel = evaluate_cel;
|
||||||
window.evaluate_cel_debug = evaluate_cel_debug;
|
|
||||||
|
|
||||||
// Enable the evaluate button
|
setTimeout(evaluate, 0);
|
||||||
document.querySelector("button").disabled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize WASM when the page loads
|
// Initialize WASM when the page loads
|
||||||
initWasm().catch(console.error);
|
initWasm().catch(console.error);
|
||||||
|
|
||||||
// Make functions globally available for onclick handlers
|
// Make functions globally available for onclick handlers
|
||||||
window.evaluateAsJs = evaluateAsJs;
|
window.evaluate = evaluate;
|
||||||
window.evaluateAsDebug = evaluateAsDebug;
|
|
||||||
window.loadExample = loadExample;
|
window.loadExample = loadExample;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function evaluateAsJs() {
|
function evaluate() {
|
||||||
if (!window.evaluate_cel) {
|
if (!window.evaluate_cel) {
|
||||||
document.getElementById("result").textContent =
|
document.getElementById("result").textContent =
|
||||||
"WASM module not loaded yet...";
|
"WASM module not loaded yet...";
|
||||||
@@ -254,40 +244,15 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
const result = window.evaluate_cel(expression, context);
|
const result = window.evaluate_cel(expression, context);
|
||||||
|
|
||||||
resultDiv.className = "result success";
|
resultDiv.className = "result success";
|
||||||
resultDiv.textContent = `Success (JS Value): ${JSON.stringify(result, null, 2)}`;
|
resultDiv.textContent = `Success (type '${typeof result}'): ${JSON.stringify(result, null, 2)}`;
|
||||||
|
|
||||||
// Log the actual JavaScript object to console for inspection
|
// Log the actual JavaScript object to console for inspection
|
||||||
console.log("CEL Result as JavaScript value:", result);
|
console.log("CEL Result:", result);
|
||||||
console.log("Type:", typeof result);
|
console.log("Type:", typeof result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
resultDiv.className = "result error";
|
resultDiv.className = "result error";
|
||||||
resultDiv.textContent = `Error: ${e}`;
|
resultDiv.textContent = `Error: ${e}`;
|
||||||
}
|
console.log("CEL Error:", e);
|
||||||
}
|
|
||||||
|
|
||||||
function evaluateAsDebug() {
|
|
||||||
if (!window.evaluate_cel_debug) {
|
|
||||||
document.getElementById("result").textContent =
|
|
||||||
"WASM module not loaded yet...";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const expression = document.getElementById("expression").value;
|
|
||||||
const contextStr = document.getElementById("context").value;
|
|
||||||
const resultDiv = document.getElementById("result");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Parse the context as JavaScript object
|
|
||||||
const context = contextStr.trim() ? JSON.parse(contextStr) : {};
|
|
||||||
|
|
||||||
// Evaluate and get debug string
|
|
||||||
const result = window.evaluate_cel_debug(expression, context);
|
|
||||||
|
|
||||||
resultDiv.className = "result success";
|
|
||||||
resultDiv.textContent = `Debug Output: ${result}`;
|
|
||||||
} catch (e) {
|
|
||||||
resultDiv.className = "result error";
|
|
||||||
resultDiv.textContent = `Error: ${e}`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,19 +265,16 @@ name + " is " + string(age) + " years old and " + (active ? "active" : "inactive
|
|||||||
document.getElementById("context").value = context;
|
document.getElementById("context").value = context;
|
||||||
|
|
||||||
// Auto-evaluate
|
// Auto-evaluate
|
||||||
setTimeout(evaluateAsJs, 100);
|
setTimeout(evaluate, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-evaluate on input change
|
// Auto-evaluate on input change
|
||||||
document.getElementById("expression").addEventListener("input", () => {
|
document.getElementById("expression").addEventListener("input", () => {
|
||||||
setTimeout(evaluateAsJs, 300);
|
setTimeout(evaluate, 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById("context").addEventListener("input", () => {
|
document.getElementById("context").addEventListener("input", () => {
|
||||||
setTimeout(evaluateAsJs, 300);
|
setTimeout(evaluate, 100);
|
||||||
});
|
|
||||||
addEventListener("input", () => {
|
|
||||||
setTimeout(evaluate, 300);
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
24
src/lib.rs
24
src/lib.rs
@@ -93,15 +93,14 @@ fn cel_value_to_serializable(cel_val: &Value) -> SerializableValue {
|
|||||||
Value::Float(f) => SerializableValue::Float(*f),
|
Value::Float(f) => SerializableValue::Float(*f),
|
||||||
Value::String(s) => SerializableValue::String(s.to_string()),
|
Value::String(s) => SerializableValue::String(s.to_string()),
|
||||||
Value::List(_list) => {
|
Value::List(_list) => {
|
||||||
// For now, convert to string representation
|
SerializableValue::List(_list.iter().map(cel_value_to_serializable).collect())
|
||||||
// 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))
|
|
||||||
}
|
}
|
||||||
|
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)),
|
_ => SerializableValue::Other(format!("{:?}", cel_val)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,15 +121,6 @@ pub fn evaluate_cel(expression: &str, context: &JsValue) -> Result<JsValue, JsVa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Evaluate a CEL expression and return the result as a debug string
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn evaluate_cel_debug(expression: &str, context: &JsValue) -> Result<String, JsValue> {
|
|
||||||
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<Value, String> {
|
fn evaluate_cel_internal(expression: &str, context: &JsValue) -> Result<Value, String> {
|
||||||
// Compile the CEL expression
|
// Compile the CEL expression
|
||||||
let program = Program::compile(expression).map_err(|e| format!("Compilation error: {}", e))?;
|
let program = Program::compile(expression).map_err(|e| format!("Compilation error: {}", e))?;
|
||||||
|
|||||||
Reference in New Issue
Block a user