standout-macros 6.2.0

Proc macros for compile-time resource embedding in Standout
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
558
//! Derive macro for declarative command dispatch.
//!
//! This module provides [`Dispatch`] derive macro that generates dispatch configuration
//! from clap `Subcommand` enums, eliminating boilerplate command-to-handler mappings.
//!
//! For working examples, see `standout/tests/dispatch_derive.rs`.
//!
//! # Motivation
//!
//! Without this macro, you must explicitly map every command to its handler.
//! With `#[derive(Dispatch)]`, the mapping becomes implicit via naming conventions:
//!
//! - `Add` variant → `handlers::add` function
//! - `ListAll` variant → `handlers::list_all` function
//!
//! # Convention-Based Defaults
//!
//! - Handler: `{handlers_module}::{variant_snake_case}`
//! - Template: `{variant_snake_case}.j2`
//!
//! # Container Attributes
//!
//! Applied to the enum with `#[dispatch(...)]`:
//!
//! | Attribute | Required | Description |
//! |-----------|----------|-------------|
//! | `handlers = path` | Yes | Module containing handler functions |
//!
//! # Variant Attributes
//!
//! Applied to enum variants with `#[dispatch(...)]`:
//!
//! | Attribute | Description | Default |
//! |-----------|-------------|---------|
//! | `handler = path` | Handler function path | `{handlers}::{snake_case}` |
//! | `template = "path"` | Template file path | `{snake_case}.j2` |
//! | `pre_dispatch = fn` | Pre-dispatch hook | None |
//! | `post_dispatch = fn` | Post-dispatch hook | None |
//! | `post_output = fn` | Post-output hook | None |
//! | `nested` | Treat variant as nested subcommand | false |
//! | `skip` | Skip this variant | false |
//! | `default` | Use as default command when no subcommand specified | false |
//! | `list_view` | Enable ListView integration | false |
//! | `item_type` | Type name for tabular spec injection | None |
//! | `simple` | Handler only takes `&ArgMatches` (no context) | false |
//!
//! # Generated Code
//!
//! The macro generates a `dispatch_config()` method returning a closure for
//! use with `App::builder().commands()`.

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    spanned::Spanned,
    Data, DeriveInput, Error, Expr, Fields, Meta, Path, Result, Token,
};

/// Container-level attributes: `#[dispatch(handlers = path)]`
#[derive(Default)]
struct ContainerAttrs {
    handlers: Option<Path>,
}

/// Variant-level attributes: `#[dispatch(handler = path, template = "...", ...)]`
#[derive(Default)]
struct VariantAttrs {
    handler: Option<Path>,
    template: Option<String>,
    pre_dispatch: Option<Path>,
    post_dispatch: Option<Path>,
    post_output: Option<Path>,
    nested: bool,
    skip: bool,
    default: bool,
    list_view: bool,
    item_type: Option<String>,
    pipe_to: Option<String>,
    pipe_through: Option<String>,
    pipe_to_clipboard: bool,
    /// Handler only takes `&ArgMatches` (no `&CommandContext`)
    simple: bool,
    /// Handler is a pure function wrapped by `#[handler]` (auto-appends `__handler`)
    pure: bool,
}

/// Information extracted from a single enum variant
struct VariantInfo {
    snake_name: String,
    attrs: VariantAttrs,
    is_nested: bool,
    nested_type: Option<Path>,
}

impl Parse for ContainerAttrs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut attrs = ContainerAttrs::default();

        let content: Punctuated<Meta, Token![,]> = Punctuated::parse_terminated(input)?;

        for meta in content {
            match &meta {
                Meta::NameValue(nv) if nv.path.is_ident("handlers") => {
                    if let Expr::Path(expr_path) = &nv.value {
                        attrs.handlers = Some(expr_path.path.clone());
                    } else {
                        return Err(Error::new(nv.value.span(), "expected path"));
                    }
                }
                _ => {
                    return Err(Error::new(
                        meta.span(),
                        "unknown attribute, expected `handlers = path`",
                    ));
                }
            }
        }

        Ok(attrs)
    }
}

impl Parse for VariantAttrs {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut attrs = VariantAttrs::default();

        let content: Punctuated<Meta, Token![,]> = Punctuated::parse_terminated(input)?;

