chewing-rs 0.1.0

Safe wrappers for the libchewing rust bindings found in the chewing-sys crate
Documentation
use failure::{bail, Error};
use chewing_sys as chewing;
use std::ffi::CStr;

pub struct Chewing {
    // This pointer must never be allowed to leave the struct
    ctx: *mut chewing::ChewingContext,
}

impl Chewing {
    pub fn new() -> Result<Self, Error> {
        let ctx = unsafe { chewing::chewing_new() };
        // Manually handle null pointer returns here
        if ctx.is_null() {
            bail!("chewing_new() failed");
        }
        Ok(Chewing { ctx })
    }

    pub fn config(&self) {
        unsafe { 
            chewing::chewing_set_candPerPage(self.ctx, 10);
            chewing::chewing_set_ChiEngMode(self.ctx, 1);
            chewing::chewing_set_KBType(self.ctx, 0);
            chewing::chewing_set_ShapeMode(self.ctx, 0);
            chewing::chewing_set_addPhraseDirection(self.ctx, 1);
            chewing::chewing_set_autoShiftCur(self.ctx, 1);
            chewing::chewing_set_easySymbolInput(self.ctx, 0);
            chewing::chewing_set_escCleanAllBuf(self.ctx, 1);
            chewing::chewing_set_maxChiSymbolLen(self.ctx, 16);
            chewing::chewing_set_phraseChoiceRearward(self.ctx, 1);
            chewing::chewing_set_spaceAsSelection(self.ctx, 1);
        }
    }

    pub fn list(&self) -> Vec<&str> {
        unsafe {
            chewing::chewing_cand_Enumerate(self.ctx);
            let mut selection = Vec::new();
            while chewing::chewing_cand_hasNext(self.ctx) > 0 {
                selection.push(
                    CStr::from_ptr(
                        chewing::chewing_cand_String_static(self.ctx))
                            .to_str()
                            .expect("Cstr from selection failed"));
            }
            selection
        }
    }

    pub fn choose_by_index(&self, index: u8){
        unsafe {
            chewing::chewing_cand_choose_by_index(self.ctx, index as i32);
        }
    }

    pub fn buffer(&self) -> &str {
        unsafe {
            let buf = chewing::chewing_buffer_String_static(self.ctx);
            let str = CStr::from_ptr(buf).to_str().expect("Cstr from buf failed");
            str
        }
    }

    pub fn bopomofo(&self) -> &str {
        unsafe {
            let buf = chewing::chewing_bopomofo_String_static(self.ctx);
            let str = CStr::from_ptr(buf).to_str().expect("Cstr from buf failed");
            str
        }
    }

    pub fn default(&self, key: char) {
        unsafe {
            chewing::chewing_handle_Default(self.ctx, key as i32);
        }
    }

    pub fn enter(&self) {
        unsafe {
            chewing::chewing_handle_Enter(self.ctx);
        }
    }

    pub fn capslock(&self) {
        unsafe {
            chewing::chewing_handle_Capslock(self.ctx);
        }
    }

    pub fn left(&self) {
        unsafe {
            chewing::chewing_handle_Left(self.ctx);
        }
    }

    pub fn right(&self) {
        unsafe {
            chewing::chewing_handle_Right(self.ctx);
        }
    }

    pub fn space(&self) {
        unsafe {
            chewing::chewing_handle_Space(self.ctx);
        }
    }

    pub fn up(&self) {
        unsafe {
            chewing::chewing_handle_Up(self.ctx);
        }
    }

    pub fn down(&self) {
        unsafe {
            chewing::chewing_handle_Down(self.ctx);
        }
    }
}

impl Drop for Chewing {
    fn drop(&mut self) {
        unsafe { chewing::chewing_delete(self.ctx) }
    }
}