summaryrefslogtreecommitdiff
path: root/mingling_macros/src/dispatcher.rs
blob: a411081608f4ff15f99d858b59d9bd51ef31a820 (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
//! Dispatcher Derive Macro Implementation
//!
//! This module provides the `Dispatcher` derive macro for automatically
//! implementing the `mingling::Dispatcher` trait for structs.

use just_fmt::dot_case;
use proc_macro::TokenStream;
use quote::quote;
use syn::{Attribute, DeriveInput, Ident, Lit, Meta, MetaNameValue, parse_macro_input};

/// Parses the `#[dispatcher("path")]` attribute if present
fn parse_dispatcher_attribute(attrs: &[Attribute]) -> Option<String> {
    for attr in attrs {
        if attr.path().is_ident("dispatcher") {
            match attr.parse_args::<Meta>() {
                Ok(Meta::NameValue(MetaNameValue {
                    value:
                        syn::Expr::Lit(syn::ExprLit {
                            lit: Lit::Str(lit_str),
                            ..
                        }),
                    ..
                })) => {
                    return Some(lit_str.value());
                }
                Ok(_) => {
                    // If it's not a string literal, we'll use a default
                    return None;
                }
                Err(_) => {
                    // If parsing fails, we'll use a default
                    return None;
                }
            }
        }
    }
    None
}

/// Generates the command node path from the struct name or attribute
fn generate_command_path(struct_name: &Ident, attr_path: Option<String>) -> String {
    if let Some(path) = attr_path {
        path
    } else {
        // Convert struct name to dot_case for default path using the dot_case! macro
        dot_case!(struct_name.to_string())
    }
}

/// Implementation of the `Dispatcher` derive macro
pub fn dispatcher_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let struct_name = &input.ident;

    // Parse the dispatcher attribute if present
    let attr_path = parse_dispatcher_attribute(&input.attrs);

    // Generate the command path
    let command_path = generate_command_path(struct_name, attr_path);

    // Generate the implementation
    let expanded = quote! {
        impl ::mingling::Dispatcher for #struct_name {
            fn node(&self) -> ::mingling::Node {
                ::mingling::macros::node!(#command_path)
            }
        }
    };

    expanded.into()
}