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

ambee/giterated

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

So. Much. Work.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨b05f964

⁨giterated-plugins/giterated-plugin-sys/src/lib.rs⁩ - ⁨5840⁩ 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 IntoPluginOperationHandler, IntoPluginSettingGetter, IntoPluginSettingSetter,
10 IntoPluginValueGetter, OperationHandlerCallback, SettingGetterCallback,
11 ValueGetterCallback,
12 },
13 handle::PluginInitializationState,
14 InitializationVTable, IntoObjectVTable, IntoOperationVTable, IntoSettingVTable,
15 IntoValueVTable, ObjectVtable, OperationVTable, SettingVtable, ValueVTable,
16 };
17 use tracing::trace_span;
18
19 pub struct PluginStackBuilder<'init, S> {
20 init_state: *mut PluginInitializationState,
21 vtable: &'init InitializationVTable,
22 state: S,
23 }
24
25 impl<'init, S> PluginStackBuilder<'init, S> {
26 pub fn new(
27 plugin_state: S,
28 state: *mut PluginInitializationState,
29 vtable: &'init InitializationVTable,
30 ) -> Self {
31 Self {
32 init_state: state,
33 vtable,
34 state: plugin_state,
35 }
36 }
37
38 pub fn object<O: IntoObjectVTable + GiteratedObject>(&mut self) -> &mut Self {
39 let _guard = trace_span!("register object").entered();
40
41 let func = self.vtable.register_object;
42
43 unsafe { func(self.init_state, O::object_name(), ObjectVtable::new::<O>()) };
44
45 self
46 }
47
48 pub fn operation<O, D>(&mut self) -> &mut Self
49 where
50 D: IntoOperationVTable<O> + GiteratedOperation<O>,
51 O: GiteratedObject,
52 {
53 let _guard = trace_span!("register operation").entered();
54
55 unsafe {
56 (self.vtable.register_operation)(
57 self.init_state,
58 O::object_name(),
59 D::operation_name(),
60 OperationVTable::new::<O, D>(),
61 )
62 }
63
64 self
65 }
66
67 pub fn object_setting<O, OS, HG, HS>(&mut self, get: HG, set: HS) -> &mut Self
68 where
69 O: GiteratedObject,
70 OS: IntoSettingVTable + Setting,
71 HG: IntoPluginSettingGetter<S, O, OS>,
72 HS: IntoPluginSettingSetter<S, O, OS>,
73 {
74 let _guard = trace_span!("register setting").entered();
75
76 unsafe {
77 (self.vtable.register_setting)(
78 self.init_state,
79 O::object_name(),
80 OS::name(),
81 SettingVtable::new::<OS>(),
82 )
83 }
84
85 self
86 }
87
88 pub fn object_user_setting<O, OS>(&mut self) -> &mut Self
89 where
90 O: GiteratedObject,
91 OS: IntoSettingVTable + Setting,
92 {
93 let _guard = trace_span!("register setting").entered();
94
95 unsafe {
96 (self.vtable.register_setting)(
97 self.init_state,
98 O::object_name(),
99 OS::name(),
100 SettingVtable::new::<OS>(),
101 )
102 }
103
104 self
105 }
106
107 pub fn value<O, V, T>(&mut self, handler: T) -> &mut Self
108 where
109 O: GiteratedObject,
110 V: IntoValueVTable<O> + GiteratedObjectValue<Object = O>,
111 T: IntoPluginValueGetter<S, O, V>,
112 {
113 let _guard = trace_span!("register value").entered();
114
115 unsafe {
116 (self.vtable.register_value)(
117 self.init_state,
118 O::object_name(),
119 V::value_name(),
120 ValueVTable::new::<O, V>(),
121 )
122 }
123
124 unsafe {
125 (self.vtable.value_getter)(
126 self.init_state,
127 O::object_name(),
128 V::value_name(),
129 ValueGetterCallback::new::<S, O, V, T>(handler),
130 )
131 }
132
133 self
134 }
135
136 pub fn operation_handler<
137 DS,
138 DF,
139 O: GiteratedObject + IntoObjectVTable,
140 D: IntoOperationVTable<O> + GiteratedOperation<O>,
141 T: IntoPluginOperationHandler<S, O, D, DS, DF>,
142 >(
143 &mut self,
144 handler: T,
145 ) -> &mut Self {
146 let _guard = trace_span!("register operation handler").entered();
147
148 unsafe {
149 (self.vtable.operation_handler)(
150 self.init_state,
151 O::object_name(),
152 D::operation_name(),
153 OperationHandlerCallback::new::<S, O, D, DS, DF, T>(handler),
154 )
155 }
156
157 // TODO: Yikes?
158 self.object::<O>();
159
160 self.operation::<O, D>();
161
162 self
163 }
164
165 // pub fn value_getter<O, V, T>(&mut self, handler: T) -> &mut Self
166 // where
167 // O: GiteratedObject + IntoObjectVTable,
168 // V: GiteratedObjectValue<Object = O> + IntoValueVTable<O>,
169 // T: IntoPluginValueGetter<S, O, V>,
170 // {
171 // let _guard = trace_span!("register value_getter handler").entered();
172
173 // unsafe {
174 // (self.vtable.value_getter)(
175 // self.init_state,
176 // O::object_name(),
177 // V::value_name(),
178 // ValueGetterCallback::new::<S, O, V, T>(handler),
179 // )
180 // }
181
182 // // TODO: Yikes?
183 // self.object::<O>();
184 // self.value::<O, V>();
185
186 // self
187 // }
188
189 // pub fn setting_getter<O, OS, T>(&mut self, handler: T) -> &mut Self
190 // where
191 // O: GiteratedObject + IntoObjectVTable,
192 // OS: Setting + IntoSettingVTable,
193 // T: IntoPluginSettingGetter<S, O, OS>,
194 // {
195 // let _guard = trace_span!("register setting_getter handler").entered();
196
197 // unsafe {
198 // (self.vtable.setting_getter)(
199 // self.init_state,
200 // O::object_name(),
201 // OS::name(),
202 // SettingGetterCallback::new::<S, O, OS, T>(handler),
203 // )
204 // }
205
206 // self.object::<O>();
207 // // self.setting::<O, OS>();
208
209 // self
210 // }
211 }
212