JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Before

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨e432306

⁨giterated-plugin/src/callback/setting.rs⁩ - ⁨5555⁩ bytes
Raw
1 use std::future::Future;
2
3 use giterated_abi::{
4 callback::{Callback, CallbackPtr},
5 value_ex::FfiValueUntyped,
6 vtable::{Object, Setting},
7 FfiValueMut, FfiValueRef,
8 };
9 use giterated_models::{error::OperationError, object::GiteratedObject};
10
11 use crate::{
12 future::{RuntimeFuture, RuntimeFuturesExt},
13 new_stack::PluginState,
14 state::State,
15 };
16
17 use super::RuntimeState;
18
19 pub struct SettingGetterCallback(FfiValueUntyped);
20
21 impl Callback for SettingGetterCallback {
22 type CallbackFunc = unsafe extern "C" fn(
23 CallbackPtr<SettingGetterCallback>,
24 state: FfiValueMut<State>,
25 object: FfiValueRef<Object>,
26 ) -> RuntimeFuture<Result<Setting, ()>>;
27 }
28
29 impl SettingGetterCallback {
30 // pub fn new<S, O, OS, T: IntoPluginSettingGetter<S, O, OS>>(callback: T) -> Self {
31 // Self {
32 // func: T::get_setting,
33 // callback_ptr: callback.callback_ptr(),
34 // }
35 // }
36 }
37
38 pub trait IntoPluginSettingGetter<S, O, OS> {
39 unsafe extern "C" fn get_setting(
40 callback_ptr: CallbackPtr<SettingGetterCallback>,
41 state: FfiValueMut<State>,
42 object: FfiValueRef<Object>,
43 ) -> RuntimeFuture<Result<Setting, ()>>;
44
45 fn callback_ptr(&self) -> CallbackPtr<SettingGetterCallback> {
46 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
47 todo!()
48 }
49 }
50
51 impl<F, S, O, OS, Fut> IntoPluginSettingGetter<S, O, OS> for F
52 where
53 Fut: Future<Output = Result<OS, OperationError<anyhow::Error>>> + Send + Sync + 'static,
54 S: Clone + Send + Sync + 'static,
55 O: GiteratedObject + Send + Sync + 'static,
56 OS: giterated_models::settings::Setting + Send + Sync + 'static,
57 F: Fn(S, O) -> Fut + Send + Sync + 'static,
58 {
59 unsafe extern "C" fn get_setting(
60 callback: CallbackPtr<SettingGetterCallback>,
61 state: FfiValueMut<State>,
62 mut object: FfiValueRef<Object>,
63 ) -> RuntimeFuture<Result<Setting, ()>> {
64 // let _guard = trace_span!(
65 // "get_setting handler",
66 // object = O::object_name(),
67 // setting = OS::name()
68 // )
69 // .entered();
70 // let state = unsafe { state.transmute_ref::<S>() };
71
72 // let object = unsafe { object.transmute_owned::<O>() };
73
74 // // Cast the callback ptr to ourselves
75 // let callback: *const F = std::mem::transmute(callback.0);
76 // let callback = callback.as_ref().unwrap();
77
78 // let state = state.clone();
79 // runtime_state.spawn_future(async move {
80 // let result = callback(state, *object).await;
81
82 // match result {
83 // Ok(success) => unsafe { Ok(NewAnySetting::new(success)) },
84 // Err(err) => match err {
85 // OperationError::Operation(_) => todo!(),
86 // OperationError::Internal(_) => todo!(),
87 // OperationError::Unhandled => todo!(),
88 // },
89 // }
90
91 todo!()
92 // })
93 }
94 }
95
96 pub trait IntoPluginSettingSetter<S, O, OS> {
97 unsafe extern "C" fn set_setting(
98 callback_ptr: CallbackPtr<SettingGetterCallback>,
99 state: &PluginState,
100 object: FfiValueRef<Object>,
101 setting: Setting,
102 ) -> RuntimeFuture<Result<(), ()>>;
103
104 fn callback_ptr(&self) -> CallbackPtr<SettingGetterCallback> {
105 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
106 todo!()
107 }
108 }
109
110 impl<F, S, O, OS, Fut> IntoPluginSettingSetter<S, O, OS> for F
111 where
112 Fut: Future<Output = Result<(), OperationError<anyhow::Error>>>,
113 S: Clone,
114 O: GiteratedObject,
115 OS: giterated_models::settings::Setting,
116 F: Fn(S, O, OS) -> Fut,
117 {
118 unsafe extern "C" fn set_setting(
119 callback: CallbackPtr<SettingGetterCallback>,
120 state: &PluginState,
121 mut object: FfiValueRef<Object>,
122 _setting: Setting,
123 ) -> RuntimeFuture<Result<(), ()>> {
124 // let _guard = trace_span!(
125 // "get_setting handler",
126 // object = O::object_name(),
127 // setting = OS::name()
128 // )
129 // .entered();
130 // let _state = unsafe { state.transmute_ref::<S>() };
131
132 // let _object = unsafe { object.transmute_owned::<O>() };
133
134 // // Cast the callback ptr to ourselves
135 // let callback: *const F = std::mem::transmute(callback.0);
136 // let _callback = callback.as_ref().unwrap();
137
138 // let result = callback(state.clone(), *object);
139
140 // match result {
141 // Ok(setting) => Ok(NewAnySetting::new(setting)),
142 // Err(_) => todo!(),
143 // }
144 todo!()
145 }
146 }
147
148 pub struct SettingChangeCallback(FfiValueUntyped);
149
150 impl Callback for SettingChangeCallback {
151 type CallbackFunc = unsafe extern "C" fn(
152 &PluginState,
153 object: FfiValueRef<Object>,
154 setting_name: &str,
155 new_setting: Setting,
156 );
157 }
158
159 pub trait IntoSettingChangeCallback<S, O> {
160 unsafe extern "C" fn setting_changed(
161 state: &PluginState,
162 object: FfiValueRef<Object>,
163 setting_name: &str,
164 new_setting: Setting,
165 );
166 }
167
168 impl<F, S, O> IntoSettingChangeCallback<S, O> for F {
169 unsafe extern "C" fn setting_changed(
170 _state: &PluginState,
171 _object: FfiValueRef<Object>,
172 _setting_name: &str,
173 _new_setting: Setting,
174 ) {
175 todo!()
176 }
177 }
178
179 // impl SettingChangeCallback {
180 // pub fn new<S, O, T: IntoSettingChangeCallback<S, O>>() -> Self {
181 // Self {
182 // func: T::setting_changed,
183 // }
184 // }
185 // }
186