gravel_ffi_macros/
declare.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use syn::{parse_quote, File, Ident, Type};

pub fn provider(plugin_name: &str, impl_type: &Type) -> File {
	let plugin = plugin(plugin_name, parse_quote! { with_provider });

	parse_quote! {
		#plugin

		#[::abi_stable::sabi_extern_fn]
		pub fn __gravel_plugin_get(config: &::gravel_ffi::PluginConfigAdapter<'_>) -> ::gravel_ffi::BoxDynProvider {
			let value = <#impl_type as ::gravel_ffi::Provider>::new(config);
			::gravel_ffi::BoxDynProvider::from_value(value, ::abi_stable::sabi_trait::TD_Opaque)
		}
	}
}

pub fn frontend(plugin_name: &str, impl_type: &Type) -> File {
	let plugin = plugin(plugin_name, parse_quote! { with_frontend });

	parse_quote! {
		#plugin

		#[::abi_stable::sabi_extern_fn]
		pub fn __gravel_plugin_get(engine: ::gravel_ffi::BoxDynFrontendContext, config: &::gravel_ffi::PluginConfigAdapter<'_>) -> ::gravel_ffi::BoxDynFrontend {
			let value = <#impl_type as ::gravel_ffi::Frontend>::new(engine, config);
			::gravel_ffi::BoxDynFrontend::from_value(value, ::abi_stable::sabi_trait::TD_Opaque)
		}
	}
}

fn plugin(plugin_name: &str, with_fn_name: Ident) -> File {
	parse_quote! {
		// omitting the root_module allows more than one plugin to be statically linked
		#[cfg(not(feature = "no-root"))]
		#[::abi_stable::export_root_module]
		pub fn __gravel_plugin_root() -> ::gravel_ffi::PluginLibRef {
			let plugin = ::gravel_ffi::PluginLib { plugin: __gravel_plugin };
			::abi_stable::prefix_type::PrefixTypeTrait::leak_into_prefix(plugin)
		}

		#[::abi_stable::sabi_extern_fn]
		pub fn __gravel_plugin(log_target: ::gravel_ffi::logging::BoxDynLogTarget) -> ::abi_stable::std_types::RVec<::gravel_ffi::PluginDefinition> {
			::gravel_ffi::logging::ForwardLogger::register(log_target, env!("CARGO_PKG_NAME"));
			::abi_stable::traits::IntoReprC::into_c(vec![__gravel_plugin_inner()])
		}

		pub fn __gravel_plugin_inner() -> ::gravel_ffi::PluginDefinition {
			::gravel_ffi::PluginMetadata::new(#plugin_name).#with_fn_name(__gravel_plugin_get)
		}
	}
}