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

ambee/giterated

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

insanity

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨6ea28ab

⁨giterated-abi/src/callback/setting.rs⁩ - ⁨4935⁩ bytes
Raw
1 use std::future::Future;
2
3 use giterated_models::{error::OperationError, object::GiteratedObject};
4
5 use crate::{
6 state::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> {
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 mut 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 pub trait IntoPluginSettingSetter<O, OS> {
82 unsafe extern "C" fn set_setting(
83 callback_ptr: CallbackPtr<SettingGetterCallback>,
84 state: FfiValueMut<State>,
85 object: FfiValueRef<Object>,
86 setting: Setting,
87 ) -> FfiFuture<Result<(), ()>>;
88
89 fn callback_ptr(&self) -> CallbackPtr<SettingGetterCallback> {
90 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
91 todo!()
92 }
93 }
94
95 impl<F, O, OS, Fut> IntoPluginSettingSetter<O, OS> for F
96 where
97 Fut: Future<Output = Result<(), OperationError<anyhow::Error>>>,
98 O: GiteratedObject,
99 OS: giterated_models::settings::Setting,
100 F: Fn(O, OS) -> Fut,
101 {
102 unsafe extern "C" fn set_setting(
103 callback: CallbackPtr<SettingGetterCallback>,
104 state: FfiValueMut<State>,
105 mut object: FfiValueRef<Object>,
106 _setting: Setting,
107 ) -> FfiFuture<Result<(), ()>> {
108 // let _guard = trace_span!(
109 // "get_setting handler",
110 // object = O::object_name(),
111 // setting = OS::name()
112 // )
113 // .entered();
114 // let _state = unsafe { state.transmute_ref::<S>() };
115
116 // let _object = unsafe { object.transmute_owned::<O>() };
117
118 // // Cast the callback ptr to ourselves
119 // let callback: *const F = std::mem::transmute(callback.0);
120 // let _callback = callback.as_ref().unwrap();
121
122 // let result = callback(state.clone(), *object);
123
124 // match result {
125 // Ok(setting) => Ok(NewAnySetting::new(setting)),
126 // Err(_) => todo!(),
127 // }
128 todo!()
129 }
130 }
131
132 pub struct SettingChangeCallback(FfiValueUntyped);
133
134 impl Callback for SettingChangeCallback {
135 type CallbackFunc = unsafe extern "C" fn(
136 state: FfiValueMut<State>,
137 object: FfiValueRef<Object>,
138 setting_name: &str,
139 new_setting: Setting,
140 );
141 }
142
143 pub trait IntoSettingChangeCallback<S, O> {
144 unsafe extern "C" fn setting_changed(
145 state: FfiValueMut<State>,
146 object: FfiValueRef<Object>,
147 setting_name: &str,
148 new_setting: Setting,
149 );
150 }
151
152 impl<F, S, O> IntoSettingChangeCallback<S, O> for F {
153 unsafe extern "C" fn setting_changed(
154 state: FfiValueMut<State>,
155 _object: FfiValueRef<Object>,
156 _setting_name: &str,
157 _new_setting: Setting,
158 ) {
159 todo!()
160 }
161 }
162