use super::*;
use crate::dice::*;
use std::str::FromStr;
#[no_mangle]
pub unsafe extern "C" fn roll_from_str(s: *const c_char) -> *mut Roll {
let c_str = {
assert!(!s.is_null());
CStr::from_ptr(s) };
let r_str = c_str.to_str().unwrap();
let roll = Roll::from_str(r_str).unwrap();
Box::into_raw(Box::new(roll))
}
#[no_mangle]
pub unsafe extern "C" fn roll_free(ptr: *mut Roll) {
if ptr.is_null() {
return;
}
Box::from_raw(ptr);
}
#[no_mangle]
pub unsafe extern "C" fn roll_base(ptr: *const Roll) -> usize {
let roll = {
assert!(!ptr.is_null());
&*ptr };
roll.get_base()
}
#[no_mangle]
pub unsafe extern "C" fn roll_repeat(ptr: *const Roll) -> usize {
let roll = {
assert!(!ptr.is_null());
&*ptr };
roll.get_repeat()
}
#[no_mangle]
pub unsafe extern "C" fn roll_execute(ptr: *const Roll) -> *mut RollResult {
let roll = {
assert!(!ptr.is_null());
&*ptr };
let result = roll.execute();
Box::into_raw(Box::new(result))
}
#[no_mangle]
pub unsafe extern "C" fn roll_to_string(ptr: *const Roll) -> *mut c_char {
let roll = {
assert!(!ptr.is_null());
&*ptr };
let c_str = CString::new(roll.to_string()).unwrap();
c_str.into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn roll_result_to_string(ptr: *const RollResult) -> *mut c_char {
let result = {
assert!(!ptr.is_null());
&*ptr };
let c_str = CString::new(result.to_string()).unwrap();
c_str.into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn roll_result_free(ptr: *mut RollResult) {
if ptr.is_null() {
return;
}
Box::from_raw(ptr);
}
#[no_mangle]
pub unsafe extern "C" fn roll_result_to_json(ptr: *const RollResult) -> *mut c_char {
let roll_result = {
assert!(!ptr.is_null());
&*ptr };
let json = serde_json::to_string(&roll_result).unwrap();
let c_str = CString::new(json).unwrap();
c_str.into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn roll_result_total(ptr: *const RollResult) -> isize {
let result = {
assert!(!ptr.is_null());
&*ptr };
result.total()
}