ayiou-macros 0.2.0

Procedural macros for the Ayiou bot framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
    Expr, ExprArray, ExprLit, FnArg, GenericArgument, ImplItem, ItemImpl, Lit, Meta, MetaNameValue,
    Pat, PatIdent, PathArguments, Result, Type,
};

struct PluginAttrs {
    name: Option<String>,
    description: Option<String>,
    version: Option<String>,
    prefixes: Vec<String>,
    register: bool,
}

impl Default for PluginAttrs {
    fn default() -> Self {
        Self {
            name: None,
            description: None,
            version: None,
            prefixes: Vec::new(),
            register: true,
        }
    }
}

#[derive(Default)]
struct CommandAttrs {
    name: Option<String>,
    aliases: Vec<String>,
}

struct CommandMethod {
    fn_name: syn::Ident,
    labels: Vec<String>,
    parser_stmts: Vec<TokenStream>,
    call_args: Vec<syn::Ident>,
}

struct PluginIdentity {
    name: String,
    description: String,
    version: String,
    register: bool,
}

pub fn expand_plugin(args: Vec<Meta>, mut item_impl: ItemImpl) -> Result<TokenStream> {
    let plugin_attrs = parse_plugin_attrs(args)?;
    let plugin_ty = item_impl.self_ty.clone();
    let plugin_ident = extract_self_type_ident(&plugin_ty)?;
    let ctx_ty: Type = syn::parse_quote!(ayiou::Context);
    let methods = collect_command_methods(&mut item_impl)?;
    let identity = plugin_identity(&plugin_attrs, &plugin_ident);

    Ok(render_plugin_impl(
        &item_impl,
        &plugin_ty,
        &ctx_ty,
        identity,
        &plugin_attrs.prefixes,
        &methods,
    ))
}

