rpc_toolkit_macro/
lib.rs

1use proc_macro::{Span, TokenStream};
2use rpc_toolkit_macro_internals::*;
3
4#[proc_macro_attribute]
5pub fn command(args: TokenStream, item: TokenStream) -> TokenStream {
6    let args = syn::parse_macro_input!(args as syn::AttributeArgs);
7    let item = syn::parse_macro_input!(item as syn::ItemFn);
8    build_command(args, item).into()
9}
10
11/// `#[arg(...)]` -> Take this argument as a parameter
12/// - `#[arg(help = "Help text")]` -> Set help text for the arg
13/// - `#[arg(alias = "ls")]` -> Set the alias `ls` in the CLI
14/// - `#[arg(aliases("show", "ls"))]` -> Set the aliases `ls` and `show` in the CLI
15/// - `#[arg(rename = "new_name")]` -> Set the name of the arg to `new_name` in the RPC and CLI
16/// - `#[arg(short = "a")]` -> Set the "short" representation of the arg to `-a` on the CLI
17/// - `#[arg(long = "arg")]` -> Set the "long" representation of the arg to `--arg` on the CLI
18/// - `#[arg(parse(custom_parse_fn))]` -> Use the function `custom_parse_fn` to parse the arg from the CLI
19///   - `custom_parse_fn :: Into<RpcError> err => (&str, &ArgMatches) -> Result<arg, err>`
20///   - note: `arg` is the type of the argument
21/// - `#[arg(stdin)]` -> Parse the argument from stdin when using the CLI
22///   - `custom_parse_fn :: Into<RpcError> err => (&[u8], &ArgMatches) -> Result<arg, err>`
23/// - `#[arg(count)]` -> Treat argument as flag, count occurrences
24/// - `#[arg(multiple)]` -> Allow the arg to be specified multiple times. Collect the args after parsing.
25#[proc_macro_attribute]
26pub fn arg(_: TokenStream, _: TokenStream) -> TokenStream {
27    syn::Error::new(
28        Span::call_site().into(),
29        "`arg` is only allowed on arguments of a function with the `command` attribute",
30    )
31    .to_compile_error()
32    .into()
33}
34
35/// - `#[context]` -> Passes the application context into this parameter
36#[proc_macro_attribute]
37pub fn context(_: TokenStream, _: TokenStream) -> TokenStream {
38    syn::Error::new(
39        Span::call_site().into(),
40        "`context` is only allowed on arguments of a function with the `command` attribute",
41    )
42    .to_compile_error()
43    .into()
44}
45
46#[proc_macro]
47pub fn rpc_handler(item: TokenStream) -> TokenStream {
48    let item = syn::parse_macro_input!(item as RpcHandlerArgs);
49    build_rpc_handler(item).into()
50}
51
52#[proc_macro]
53pub fn rpc_server(item: TokenStream) -> TokenStream {
54    let item = syn::parse_macro_input!(item as RpcServerArgs);
55    build_rpc_server(item).into()
56}
57
58#[proc_macro]
59pub fn run_cli(item: TokenStream) -> TokenStream {
60    let item = syn::parse_macro_input!(item as RunCliArgs);
61    build_run_cli(item).into()
62}