        for meta in content {
            match &meta {
                Meta::NameValue(nv) if nv.path.is_ident("handler") => {
                    if let Expr::Path(expr_path) = &nv.value {
                        attrs.handler = Some(expr_path.path.clone());
                    } else {
                        return Err(Error::new(nv.value.span(), "expected path"));
                    }
                }
                Meta::NameValue(nv) if nv.path.is_ident("template") => {
                    if let Expr::Lit(expr_lit) = &nv.value {
                        if let syn::Lit::Str(lit_str) = &expr_lit.lit {
                            attrs.template = Some(lit_str.value());
                        } else {
                            return Err(Error::new(nv.value.span(), "expected string literal"));
                        }
                    } else {
                        return Err(Error::new(nv.value.span(), "expected string literal"));
                    }
                }
                Meta::NameValue(nv) if nv.path.is_ident("pre_dispatch") => {
                    if let Expr::Path(expr_path) = &nv.value {
                        attrs.pre_dispatch = Some(expr_path.path.clone());
                    } else {
                        return Err(Error::new(nv.value.span(), "expected path"));
                    }
                }
                Meta::NameValue(nv) if nv.path.is_ident("post_dispatch") => {
                    if let Expr::Path(expr_path) = &nv.value {
                        attrs.post_dispatch = Some(expr_path.path.clone());
                    } else {
                        return Err(Error::new(nv.value.span(), "expected path"));
                    }
                }
                Meta::NameValue(nv) if nv.path.is_ident("post_output") => {
                    if let Expr::Path(expr_path) = &nv.value {
                        attrs.post_output = Some(expr_path.path.clone());
                    } else {
                        return Err(Error::new(nv.value.span(), "expected path"));
                    }
                }
                Meta::Path(p) if p.is_ident("nested") => {
                    attrs.nested = true;
                }
                Meta::Path(p) if p.is_ident("skip") => {
                    attrs.skip = true;
                }
                Meta::Path(p) if p.is_ident("default") => {
                    attrs.default = true;
                }
                Meta::Path(p) if p.is_ident("list_view") => {
                    attrs.list_view = true;
                }
                Meta::NameValue(nv) if nv.path.is_ident("item_type") => {
                    if let Expr::Lit(expr_lit) = &nv.value {
                        if let syn::Lit::Str(lit_str) = &expr_lit.lit {
                            attrs.item_type = Some(lit_str.value());
                        } else {
                            return Err(Error::new(nv.value.span(), "expected string literal"));
                        }
                    } else {
                        return Err(Error::new(nv.value.span(), "expected string literal"));
                    }
                }
                Meta::NameValue(nv) if nv.path.is_ident("pipe_to") => {
                    if let Expr::Lit(expr_lit) = &nv.value {
                        if let syn::Lit::Str(lit_str) = &expr_lit.lit {
                            attrs.pipe_to = Some(lit_str.value());
                        } else {
                            return Err(Error::new(nv.value.span(), "expected string literal"));
                        }
                    } else {
                        return Err(Error::new(nv.value.span(), "expected string literal"));
                    }
                }
                Meta::NameValue(nv) if nv.path.is_ident("pipe_through") => {
                    if let Expr::Lit(expr_lit) = &nv.value {
                        if let syn::Lit::Str(lit_str) = &expr_lit.lit {
                            attrs.pipe_through = Some(lit_str.value());
                        } else {
                            return Err(Error::new(nv.value.span(), "expected string literal"));
                        }
                    } else {
                        return Err(Error::new(nv.value.span(), "expected string literal"));
                    }
                }
                Meta::Path(p) if p.is_ident("pipe_to_clipboard") => {
                    attrs.pipe_to_clipboard = true;
                }
                Meta::Path(p) if p.is_ident("simple") => {
                    attrs.simple = true;
                }
                Meta::Path(p) if p.is_ident("pure") => {
                    attrs.pure = true;
                }
                _ => {
                    return Err(Error::new(
                        meta.span(),
                        "unknown attribute, expected one of: handler, template, pre_dispatch, post_dispatch, post_output, nested, skip, default, list_view, item_type, pipe_to, pipe_through, pipe_to_clipboard, simple, pure",
                    ));
                }
            }
        }

        Ok(attrs)
    }
}

/// Converts PascalCase to snake_case
fn to_snake_case(s: &str) -> String {
    let mut result = String::new();
    for (i, c) in s.chars().enumerate() {
        if c.is_uppercase() {
            if i > 0 {
                result.push('_');
            }
            result.push(c.to_lowercase().next().unwrap());
        } else {
            result.push(c);
        }
    }
    result
}

/// Extract container-level `#[dispatch(...)]` attributes
fn parse_container_attrs(input: &DeriveInput) -> Result<ContainerAttrs> {
    for attr in &input.attrs {
        if attr.path().is_ident("dispatch") {
            return attr.parse_args::<ContainerAttrs>();
        }
    }

    Err(Error::new(
        input.span(),
        "missing `#[dispatch(handlers = path)]` attribute",
    ))
}

/// Extract variant-level `#[dispatch(...)]` attributes
fn parse_variant_attrs(attrs: &[syn::Attribute]) -> Result<VariantAttrs> {
    for attr in attrs {
        if attr.path().is_ident("dispatch") {
            return attr.parse_args::<VariantAttrs>();
        }
    }
    Ok(VariantAttrs::default())
}

