1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#![forbid(unsafe_code)] extern crate ternary_tree; extern crate js_sys; use wasm_bindgen::prelude::*; use ternary_tree::{TstIterator, TstCompleteIterator, TstNeighborIterator, TstCrosswordIterator}; #[wasm_bindgen] pub struct Tst { tree: ternary_tree::Tst<JsValue> } #[wasm_bindgen] #[derive(PartialEq)] pub enum Direction { Forward, Backward } #[wasm_bindgen] impl Tst { pub fn new() -> Self { Tst { tree: ternary_tree::Tst::new() } } pub fn insert(&mut self, key: &str, value: JsValue) -> JsValue { match self.tree.insert(key, value) { None => JsValue::null(), Some(value) => value } } pub fn get(&self, key: &str) -> JsValue { match self.tree.get(key) { None => JsValue::null(), Some(value) => value.clone() } } pub fn remove(&mut self, key: &str) -> JsValue { match self.tree.remove(key) { None => JsValue::null(), Some(value) => value.clone() } } pub fn count(&self) -> usize { self.tree.len() } pub fn clear(&mut self) { self.tree.clear() } pub fn pretty_print(&self) -> String { let mut w = Vec::new(); self.tree.pretty_print(&mut w); match String::from_utf8(w) { Ok(s) => s, Err(_) => String::new() } } pub fn visit(&self, callback: &js_sys::Function, direction: Direction) { let mut it = self.tree.iter(); let this = JsValue::NULL; let next_fn = if direction == Direction::Forward { TstIterator::<JsValue>::next } else { TstIterator::<JsValue>::next_back }; let key_fn = if direction == Direction::Forward { TstIterator::<JsValue>::current_key } else { TstIterator::<JsValue>::current_key_back }; while let Some(value) = (next_fn)(&mut it) { let key = JsValue::from((key_fn)(&mut it)); let should_break = match callback.call2(&this, &key, value) { Ok(res) => res.is_truthy(), Err(_) => true }; if should_break { break; } } } pub fn complete(&self, prefix: &str, callback: &js_sys::Function, direction: Direction) { let mut it = self.tree.iter_complete(prefix); let this = JsValue::NULL; let next_fn = if direction == Direction::Forward { TstCompleteIterator::<JsValue>::next } else { TstCompleteIterator::<JsValue>::next_back }; let key_fn = if direction == Direction::Forward { TstCompleteIterator::<JsValue>::current_key } else { TstCompleteIterator::<JsValue>::current_key_back }; while let Some(value) = (next_fn)(&mut it) { let key = JsValue::from((key_fn)(&mut it)); let res = callback.call2(&this, &key, value); let should_break = match res { Ok(val) => val.is_truthy(), Err(_) => true }; if should_break { break; } } } pub fn neighbor(&self, neighbor_key: &str, range: usize, callback: &js_sys::Function, direction: Direction) { let mut it = self.tree.iter_neighbor(neighbor_key, range); let this = JsValue::NULL; let next_fn = if direction == Direction::Forward { TstNeighborIterator::<JsValue>::next } else { TstNeighborIterator::<JsValue>::next_back }; let key_fn = if direction == Direction::Forward { TstNeighborIterator::<JsValue>::current_key } else { TstNeighborIterator::<JsValue>::current_key_back }; while let Some(value) = (next_fn)(&mut it) { let key = JsValue::from((key_fn)(&mut it)); let res = callback.call2(&this, &key, value); let should_break = match res { Ok(val) => val.is_truthy(), Err(_) => true }; if should_break { break; } } } pub fn crossword(&self, pattern: &str, joker: char, callback: &js_sys::Function, direction: Direction) { let mut it = self.tree.iter_crossword(pattern, joker); let this = JsValue::NULL; let next_fn = if direction == Direction::Forward { TstCrosswordIterator::<JsValue>::next } else { TstCrosswordIterator::<JsValue>::next_back }; let key_fn = if direction == Direction::Forward { TstCrosswordIterator::<JsValue>::current_key } else { TstCrosswordIterator::<JsValue>::current_key_back }; while let Some(value) = (next_fn)(&mut it) { let key = JsValue::from((key_fn)(&mut it)); let res = callback.call2(&this, &key, value); let should_break = match res { Ok(val) => val.is_truthy(), Err(_) => true }; if should_break { break; } } } }