gravel_ffi_macros/
lib.rs

1//! Contains procedural macros used to auto-implement FFI loader code.
2//! See `gravel_ffi::Provider` and `gravel_ffi::Frontend` for usage instructions
3
4use proc_macro::TokenStream as TokenStream1;
5use syn::{File, ItemImpl, parse_quote};
6
7mod declare;
8mod util;
9
10/// Automatically implements the necessary FFI-glue for plugins to work.
11///
12/// The Argument is the plugin name, which is used to identify it.  
13/// See `gravel_ffi::Provider` for more detailed information.
14#[proc_macro_attribute]
15pub fn gravel_provider(attr: TokenStream1, item: TokenStream1) -> TokenStream1 {
16	util::wrap_syn(item, |impl_block: ItemImpl| {
17		let provider_type = &impl_block.self_ty;
18
19		let declaration = declare::provider(&util::get_name(attr)?, provider_type);
20
21		let ast: File = parse_quote! {
22			impl ::gravel_ffi::ProviderInner for #provider_type {
23				fn query(&self, query: ::abi_stable::std_types::RStr<'_>) -> ::gravel_ffi::ProviderResult {
24					::gravel_ffi::Provider::query(self, query.as_str())
25				}
26
27				fn clear_caches(&self) {
28					::gravel_ffi::Provider::clear_caches(self);
29				}
30			}
31
32			#declaration
33			#impl_block
34		};
35
36		Ok(ast)
37	})
38}
39
40/// Automatically implements the necessary FFI-glue for plugins to work.
41///
42/// The Argument is the plugin name, which is used to identify it.  
43/// See `gravel_ffi::Frontend` for more detailed information.
44#[proc_macro_attribute]
45pub fn gravel_frontend(attr: TokenStream1, item: TokenStream1) -> TokenStream1 {
46	util::wrap_syn(item, |impl_block: ItemImpl| {
47		let frontend_type = &impl_block.self_ty;
48
49		let declaration = declare::frontend(&util::get_name(attr)?, frontend_type);
50
51		let ast: File = parse_quote! {
52			impl ::gravel_ffi::FrontendInner for #frontend_type {
53				fn run(&mut self) -> ::gravel_ffi::FrontendExitStatusNe {
54					let status = ::gravel_ffi::Frontend::run(self);
55					::gravel_ffi::FrontendExitStatusNe::new(status)
56				}
57			}
58
59			#declaration
60			#impl_block
61		};
62
63		Ok(ast)
64	})
65}