plexus-macros 0.5.1

Procedural macros for Plexus RPC activations
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! HandleEnum derive macro
//!
//! Generates type-safe handle creation and parsing for plugin handle enums.
//!
//! # Example
//!
//! ```ignore
//! #[derive(HandleEnum)]
//! #[handle(plugin_id = "CLAUDECODE_PLUGIN_ID", version = "1.0.0")]
//! pub enum ClaudeCodeHandle {
//!     #[handle(method = "chat_event")]
//!     ChatEvent { event_id: String },
//!
//!     #[handle(method = "message")]
//!     Message { message_id: String, role: String },
//! }
//! ```
//!
//! Generates:
//! - `to_handle(&self) -> Handle` - converts enum variant to Handle
//! - `impl TryFrom<&Handle>` - parses Handle back to enum variant
//! - `impl From<EnumName> for Handle` - convenience conversion

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{
    parse::{Parse, ParseStream},
    parse_macro_input, punctuated::Punctuated, DeriveInput, Expr, ExprLit, Fields, Lit, Meta,
    MetaList, MetaNameValue, Token,
};

/// Entry point for the derive macro
pub fn derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match derive_impl(input) {
        Ok(tokens) => tokens.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

/// Parsed enum-level attributes: #[handle(plugin_id = "...", version = "...")]
struct HandleEnumAttrs {
    /// The constant name holding the plugin UUID (e.g., "CLAUDECODE_PLUGIN_ID")
    plugin_id: String,
    /// Optional concrete type whose associated constant `plugin_id` names, used
    /// when the owning activation is generic (e.g., `Cone<P: HubContext = NoParent>`).
    /// When set, codegen emits `<plugin_id_type>::<plugin_id_tail>` instead of
    /// `plugin_id` directly, avoiding E0283 "cannot infer type" errors.
    ///
    /// Example: `plugin_id_type = "Cone<NoParent>"` paired with
    /// `plugin_id = "Cone::PLUGIN_ID"` emits `<Cone<NoParent>>::PLUGIN_ID`.
    plugin_id_type: Option<String>,
    /// Semantic version for handles (e.g., "1.0.0")
    version: String,
    /// Base crate path for imports (default: "plexus_core")
    crate_path: String,
}

impl Parse for HandleEnumAttrs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut plugin_id = None;
        let mut plugin_id_type = None;
        let mut version = None;
        let mut crate_path = "plexus_core".to_string();

        if !input.is_empty() {
            let metas = Punctuated::<Meta, Token![,]>::parse_terminated(input)?;
            for meta in metas {
                if let Meta::NameValue(MetaNameValue { path, value, .. }) = meta {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(s), ..
                    }) = value
                    {
                        if path.is_ident("plugin_id") {
                            plugin_id = Some(s.value());
                        } else if path.is_ident("plugin_id_type") {
                            plugin_id_type = Some(s.value());
                        } else if path.is_ident("version") {
                            version = Some(s.value());
                        } else if path.is_ident("crate_path") {
                            crate_path = s.value();
                        }
                    }
                }
            }
        }

        Ok(HandleEnumAttrs {
            plugin_id: plugin_id.ok_or_else(|| {
                syn::Error::new(input.span(), "HandleEnum requires plugin_id = \"...\" attribute")
            })?,
            plugin_id_type,
            version: version.ok_or_else(|| {
                syn::Error::new(input.span(), "HandleEnum requires version = \"...\" attribute")
            })?,
            crate_path,
        })
    }
}

/// Parsed variant-level attributes: #[handle(method = "...", table = "...", key = "...", key_field = "...", strip_prefix = "...")]
struct HandleVariantAttrs {
    /// The handle.method value (required)
    method: String,
    /// SQLite table name (optional, for resolution)
    table: Option<String>,
    /// Primary key column (optional, for resolution)
    key: Option<String>,
    /// Which field contains the key value (optional, defaults to first field)
    key_field: Option<String>,
    /// Prefix to strip from key before querying (optional)
    strip_prefix: Option<String>,
}

