use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
use crate::phonetic::llev::LLevError;
use crate::phonetic::{parse_str, RuleSetChar};
#[derive(Serialize, Deserialize)]
pub struct WasmRewriteVariant {
pub text: String,
}
#[wasm_bindgen]
pub struct WasmRuleSet {
inner: RuleSetChar,
}
#[wasm_bindgen]
impl WasmRuleSet {
#[wasm_bindgen]
pub fn parse(source: &str) -> Result<WasmRuleSet, JsValue> {
let file = parse_str(source).map_err(|e: LLevError| JsValue::from_str(&e.to_string()))?;
let inner = RuleSetChar::from_llev(&file)
.map_err(|e: LLevError| JsValue::from_str(&e.to_string()))?;
Ok(WasmRuleSet { inner })
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn rewrite(&self, term: &str) -> String {
self.inner.apply(term)
}
pub fn name(&self) -> Option<String> {
self.inner.name.clone()
}
pub fn version(&self) -> Option<String> {
self.inner.version.clone()
}
}
#[wasm_bindgen]
pub fn normalize_english(text: &str) -> String {
use crate::phonetic::orthography_rules_char;
let rules = orthography_rules_char();
let ruleset = RuleSetChar {
rules,
name: None,
version: None,
};
ruleset.apply(text)
}
#[wasm_bindgen]
pub fn phonetic_transform(text: &str) -> String {
use crate::phonetic::phonetic_rules_char;
let rules = phonetic_rules_char();
let ruleset = RuleSetChar {
rules,
name: None,
version: None,
};
ruleset.apply(text)
}