• 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::TokenStream;
16 use proc_macro_error::abort_call_site;
17 use quote::quote;
18 use syn::Ident;
19 use syn::Variant;
20 use syn::{
21     self, punctuated::Punctuated, token::Comma, Data, DataStruct, DeriveInput, Field, Fields,
22     Generics,
23 };
24 
25 use crate::derives::{args, into_app, subcommand};
26 use crate::dummies;
27 use crate::item::Item;
28 use crate::item::Name;
29 
derive_parser(input: &DeriveInput) -> TokenStream30 pub fn derive_parser(input: &DeriveInput) -> TokenStream {
31     let ident = &input.ident;
32     let pkg_name = std::env::var("CARGO_PKG_NAME").ok().unwrap_or_default();
33 
34     match input.data {
35         Data::Struct(DataStruct {
36             fields: Fields::Named(ref fields),
37             ..
38         }) => {
39             dummies::parser_struct(ident);
40 
41             let name = Name::Assigned(quote!(#pkg_name));
42             let item = Item::from_args_struct(input, name);
43             let fields = fields
44                 .named
45                 .iter()
46                 .map(|field| {
47                     let item = Item::from_args_field(field, item.casing(), item.env_casing());
48                     (field, item)
49                 })
50                 .collect::<Vec<_>>();
51             gen_for_struct(&item, ident, &input.generics, &fields)
52         }
53         Data::Struct(DataStruct {
54             fields: Fields::Unit,
55             ..
56         }) => {
57             dummies::parser_struct(ident);
58 
59             let name = Name::Assigned(quote!(#pkg_name));
60             let item = Item::from_args_struct(input, name);
61             let fields = Punctuated::<Field, Comma>::new();
62             let fields = fields
63                 .iter()
64                 .map(|field| {
65                     let item = Item::from_args_field(field, item.casing(), item.env_casing());
66                     (field, item)
67                 })
68                 .collect::<Vec<_>>();
69             gen_for_struct(&item, ident, &input.generics, &fields)
70         }
71         Data::Enum(ref e) => {
72             dummies::parser_enum(ident);
73 
74             let name = Name::Assigned(quote!(#pkg_name));
75             let item = Item::from_subcommand_enum(input, name);
76             let variants = e
77                 .variants
78                 .iter()
79                 .map(|variant| {
80                     let item =
81                         Item::from_subcommand_variant(variant, item.casing(), item.env_casing());
82                     (variant, item)
83                 })
84                 .collect::<Vec<_>>();
85             gen_for_enum(&item, ident, &input.generics, &variants)
86         }
87         _ => abort_call_site!("`#[derive(Parser)]` only supports non-tuple structs and enums"),
88     }
89 }
90 
gen_for_struct( item: &Item, item_name: &Ident, generics: &Generics, fields: &[(&Field, Item)], ) -> TokenStream91 fn gen_for_struct(
92     item: &Item,
93     item_name: &Ident,
94     generics: &Generics,
95     fields: &[(&Field, Item)],
96 ) -> TokenStream {
97     let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
98 
99     let into_app = into_app::gen_for_struct(item, item_name, generics);
100     let args = args::gen_for_struct(item, item_name, generics, fields);
101 
102     quote! {
103         impl #impl_generics clap::Parser for #item_name #ty_generics #where_clause {}
104 
105         #into_app
106         #args
107     }
108 }
109 
gen_for_enum( item: &Item, item_name: &Ident, generics: &Generics, variants: &[(&Variant, Item)], ) -> TokenStream110 fn gen_for_enum(
111     item: &Item,
112     item_name: &Ident,
113     generics: &Generics,
114     variants: &[(&Variant, Item)],
115 ) -> TokenStream {
116     let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
117 
118     let into_app = into_app::gen_for_enum(item, item_name, generics);
119     let subcommand = subcommand::gen_for_enum(item, item_name, generics, variants);
120 
121     quote! {
122         impl #impl_generics clap::Parser for #item_name #ty_generics #where_clause {}
123 
124         #into_app
125         #subcommand
126     }
127 }
128