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

ambee/giterated

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

More progress :)

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨92c3f32

⁨giterated-plugin/giterated-plugin-sys/src/lib.rs⁩ - ⁨7746⁩ bytes
Raw
1 mod local_runtime;
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 ) -> PluginStackBuilder<'init, S> {
35 PluginStackBuilder {
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 A,
142 O: GiteratedObject + IntoObjectVTable,
143 D: IntoOperationVTable<O> + GiteratedOperation<O>,
144 T: IntoPluginOperationHandler<S, O, D, A>,
145 >(
146 &mut self,
147 handler: T,
148 ) -> &mut Self {
149 let _guard = trace_span!("register operation handler").entered();
150
151 unsafe {
152 (self.vtable.operation_handler)(
153 self.init_state,
154 O::object_name(),
155 D::operation_name(),
156 OperationHandlerCallback::new::<S, O, D, A, T>(handler),
157 )
158 }
159
160 // TODO: Yikes?
161 self.object::<O>();
162
163 self.register_operation::<O, D>();
164
165 self
166 }
167
168 // pub fn value_getter<O, V, T>(&mut self, handler: T) -> &mut Self
169 // where
170 // O: GiteratedObject + IntoObjectVTable,
171 // V: GiteratedObjectValue<Object = O> + IntoValueVTable<O>,
172 // T: IntoPluginValueGetter<S, O, V>,
173 // {
174 // let _guard = trace_span!("register value_getter handler").entered();
175
176 // unsafe {
177 // (self.vtable.value_getter)(
178 // self.init_state,
179 // O::object_name(),
180 // V::value_name(),
181 // ValueGetterCallback::new::<S, O, V, T>(handler),
182 // )
183 // }
184
185 // // TODO: Yikes?
186 // self.object::<O>();
187 // self.value::<O, V>();
188
189 // self
190 // }
191
192 pub fn setting_getter<O, OS, T>(&mut self, handler: T) -> &mut Self
193 where
194 O: GiteratedObject + IntoObjectVTable,
195 OS: Setting + IntoSettingVTable,
196 T: IntoPluginSettingGetter<S, O, OS>,
197 {
198 let _guard = trace_span!("register setting_getter handler").entered();
199
200 unsafe {
201 (self.vtable.setting_getter)(
202 self.init_state,
203 O::object_name(),
204 OS::name(),
205 SettingGetterCallback::new::<S, O, OS, T>(handler),
206 )
207 }
208
209 self.object::<O>();
210
211 unsafe {
212 (self.vtable.register_setting)(
213 self.init_state,
214 O::object_name(),
215 OS::name(),
216 SettingVtable::new::<OS>(),
217 )
218 };
219
220 self
221 }
222 }
223
224 pub trait ValueSettingExt<PS> {
225 fn value_setting<O, VS, HG, HS>(&mut self, get: HG, set: HS) -> &mut Self
226 where
227 O: GiteratedObject + IntoObjectVTable + 'static,
228 VS: GiteratedObjectValue<Object = O> + IntoValueVTable<O> + Setting + IntoSettingVTable,
229 HG: IntoPluginSettingGetter<PS, O, VS>,
230 HS: IntoPluginSettingSetter<PS, O, VS>;
231 }
232
233 impl<PS> ValueSettingExt<PS> for PluginStackBuilder<'_, PS> {
234 fn value_setting<O, VS, HG, HS>(&mut self, _get: HG, _set: HS) -> &mut Self
235 where
236 O: GiteratedObject + IntoObjectVTable + 'static,
237 VS: GiteratedObjectValue<Object = O> + IntoValueVTable<O> + Setting + IntoSettingVTable,
238 HG: IntoPluginSettingGetter<PS, O, VS>,
239 HS: IntoPluginSettingSetter<PS, O, VS>,
240 {
241 self
242 }
243 }
244
245 pub trait ValueSettingGetter<S, O, V> {
246 unsafe extern "C" fn get_value(
247 callback: CallbackPtr,
248 state: &PluginState,
249 object: AnyObject,
250 ) -> Result<NewAnyValue, ()>;
251
252 fn callback_ptr(&self) -> CallbackPtr;
253 }
254
255 impl<S, O, VS, HG> ValueSettingGetter<S, O, VS> for HG
256 where
257 O: GiteratedObject,
258 VS: GiteratedObjectValue<Object = O>,
259 HG: IntoPluginSettingGetter<S, O, VS>,
260 {
261 unsafe extern "C" fn get_value(
262 _callback: CallbackPtr,
263 _state: &PluginState,
264 _object: AnyObject,
265 ) -> Result<NewAnyValue, ()> {
266 // let result = HG::get_setting(callback, state, object)?;
267
268 // let setting = *result.transmute_owned::<VS>();
269
270 todo!();
271
272 // Ok(NewAnyValue::new(setting))
273 }
274
275 fn callback_ptr(&self) -> CallbackPtr {
276 self.callback_ptr()
277 }
278 }
279