extern crate kawaii;

use self::kawaii::Measure;

use std::ffi::CStr;
use std::ptr::null_mut;
use std::mem::transmute;
use std::os::raw::c_char;

#[no_mangle]
pub extern "C" fn measure_init(name: *const c_char) -> *mut Measure {
    if name.is_null() {
        return null_mut();
    }

    unsafe {
        match CStr::from_ptr(name).to_str() {
            Err(_) => null_mut(),
            Ok(name) => transmute(Box::new(Measure::new(String::from(name)))),
        }
    }
}

#[no_mangle]
pub extern "C" fn measure_clean(measure: *mut Measure) {
    let _measure: Box<Measure> = unsafe { transmute(measure) };
}

#[no_mangle]
pub extern "C" fn measure_start(measure: *mut Measure) {
    let measure = unsafe { &mut *measure };

    measure.start();
}

#[no_mangle]
pub extern "C" fn measure_pause(measure: *mut Measure) {
    let measure = unsafe { &mut *measure };

    measure.pause();
}

#[no_mangle]
pub extern "C" fn measure_stop(measure: *mut Measure) {
    let measure = unsafe { &mut *measure };

    measure.stop();
}