fix compilation issues

This commit is contained in:
2025-08-08 07:17:39 +02:00
parent 7f102b8e0b
commit c84f730618
5 changed files with 39 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
use cel::{Context, Program, Value};
use serde_wasm_bindgen;
use js_sys;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
@@ -48,12 +48,6 @@ impl CelResult {
fn js_value_to_cel_value(js_val: &JsValue) -> Result<Value, String> {
if js_val.is_null() || js_val.is_undefined() {
Ok(Value::Null)
} else if js_val.is_bigint() {
// Handle BigInt
match js_val.as_f64() {
Some(n) => Ok(Value::Int(n as i64)),
None => Err("Failed to convert BigInt to number".to_string()),
}
} else if let Some(b) = js_val.as_bool() {
Ok(Value::Bool(b))
} else if let Some(n) = js_val.as_f64() {
@@ -96,58 +90,7 @@ fn js_value_to_cel_value(js_val: &JsValue) -> Result<Value, String> {
Ok(Value::Map(cel_map.into()))
} else {
Err(format!("Unsupported JavaScript type: {:?}", js_val))
}
}
/// Convert a CEL Value back to a JavaScript value
fn cel_value_to_js_value(cel_val: &Value) -> Result<JsValue, String> {
match cel_val {
Value::Null => Ok(JsValue::NULL),
Value::Bool(b) => Ok(JsValue::from_bool(*b)),
Value::Int(i) => Ok(JsValue::from_f64(*i as f64)),
Value::UInt(u) => Ok(JsValue::from_f64(*u as f64)),
Value::Float(f) => Ok(JsValue::from_f64(*f)),
Value::String(s) => Ok(JsValue::from_str(s)),
Value::Bytes(b) => {
// Convert bytes to Uint8Array
let array = js_sys::Uint8Array::new_with_length(b.len() as u32);
for (i, byte) in b.iter().enumerate() {
array.set_index(i as u32, *byte);
}
Ok(array.into())
}
Value::List(list) => {
let array = js_sys::Array::new();
for item in list.iter() {
let js_item = cel_value_to_js_value(item)?;
array.push(&js_item);
}
Ok(array.into())
}
Value::Map(map) => {
let obj = js_sys::Object::new();
for (key, value) in map.iter() {
let js_value = cel_value_to_js_value(value)?;
js_sys::Reflect::set(&obj, &JsValue::from_str(key), &js_value)
.map_err(|_| "Failed to set object property".to_string())?;
}
Ok(obj.into())
}
Value::Duration(d) => {
// Convert duration to string representation
Ok(JsValue::from_str(&format!("{}s", d.as_secs_f64())))
}
Value::Timestamp(ts) => {
// Convert timestamp to ISO string
let datetime = chrono::DateTime::from_timestamp(ts.as_secs() as i64, ts.subsec_nanos())
.ok_or_else(|| "Invalid timestamp".to_string())?;
Ok(JsValue::from_str(&datetime.to_rfc3339()))
}
_ => Err(format!(
"Unsupported CEL type for conversion: {:?}",
cel_val
)),
Err(format!("Unsupported JavaScript type"))
}
}
@@ -170,14 +113,18 @@ pub fn evaluate_cel(expression: &str, context: &JsValue) -> CelResult {
}
}
/// Evaluate a CEL expression and return the result as a JavaScript value
/// Evaluate a CEL expression and return the result as a string
#[wasm_bindgen]
pub fn evaluate_cel_js(expression: &str, context: &JsValue) -> Result<JsValue, JsValue> {
pub fn evaluate_cel_string(expression: &str, context: &JsValue) -> Result<String, JsValue> {
match evaluate_cel_internal(expression, context) {
Ok(cel_value) => match cel_value_to_js_value(&cel_value) {
Ok(js_val) => Ok(js_val),
Err(e) => Err(JsValue::from_str(&format!("Conversion error: {}", e))),
},
Ok(cel_value) => {
// Convert the result to a reasonable string representation
let result_string = match &cel_value {
Value::String(s) => s.clone(),
_ => format!("{:?}", cel_value).into(),
};
Ok(result_string.to_string())
}
Err(e) => Err(JsValue::from_str(&e)),
}
}