fn parse_variant_attrs(attrs: &[syn::Attribute]) -> syn::Result<Option<HandleVariantAttrs>> {
    for attr in attrs {
        if attr.path().is_ident("handle") {
            if let Meta::List(MetaList { tokens, .. }) = &attr.meta {
                let mut method = None;
                let mut table = None;
                let mut key = None;
                let mut key_field = None;
                let mut strip_prefix = None;

                let parser = Punctuated::<Meta, Token![,]>::parse_terminated;
                let nested = syn::parse::Parser::parse2(parser, tokens.clone())?;

                for meta in nested {
                    if let Meta::NameValue(MetaNameValue { path, value, .. }) = meta {
                        if let Expr::Lit(ExprLit {
                            lit: Lit::Str(s), ..
                        }) = value
                        {
                            if path.is_ident("method") {
                                method = Some(s.value());
                            } else if path.is_ident("table") {
                                table = Some(s.value());
                            } else if path.is_ident("key") {
                                key = Some(s.value());
                            } else if path.is_ident("key_field") {
                                key_field = Some(s.value());
                            } else if path.is_ident("strip_prefix") {
                                strip_prefix = Some(s.value());
                            }
                        }
                    }
                }

                if let Some(method) = method {
                    return Ok(Some(HandleVariantAttrs { method, table, key, key_field, strip_prefix }));
                }
            }
        }
    }
    Ok(None)
}

/// Information about a variant's named field
struct FieldInfo {
    name: syn::Ident,
    name_str: String,
}

/// Resolution configuration for a variant
struct ResolutionInfo {
    /// SQLite table name
    table: String,
    /// Primary key column name
    key: String,
    /// Which field contains the key value (index into fields)
    key_field_index: usize,
    /// Prefix to strip from key before querying
    strip_prefix: Option<String>,
}

/// Information extracted from an enum variant
struct VariantInfo {
    name: syn::Ident,
    method: String,
    fields: Vec<FieldInfo>,
    /// Resolution configuration (if table and key are specified)
    resolution: Option<ResolutionInfo>,
}

fn derive_impl(input: DeriveInput) -> syn::Result<TokenStream2> {
    let enum_name = &input.ident;

    // Extract enum-level #[handle(...)] attributes
    let enum_attrs = extract_enum_attrs(&input)?;
    let crate_path: syn::Path = syn::parse_str(&enum_attrs.crate_path)?;

    // Resolve the plugin_id expression used in codegen.
    //
    // When `plugin_id_type` is supplied, the author is pinning a concrete
    // instantiation of a generic activation (e.g. `Cone<NoParent>`) so that the
    // referenced associated constant is unambiguous at the derive-expansion
    // site. In that case we emit `<TypePath>::TailIdent` — a fully-qualified
    // path expression — rather than the bare path, which would otherwise
    // trigger E0283 for `Cone<P: HubContext>` since `P` cannot be inferred.
    //
    // Without `plugin_id_type` we preserve the historical behaviour and emit
    // the `plugin_id` string as a plain path. This keeps all existing
    // non-generic call sites working unchanged.
    let plugin_id_expr: TokenStream2 = match enum_attrs.plugin_id_type.as_deref() {
        Some(type_str) => {
            let qualified_type: syn::Type = syn::parse_str(type_str).map_err(|e| {
                syn::Error::new_spanned(
                    &input,
                    format!("Invalid plugin_id_type '{}': {}", type_str, e),
                )
            })?;
            // The `plugin_id` string is treated as `TypeName::TAIL` — we keep
            // only the trailing segment and re-root it against the user-supplied
            // type. This lets the existing `Cone::PLUGIN_ID` surface keep
            // working when the author adds `plugin_id_type = "Cone<NoParent>"`.
            let plugin_id_path: syn::Path =
                syn::parse_str(&enum_attrs.plugin_id).map_err(|e| {
                    syn::Error::new_spanned(
                        &input,
                        format!(
                            "Invalid plugin_id path '{}': {}",
                            enum_attrs.plugin_id, e
                        ),
                    )
                })?;
            let tail = plugin_id_path.segments.last().ok_or_else(|| {
                syn::Error::new_spanned(
                    &input,
                    format!("plugin_id '{}' has no path segments", enum_attrs.plugin_id),
                )
            })?;
            let tail_ident = &tail.ident;
            quote! { <#qualified_type>::#tail_ident }
        }
        None => {
            let plugin_id_path: syn::Path =
                syn::parse_str(&enum_attrs.plugin_id).map_err(|e| {
                    syn::Error::new_spanned(
                        &input,
                        format!(
                            "Invalid plugin_id constant name '{}': {}",
                            enum_attrs.plugin_id, e
                        ),
                    )
                })?;
            quote! { #plugin_id_path }
        }
    };

    let version = &enum_attrs.version;

    // Extract variant information
    let variants = extract_variants(&input)?;

    if variants.is_empty() {
        return Err(syn::Error::new_spanned(
            &input,
            "HandleEnum requires at least one variant with #[handle(method = \"...\")]",
        ));
    }

    // Generate to_handle() match arms
    let to_handle_arms = generate_to_handle_arms(&variants, &plugin_id_expr, version);

    // Generate TryFrom match arms
    let try_from_arms = generate_try_from_arms(&variants, &crate_path);

    // Generate resolution_params() match arms
    let resolution_arms = generate_resolution_arms(&variants, &crate_path);

    Ok(quote! {
        impl #enum_name {
            /// Convert this handle variant to a generic Handle
            pub fn to_handle(&self) -> #crate_path::Handle {
                match self {
                    #(#to_handle_arms)*
                }
            }

            /// Get resolution parameters for database lookup
            ///
            /// Returns `Some(params)` for variants with `#[handle(table = "...", key = "...")]`,
            /// `None` for variants without resolution configuration.
            pub fn resolution_params(&self) -> Option<#crate_path::HandleResolutionParams> {
                match self {
                    #(#resolution_arms)*
                }
            }
        }

        impl From<#enum_name> for #crate_path::Handle {
            fn from(h: #enum_name) -> #crate_path::Handle {
                h.to_handle()
            }
        }

        impl TryFrom<&#crate_path::Handle> for #enum_name {
            type Error = #crate_path::HandleParseError;

            fn try_from(handle: &#crate_path::Handle) -> Result<Self, Self::Error> {
                // Verify plugin ownership
                if handle.plugin_id != #plugin_id_expr {
                    return Err(#crate_path::HandleParseError::WrongPlugin {
                        expected: #plugin_id_expr,
                        got: handle.plugin_id,
                    });
                }

                match handle.method.as_str() {
                    #(#try_from_arms)*
                    _ => Err(#crate_path::HandleParseError::UnknownMethod(handle.method.clone()))
                }
            }
        }
    })
}

