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
//! Custom derive support for `abscissa_core::command::Command`.

use ident_case::RenameRule;
use proc_macro2::TokenStream;
use quote::quote;
use synstructure::Structure;

/// Custom derive for `abscissa_core::command::Command`
pub fn derive_command(s: Structure<'_>) -> TokenStream {
    let subcommand_usage = match &s.ast().data {
        syn::Data::Enum(data) => impl_subcommand_usage_for_enum(data),
        _ => quote!(),
    };

    s.gen_impl(quote! {
        gen impl Command for @Self {
            #[doc = "Name of this program as a string"]
            fn name() -> &'static str {
                env!("CARGO_PKG_NAME")
            }

            #[doc = "Description of this program"]
            fn description() -> &'static str {
                env!("CARGO_PKG_DESCRIPTION").trim()
            }

            #[doc = "Version of this program"]
            fn version() -> &'static str {
                env!("CARGO_PKG_VERSION")
            }

            #[doc = "Authors of this program"]
            fn authors() -> &'static str {
                env!("CARGO_PKG_AUTHORS")
            }

            #subcommand_usage
        }
    })
}

/// Impl `subcommand_usage` which walks the enum variants and returns
/// usage info for them.
fn impl_subcommand_usage_for_enum(data: &syn::DataEnum) -> TokenStream {
    let match_arms = data.variants.iter().map(|variant| {
        // TODO(tarcieri): support `#[options(name = "...")]` attribute
        let name = RenameRule::KebabCase.apply_to_variant(variant.ident.to_string());

        let subcommand = match &variant.fields {
            syn::Fields::Unnamed(fields) => {
                if fields.unnamed.len() == 1 {
                    Some(&fields.unnamed.first().unwrap().ty)
                } else {
                    None
                }
            }
            syn::Fields::Unit | syn::Fields::Named(_) => None,
        }
        .unwrap_or_else(|| panic!("command variants must be unary tuples"));

        quote! {
            #name => {
                Some(abscissa_core::command::Usage::for_command::<#subcommand>())
            }
        }
    });

    quote! {
        #[doc = "get usage information for the named subcommand"]
        fn subcommand_usage(command: &str) -> Option<abscissa_core::command::Usage> {
            match command {
                #(#match_arms)*
                _ => None
            }
        }
    }
}

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

    #[test]
    fn derive_command_on_struct() {
        test_derive! {
            derive_command {
                struct MyCommand {}
            }
            expands to {
                #[allow(non_upper_case_globals)]
                const _DERIVE_Command_FOR_MyCommand: () = {
                    impl Command for MyCommand {
                        #[doc = "Name of this program as a string"]
                        fn name() -> & 'static str {
                            env!("CARGO_PKG_NAME")
                        }

                        #[doc = "Description of this program"]
                        fn description () -> & 'static str {
                            env!("CARGO_PKG_DESCRIPTION" ).trim()
                        }

                        #[doc = "Version of this program"]
                        fn version() -> & 'static str {
                            env!( "CARGO_PKG_VERSION")
                        }

                        #[doc = "Authors of this program"]
                        fn authors() -> & 'static str {
                            env!("CARGO_PKG_AUTHORS")
                        }
                    }
                };
            }
            no_build // tests the code compiles are in the `abscissa` crate
        }
    }

    #[test]
    fn derive_command_on_enum() {
        test_derive! {
            derive_command {
                enum MyCommand {
                    Foo(A),
                    Bar(B),
                    Baz(C),
                }
            }
            expands to {
                #[allow(non_upper_case_globals)]
                const _DERIVE_Command_FOR_MyCommand: () = {
                    impl Command for MyCommand {
                        #[doc = "Name of this program as a string"]
                        fn name() -> & 'static str {
                            env!("CARGO_PKG_NAME")
                        }

                        #[doc = "Description of this program"]
                        fn description () -> & 'static str {
                            env!("CARGO_PKG_DESCRIPTION" ).trim()
                        }

                        #[doc = "Version of this program"]
                        fn version() -> & 'static str {
                            env!( "CARGO_PKG_VERSION")
                        }

                        #[doc = "Authors of this program"]
                        fn authors() -> & 'static str {
                            env!("CARGO_PKG_AUTHORS")
                        }

                        #[doc = "get usage information for the named subcommand"]
                        fn subcommand_usage(command: &str) -> Option <abscissa_core::command::Usage > {
                            match command {
                                "foo" => {
                                    Some(abscissa_core::command::Usage::for_command::<A>())
                                }
                                "bar" => {
                                    Some(abscissa_core::command::Usage::for_command::<B>())
                                }
                                "baz" => {
                                    Some(abscissa_core::command::Usage::for_command::<C>())
                                }
                                _ => None
                            }
                        }
                    }
                };
            }
            no_build // tests the code compiles are in the `abscissa` crate
        }
    }
}