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

ambee/giterated

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

More progress :)

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨92c3f32

⁨plugins/example-plugin/src/lib.rs⁩ - ⁨2891⁩ 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 initialize() -> PluginState {
45 tracing_subscriber::fmt()
46 .pretty()
47 .with_thread_names(true)
48 .with_max_level(Level::TRACE)
49 .init();
50
51 println!("Building runtime");
52 let runtime = tokio::runtime::Builder::new_multi_thread()
53 .enable_all()
54 .build()
55 .unwrap();
56
57 let _guard = runtime.enter();
58
59 forget(_guard);
60
61 PluginState {
62 inner: Box::into_raw(Box::new(())),
63 }
64 }
65
66 #[no_mangle]
67 pub extern "C" fn initialize_registration(
68 state: *mut PluginInitializationState,
69 ) -> *mut PluginInitializationState {
70 let _guard: tracing::span::EnteredSpan = trace_span!("initialize_registration").entered();
71 let init_vtable = INIT_VTABLE.get().unwrap();
72 let mut builder = PluginStackBuilder::new((), state, init_vtable);
73
74 builder.object::<Instance>().object::<User>();
75 builder.operation(handler);
76 builder.value(value_getter);
77 builder.setting_getter(setting_getter);
78
79 state
80 }
81
82 async fn handler(
83 _state: (),
84 object: Instance,
85 operation: ObjectRequest,
86 ) -> Result<ObjectResponse, OperationError<ObjectRequestError>> {
87 info!("handling operation!");
88
89 info!("Try get object {} from instance {}", operation.0, object);
90
91 Ok(ObjectResponse(operation.0))
92 }
93
94 async fn value_getter(
95 _state: (),
96 _object: User,
97 ) -> Result<DisplayName, OperationError<anyhow::Error>> {
98 info!("OwO, value gotten!");
99
100 Ok(DisplayName(String::from("heya!")))
101 }
102
103 async fn setting_getter(
104 _state: (),
105 _object: User,
106 ) -> Result<DisplayName, OperationError<anyhow::Error>> {
107 info!("OwO, setting gotten!");
108
109 Ok(DisplayName(String::from("heya! (but from a setting)")))
110 }
111