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

ambee/giterated

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

Wow!

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨6530104

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