fn collect_command_methods(item_impl: &mut ItemImpl) -> Result<Vec<CommandMethod>> {
    let mut methods = Vec::new();

    for impl_item in &mut item_impl.items {
        let ImplItem::Fn(method) = impl_item else {
            continue;
        };

        if method.sig.asyncness.is_none() {
            continue;
        }

        let cmd_attrs = if let Some(attr_index) = command_attr_index(method) {
            let command_attr = method.attrs.remove(attr_index);
            parse_command_attr(&command_attr)?
        } else {
            CommandAttrs::default()
        };

        method
            .attrs
            .push(syn::parse_quote!(#[allow(clippy::unused_async)]));

        methods.push(parse_command_method(method, cmd_attrs)?);
    }

    if methods.is_empty() {
        return Err(syn::Error::new_spanned(
            &item_impl,
            "#[plugin] requires at least one async command method",
        ));
    }

    Ok(methods)
}

fn command_attr_index(method: &syn::ImplItemFn) -> Option<usize> {
    method
        .attrs
        .iter()
        .position(|attr| attr.path().is_ident("command"))
}

fn plugin_identity(attrs: &PluginAttrs, plugin_ident: &syn::Ident) -> PluginIdentity {
    PluginIdentity {
        name: attrs
            .name
            .clone()
            .unwrap_or_else(|| plugin_ident.to_string().to_lowercase()),
        description: attrs.description.clone().unwrap_or_default(),
        version: attrs.version.clone().unwrap_or_else(|| "0.1.0".to_string()),
        register: attrs.register,
    }
}

fn render_plugin_impl(
    item_impl: &ItemImpl,
    plugin_ty: &Type,
    ctx_ty: &Type,
    identity: PluginIdentity,
    prefixes: &[String],
    methods: &[CommandMethod],
) -> TokenStream {
    let all_commands: Vec<String> = methods
        .iter()
        .flat_map(|method| method.labels.iter().cloned())
        .collect();

    let command_values = all_commands
        .iter()
        .map(|value| quote! { #value.to_string() });
    let prefix_values = prefixes.iter().map(|value| quote! { #value.to_string() });
    let plugin_name = identity.name;
    let plugin_description = identity.description;
    let plugin_version = identity.version;
    let registration = if identity.register {
        quote! {
                ayiou::inventory::submit! {
                ayiou::core::plugin::PluginRegistration {
                    instance_id: #plugin_name,
                    factory: || -> Box<dyn ayiou::core::plugin::RuntimePlugin> {
                        Box::new(<#plugin_ty as ::std::default::Default>::default())
                    },
                }
            }
        }
    } else {
        quote! {}
    };

    let dispatch_arms = methods.iter().map(|method| {
        let fn_name = &method.fn_name;
        let labels = method.labels.iter();
        let parser_stmts = &method.parser_stmts;
        let call_args = &method.call_args;

        quote! {
            #(#labels)|* => {
                #(#parser_stmts)*
                self.#fn_name(ctx, #(#call_args),*).await?;
                Ok(ayiou::core::plugin::HandleOutcome::block())
            }
        }
    });

    quote! {
        #item_impl

        #[async_trait::async_trait]
        impl ayiou::core::plugin::RuntimePlugin for #plugin_ty {
            fn kind(&self) -> &str {
                #plugin_name
            }

            fn manifest(&self) -> ayiou::core::plugin::RuntimePluginManifest {
                ayiou::core::plugin::RuntimePluginManifest::new(#plugin_name)
                    .description(#plugin_description)
                    .version(#plugin_version)
            }

            fn declared_handlers(&self) -> Vec<ayiou::core::plugin::HandlerDecl> {
                vec![ayiou::core::plugin::HandlerDecl::message_commands(
                    vec![#(#command_values),*],
                    Vec::<String>::from([#(#prefix_values),*]),
                )]
            }

            async fn handle_with_invocation(
                &self,
                ctx: &#ctx_ty,
                invocation: Option<ayiou::core::model::CommandInvocation>,
            ) -> anyhow::Result<ayiou::core::plugin::HandleOutcome> {
                let Some(line) = invocation else {
                    return Ok(ayiou::core::plugin::HandleOutcome::pass());
                };
                self.__ayiou_dispatch_command(ctx, line.command(), line.args()).await
            }

            async fn handle(&self, _ctx: &#ctx_ty) -> anyhow::Result<ayiou::core::plugin::HandleOutcome> {
                Ok(ayiou::core::plugin::HandleOutcome::pass())
            }
        }

        impl #plugin_ty {
            async fn __ayiou_dispatch_command(
                &self,
                ctx: &#ctx_ty,
                command: &str,
                args: &str,
            ) -> anyhow::Result<ayiou::core::plugin::HandleOutcome> {
                match command {
                    #(#dispatch_arms,)*
                    _ => Ok(ayiou::core::plugin::HandleOutcome::pass()),
                }
            }
        }

        #registration
    }
}

fn parse_plugin_attrs(args: Vec<Meta>) -> Result<PluginAttrs> {
    let mut out = PluginAttrs::default();

    for meta in args {
        match meta {
            Meta::NameValue(MetaNameValue { path, value, .. }) => {
                let key = path
                    .get_ident()
                    .map(std::string::ToString::to_string)
                    .ok_or_else(|| syn::Error::new_spanned(path, "Unsupported plugin key"))?;

                match key.as_str() {
                    "name" => out.name = Some(expect_string_expr(value)?),
                    "description" => out.description = Some(expect_string_expr(value)?),
                    "version" => out.version = Some(expect_string_expr(value)?),
                    "prefix" => out.prefixes.push(expect_string_expr(value)?),
                    "register" => out.register = expect_bool_expr(value)?,
                    _ => {
                        return Err(syn::Error::new(
                            Span::call_site(),
                            format!("Unsupported plugin key `{key}`"),
                        ));
                    }
                }
            }
            other => {
                return Err(syn::Error::new_spanned(
                    other,
                    "Unsupported plugin attribute format",
                ));
            }
        }
    }

    Ok(out)
}

fn parse_command_attr(attr: &syn::Attribute) -> Result<CommandAttrs> {
    let mut out = CommandAttrs::default();

    if matches!(&attr.meta, Meta::Path(_)) {
        return Ok(out);
    }

    attr.parse_nested_meta(|meta| {
        let key = meta
            .path
            .get_ident()
            .map(std::string::ToString::to_string)
            .ok_or_else(|| syn::Error::new_spanned(&meta.path, "Unsupported command key"))?;

        match key.as_str() {
            "name" => {
                let value: Expr = meta.value()?.parse()?;
                out.name = Some(expect_string_expr(value)?);
            }
            "alias" => {
                let value: Expr = meta.value()?.parse()?;
                out.aliases.push(expect_string_expr(value)?);
            }
            "aliases" => {
                let value: Expr = meta.value()?.parse()?;
                out.aliases.extend(expect_string_array_expr(value)?);
            }
            _ => {
                return Err(syn::Error::new(
                    Span::call_site(),
                    format!("Unsupported command key `{key}`"),
                ));
            }
        }

        Ok(())
    })?;

    Ok(out)
}

fn parse_command_method(method: &syn::ImplItemFn, attrs: CommandAttrs) -> Result<CommandMethod> {
    let fn_name = method.sig.ident.clone();
    let mut labels = vec![attrs.name.unwrap_or_else(|| fn_name.to_string())];
    labels.extend(attrs.aliases);

    let mut inputs = method.sig.inputs.iter();

    match inputs.next() {
        Some(FnArg::Receiver(receiver)) if receiver.reference.is_some() => {}
        _ => {
            return Err(syn::Error::new_spanned(
                &method.sig.inputs,
                "command method must start with `&self`",
            ));
        }
    }

    let Some(FnArg::Typed(ctx_arg)) = inputs.next() else {
        return Err(syn::Error::new_spanned(
            &method.sig.inputs,
            "command method must include context as the second argument",
        ));
    };

    match &*ctx_arg.ty {
        Type::Reference(_) => {}
        _ => {
            return Err(syn::Error::new_spanned(
                &ctx_arg.ty,
                "second argument must be a reference context type",
            ));
        }
    }

    let args_inputs: Vec<_> = inputs.collect();

    let mut parser_stmts = vec![quote! {
        let __ayiou_tokens = ayiou::core::command::tokenize_command_args(args)?;
        let mut __ayiou_index = 0usize;
    }];

    let mut call_args = Vec::new();

    for (idx, arg) in args_inputs.iter().enumerate() {
        let FnArg::Typed(pat_type) = arg else {
            return Err(syn::Error::new_spanned(arg, "Invalid command argument"));
        };

        let Pat::Ident(PatIdent { ident, .. }) = pat_type.pat.as_ref() else {
            return Err(syn::Error::new_spanned(
                &pat_type.pat,
                "argument pattern must be an identifier",
            ));
        };

        let var = ident.clone();
        let arg_name = ident.to_string();
        let ty = &pat_type.ty;
        let is_last = idx == args_inputs.len() - 1;

        if let Some(inner_ty) = unwrap_option_type(ty) {
            parser_stmts.push(quote! {
                let #var = if __ayiou_index < __ayiou_tokens.len() {
                    Some(ayiou::core::command::parse_typed_arg::<#inner_ty>(
                        &__ayiou_tokens,
                        &mut __ayiou_index,
                        #arg_name,
                    )?)
                } else {
                    None
                };
            });
        } else if is_last && is_string_type(ty) {
            parser_stmts.push(quote! {
                let #var = __ayiou_tokens[__ayiou_index..].join(" ");
                __ayiou_index = __ayiou_tokens.len();
            });
        } else if is_last && is_vec_string_type(ty) {
            parser_stmts.push(quote! {
                let #var = __ayiou_tokens[__ayiou_index..].to_vec();
                __ayiou_index = __ayiou_tokens.len();
            });
        } else {
            parser_stmts.push(quote! {
                let #var = ayiou::core::command::parse_typed_arg::<#ty>(
                    &__ayiou_tokens,
                    &mut __ayiou_index,
                    #arg_name,
                )?;
            });
        }

        call_args.push(var);
    }

    parser_stmts.push(quote! {
        ayiou::core::command::ensure_no_extra_args(&__ayiou_tokens, __ayiou_index)?;
    });

    Ok(CommandMethod {
        fn_name,
        labels,
        parser_stmts,
        call_args,
    })
}

fn expect_string_expr(value: Expr) -> Result<String> {
    if let Expr::Lit(ExprLit {
        lit: Lit::Str(value),
        ..
    }) = value
    {
        Ok(value.value())
    } else {
        Err(syn::Error::new_spanned(value, "Expected string literal"))
    }
}

fn expect_bool_expr(value: Expr) -> Result<bool> {
    if let Expr::Lit(ExprLit {
        lit: Lit::Bool(value),
        ..
    }) = value
    {
        Ok(value.value)
    } else {
        Err(syn::Error::new_spanned(value, "Expected bool literal"))
    }
}

fn expect_string_array_expr(value: Expr) -> Result<Vec<String>> {
    let Expr::Array(ExprArray { elems, .. }) = value else {
        return Err(syn::Error::new_spanned(
            value,
            "Expected string array literal",
        ));
    };

    let mut out = Vec::with_capacity(elems.len());
    for expr in elems {
        out.push(expect_string_expr(expr)?);
    }

    Ok(out)
}

fn extract_self_type_ident(plugin_ty: &Type) -> Result<syn::Ident> {
    if let Type::Path(path) = plugin_ty
        && let Some(seg) = path.path.segments.last()
    {
        return Ok(seg.ident.clone());
    }

    Err(syn::Error::new_spanned(
        plugin_ty,
        "Unsupported plugin self type",
    ))
}

fn is_string_type(ty: &Type) -> bool {
    if let Type::Path(path) = ty
        && let Some(seg) = path.path.segments.last()
    {
        return seg.ident == "String";
    }

    false
}

fn is_vec_string_type(ty: &Type) -> bool {
    if let Type::Path(path) = ty
        && let Some(seg) = path.path.segments.last()
        && seg.ident == "Vec"
        && let PathArguments::AngleBracketed(inner) = &seg.arguments
        && inner.args.len() == 1
        && let Some(GenericArgument::Type(inner_ty)) = inner.args.first()
    {
        return is_string_type(inner_ty);
    }

    false
}

fn unwrap_option_type(ty: &Type) -> Option<Type> {
    if let Type::Path(path) = ty
        && let Some(seg) = path.path.segments.last()
        && seg.ident == "Option"
        && let PathArguments::AngleBracketed(inner) = &seg.arguments
        && inner.args.len() == 1
        && let Some(GenericArgument::Type(inner_ty)) = inner.args.first()
    {
        return Some(inner_ty.clone());
    }

    None
}