use wasm_bindgen::prelude::*;
use libdictenstein::double_array_trie::DoubleArrayTrie;
use libdictenstein::dynamic_dawg::DynamicDawg;
use libdictenstein::Dictionary;
#[wasm_bindgen]
pub struct WasmDoubleArrayTrie {
inner: DoubleArrayTrie<()>,
}
#[wasm_bindgen]
impl WasmDoubleArrayTrie {
#[wasm_bindgen(constructor)]
pub fn new(terms: Vec<JsValue>) -> Result<WasmDoubleArrayTrie, JsValue> {
let terms: Result<Vec<String>, _> = terms
.into_iter()
.map(|v| {
v.as_string()
.ok_or_else(|| JsValue::from_str("all terms must be strings"))
})
.collect();
let terms = terms?;
let term_refs: Vec<&str> = terms.iter().map(|s| s.as_str()).collect();
Ok(WasmDoubleArrayTrie {
inner: DoubleArrayTrie::from_terms(term_refs),
})
}
pub fn contains(&self, term: &str) -> bool {
self.inner.contains(term)
}
pub fn len(&self) -> usize {
self.inner.len().unwrap_or(0)
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[wasm_bindgen]
pub struct WasmDynamicDawg {
inner: DynamicDawg<()>,
}
#[wasm_bindgen]
impl WasmDynamicDawg {
#[wasm_bindgen(constructor)]
pub fn new() -> WasmDynamicDawg {
WasmDynamicDawg {
inner: DynamicDawg::new(),
}
}
#[wasm_bindgen(js_name = fromTerms)]
pub fn from_terms(terms: Vec<JsValue>) -> Result<WasmDynamicDawg, JsValue> {
let terms: Result<Vec<String>, _> = terms
.into_iter()
.map(|v| {
v.as_string()
.ok_or_else(|| JsValue::from_str("all terms must be strings"))
})
.collect();
let terms = terms?;
let term_refs: Vec<&str> = terms.iter().map(|s| s.as_str()).collect();
Ok(WasmDynamicDawg {
inner: DynamicDawg::from_terms(term_refs),
})
}
pub fn insert(&self, term: &str) -> bool {
self.inner.insert(term)
}
pub fn remove(&self, term: &str) -> bool {
self.inner.remove(term)
}
pub fn contains(&self, term: &str) -> bool {
self.inner.contains(term)
}
pub fn len(&self) -> usize {
self.inner.len().unwrap_or(0)
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl Default for WasmDynamicDawg {
fn default() -> Self {
Self::new()
}
}