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

ambee/giterated

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

Huge refactor to prep for moving the daemon over to the plugin architecture

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨5df753c

⁨giterated-plugins/giterated-plugin/src/callback/value.rs⁩ - ⁨2952⁩ bytes
Raw
1 use std::future::Future;
2
3 use giterated_models::{
4 error::OperationError, object::GiteratedObject, settings::Setting, value::GiteratedObjectValue,
5 };
6
7 use crate::{
8 new_stack::PluginState,
9 vtable::{AnyObject, NewAnyValue},
10 };
11
12 use super::CallbackPtr;
13
14 #[derive(Copy, Clone)]
15 pub struct ValueGetterCallback {
16 pub callback_ptr: CallbackPtr,
17 pub func: unsafe extern "C" fn(
18 CallbackPtr,
19 &PluginState,
20 object: AnyObject,
21 ) -> Result<NewAnyValue, ()>,
22 }
23
24 impl ValueGetterCallback {
25 pub fn new<S, O, V, T: IntoPluginValueGetter<S, O, V>>(handler: T) -> Self {
26 Self {
27 func: T::get_value,
28 callback_ptr: handler.callback_ptr(),
29 }
30 }
31 }
32
33 pub trait IntoPluginValueGetter<S, O, V> {
34 unsafe extern "C" fn get_value(
35 callback: CallbackPtr,
36 state: &PluginState,
37 object: AnyObject,
38 ) -> Result<NewAnyValue, ()>;
39
40 fn callback_ptr(&self) -> CallbackPtr;
41 }
42
43 impl<F, S, O, V, Fut> IntoPluginValueGetter<S, O, V> for F
44 where
45 Fut: Future<Output = Result<V, OperationError<anyhow::Error>>>,
46 S: Clone,
47 O: GiteratedObject,
48 V: GiteratedObjectValue<Object = O>,
49 F: Fn(S, O) -> Fut,
50 {
51 unsafe extern "C" fn get_value(
52 callback: CallbackPtr,
53 state: &PluginState,
54 mut object: AnyObject,
55 ) -> Result<NewAnyValue, ()> {
56 let _guard = trace_span!(
57 "get_value handler",
58 object = O::object_name(),
59 value = V::value_name()
60 )
61 .entered();
62 let state = unsafe { state.transmute_ref::<S>() };
63
64 let object = unsafe { object.transmute_owned::<O>() };
65
66 // Cast the callback ptr to ourselves
67 let callback: *const F = std::mem::transmute(callback.0);
68 let callback = callback.as_ref().unwrap();
69
70 let result = callback(state.clone(), *object);
71
72 // match result {
73 // Ok(value) => Ok(NewAnyValue::new(value)),
74 // Err(_) => todo!(),
75 // }
76
77 todo!()
78 }
79
80 fn callback_ptr(&self) -> CallbackPtr {
81 unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
82 }
83 }
84
85 pub struct ValueChangeCallback {
86 func: unsafe extern "C" fn(
87 &PluginState,
88 object: AnyObject,
89 value_name: &str,
90 new_value: NewAnyValue,
91 ),
92 }
93
94 pub trait IntoValueChangeCallback<S, O> {
95 unsafe extern "C" fn value_changed(
96 state: &PluginState,
97 object: AnyObject,
98 value_name: &str,
99 new_value: NewAnyValue,
100 );
101 }
102
103 impl<F, S, O> IntoValueChangeCallback<S, O> for F {
104 unsafe extern "C" fn value_changed(
105 state: &PluginState,
106 object: AnyObject,
107 value_name: &str,
108 new_value: NewAnyValue,
109 ) {
110 todo!()
111 }
112 }
113
114 impl ValueChangeCallback {
115 pub fn new<S, O, T: IntoValueChangeCallback<S, O>>() -> Self {
116 Self {
117 func: T::value_changed,
118 }
119 }
120 }
121