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