chewing_rs/
lib.rs

1use chewing_sys as chewing;
2use failure::{bail, Error};
3use std::ffi::CStr;
4
5pub struct Chewing {
6    // This pointer must never be allowed to leave the struct
7    ctx: *mut chewing::ChewingContext,
8    mode: u32
9}
10
11impl Chewing {
12    pub fn new() -> Result<Self, Error> {
13        let ctx = unsafe { chewing::chewing_new() };
14        // Manually handle null pointer returns here
15        if ctx.is_null() {
16            bail!("chewing_new() failed");
17        }
18
19        unsafe { chewing::chewing_set_ChiEngMode(ctx, chewing::CHINESE_MODE as i32) };
20        Ok(Chewing { ctx, mode :chewing::CHINESE_MODE })
21    }
22
23    pub fn config(&self) {
24        unsafe {
25            chewing::chewing_set_candPerPage(self.ctx, 10);
26            chewing::chewing_set_KBType(self.ctx, 0);
27            chewing::chewing_set_ShapeMode(self.ctx, 0);
28            chewing::chewing_set_addPhraseDirection(self.ctx, 1);
29            chewing::chewing_set_autoShiftCur(self.ctx, 1);
30            chewing::chewing_set_easySymbolInput(self.ctx, 0);
31            chewing::chewing_set_escCleanAllBuf(self.ctx, 1);
32            chewing::chewing_set_phraseChoiceRearward(self.ctx, 1);
33            chewing::chewing_set_spaceAsSelection(self.ctx, 1);
34        }
35    }
36
37    pub fn chinese_or_english_mode(&self){
38        if self.mode == chewing::CHINESE_MODE {
39            unsafe { chewing::chewing_set_ChiEngMode(self.ctx, chewing::SYMBOL_MODE as i32)}
40        } else {
41            unsafe { chewing::chewing_set_ChiEngMode(self.ctx, chewing::CHINESE_MODE as i32)}
42        }
43    }
44
45    pub fn symbol_length(&self, symbols: u8){
46        unsafe {
47            chewing::chewing_set_maxChiSymbolLen(self.ctx, symbols as i32);
48        }
49    }
50
51    pub fn list(&self) -> Vec<&str> {
52        unsafe {
53            chewing::chewing_cand_Enumerate(self.ctx);
54            let mut selection = Vec::new();
55            while chewing::chewing_cand_hasNext(self.ctx) > 0 {
56                selection.push(
57                    CStr::from_ptr(chewing::chewing_cand_String_static(self.ctx))
58                        .to_str()
59                        .expect("Cstr from selection failed"),
60                );
61            }
62            selection
63        }
64    }
65
66    pub fn choose_by_index(&self, index: u8) {
67        unsafe {
68            chewing::chewing_cand_choose_by_index(self.ctx, index as i32);
69        }
70    }
71
72    pub fn buffer(&self) -> &str {
73        unsafe {
74            let buf = chewing::chewing_buffer_String_static(self.ctx);
75            let str = CStr::from_ptr(buf).to_str().expect("Cstr from buf failed");
76            str
77        }
78    }
79
80    pub fn bopomofo(&self) -> &str {
81        unsafe {
82            let buf = chewing::chewing_bopomofo_String_static(self.ctx);
83            let str = CStr::from_ptr(buf).to_str().expect("Cstr from buf failed");
84            str
85        }
86    }
87
88    pub fn backspace(&self) {
89        unsafe {
90            chewing::chewing_handle_Backspace(self.ctx);
91        }
92    }
93
94    pub fn capslock(&self) {
95        unsafe {
96            chewing::chewing_handle_Capslock(self.ctx);
97        }
98    }
99
100    pub fn ctrl_num(&self, key: char) {
101        unsafe {
102            chewing::chewing_handle_CtrlNum(self.ctx, key as i32);
103        }
104    }
105
106    pub fn double_tab(&self) {
107        unsafe {
108            chewing::chewing_handle_DblTab(self.ctx);
109        }
110    }
111
112    pub fn default(&self, key: char) {
113        unsafe {
114            chewing::chewing_handle_Default(self.ctx, key as i32);
115        }
116    }
117
118    pub fn del(&self) {
119        unsafe {
120            chewing::chewing_handle_Del(self.ctx);
121        }
122    }
123
124    pub fn down(&self) {
125        unsafe {
126            chewing::chewing_handle_Down(self.ctx);
127        }
128    }
129
130    pub fn end(&self) {
131        unsafe {
132            chewing::chewing_handle_End(self.ctx);
133        }
134    }
135
136    pub fn enter(&self) {
137        unsafe {
138            chewing::chewing_handle_Enter(self.ctx);
139        }
140    }
141
142    pub fn esc(&self) {
143        unsafe {
144            chewing::chewing_handle_Esc(self.ctx);
145        }
146    }
147
148    pub fn home(&self) {
149        unsafe {
150            chewing::chewing_handle_Home(self.ctx);
151        }
152    }
153
154    pub fn left(&self) {
155        unsafe {
156            chewing::chewing_handle_Left(self.ctx);
157        }
158    }
159
160    pub fn num_lock(&self, key: char) {
161        unsafe {
162            chewing::chewing_handle_Numlock(self.ctx, key as i32);
163        }
164    }
165
166    pub fn page_up(&self) {
167        unsafe {
168            chewing::chewing_handle_PageUp(self.ctx);
169        }
170    }
171
172    pub fn page_down(&self) {
173        unsafe {
174            chewing::chewing_handle_PageDown(self.ctx);
175        }
176    }
177
178    pub fn right(&self) {
179        unsafe {
180            chewing::chewing_handle_Right(self.ctx);
181        }
182    }
183
184    pub fn shift_left(&self) {
185        unsafe {
186            chewing::chewing_handle_ShiftLeft(self.ctx);
187        }
188    }
189
190    pub fn shift_right(&self) {
191        unsafe {
192            chewing::chewing_handle_ShiftRight(self.ctx);
193        }
194    }
195
196    pub fn shift_space(&self) {
197        unsafe {
198            chewing::chewing_handle_ShiftSpace(self.ctx);
199        }
200    }
201
202    pub fn space(&self) {
203        unsafe {
204            chewing::chewing_handle_Space(self.ctx);
205        }
206    }
207
208    pub fn tab(&self) {
209        unsafe {
210            chewing::chewing_handle_Tab(self.ctx);
211        }
212    }
213
214    pub fn up(&self) {
215        unsafe {
216            chewing::chewing_handle_Up(self.ctx);
217        }
218    }
219}
220
221impl Drop for Chewing {
222    fn drop(&mut self) {
223        unsafe { chewing::chewing_delete(self.ctx) }
224    }
225}