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

ambee/giterated

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

`[feature/plugins]` Some plugin work?

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨10a447b

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