• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) -> TokenStream21 pub fn gen_for_struct(item: &Item, item_name: &Ident, generics: &Generics) -> TokenStream {
22     let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
23 
24     let name = item.cased_name();
25     let app_var = Ident::new("__clap_app", Span::call_site());
26 
27     let tokens = quote! {
28         #[allow(dead_code, unreachable_code, unused_variables, unused_braces)]
29         #[allow(
30             clippy::style,
31             clippy::complexity,
32             clippy::pedantic,
33             clippy::restriction,
34             clippy::perf,
35             clippy::deprecated,
36             clippy::nursery,
37             clippy::cargo,
38             clippy::suspicious_else_formatting,
39             clippy::almost_swapped,
40         )]
41         impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause {
42             fn command<'b>() -> clap::Command {
43                 let #app_var = clap::Command::new(#name);
44                 <Self as clap::Args>::augment_args(#app_var)
45             }
46 
47             fn command_for_update<'b>() -> clap::Command {
48                 let #app_var = clap::Command::new(#name);
49                 <Self as clap::Args>::augment_args_for_update(#app_var)
50             }
51         }
52     };
53 
54     tokens
55 }
56 
gen_for_enum(item: &Item, item_name: &Ident, generics: &Generics) -> TokenStream57 pub fn gen_for_enum(item: &Item, item_name: &Ident, generics: &Generics) -> TokenStream {
58     let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
59 
60     let name = item.cased_name();
61     let app_var = Ident::new("__clap_app", Span::call_site());
62 
63     quote! {
64         #[allow(dead_code, unreachable_code, unused_variables, unused_braces)]
65         #[allow(
66             clippy::style,
67             clippy::complexity,
68             clippy::pedantic,
69             clippy::restriction,
70             clippy::perf,
71             clippy::deprecated,
72             clippy::nursery,
73             clippy::cargo,
74             clippy::suspicious_else_formatting,
75             clippy::almost_swapped,
76         )]
77         impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause {
78             fn command<'b>() -> clap::Command {
79                 let #app_var = clap::Command::new(#name)
80                     .subcommand_required(true)
81                     .arg_required_else_help(true);
82                 <Self as clap::Subcommand>::augment_subcommands(#app_var)
83             }
84 
85             fn command_for_update<'b>() -> clap::Command {
86                 let #app_var = clap::Command::new(#name);
87                 <Self as clap::Subcommand>::augment_subcommands_for_update(#app_var)
88                     .subcommand_required(false)
89                     .arg_required_else_help(false)
90             }
91         }
92     }
93 }
94