|
| 1 | +//! WebAssembly exports for JavaScript interop. |
| 2 | +
|
| 3 | +use wasm_bindgen::prelude::*; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + custom_rules, normalize, normalize_sentence, normalize_sentence_with_max_span, |
| 7 | + normalize_with_lang, tn_normalize, tn_normalize_lang, tn_normalize_sentence, |
| 8 | + tn_normalize_sentence_lang, tn_normalize_sentence_with_max_span, |
| 9 | + tn_normalize_sentence_with_max_span_lang, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Initialize panic hook for better error messages in browser devtools. |
| 13 | +#[wasm_bindgen] |
| 14 | +pub fn set_panic_hook() { |
| 15 | + console_error_panic_hook::set_once(); |
| 16 | +} |
| 17 | + |
| 18 | +#[wasm_bindgen(js_name = normalize)] |
| 19 | +pub fn normalize_js(input: &str) -> String { |
| 20 | + normalize(input) |
| 21 | +} |
| 22 | + |
| 23 | +#[wasm_bindgen(js_name = normalizeWithLang)] |
| 24 | +pub fn normalize_with_lang_js(input: &str, lang: &str) -> String { |
| 25 | + normalize_with_lang(input, lang) |
| 26 | +} |
| 27 | + |
| 28 | +#[wasm_bindgen(js_name = normalizeSentence)] |
| 29 | +pub fn normalize_sentence_js(input: &str) -> String { |
| 30 | + normalize_sentence(input) |
| 31 | +} |
| 32 | + |
| 33 | +#[wasm_bindgen(js_name = normalizeSentenceWithMaxSpan)] |
| 34 | +pub fn normalize_sentence_with_max_span_js(input: &str, max_span_tokens: u32) -> String { |
| 35 | + normalize_sentence_with_max_span(input, max_span_tokens as usize) |
| 36 | +} |
| 37 | + |
| 38 | +#[wasm_bindgen(js_name = tnNormalize)] |
| 39 | +pub fn tn_normalize_js(input: &str) -> String { |
| 40 | + tn_normalize(input) |
| 41 | +} |
| 42 | + |
| 43 | +#[wasm_bindgen(js_name = tnNormalizeLang)] |
| 44 | +pub fn tn_normalize_lang_js(input: &str, lang: &str) -> String { |
| 45 | + tn_normalize_lang(input, lang) |
| 46 | +} |
| 47 | + |
| 48 | +#[wasm_bindgen(js_name = tnNormalizeSentence)] |
| 49 | +pub fn tn_normalize_sentence_js(input: &str) -> String { |
| 50 | + tn_normalize_sentence(input) |
| 51 | +} |
| 52 | + |
| 53 | +#[wasm_bindgen(js_name = tnNormalizeSentenceLang)] |
| 54 | +pub fn tn_normalize_sentence_lang_js(input: &str, lang: &str) -> String { |
| 55 | + tn_normalize_sentence_lang(input, lang) |
| 56 | +} |
| 57 | + |
| 58 | +#[wasm_bindgen(js_name = tnNormalizeSentenceWithMaxSpan)] |
| 59 | +pub fn tn_normalize_sentence_with_max_span_js(input: &str, max_span_tokens: u32) -> String { |
| 60 | + tn_normalize_sentence_with_max_span(input, max_span_tokens as usize) |
| 61 | +} |
| 62 | + |
| 63 | +#[wasm_bindgen(js_name = tnNormalizeSentenceWithMaxSpanLang)] |
| 64 | +pub fn tn_normalize_sentence_with_max_span_lang_js( |
| 65 | + input: &str, |
| 66 | + lang: &str, |
| 67 | + max_span_tokens: u32, |
| 68 | +) -> String { |
| 69 | + tn_normalize_sentence_with_max_span_lang(input, lang, max_span_tokens as usize) |
| 70 | +} |
| 71 | + |
| 72 | +#[wasm_bindgen(js_name = addRule)] |
| 73 | +pub fn add_rule_js(spoken: &str, written: &str) { |
| 74 | + custom_rules::add_rule(spoken, written); |
| 75 | +} |
| 76 | + |
| 77 | +#[wasm_bindgen(js_name = removeRule)] |
| 78 | +pub fn remove_rule_js(spoken: &str) -> bool { |
| 79 | + custom_rules::remove_rule(spoken) |
| 80 | +} |
| 81 | + |
| 82 | +#[wasm_bindgen(js_name = clearRules)] |
| 83 | +pub fn clear_rules_js() { |
| 84 | + custom_rules::clear_rules(); |
| 85 | +} |
| 86 | + |
| 87 | +#[wasm_bindgen(js_name = ruleCount)] |
| 88 | +pub fn rule_count_js() -> u32 { |
| 89 | + custom_rules::rule_count() as u32 |
| 90 | +} |
0 commit comments