changes saved in my editor
This commit is contained in:
128
core/src/config.rs
Normal file
128
core/src/config.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
use std::any::Any;
|
||||
|
||||
use crate::MizeResult;
|
||||
|
||||
use crate::c_api::get_global_mize;
|
||||
use crate::id::MizeId;
|
||||
use crate::item::{IntoItemData, ItemData};
|
||||
use crate::Mize;
|
||||
|
||||
type ConfigFun = Box<dyn Fn() -> MizeResult<ItemData> + Send + Sync + 'static>;
|
||||
|
||||
pub struct ConfigOpt {
|
||||
pub name: String,
|
||||
pub val: Option<ItemData>,
|
||||
pub fun: Option<ConfigFun>,
|
||||
pub deps: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(C)]
|
||||
pub struct ConfigOptId {
|
||||
pub id: MizeId,
|
||||
}
|
||||
|
||||
impl ConfigOptId {
|
||||
pub fn default_val(self, val: ItemData) -> Self {
|
||||
let mize = get_global_mize().unwrap();
|
||||
let mut config_opts = mize.config_opts.lock().unwrap();
|
||||
let opt = config_opts.get_mut(&self.id).unwrap();
|
||||
opt.val = Some(val);
|
||||
self.clone()
|
||||
}
|
||||
pub fn fun(self, fun: ConfigFun) -> Self {
|
||||
let mize = get_global_mize().unwrap();
|
||||
let mut config_opts = mize.config_opts.lock().unwrap();
|
||||
println!("get_mut: {}", self.id);
|
||||
let opt = config_opts.get_mut(&self.id).unwrap();
|
||||
opt.fun = Some(fun);
|
||||
self.clone()
|
||||
}
|
||||
pub fn method<F: Fn(ItemData) -> MizeResult<ItemData>>(self, _name: &str, _fun: F) -> ConfigOptId {
|
||||
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
//pub trait ToItemDataHelper {
|
||||
//fn into_item_data(self) -> ItemData;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
pub fn opt(name: &str) -> crate::config::ConfigOptId {
|
||||
let mize = get_global_mize().unwrap();
|
||||
let mut cur_part = mize.cur_eval_part.lock().unwrap().clone().unwrap();
|
||||
let mut opts = mize.config_opts.lock().unwrap();
|
||||
let id = cur_part.join(name).unwrap();
|
||||
let opt = ConfigOpt {
|
||||
name: name.to_owned(),
|
||||
val: None,
|
||||
fun: None,
|
||||
deps: Vec::new(),
|
||||
};
|
||||
opts.insert(id.clone(), opt);
|
||||
ConfigOptId { id }
|
||||
}
|
||||
|
||||
pub fn method<F: Fn(ItemData) -> MizeResult<ItemData>>(_name: &str, _fun: F) {
|
||||
}
|
||||
|
||||
pub fn effect<F: Fn(ItemData) -> MizeResult<ItemData>>(_name: &str, _fun: F) {
|
||||
}
|
||||
|
||||
pub fn thread<F: Fn(ItemData) -> MizeResult<ItemData>>(_name: &str, _fun: F) {
|
||||
}
|
||||
|
||||
type CFun = unsafe extern "C" fn (*mut ItemData) -> *mut ItemData;
|
||||
|
||||
pub mod c_api {
|
||||
use std::ffi::c_char;
|
||||
|
||||
use crate::config::{CFun, ConfigOptId};
|
||||
use std::ffi::CStr;
|
||||
|
||||
pub extern "C" fn opt(name: *const c_char) -> ConfigOptId {
|
||||
unsafe {
|
||||
let c_str = CStr::from_ptr(name);
|
||||
let string = c_str.to_string_lossy();
|
||||
super::opt(string.as_str())
|
||||
}
|
||||
}
|
||||
pub extern "C" fn method(name: *const c_char, c_fun: CFun) {
|
||||
unsafe {
|
||||
let c_str = CStr::from_ptr(name);
|
||||
let string = c_str.to_string_lossy();
|
||||
super::method(string.as_str(), move |args| {
|
||||
let raw_ptr = Box::into_raw(Box::new(args));
|
||||
let ret_ptr = c_fun(raw_ptr);
|
||||
let data = *Box::from_raw(ret_ptr);
|
||||
Ok(data)
|
||||
})
|
||||
}
|
||||
}
|
||||
pub extern "C" fn effect(name: *const c_char, c_fun: CFun) {
|
||||
unsafe {
|
||||
let c_str = CStr::from_ptr(name);
|
||||
let string = c_str.to_string_lossy();
|
||||
super::effect(string.as_str(), move |args| {
|
||||
let raw_ptr = Box::into_raw(Box::new(args));
|
||||
let ret_ptr = c_fun(raw_ptr);
|
||||
let data = *Box::from_raw(ret_ptr);
|
||||
Ok(data)
|
||||
})
|
||||
}
|
||||
}
|
||||
pub extern "C" fn thread(name: *const c_char, c_fun: CFun) {
|
||||
unsafe {
|
||||
let c_str = CStr::from_ptr(name);
|
||||
let string = c_str.to_string_lossy();
|
||||
super::thread(string.as_str(), move |args| {
|
||||
let raw_ptr = Box::into_raw(Box::new(args));
|
||||
let ret_ptr = c_fun(raw_ptr);
|
||||
let data = *Box::from_raw(ret_ptr);
|
||||
Ok(data)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user