Skip to main content

ayiou_macros/
lib.rs

1#![allow(clippy::multiple_crate_versions)]
2
3use proc_macro::TokenStream;
4use syn::{Meta, parse_macro_input, punctuated::Punctuated};
5
6mod attr_plugin;
7
8use attr_plugin::expand_plugin;
9
10/// Generate a `RuntimePlugin` implementation from an `impl` block.
11///
12/// `#[plugin]` is the default authoring entrypoint. Put it on an `impl` block;
13/// async methods in the impl become command handlers named after the method.
14///
15/// # Example
16///
17/// ```ignore
18/// use ayiou::{plugin, Context};
19///
20/// #[derive(Default)]
21/// struct EchoPlugin;
22///
23/// #[plugin(name = "echo", prefix = "/")]
24/// impl EchoPlugin {
25///     async fn echo(&self, ctx: &Context, text: String) -> anyhow::Result<()> {
26///         ctx.reply_text(format!("Echo: {}", text)).await
27///     }
28/// }
29/// ```
30///
31/// # Attributes
32///
33/// - `name`: Plugin name (defaults to struct name in lowercase)
34/// - `description`: Plugin description
35/// - `version`: Plugin version (defaults to "0.1.0")
36/// - `prefix`: Command prefix accepted by this plugin (repeatable)
37/// - `context`: Custom context type (defaults to `Context`)
38/// - `register`: Whether to auto-register this plugin (defaults to `true`)
39#[proc_macro_attribute]
40pub fn plugin(attr: TokenStream, item: TokenStream) -> TokenStream {
41    let attrs = parse_macro_input!(attr with Punctuated::<Meta, syn::Token![,]>::parse_terminated);
42    let item_impl = parse_macro_input!(item as syn::ItemImpl);
43    expand_plugin(attrs.into_iter().collect(), item_impl)
44        .unwrap_or_else(|err| err.to_compile_error())
45        .into()
46}
47
48/// Mark a method in `#[plugin] impl` as a command handler.
49///
50/// This attribute is only consumed by `#[plugin]` and is otherwise a no-op.
51#[proc_macro_attribute]
52pub fn command(_attr: TokenStream, item: TokenStream) -> TokenStream {
53    item
54}