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

ambee/giterated

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

Spinning

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨1788060

⁨giterated-plugin/src/local.rs⁩ - ⁨8784⁩ bytes
Raw
1 use giterated_abi::{
2 callback::{
3 operation::IntoPluginOperationHandler,
4 setting::{IntoPluginSettingGetter, IntoPluginSettingSetter},
5 value::IntoPluginValueGetter,
6 Callback, CallbackPtr,
7 },
8 result::FfiError,
9 state::{State, StateUUID},
10 value_ex::FfiValueUntyped,
11 vtable::{
12 operation::IntoOperationVTable, plugin_initialization::InitializationVTable,
13 IntoObjectVTable, IntoSettingVTable, IntoValueVTable, Object, VTable,
14 },
15 FfiValueMut, FfiValueRef,
16 };
17 use giterated_models::{
18 object::GiteratedObject, operation::GiteratedOperation, settings::Setting,
19 value::GiteratedObjectValue,
20 };
21
22 use tracing::trace_span;
23
24 pub struct PluginStackBuilder {
25 init_state: FfiValueUntyped,
26 vtable: &'static VTable<InitializationVTable>,
27 }
28
29 impl Default for PluginStackBuilder {
30 fn default() -> Self {
31 Self::new()
32 }
33 }
34
35 impl PluginStackBuilder {
36 pub fn new(// state: FfiValueUntyped,
37 // vtable: &'static VTable<InitializationVTable>,
38 ) -> PluginStackBuilder {
39 todo!()
40 // PluginStackBuilder {
41 // init_state: state,
42 // vtable
43 // }
44 }
45
46 pub fn insert_state<S: StateUUID>(&mut self, state: S) -> &mut Self {
47 todo!()
48 }
49
50 pub fn object<O: IntoObjectVTable + GiteratedObject>(&mut self) -> &mut Self {
51 let _guard = trace_span!("register object").entered();
52
53 let func = self.vtable.register_object;
54
55 let mut init_state = self.init_state.pin();
56
57 unsafe {
58 func(
59 unsafe { init_state.grant_mut() },
60 O::object_name(),
61 O::VTABLE,
62 )
63 };
64
65 self
66 }
67
68 pub fn register_operation<O, D>(&mut self) -> &mut Self
69 where
70 D: IntoOperationVTable<O> + GiteratedOperation<O>,
71 O: GiteratedObject,
72 {
73 let _guard = trace_span!("register operation").entered();
74
75 let mut init_state = self.init_state.pin();
76
77 unsafe {
78 (self.vtable.register_operation)(
79 unsafe { init_state.grant_mut() },
80 O::object_name(),
81 D::operation_name(),
82 <D as IntoOperationVTable<O>>::VTABLE,
83 )
84 }
85
86 self
87 }
88
89 pub fn object_setting<O, OS, HG, HS, A1, A2>(&mut self, _get: HG, _set: HS) -> &mut Self
90 where
91 O: GiteratedObject,
92 OS: IntoSettingVTable + Setting,
93 HG: IntoPluginSettingGetter<O, OS, A1>,
94 HS: IntoPluginSettingSetter<O, OS, A2>,
95 {
96 let _guard = trace_span!("register setting").entered();
97
98 let mut init_state = self.init_state.pin();
99
100 unsafe {
101 (self.vtable.register_setting)(
102 unsafe { init_state.grant_mut() },
103 O::object_name(),
104 OS::name(),
105 OS::VTABLE,
106 )
107 }
108
109 self
110 }
111
112 pub fn object_user_setting<O, OS>(&mut self) -> &mut Self
113 where
114 O: GiteratedObject,
115 OS: IntoSettingVTable + Setting,
116 {
117 let _guard = trace_span!("register setting").entered();
118
119 let mut init_state = self.init_state.pin();
120
121 unsafe {
122 (self.vtable.register_setting)(
123 unsafe { init_state.grant_mut() },
124 O::object_name(),
125 OS::name(),
126 OS::VTABLE,
127 )
128 }
129
130 self
131 }
132
133 pub fn value<O, V, T, A>(&mut self, handler: T) -> &mut Self
134 where
135 O: GiteratedObject,
136 V: IntoValueVTable + GiteratedObjectValue<Object = O>,
137 T: IntoPluginValueGetter<O, V, A>,
138 {
139 let _guard = trace_span!("register value").entered();
140
141 let mut init_state = self.init_state.pin();
142
143 unsafe {
144 (self.vtable.register_value)(
145 unsafe { init_state.grant_mut() },
146 O::object_name(),
147 V::value_name(),
148 V::VTABLE,
149 )
150 }
151
152 // unsafe {
153 // (self.vtable.value_getter)(
154 // self.init_state,
155 // O::object_name(),
156 // V::value_name(),
157 // ValueGetterCallback::new::<S, O, V, T>(handler),
158 // )
159 // }
160
161 self
162 }
163
164 pub fn operation<
165 A,
166 O: GiteratedObject + IntoObjectVTable,
167 D: IntoOperationVTable<O> + GiteratedOperation<O>,
168 T: IntoPluginOperationHandler<O, D, A>,
169 >(
170 &mut self,
171 handler: T,
172 ) -> &mut Self {
173 let _guard = trace_span!("register operation handler").entered();
174
175 // unsafe {
176 // (self.vtable.operation_handler)(
177 // self.init_state,
178 // O::object_name(),
179 // D::operation_name(),
180 // OperationHandlerCallback::new::<S, O, D, A, T>(handler),
181 // )
182 // }
183
184 // TODO: Yikes?
185 self.object::<O>();
186
187 self.register_operation::<O, D>();
188
189 self
190 }
191
192 // pub fn value_getter<O, V, T>(&mut self, handler: T) -> &mut Self
193 // where
194 // O: GiteratedObject + IntoObjectVTable,
195 // V: GiteratedObjectValue<Object = O> + IntoValueVTable<O>,
196 // T: IntoPluginValueGetter<S, O, V>,
197 // {
198 // let _guard = trace_span!("register value_getter handler").entered();
199
200 // unsafe {
201 // (self.vtable.value_getter)(
202 // self.init_state,
203 // O::object_name(),
204 // V::value_name(),
205 // ValueGetterCallback::new::<S, O, V, T>(handler),
206 // )
207 // }
208
209 // // TODO: Yikes?
210 // self.object::<O>();
211 // self.value::<O, V>();
212
213 // self
214 // }
215
216 pub fn setting_getter<O, OS, T, A>(&mut self, handler: T) -> &mut Self
217 where
218 O: GiteratedObject + IntoObjectVTable,
219 OS: Setting + IntoSettingVTable,
220 T: IntoPluginSettingGetter<O, OS, A>,
221 {
222 let _guard = trace_span!("register setting_getter handler").entered();
223
224 // unsafe {
225 // (self.vtable.setting_getter)(
226 // self.init_state,
227 // O::object_name(),
228 // OS::name(),
229 // SettingGetterCallback::new::<S, O, OS, T>(handler),
230 // )
231 // }
232
233 self.object::<O>();
234
235 let mut init_state = self.init_state.pin();
236
237 unsafe {
238 (self.vtable.register_setting)(
239 unsafe { init_state.grant_mut() },
240 O::object_name(),
241 OS::name(),
242 OS::VTABLE,
243 )
244 };
245
246 self
247 }
248 }
249
250 pub trait ValueSettingExt {
251 fn value_setting<O, VS, HG, HS, A>(&mut self, get: HG, set: HS) -> &mut Self
252 where
253 O: GiteratedObject + IntoObjectVTable + 'static,
254 VS: GiteratedObjectValue<Object = O> + IntoValueVTable + Setting + IntoSettingVTable,
255 HG: IntoPluginSettingGetter<O, VS, A>,
256 HS: IntoPluginSettingSetter<O, VS, A>;
257 }
258
259 impl ValueSettingExt for PluginStackBuilder {
260 fn value_setting<O, VS, HG, HS, A>(&mut self, _get: HG, _set: HS) -> &mut Self
261 where
262 O: GiteratedObject + IntoObjectVTable + 'static,
263 VS: GiteratedObjectValue<Object = O> + IntoValueVTable + Setting + IntoSettingVTable,
264 HG: IntoPluginSettingGetter<O, VS, A>,
265 HS: IntoPluginSettingSetter<O, VS, A>,
266 {
267 self
268 }
269 }
270
271 #[derive(Clone, Copy)]
272 pub struct ValueSettingGetterCallback;
273
274 impl Callback for ValueSettingGetterCallback {
275 type CallbackFunc = unsafe extern "C" fn(
276 callback: CallbackPtr<ValueSettingGetterCallback>,
277 state: FfiValueMut<State>,
278 object: FfiValueRef<Object>,
279 ) -> Result<FfiValueUntyped, FfiError>;
280 }
281
282 pub trait ValueSettingGetter<O, V, A> {
283 unsafe extern "C" fn get_value(
284 callback: CallbackPtr<ValueSettingGetterCallback>,
285 state: FfiValueMut<State>,
286 object: FfiValueRef<Object>,
287 ) -> Result<FfiValueUntyped, FfiError>;
288
289 fn callback_ptr(&self) -> CallbackPtr<ValueSettingGetterCallback>;
290 }
291
292 impl<O, VS, HG, A> ValueSettingGetter<O, VS, A> for HG
293 where
294 O: GiteratedObject,
295 VS: GiteratedObjectValue<Object = O>,
296 HG: IntoPluginSettingGetter<O, VS, A>,
297 {
298 unsafe extern "C" fn get_value(
299 _callback: CallbackPtr<ValueSettingGetterCallback>,
300 _state: FfiValueMut<State>,
301 _object: FfiValueRef<Object>,
302 ) -> Result<FfiValueUntyped, FfiError> {
303 // let result = HG::get_setting(callback, state, object)?;
304
305 // let setting = *result.transmute_owned::<VS>();
306
307 todo!();
308
309 // Ok(NewAnyValue::new(setting))
310 }
311
312 fn callback_ptr(&self) -> CallbackPtr<ValueSettingGetterCallback> {
313 todo!()
314 }
315 }
316