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;

/// Constructs a new `Measure` on heap and returns pointer.
#[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)))),
        }
    }
}

/// Drops a `Measure` pointer on heap.
#[no_mangle]
pub extern "C" fn measure_clean(measure: *mut Measure) {
    let _measure: Box<Measure> = unsafe { transmute(measure) };
}

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

    measure.start();
}

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

    measure.pause();
}

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

    measure.stop();
}
