gravel_ffi_macros/
declare.rs

1use syn::{File, Ident, Type, parse_quote};
2
3pub fn provider(plugin_name: &str, impl_type: &Type) -> File {
4	let plugin = plugin(plugin_name, parse_quote! { with_provider });
5
6	parse_quote! {
7		#plugin
8
9		#[::abi_stable::sabi_extern_fn]
10		pub fn __gravel_plugin_get(config: &::gravel_ffi::PluginConfigAdapter<'_>) -> ::gravel_ffi::BoxDynProvider {
11			let value = <#impl_type as ::gravel_ffi::Provider>::new(config);
12			::gravel_ffi::BoxDynProvider::from_value(value, ::abi_stable::sabi_trait::TD_Opaque)
13		}
14	}
15}
16
17pub fn frontend(plugin_name: &str, impl_type: &Type) -> File {
18	let plugin = plugin(plugin_name, parse_quote! { with_frontend });
19
20	parse_quote! {
21		#plugin
22
23		#[::abi_stable::sabi_extern_fn]
24		pub fn __gravel_plugin_get(engine: ::gravel_ffi::BoxDynFrontendContext, config: &::gravel_ffi::PluginConfigAdapter<'_>) -> ::gravel_ffi::BoxDynFrontend {
25			let value = <#impl_type as ::gravel_ffi::Frontend>::new(engine, config);
26			::gravel_ffi::BoxDynFrontend::from_value(value, ::abi_stable::sabi_trait::TD_Opaque)
27		}
28	}
29}
30
31fn plugin(plugin_name: &str, with_fn_name: Ident) -> File {
32	parse_quote! {
33		// omitting the root_module allows more than one plugin to be statically linked
34		#[cfg(not(feature = "no-root"))]
35		#[::abi_stable::export_root_module]
36		pub fn __gravel_plugin_root() -> ::gravel_ffi::PluginLibRef {
37			let plugin = ::gravel_ffi::PluginLib { plugin: __gravel_plugin };
38			::abi_stable::prefix_type::PrefixTypeTrait::leak_into_prefix(plugin)
39		}
40
41		#[::abi_stable::sabi_extern_fn]
42		pub fn __gravel_plugin(log_target: ::gravel_ffi::logging::BoxDynLogTarget) -> ::abi_stable::std_types::RVec<::gravel_ffi::PluginDefinition> {
43			::gravel_ffi::logging::ForwardLogger::register(log_target, env!("CARGO_PKG_NAME"));
44			::abi_stable::traits::IntoReprC::into_c(vec![__gravel_plugin_inner()])
45		}
46
47		pub fn __gravel_plugin_inner() -> ::gravel_ffi::PluginDefinition {
48			::gravel_ffi::PluginMetadata::new(#plugin_name).#with_fn_name(__gravel_plugin_get)
49		}
50	}
51}