use crate::wasm::common::error::{WasmError, WasmResult};
use js_sys::{Array, Object, Reflect};
use wasm_bindgen::prelude::*;
pub struct JsObjectBuilder {
obj: Object,
}
impl JsObjectBuilder {
pub fn new() -> Self {
Self { obj: Object::new() }
}
pub fn set_f64(self, key: &str, value: f64) -> WasmResult<Self> {
Reflect::set(&self.obj, &key.into(), &JsValue::from_f64(value))
.map_err(|_| WasmError::invalid_param("property", key, "failed to set"))?;
Ok(self)
}
pub fn set_f32(self, key: &str, value: f32) -> WasmResult<Self> {
self.set_f64(key, value as f64)
}
pub fn set_usize(self, key: &str, value: usize) -> WasmResult<Self> {
self.set_f64(key, value as f64)
}
pub fn set_string(self, key: &str, value: &str) -> WasmResult<Self> {
Reflect::set(&self.obj, &key.into(), &JsValue::from_str(value))
.map_err(|_| WasmError::invalid_param("property", key, "failed to set"))?;
Ok(self)
}
pub fn set_bool(self, key: &str, value: bool) -> WasmResult<Self> {
Reflect::set(&self.obj, &key.into(), &JsValue::from_bool(value))
.map_err(|_| WasmError::invalid_param("property", key, "failed to set"))?;
Ok(self)
}
pub fn build(self) -> Object {
self.obj
}
pub fn into_js_value(self) -> JsValue {
self.obj.into()
}
}
pub struct JsArrayBuilder {
array: Array,
}
impl JsArrayBuilder {
pub fn new() -> Self {
Self {
array: Array::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
let array = Array::new();
array.set_length(capacity as u32);
array.set_length(0); Self { array }
}
pub fn push_f64(self, value: f64) -> Self {
self.array.push(&JsValue::from_f64(value));
self
}
pub fn push_f32(self, value: f32) -> Self {
self.push_f64(value as f64)
}
pub fn push_string(self, value: &str) -> Self {
self.array.push(&JsValue::from_str(value));
self
}
pub fn push_object(self, obj: Object) -> Self {
self.array.push(&obj);
self
}
pub fn build(self) -> Array {
self.array
}
}
pub struct WasmVersion;
impl WasmVersion {
pub fn module_version(module_name: &str) -> String {
format!(
"RusTorch WASM {} v{}",
module_name,
env!("CARGO_PKG_VERSION")
)
}
pub fn core_version() -> String {
format!("RusTorch WASM Core v{}", env!("CARGO_PKG_VERSION"))
}
}
pub struct JsonFormatter;
impl JsonFormatter {
pub fn stats_json(mean: f32, std_dev: f32, min: f32, max: f32, count: usize) -> String {
format!(
"{{\"mean\":{:.6},\"std\":{:.6},\"min\":{:.6},\"max\":{:.6},\"count\":{}}}",
mean, std_dev, min, max, count
)
}
pub fn quality_json(
completeness: f32,
validity: f32,
consistency: f32,
uniqueness: f32,
overall: f32,
) -> String {
format!(
"{{\"completeness\":{:.2},\"validity\":{:.2},\"consistency\":{:.2},\"uniqueness\":{:.2},\"overall_quality\":{:.2}}}",
completeness, validity, consistency, uniqueness, overall
)
}
pub fn anomaly_object(
index: usize,
value: f32,
score: f32,
anomaly_type: &str,
) -> WasmResult<Object> {
JsObjectBuilder::new()
.set_usize("index", index)?
.set_f32("value", value)?
.set_f32("score", score)?
.set_string("type", anomaly_type)?
.build();
Ok(JsObjectBuilder::new()
.set_usize("index", index)?
.set_f32("value", value)?
.set_f32("score", score)?
.set_string("type", anomaly_type)?
.build())
}
}