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

ambee/giterated

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

Fucking whatever there you go

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨57c2ca5

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