fn extract_enum_attrs(input: &DeriveInput) -> syn::Result<HandleEnumAttrs> {
    for attr in &input.attrs {
        if attr.path().is_ident("handle") {
            if let Meta::List(MetaList { tokens, .. }) = &attr.meta {
                return syn::parse2(tokens.clone());
            }
        }
    }
    Err(syn::Error::new_spanned(
        input,
        "HandleEnum requires #[handle(plugin_id = \"...\", version = \"...\")] attribute",
    ))
}

fn extract_variants(input: &DeriveInput) -> syn::Result<Vec<VariantInfo>> {
    let data_enum = match &input.data {
        syn::Data::Enum(data) => data,
        _ => {
            return Err(syn::Error::new_spanned(
                input,
                "HandleEnum can only be derived for enums",
            ))
        }
    };

    let mut variants = Vec::new();

    for variant in &data_enum.variants {
        if let Some(attrs) = parse_variant_attrs(&variant.attrs)? {
            let fields: Vec<FieldInfo> = match &variant.fields {
                Fields::Named(named) => named
                    .named
                    .iter()
                    .map(|f| {
                        let name = f.ident.clone().unwrap();
                        let name_str = name.to_string();
                        FieldInfo { name, name_str }
                    })
                    .collect(),
                Fields::Unit => Vec::new(),
                Fields::Unnamed(_) => {
                    return Err(syn::Error::new_spanned(
                        variant,
                        "HandleEnum variants must use named fields (e.g., `Variant { field: Type }`)",
                    ))
                }
            };

            // Build resolution info if table and key are specified
            let resolution = if let (Some(table), Some(key)) = (attrs.table, attrs.key) {
                // Find the key field index
                let key_field_name = attrs.key_field.unwrap_or_else(|| {
                    // Default to first field
                    fields.first().map(|f| f.name_str.clone()).unwrap_or_default()
                });
                let key_field_index = fields
                    .iter()
                    .position(|f| f.name_str == key_field_name)
                    .unwrap_or(0);

                Some(ResolutionInfo {
                    table,
                    key,
                    key_field_index,
                    strip_prefix: attrs.strip_prefix,
                })
            } else {
                None
            };

            variants.push(VariantInfo {
                name: variant.ident.clone(),
                method: attrs.method,
                fields,
                resolution,
            });
        }
    }

    Ok(variants)
}

