aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/completion.rs
blob: 334affd6f4ea06ff39a0fac4bf04f5c31c5ce299 (plain) (blame)
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
//! Completion Attribute Macro Implementation
//!
//! This module provides the `#[completion]` attribute macro for automatically
//! generating structs that implement the `Completion` trait from functions.

use proc_macro::TokenStream;
use quote::quote;
use syn::{Ident, ItemFn, parse_macro_input};

#[cfg(feature = "comp")]
pub fn completion_attr(attr: TokenStream, item: TokenStream) -> TokenStream {
    // Parse the attribute arguments (e.g., HelloEntry from #[completion(HelloEntry)])
    let previous_type_ident = if attr.is_empty() {
        return syn::Error::new(
            proc_macro2::Span::call_site(),
            "completion attribute requires a previous type argument, e.g. #[completion(HelloEntry)]",
        )
        .to_compile_error()
        .into();
    } else {
        parse_macro_input!(attr as Ident)
    };

    // Parse the function item
    let input_fn = parse_macro_input!(item as ItemFn);

    // Validate the function is not async
    if input_fn.sig.asyncness.is_some() {
        use syn::spanned::Spanned;

        return syn::Error::new(input_fn.sig.span(), "Completion function cannot be async")
            .to_compile_error()
            .into();
    }

    // Get the function signature parts
    let sig = &input_fn.sig;
    let inputs = &sig.inputs;
    let output = &sig.output;

    // Check that the function has exactly one parameter
    if inputs.len() != 1 {
        use syn::spanned::Spanned;

        return syn::Error::new(
            inputs.span(),
            "Completion function must have exactly one parameter",
        )
        .to_compile_error()
        .into();
    }

    // Get the function body
    let fn_body = &input_fn.block;

    // Get function attributes (excluding the completion attribute)
    let mut fn_attrs = input_fn.attrs.clone();
    fn_attrs.retain(|attr| !attr.path().is_ident("completion"));

    // Get function visibility
    let vis = &input_fn.vis;

    // Get function name
    let fn_name = &sig.ident;

    // Generate struct name from function name using pascal_case
    let pascal_case_name = just_fmt::pascal_case!(fn_name.to_string());
    let struct_name = Ident::new(&pascal_case_name, fn_name.span());

    // Generate the struct and implementation
    let expanded = quote! {
        #(#fn_attrs)*
        #vis struct #struct_name;

        impl ::mingling::Completion for #struct_name {
            type Previous = #previous_type_ident;

            fn comp(#inputs) #output {
                #fn_body
            }
        }

        // Keep the original function for internal use
        #(#fn_attrs)*
        #vis fn #fn_name(#inputs) #output {
            #fn_body
        }
    };

    let completion_entry = quote! {
        Self::#previous_type_ident => <#struct_name as ::mingling::Completion>::comp(ctx),
    };

    let mut completions = crate::COMPLETIONS.lock().unwrap();
    let completion_str = completion_entry.to_string();
    completions.insert(completion_str);

    expanded.into()
}