clightningrpc_plugin_macros/lib.rs
1//! plugin_macros is a rust crate that provide a sequence of helper
2//! function to allow the user of the API to write a plugin
3//! with less code.
4//!
5//! author: https://github.com/vincenzopalazzo
6use kproc_parser::kparser::KParserTracer;
7use kproc_parser::proc_macro::TokenStream;
8
9mod notification;
10mod plugin;
11mod rpc_method;
12
13mod attr_parser;
14
15struct Tracer;
16
17impl KParserTracer for Tracer {
18 fn log(&self, msg: &str) {
19 eprintln!("\x1b[93mkproc-tracing\x1b[1;97m {msg}");
20 }
21}
22/// procedural macros that can be used wit the following code
23/// ```no_run
24/// use serde_json::{json, Value};
25/// use clightningrpc_plugin_macros::{rpc_method, plugin};
26/// use clightningrpc_plugin::commands::RPCCommand;
27/// use clightningrpc_plugin::plugin::Plugin;
28/// use clightningrpc_plugin::errors::PluginError;
29///
30/// #[derive(Clone)]
31/// struct State;
32///
33/// impl State {
34/// pub fn new() -> Self {
35/// Self
36/// }
37/// }
38///
39/// #[rpc_method(
40/// rpc_name = "foo",
41/// description = "This is a simple and short description"
42/// )]
43/// pub fn foo_rpc(plugin: &mut Plugin<State>, request: Value) -> Result<Value, PluginError> {
44/// Ok(json!({"is_dynamic": plugin.dynamic, "rpc_request": request}))
45/// }
46///
47/// fn main() {
48/// let plugin = plugin! {
49/// state: State::new(),
50/// dynamic: true,
51/// notification: [],
52/// methods: [
53/// foo_rpc,
54/// ],
55/// };
56/// plugin.start();
57/// }
58/// ```
59#[proc_macro]
60pub fn plugin(attr: TokenStream) -> TokenStream {
61 plugin::parse(attr)
62}
63
64/// procedural macros that can be used wit the following code
65/// ```no_run
66/// use serde_json::{json, Value};
67/// use clightningrpc_plugin_macros::rpc_method;
68/// use clightningrpc_plugin::commands::RPCCommand;
69/// use clightningrpc_plugin::plugin::Plugin;
70/// use clightningrpc_plugin::errors::PluginError;
71///
72/// #[derive(Clone)]
73/// struct State;
74///
75/// #[rpc_method(
76/// rpc_name = "foo",
77/// description = "This is a simple and short description"
78/// )]
79/// pub fn foo_rpc(plugin: &mut Plugin<State>, request: Value) -> Result<Value, PluginError> {
80/// Ok(json!({"is_dynamic": plugin.dynamic, "rpc_request": request}))
81/// }
82/// ```
83#[proc_macro_attribute]
84pub fn rpc_method(attr: TokenStream, item: TokenStream) -> TokenStream {
85 rpc_method::parse(attr, item)
86}
87
88/// procedural macros that can be used wit the following code
89/// ```no_run
90/// use serde_json::{json, Value};
91/// use clightningrpc_plugin_macros::notification;
92/// use clightningrpc_plugin::commands::RPCCommand;
93/// use clightningrpc_plugin::plugin::Plugin;
94/// use clightningrpc_plugin::types::LogLevel;
95/// use clightningrpc_plugin::errors::PluginError;
96///
97/// #[derive(Clone)]
98/// struct State;
99///
100/// #[notification(on = "rpc_command")]
101/// fn on_rpc(plugin: &mut Plugin<State>, request: &Value) {
102/// plugin.log(LogLevel::Info, "received an RPC notification");
103/// }
104/// ```
105#[proc_macro_attribute]
106pub fn notification(attr: TokenStream, item: TokenStream) -> TokenStream {
107 notification::parse(attr, item)
108}