use std::future::Future; use giterated_models::{ error::OperationError, object::GiteratedObject, settings::{AnySetting, Setting}, }; use crate::{new_stack::PluginState, AnyObject, NewAnySetting}; use super::CallbackPtr; #[derive(Clone, Copy)] pub struct SettingGetterCallback { pub callback_ptr: CallbackPtr, pub func: unsafe extern "C" fn( CallbackPtr, &PluginState, object: AnyObject, ) -> Result, } impl SettingGetterCallback { pub fn new>(callback: T) -> Self { Self { func: T::get_setting, callback_ptr: callback.callback_ptr(), } } } pub trait IntoPluginSettingGetter { unsafe extern "C" fn get_setting( callback_ptr: CallbackPtr, state: &PluginState, object: AnyObject, ) -> Result; fn callback_ptr(&self) -> CallbackPtr { unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } } } impl IntoPluginSettingGetter for F where Fut: Future>>, S: Clone, O: GiteratedObject, OS: Setting, F: Fn(S, O) -> Fut, { unsafe extern "C" fn get_setting( callback: CallbackPtr, state: &PluginState, mut object: AnyObject, ) -> Result { let _guard = trace_span!( "get_setting handler", object = O::object_name(), setting = OS::name() ) .entered(); let state = unsafe { state.transmute_ref::() }; let object = unsafe { object.transmute_owned::() }; // Cast the callback ptr to ourselves let callback: *const F = std::mem::transmute(callback.0); let callback = callback.as_ref().unwrap(); // let result = callback(state.clone(), *object); // match result { // Ok(setting) => Ok(NewAnySetting::new(setting)), // Err(_) => todo!(), // } todo!() } } pub trait IntoPluginSettingSetter { unsafe extern "C" fn set_setting( callback_ptr: CallbackPtr, state: &PluginState, object: AnyObject, setting: AnySetting, ) -> Result<(), ()>; fn callback_ptr(&self) -> CallbackPtr { unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } } } impl IntoPluginSettingSetter for F where Fut: Future>>, S: Clone, O: GiteratedObject, OS: Setting, F: Fn(S, O, OS) -> Fut, { unsafe extern "C" fn set_setting( callback: CallbackPtr, state: &PluginState, mut object: AnyObject, setting: AnySetting, ) -> Result<(), ()> { let _guard = trace_span!( "get_setting handler", object = O::object_name(), setting = OS::name() ) .entered(); let state = unsafe { state.transmute_ref::() }; let object = unsafe { object.transmute_owned::() }; // Cast the callback ptr to ourselves let callback: *const F = std::mem::transmute(callback.0); let callback = callback.as_ref().unwrap(); // let result = callback(state.clone(), *object); // match result { // Ok(setting) => Ok(NewAnySetting::new(setting)), // Err(_) => todo!(), // } todo!() } } pub struct SettingChangeCallback { func: unsafe extern "C" fn( &PluginState, object: AnyObject, setting_name: &str, new_setting: NewAnySetting, ), } pub trait IntoSettingChangeCallback { unsafe extern "C" fn setting_changed( state: &PluginState, object: AnyObject, setting_name: &str, new_setting: NewAnySetting, ); } impl IntoSettingChangeCallback for F { unsafe extern "C" fn setting_changed( state: &PluginState, object: AnyObject, setting_name: &str, new_setting: NewAnySetting, ) { todo!() } } impl SettingChangeCallback { pub fn new>() -> Self { Self { func: T::setting_changed, } } }