use crate::cleaner::{clean, inspect};
use crate::stochastic::StochasticEnhancer;
use crate::unicode::{CleanOpts, InspectOpts, clean_text, inspect_text};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn init_panic_hook() {
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
pub fn clean_text_wasm(text: &str) -> Result<JsValue, JsValue> {
let opts = CleanOpts {
aggressive_confusables: true,
..CleanOpts::safe()
};
let (cleaned, stats) =
clean_text(text, &opts).map_err(|e| JsValue::from_str(&e.to_string()))?;
let obj = serde_json::json!({
"cleaned": cleaned,
"removed_count": stats.removed_count,
"replaced_count": stats.replaced_count,
"summary": stats.summary,
});
serde_wasm_bindgen::to_value(&obj).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn inspect_text_wasm(text: &str) -> Result<JsValue, JsValue> {
let opts = InspectOpts {
aggressive_confusables: true,
..InspectOpts::default()
};
let report = inspect_text(text, &opts).map_err(|e| JsValue::from_str(&e.to_string()))?;
serde_wasm_bindgen::to_value(&report).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn clean_bytes_wasm(data: &[u8]) -> Result<Vec<u8>, JsValue> {
clean(data, None)
.map(|out| out.bytes)
.map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn inspect_bytes_wasm(data: &[u8]) -> Result<JsValue, JsValue> {
let output = inspect(data, None).map_err(|e| JsValue::from_str(&e.to_string()))?;
let obj = serde_json::json!({
"format": format!("{:?}", output.format),
"text_report": output.text_report,
"image_report": output.image_report,
"meta_findings": output.meta_findings,
});
serde_wasm_bindgen::to_value(&obj).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn version() -> String {
env!("CARGO_PKG_VERSION").to_string()
}
#[wasm_bindgen]
pub fn enhance_text_wasm(text: &str, probability: f64) -> Result<JsValue, JsValue> {
let enhancer = StochasticEnhancer::new(probability);
let out = enhancer.enhance(text);
let obj = serde_json::json!({
"enhanced": out.text,
"probability": out.probability,
"words_substituted": out.words_substituted,
});
serde_wasm_bindgen::to_value(&obj).map_err(|e| JsValue::from_str(&e.to_string()))
}