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

ambee/giterated

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

Before

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨e432306

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