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

ambee/giterated

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

Spinning

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨1788060

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