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/src/callback/setting.rs⁩ - ⁨5061⁩ bytes
Raw
1 use std::future::Future;
2
3 use giterated_models::{
4 error::OperationError,
5 object::GiteratedObject,
6 settings::{AnySetting, Setting},
7 };
8
9 use crate::{
10 future::{RuntimeFuture, RuntimeFuturesExt},
11 new_stack::PluginState,
12 AnyObject, NewAnySetting,
13 };
14
15 use super::{CallbackPtr, RuntimeState};
16
17 #[derive(Clone, Copy)]
18 pub struct SettingGetterCallback {
19 pub callback_ptr: CallbackPtr,
20 pub func: unsafe extern "C" fn(
21 CallbackPtr,
22 runtime_state: &RuntimeState,
23 &PluginState,
24 object: AnyObject,
25 ) -> RuntimeFuture<Result<NewAnySetting, ()>>,
26 }
27
28 impl SettingGetterCallback {
29 pub fn new<S, O, OS, T: IntoPluginSettingGetter<S, O, OS>>(callback: T) -> Self {
30 Self {
31 func: T::get_setting,
32 callback_ptr: callback.callback_ptr(),
33 }
34 }
35 }
36
37 pub trait IntoPluginSettingGetter<S, O, OS> {
38 unsafe extern "C" fn get_setting(
39 callback_ptr: CallbackPtr,
40 runtime_state: &RuntimeState,
41 state: &PluginState,
42 object: AnyObject,
43 ) -> RuntimeFuture<Result<NewAnySetting, ()>>;
44
45 fn callback_ptr(&self) -> CallbackPtr {
46 unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
47 }
48 }
49
50 impl<F, S, O, OS, Fut> IntoPluginSettingGetter<S, O, OS> for F
51 where
52 Fut: Future<Output = Result<OS, OperationError<anyhow::Error>>> + Send + Sync + 'static,
53 S: Clone + Send + Sync + 'static,
54 O: GiteratedObject + Send + Sync + 'static,
55 OS: Setting + Send + Sync + 'static,
56 F: Fn(S, O) -> Fut + Send + Sync + 'static,
57 {
58 unsafe extern "C" fn get_setting(
59 callback: CallbackPtr,
60 runtime_state: &RuntimeState,
61 state: &PluginState,
62 mut object: AnyObject,
63 ) -> RuntimeFuture<Result<NewAnySetting, ()>> {
64 let _guard = trace_span!(
65 "get_setting handler",
66 object = O::object_name(),
67 setting = OS::name()
68 )
69 .entered();
70 let state = unsafe { state.transmute_ref::<S>() };
71
72 let object = unsafe { object.transmute_owned::<O>() };
73
74 // Cast the callback ptr to ourselves
75 let callback: *const F = std::mem::transmute(callback.0);
76 let callback = callback.as_ref().unwrap();
77
78 let state = state.clone();
79 runtime_state.spawn_future(async move {
80 let result = callback(state, *object).await;
81
82 match result {
83 Ok(success) => unsafe { Ok(NewAnySetting::new(success)) },
84 Err(err) => match err {
85 OperationError::Operation(_) => todo!(),
86 OperationError::Internal(_) => todo!(),
87 OperationError::Unhandled => todo!(),
88 },
89 }
90 })
91 }
92 }
93
94 pub trait IntoPluginSettingSetter<S, O, OS> {
95 unsafe extern "C" fn set_setting(
96 callback_ptr: CallbackPtr,
97 state: &PluginState,
98 object: AnyObject,
99 setting: AnySetting,
100 ) -> RuntimeFuture<Result<(), ()>>;
101
102 fn callback_ptr(&self) -> CallbackPtr {
103 unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
104 }
105 }
106
107 impl<F, S, O, OS, Fut> IntoPluginSettingSetter<S, O, OS> for F
108 where
109 Fut: Future<Output = Result<(), OperationError<anyhow::Error>>>,
110 S: Clone,
111 O: GiteratedObject,
112 OS: Setting,
113 F: Fn(S, O, OS) -> Fut,
114 {
115 unsafe extern "C" fn set_setting(
116 callback: CallbackPtr,
117 state: &PluginState,
118 mut object: AnyObject,
119 _setting: AnySetting,
120 ) -> RuntimeFuture<Result<(), ()>> {
121 let _guard = trace_span!(
122 "get_setting handler",
123 object = O::object_name(),
124 setting = OS::name()
125 )
126 .entered();
127 let _state = unsafe { state.transmute_ref::<S>() };
128
129 let _object = unsafe { object.transmute_owned::<O>() };
130
131 // Cast the callback ptr to ourselves
132 let callback: *const F = std::mem::transmute(callback.0);
133 let _callback = callback.as_ref().unwrap();
134
135 // let result = callback(state.clone(), *object);
136
137 // match result {
138 // Ok(setting) => Ok(NewAnySetting::new(setting)),
139 // Err(_) => todo!(),
140 // }
141 todo!()
142 }
143 }
144
145 pub struct SettingChangeCallback {
146 func: unsafe extern "C" fn(
147 &PluginState,
148 object: AnyObject,
149 setting_name: &str,
150 new_setting: NewAnySetting,
151 ),
152 }
153
154 pub trait IntoSettingChangeCallback<S, O> {
155 unsafe extern "C" fn setting_changed(
156 state: &PluginState,
157 object: AnyObject,
158 setting_name: &str,
159 new_setting: NewAnySetting,
160 );
161 }
162
163 impl<F, S, O> IntoSettingChangeCallback<S, O> for F {
164 unsafe extern "C" fn setting_changed(
165 _state: &PluginState,
166 _object: AnyObject,
167 _setting_name: &str,
168 _new_setting: NewAnySetting,
169 ) {
170 todo!()
171 }
172 }
173
174 impl SettingChangeCallback {
175 pub fn new<S, O, T: IntoSettingChangeCallback<S, O>>() -> Self {
176 Self {
177 func: T::setting_changed,
178 }
179 }
180 }
181