Skip to main content

malbox_plugin_macros/
lib.rs

1//! Procedural macros for the Malbox plugin framework.
2//!
3//! This crate provides attribute macros that plugin authors use to declare
4//! plugins and wire up handler methods. It is not intended to be used
5//! directly — import it via [`malbox_plugin_sdk`] instead:
6//!
7//! ```ignore
8//! extern crate malbox_plugin_sdk as malbox;
9//! ```
10//!
11//! See the [`malbox_plugin_sdk`] crate documentation for a full usage guide.
12
13mod codegen;
14mod handlers_impl;
15mod metadata;
16mod plugin_attr;
17
18use proc_macro::TokenStream;
19
20/// Marks a struct as a host plugin (IPC-based, runs on daemon host).
21///
22/// Requires the `host` feature on `malbox-plugin-sdk`.
23///
24/// # Example
25/// ```ignore
26/// #[malbox::host_plugin]
27/// struct MyPlugin;
28/// ```
29#[proc_macro_attribute]
30pub fn host_plugin(_attr: TokenStream, item: TokenStream) -> TokenStream {
31    plugin_attr::expand_plugin(item.into(), metadata::PluginKind::Host)
32        .unwrap_or_else(|e| e.to_compile_error())
33        .into()
34}
35
36/// Marks a struct as a guest plugin (gRPC-based, runs inside VM).
37///
38/// Requires the `guest` feature on `malbox-plugin-sdk`.
39///
40/// # Example
41/// ```ignore
42/// #[malbox::guest_plugin]
43/// struct MyPlugin;
44/// ```
45#[proc_macro_attribute]
46pub fn guest_plugin(_attr: TokenStream, item: TokenStream) -> TokenStream {
47    plugin_attr::expand_plugin(item.into(), metadata::PluginKind::Guest)
48        .unwrap_or_else(|e| e.to_compile_error())
49        .into()
50}
51
52/// Applied to an `impl` block to scan for handler methods and generate trait impls.
53///
54/// Methods tagged with `#[malbox::on_task]`, `#[malbox::on_start]`,
55/// `#[malbox::on_stop]`, `#[malbox::health_check]` are wired to the
56/// corresponding internal handler traits. Missing handlers get default
57/// no-op implementations.
58///
59/// # Example
60/// ```ignore
61/// #[malbox::handlers]
62/// impl MyPlugin {
63///     #[malbox::on_start]
64///     fn init(&self) -> Result<()> { Ok(()) }
65///
66///     #[malbox::on_task]
67///     fn process(&self, task: Task, ctx: &Context) -> Result<()> {
68///         Ok(())
69///     }
70/// }
71/// ```
72#[proc_macro_attribute]
73pub fn handlers(_attr: TokenStream, item: TokenStream) -> TokenStream {
74    handlers_impl::expand_handlers(item.into())
75        .unwrap_or_else(|e| e.to_compile_error())
76        .into()
77}
78
79/// Marks a method as the task handler (used inside `#[malbox::handlers]`).
80///
81/// Can also be used standalone — passes through the method unchanged.
82#[proc_macro_attribute]
83pub fn on_task(_attr: TokenStream, item: TokenStream) -> TokenStream {
84    // Pass through — the actual wiring is done by #[malbox::handlers]
85    item
86}
87
88/// Marks a method as the startup handler (used inside `#[malbox::handlers]`).
89#[proc_macro_attribute]
90pub fn on_start(_attr: TokenStream, item: TokenStream) -> TokenStream {
91    item
92}
93
94/// Marks a method as the shutdown handler (used inside `#[malbox::handlers]`).
95#[proc_macro_attribute]
96pub fn on_stop(_attr: TokenStream, item: TokenStream) -> TokenStream {
97    item
98}
99
100/// Marks a method as an event handler (used inside `#[malbox::handlers]`).
101#[proc_macro_attribute]
102pub fn on_event(_attr: TokenStream, item: TokenStream) -> TokenStream {
103    item
104}
105
106/// Marks a method as a custom health check handler (used inside `#[malbox::handlers]`).
107#[proc_macro_attribute]
108pub fn health_check(_attr: TokenStream, item: TokenStream) -> TokenStream {
109    item
110}