1 // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
2 // Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
3 // Ana Hobden (@hoverbear) <operator@hoverbear.org>
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //
11 // This work was derived from Structopt (https://github.com/TeXitoi/structopt)
12 // commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
13 // MIT/Apache 2.0 license.
14
15 use proc_macro2::{Span, TokenStream};
16 use quote::quote;
17 use syn::{Generics, Ident};
18
19 use crate::item::Item;
20
gen_for_struct( item: &Item, item_name: &Ident, generics: &Generics, ) -> Result<TokenStream, syn::Error>21 pub fn gen_for_struct(
22 item: &Item,
23 item_name: &Ident,
24 generics: &Generics,
25 ) -> Result<TokenStream, syn::Error> {
26 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
27
28 let name = item.cased_name();
29 let app_var = Ident::new("__clap_app", Span::call_site());
30
31 let tokens = quote! {
32 #[allow(dead_code, unreachable_code, unused_variables, unused_braces)]
33 #[allow(
34 clippy::style,
35 clippy::complexity,
36 clippy::pedantic,
37 clippy::restriction,
38 clippy::perf,
39 clippy::deprecated,
40 clippy::nursery,
41 clippy::cargo,
42 clippy::suspicious_else_formatting,
43 clippy::almost_swapped,
44 )]
45 impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause {
46 fn command<'b>() -> clap::Command {
47 let #app_var = clap::Command::new(#name);
48 <Self as clap::Args>::augment_args(#app_var)
49 }
50
51 fn command_for_update<'b>() -> clap::Command {
52 let #app_var = clap::Command::new(#name);
53 <Self as clap::Args>::augment_args_for_update(#app_var)
54 }
55 }
56 };
57
58 Ok(tokens)
59 }
60
gen_for_enum( item: &Item, item_name: &Ident, generics: &Generics, ) -> Result<TokenStream, syn::Error>61 pub fn gen_for_enum(
62 item: &Item,
63 item_name: &Ident,
64 generics: &Generics,
65 ) -> Result<TokenStream, syn::Error> {
66 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
67
68 let name = item.cased_name();
69 let app_var = Ident::new("__clap_app", Span::call_site());
70
71 Ok(quote! {
72 #[allow(dead_code, unreachable_code, unused_variables, unused_braces)]
73 #[allow(
74 clippy::style,
75 clippy::complexity,
76 clippy::pedantic,
77 clippy::restriction,
78 clippy::perf,
79 clippy::deprecated,
80 clippy::nursery,
81 clippy::cargo,
82 clippy::suspicious_else_formatting,
83 clippy::almost_swapped,
84 )]
85 impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause {
86 fn command<'b>() -> clap::Command {
87 let #app_var = clap::Command::new(#name)
88 .subcommand_required(true)
89 .arg_required_else_help(true);
90 <Self as clap::Subcommand>::augment_subcommands(#app_var)
91 }
92
93 fn command_for_update<'b>() -> clap::Command {
94 let #app_var = clap::Command::new(#name);
95 <Self as clap::Subcommand>::augment_subcommands_for_update(#app_var)
96 .subcommand_required(false)
97 .arg_required_else_help(false)
98 }
99 }
100 })
101 }
102