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