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