aboutsummaryrefslogtreecommitdiff
path: root/mingling_pathf/src/patterns/dispatcher.rs
blob: 859f198d99122d1d632f821aba7265ee56dd8120 (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
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
// Doc Not Optimize
//! The `DispatcherPattern` matches invocations of the `dispatcher!` macro and
//! extracts the generated type names from its arguments. It supports:
//! - `Entry*` — the entry type (always generated)
//! - `__Dispatcher*` — the hidden dispatcher struct (always generated)
//! - `__internal_dispatcher_*` — the compile-time collected static (always generated)
//!
//! Supported forms:
//! - Explicit: `dispatcher!("greet", EntryGreet)`
//! - Implicit: `dispatcher!("greet")` — infers `EntryGreet`
//! - With braces: `dispatcher! { ... }`
//!
//! This pattern is used to track dispatcher types for code generation or analysis.

use syn::Item;

use crate::pattern_analyzer::{AnalyzeItem, AnalyzePattern};

/// Matches the `dispatcher!` macro, extracts:
/// - `Entry*` — the entry type (always)
/// - `__Dispatcher*` — the hidden dispatcher struct (always)
/// - `__internal_dispatcher_*` — the compile-time collected static (always)
#[derive(Default)]
pub struct DispatcherPattern;

impl DispatcherPattern {
    /// Creates a new `DispatcherPattern`.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

/// Supported forms:
/// - `dispatcher!("greet", CMDGreet => EntryGreet)` — explicit
/// - `dispatcher!("greet")` — implicit, infers names
/// - `dispatcher! { ... }` — with braces
impl AnalyzePattern for DispatcherPattern {
    fn contains(&self, content: &str) -> bool {
        content.contains("dispatcher!")
    }

    fn analyze(&self, content: &str) -> Vec<AnalyzeItem> {
        let Ok(syntax) = syn::parse_file(content) else {
            return Vec::new();
        };

        let mut items = Vec::new();

        for item in &syntax.items {
            match item {
                Item::Macro(m) => {
                    let macro_name = macro_simple_name(m);
                    if macro_name != "dispatcher" {
                        continue;
                    }
                    items.extend(extract_all_types(&m.mac.tokens, ""));
                }
                Item::Mod(item_mod) => {
                    if let Some((_, nested)) = &item_mod.content {
                        for n in nested {
                            if let Item::Macro(m) = n {
                                if macro_simple_name(m) != "dispatcher" {
                                    continue;
                                }
                                items.extend(extract_all_types(
                                    &m.mac.tokens,
                                    &item_mod.ident.to_string(),
                                ));
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        items
    }
}

fn macro_simple_name(m: &syn::ItemMacro) -> String {
    m.mac
        .path
        .segments
        .last()
        .map(|s| s.ident.to_string())
        .unwrap_or_default()
}

/// Extracts all types generated by a `dispatcher!` call.
fn extract_all_types(tokens: &proc_macro2::TokenStream, module: &str) -> Vec<AnalyzeItem> {
    let (cmd_name, entry_struct) = parse_dispatcher_args(tokens);
    let Some(cmd_name) = cmd_name else {
        return Vec::new();
    };

    let mut items = Vec::new();

    // Entry type — always (derived from the command name in the implicit form)
    let entry = entry_struct.unwrap_or_else(|| format!("Entry{}", to_pascal_case(&cmd_name)));
    items.push(AnalyzeItem::local(module.to_string(), entry));

    // Hidden dispatcher struct — always
    let hidden_name = format!("__Dispatcher{}", to_pascal_case(&cmd_name));
    items.push(AnalyzeItem::local(module.to_string(), hidden_name));

    // __internal_dispatcher_* — the compile-time collected static
    let internal_name = format!("__internal_dispatcher_{}", snake_case(&cmd_name));
    items.push(AnalyzeItem::local(module.to_string(), internal_name));

    items
}

/// Parses dispatcher arguments and returns (`command_name`, `entry_struct`).
fn parse_dispatcher_args(tokens: &proc_macro2::TokenStream) -> (Option<String>, Option<String>) {
    let stream = tokens.to_string();

    let Some(cmd_name) = extract_string_literal(&stream) else {
        return (None, None);
    };

    // Explicit form: "name", EntryType — the entry is the first bare
    // identifier after the string literal. (The old `CMD => Entry` form is
    // no longer supported.)
    let after_lit = {
        let start = stream.find('"').unwrap_or_default();
        &stream[start + cmd_name.len() + 2..]
    };
    let entry_type = after_lit
        .split(|c: char| c.is_whitespace() || c == ',' || c == ')' || c == '}' || c == '=')
        .map(str::trim)
        .find(|s| !s.is_empty() && !s.starts_with('"'))
        .map(str::to_string);

    (Some(cmd_name), entry_type)
}

/// Extracts the first string literal from a token string.
fn extract_string_literal(s: &str) -> Option<String> {
    let s = s.trim();
    let start = s.find('"')?;
    let rest = &s[start + 1..];
    let end = rest.find('"')?;
    Some(rest[..end].to_string())
}

fn to_pascal_case(s: &str) -> String {
    s.split(['-', '_', '.'])
        .filter(|s| !s.is_empty())
        .map(|s| {
            let mut c = s.chars();
            c.next().map_or_else(String::new, |f| {
                f.to_uppercase().collect::<String>() + c.as_str()
            })
        })
        .collect()
}

/// Simple `snake_case` conversion (replaces `.`, `-` with `_`).
fn snake_case(s: &str) -> String {
    s.replace(['.', '-'], "_").to_lowercase()
}