/// Check if a variant is a nested subcommand (tuple with single type argument)
fn is_nested_subcommand(fields: &Fields) -> Option<Path> {
    if let Fields::Unnamed(unnamed) = fields {
        if unnamed.unnamed.len() == 1 {
            let field = unnamed.unnamed.first().unwrap();
            if let syn::Type::Path(type_path) = &field.ty {
                // Assume it's a nested subcommand if it's a path type
                // This heuristic works because Args types typically don't have
                // a Dispatch derive, so the generated code will fail at compile
                // time if misused
                return Some(type_path.path.clone());
            }
        }
    }
    None
}

/// Main implementation of the Dispatch derive macro
pub fn dispatch_derive_impl(input: DeriveInput) -> Result<TokenStream> {
    let container_attrs = parse_container_attrs(&input)?;
    let handlers_path = container_attrs.handlers.ok_or_else(|| {
        Error::new(
            input.span(),
            "missing `handlers` in `#[dispatch(handlers = path)]`",
        )
    })?;

    let enum_name = &input.ident;

    let data = match &input.data {
        Data::Enum(data) => data,
        _ => {
            return Err(Error::new(
                input.span(),
                "Dispatch can only be derived for enums",
            ))
        }
    };

    // Collect variant info
    let mut variants: Vec<VariantInfo> = Vec::new();

    for variant in &data.variants {
        let attrs = parse_variant_attrs(&variant.attrs)?;

        if attrs.skip {
            continue;
        }

        let snake_name = to_snake_case(&variant.ident.to_string());
        let nested_type_candidate = is_nested_subcommand(&variant.fields);

        // Determine is_nested:
        // 1. If explicit #[dispatch(nested)], it MUST be nested (and must have a valid nested type).
        // 2. If NO explicit nested, it is a leaf command (default), even if it looks like a nested one.
        //    This fixes the bug where Command(String) was treated as nested.
        let is_nested = attrs.nested;

        if is_nested && nested_type_candidate.is_none() {
            return Err(Error::new(
                variant.span(),
                "#[dispatch(nested)] requires a tuple variant with a single field (the nested subcommand enum)",
            ));
        }

        variants.push(VariantInfo {
            snake_name,
            attrs,
            is_nested,
            nested_type: nested_type_candidate,
        });
    }

    // Find the default command (if any)
    let default_command: Option<&str> = {
        let defaults: Vec<_> = variants.iter().filter(|v| v.attrs.default).collect();

        if defaults.len() > 1 {
            // This will be caught at runtime by GroupBuilder::default_command panic,
            // but we can provide a better error at compile time
            let names: Vec<_> = defaults.iter().map(|v| v.snake_name.as_str()).collect();
            return Err(Error::new(
                input.span(),
                format!(
                    "Only one command can be marked as default. Found multiple: {}",
                    names.join(", ")
                ),
            ));
        }

        defaults.first().map(|v| v.snake_name.as_str())
    };

    // Generate the command registration calls
    let command_registrations: Vec<TokenStream> = variants
        .iter()
        .map(|v| {
            let cmd_name = &v.snake_name;

            if v.is_nested {
                // Nested subcommand - delegate to its dispatch_config
                let nested_type = v.nested_type.as_ref().unwrap();
                quote! {
                    let __builder = __builder.group(#cmd_name, #nested_type::dispatch_config());
                }
            } else {
                // Leaf command
                let handler_path = v.attrs.handler.clone().unwrap_or_else(|| {
                    let mut handler_name = v.snake_name.clone();
                    if v.attrs.pure {
                        handler_name = format!("{}__handler", handler_name);
                    }
                    let handler_ident = format_ident!("{}", handler_name);
                    let mut path = handlers_path.clone();
                    path.segments.push(syn::PathSegment {
                        ident: handler_ident,
                        arguments: syn::PathArguments::None,
                    });
                    path
                });

                // If list_view is enabled, default template if not set
                let mut v_template = v.attrs.template.clone();
                if v.attrs.list_view && v_template.is_none() {
                    v_template = Some("standout/list-view".to_string());
                }

                let has_config = v_template.is_some()
                    || v.attrs.pre_dispatch.is_some()
                    || v.attrs.post_dispatch.is_some()
                    || v.attrs.post_output.is_some()
                    || (v.attrs.list_view && v.attrs.item_type.is_some())
                    || v.attrs.pipe_to.is_some()
                    || v.attrs.pipe_through.is_some()
                    || v.attrs.pipe_to_clipboard;

                // Determine the handler expression (original or wrapped)
                // Simple handlers only take &ArgMatches, so we wrap them in a closure
                // that ignores the context parameter
                let handler_expr = if v.attrs.list_view {
                     if let Some(item_type_str) = &v.attrs.item_type {
                        let item_type_path: syn::Path = syn::parse_str(item_type_str)
                            .expect("Failed to parse item_type as path");
                        // Generate wrapper to inject tabular spec
                        // Handle both simple and regular handlers
                        if v.attrs.simple {
                            quote! {
                                |matches, _ctx| {
                                    let result = #handler_path(matches);
                                    result.map(|output| {
                                        match output {
                                            ::standout::cli::handler::Output::Render(mut lv) => {
                                                 lv.tabular_spec = Some(<#item_type_path as ::standout::tabular::Tabular>::tabular_spec());
                                                 ::standout::cli::handler::Output::Render(lv)
                                            }
                                            o => o
                                        }
                                    })
                                }
                            }
                        } else {
                            quote! {
                                |matches, ctx| {
                                    let result = #handler_path(matches, ctx);
                                    result.map(|output| {
                                        match output {
                                            ::standout::cli::handler::Output::Render(mut lv) => {
                                                 lv.tabular_spec = Some(<#item_type_path as ::standout::tabular::Tabular>::tabular_spec());
                                                 ::standout::cli::handler::Output::Render(lv)
                                            }
                                            o => o
                                        }
                                    })
                                }
                            }
                        }
                     } else if v.attrs.simple {
                        // Simple handler without list_view
                        quote! { |matches, _ctx| #handler_path(matches) }
                     } else {
                        quote! { #handler_path }
                     }
                } else if v.attrs.simple {
                    // Simple handler (only takes &ArgMatches, no context)
                    quote! { |matches, _ctx| #handler_path(matches) }
                } else {
                    quote! { #handler_path }
                };

                if has_config {
                    // Use command_with for custom configuration
                    let template_call = v_template.as_ref().map(|t| {
                        quote! { __cfg = __cfg.template(#t); }
                    });
                    let pre_dispatch_call = v.attrs.pre_dispatch.as_ref().map(|p| {
                        quote! { __cfg = __cfg.pre_dispatch(#p); }
                    });
                    let post_dispatch_call = v.attrs.post_dispatch.as_ref().map(|p| {
                        quote! { __cfg = __cfg.post_dispatch(#p); }
                    });
                    let post_output_call = v.attrs.post_output.as_ref().map(|p| {
                        quote! { __cfg = __cfg.post_output(#p); }
                    });
                    let pipe_to_call = v.attrs.pipe_to.as_ref().map(|p| {
                        quote! { __cfg = __cfg.pipe_to(#p); }
                    });
                    let pipe_through_call = v.attrs.pipe_through.as_ref().map(|p| {
                        quote! { __cfg = __cfg.pipe_through(#p); }
                    });
                     let pipe_clipboard_call = if v.attrs.pipe_to_clipboard {
                        Some(quote! { __cfg = __cfg.pipe_to_clipboard(); })
                    } else {
                        None
                    };

                    quote! {
                        let __builder = __builder.command_with(#cmd_name, #handler_expr, |mut __cfg| {
                            #template_call
                            #pre_dispatch_call
                            #post_dispatch_call
                            #post_output_call
                            #pipe_to_call
                            #pipe_through_call
                            #pipe_clipboard_call
                            __cfg
                        });
                    }
                } else {
                    // Simple command registration
                    quote! {
                        let __builder = __builder.command(#cmd_name, #handler_expr);
                    }
                }
            }
        })
        .collect();

    // Generate default command registration if one was marked
    let default_command_registration = default_command.map(|name| {
        quote! {
            let __builder = __builder.default_command(#name);
        }
    });

    let expanded = quote! {
        impl #enum_name {
            /// Returns a dispatch configuration closure for use with `App::builder().commands()`.
            ///
            /// Generated by `#[derive(Dispatch)]`.
            pub fn dispatch_config() -> impl FnOnce(::standout::cli::GroupBuilder) -> ::standout::cli::GroupBuilder {
                |__builder: ::standout::cli::GroupBuilder| {
                    #(#command_registrations)*
                    #default_command_registration
                    __builder
                }
            }
        }
    };

    Ok(expanded)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_snake_case() {
        assert_eq!(to_snake_case("Add"), "add");
        assert_eq!(to_snake_case("ListAll"), "list_all");
        assert_eq!(to_snake_case("HTTPServer"), "h_t_t_p_server");
        assert_eq!(to_snake_case("getHTTPResponse"), "get_h_t_t_p_response");
    }

    #[test]
    fn test_to_snake_case_simple() {
        assert_eq!(to_snake_case("Complete"), "complete");
        assert_eq!(to_snake_case("Delete"), "delete");
    }
}