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

ambee/giterated

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

MOre pre vtable changes

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨9cfa135

⁨plugins/example-plugin/src/lib.rs⁩ - ⁨3095⁩ bytes
Raw
1 use std::{mem::forget, sync::OnceLock};
2
3 use giterated_models::{
4 error::OperationError,
5 instance::Instance,
6 object::{ObjectRequest, ObjectRequestError, ObjectResponse},
7 user::{DisplayName, User},
8 };
9 use giterated_plugin::{
10 handle::PluginInitializationState,
11 new_stack::{FFIPluginMeta, PluginState},
12 vtable::{HostVTable, InitializationVTable},
13 };
14 use giterated_plugin_sys::PluginStackBuilder;
15 use tracing::{info, trace_span, Level};
16
17 static INIT_VTABLE: OnceLock<InitializationVTable> = OnceLock::new();
18
19 #[no_mangle]
20 pub extern "C" fn plugin_meta() -> FFIPluginMeta {
21 const PLUGIN_NAME: &str = "Example Plugin";
22 const PLUGIN_VERSION: &str = "1.0.0";
23
24 FFIPluginMeta {
25 name: PLUGIN_NAME.as_ptr(),
26 name_len: PLUGIN_NAME.len(),
27 version: PLUGIN_VERSION.as_ptr(),
28 version_len: PLUGIN_VERSION.len(),
29 }
30 }
31
32 #[no_mangle]
33 pub extern "C" fn load_host_vtable(_vtable: &HostVTable) {
34 println!("Loading vtable");
35 }
36
37 #[no_mangle]
38 pub extern "C" fn load_initialization_vtable(init_vtable: &InitializationVTable) {
39 INIT_VTABLE.set(*init_vtable).unwrap();
40 println!("Loaded initialization vtable");
41 }
42
43 #[no_mangle]
44 pub extern "C" fn load_type_metadata(metadata: *mut ()) {
45 unsafe { giterated_static_runtime::initialize_type_metadata(metadata) }
46 println!("Initialized type metadata for plugin");
47 }
48
49 #[no_mangle]
50 pub extern "C" fn initialize() -> PluginState {
51 tracing_subscriber::fmt()
52 .pretty()
53 .with_thread_names(true)
54 .with_max_level(Level::TRACE)
55 .init();
56
57 println!("Building runtime");
58 let runtime = tokio::runtime::Builder::new_multi_thread()
59 .enable_all()
60 .build()
61 .unwrap();
62
63 let _guard = runtime.enter();
64
65 forget(_guard);
66
67 PluginState {
68 inner: Box::into_raw(Box::new(())),
69 }
70 }
71
72 #[no_mangle]
73 pub extern "C" fn initialize_registration(
74 state: *mut PluginInitializationState,
75 ) -> *mut PluginInitializationState {
76 let _guard: tracing::span::EnteredSpan = trace_span!("initialize_registration").entered();
77 let init_vtable = INIT_VTABLE.get().unwrap();
78 let mut builder = PluginStackBuilder::new((), state, init_vtable);
79
80 builder.object::<Instance>().object::<User>();
81 builder.operation(handler);
82 builder.value(value_getter);
83 builder.setting_getter(setting_getter);
84
85 state
86 }
87
88 async fn handler(
89 _state: (),
90 object: Instance,
91 operation: ObjectRequest,
92 ) -> Result<ObjectResponse, OperationError<ObjectRequestError>> {
93 info!("handling operation!");
94
95 info!("Try get object {} from instance {}", operation.0, object);
96
97 Ok(ObjectResponse(operation.0))
98 }
99
100 async fn value_getter(
101 _state: (),
102 _object: User,
103 ) -> Result<DisplayName, OperationError<anyhow::Error>> {
104 info!("OwO, value gotten!");
105
106 Ok(DisplayName(String::from("heya!")))
107 }
108
109 async fn setting_getter(
110 _state: (),
111 _object: User,
112 ) -> Result<DisplayName, OperationError<anyhow::Error>> {
113 info!("OwO, setting gotten!");
114
115 Ok(DisplayName(String::from("heya! (but from a setting)")))
116 }
117