fn generate_to_handle_arms(
    variants: &[VariantInfo],
    plugin_id_expr: &TokenStream2,
    version: &str,
) -> Vec<TokenStream2> {
    variants
        .iter()
        .map(|v| {
            let variant_name = &v.name;
            let method = &v.method;

            if v.fields.is_empty() {
                // Unit-like variant with #[handle]
                quote! {
                    Self::#variant_name => {
                        plexus_core::Handle::new(#plugin_id_expr, #version, #method)
                    }
                }
            } else {
                // Named fields - extract them
                let field_names: Vec<_> = v.fields.iter().map(|f| &f.name).collect();
                let field_clones: Vec<_> = v
                    .fields
                    .iter()
                    .map(|f| {
                        let name = &f.name;
                        quote! { #name.clone() }
                    })
                    .collect();

                quote! {
                    Self::#variant_name { #(#field_names),* } => {
                        plexus_core::Handle::new(#plugin_id_expr, #version, #method)
                            .with_meta(vec![#(#field_clones),*])
                    }
                }
            }
        })
        .collect()
}

fn generate_try_from_arms(variants: &[VariantInfo], crate_path: &syn::Path) -> Vec<TokenStream2> {
    variants
        .iter()
        .map(|v| {
            let variant_name = &v.name;
            let method = &v.method;

            if v.fields.is_empty() {
                // Unit-like variant
                quote! {
                    #method => Ok(Self::#variant_name),
                }
            } else {
                // Named fields - extract from meta
                let field_extractions: Vec<_> = v
                    .fields
                    .iter()
                    .enumerate()
                    .map(|(idx, f)| {
                        let name = &f.name;
                        let name_str = &f.name_str;
                        quote! {
                            let #name = handle.meta.get(#idx)
                                .ok_or(#crate_path::HandleParseError::MissingMeta {
                                    index: #idx,
                                    field: #name_str,
                                })?
                                .clone();
                        }
                    })
                    .collect();

                let field_names: Vec<_> = v.fields.iter().map(|f| &f.name).collect();

                quote! {
                    #method => {
                        #(#field_extractions)*
                        Ok(Self::#variant_name { #(#field_names),* })
                    }
                }
            }
        })
        .collect()
}

fn generate_resolution_arms(variants: &[VariantInfo], crate_path: &syn::Path) -> Vec<TokenStream2> {
    variants
        .iter()
        .map(|v| {
            let variant_name = &v.name;

            match &v.resolution {
                Some(res) => {
                    let table = &res.table;
                    let key = &res.key;
                    let key_field_index = res.key_field_index;
                    let key_field_name = &v.fields[key_field_index].name;

                    // Build context from non-key fields
                    let context_items: Vec<_> = v
                        .fields
                        .iter()
                        .enumerate()
                        .filter(|(idx, _)| *idx != key_field_index)
                        .map(|(_, f)| {
                            let name = &f.name;
                            let name_str = &f.name_str;
                            quote! {
                                (#name_str.to_string(), #name.clone())
                            }
                        })
                        .collect();

                    // Handle strip_prefix
                    let key_value_expr = if let Some(prefix) = &res.strip_prefix {
                        quote! {
                            #key_field_name.strip_prefix(#prefix).unwrap_or(&#key_field_name).to_string()
                        }
                    } else {
                        quote! {
                            #key_field_name.clone()
                        }
                    };

                    if v.fields.is_empty() {
                        quote! {
                            Self::#variant_name => None,
                        }
                    } else {
                        let field_names: Vec<_> = v.fields.iter().map(|f| &f.name).collect();
                        quote! {
                            Self::#variant_name { #(#field_names),* } => Some(#crate_path::HandleResolutionParams {
                                table: #table,
                                key_column: #key,
                                key_value: #key_value_expr,
                                context: vec![#(#context_items),*],
                            }),
                        }
                    }
                }
                None => {
                    // No resolution configured - use `..` to ignore all fields
                    if v.fields.is_empty() {
                        quote! {
                            Self::#variant_name => None,
                        }
                    } else {
                        quote! {
                            Self::#variant_name { .. } => None,
                        }
                    }
                }
            }
        })
        .collect()
}