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
#![feature(proc_macro, proc_macro_lib)]
#![allow(unused_imports, unused_variables)]
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
use quote::ToTokens;
use std::collections::HashSet as Set;
use syn::fold::{self, Fold};
use syn::punctuated::Punctuated;
use syn::synom::Synom;
use syn::LitStr;
use syn::{
Expr, FnArg, Ident, ImplItem, ImplItemMethod, Item, ItemImpl, ItemStatic, Pat, Stmt, Type,
};
#[proc_macro_attribute]
pub fn thunderclap(_args: TokenStream, input: TokenStream) -> TokenStream {
let i: ItemImpl = match syn::parse(input.clone()) {
Ok(input) => input,
Err(e) => panic!("Error: '{}'", e),
};
let (name, app_token) = match *i.self_ty {
Type::Path(ref p) => {
let meh = p.path.segments[0].ident;
(format!("{}", p.path.segments[0].ident), quote!( #meh ))
}
_ => (format!("Unknown App"), quote!()),
};
let about = match i.attrs.first() {
Some(a) => String::from(
format!("{}", a.tts)
.replace("/", "")
.replace("\\", "")
.replace("\"", "")
.replace("=", "").trim(),
),
_ => String::new(),
};
let mut matches: Vec<quote::Tokens> = Vec::new();
let orignal = quote!(#i);
let mut app = quote! {
App::new(#name).about(#about).setting(AppSettings::SubcommandRequired)
};
for item in &i.items {
match item {
&ImplItem::Method(ref i) => {
let name = LitStr::new(&i.sig.ident.to_string(), i.sig.ident.span);
let func_id = &i.sig.ident;
let about = match i.attrs.first() {
Some(a) => String::from(
format!("{}", a.tts)
.replace("/", "")
.replace("\\", "")
.replace("\"", "")
.replace("=", "").trim(),
),
_ => String::new(),
};
let mut arguments = quote!();
let mut index: usize = 0;
let args = i.sig
.decl
.inputs
.iter()
.fold(quote!{}, |acc, arg| match arg {
&FnArg::Captured(ref arg) => match &arg.pat {
&Pat::Ident(ref i) => {
let n = format!("{}", i.ident);
arguments = quote! {
#arguments
m.value_of(#n).unwrap(),
};
index += 1;
quote! { #acc.arg(Arg::with_name(#n)) }
}
_ => quote!{ #acc },
},
_ => quote!{ #acc },
});
app = quote! {
#app.subcommand(
SubCommand::with_name(#name).about(#about)#args
)
};
matches.push(quote! { (#name, Some(m)) => #app_token :: #func_id ( #arguments ), });
}
_ => {}
}
}
let mut matchy = quote!{};
for m in &matches {
matchy = quote! {
#matchy
#m
};
}
matchy = quote! {
match args.subcommand() {
#matchy
_ => { },
}
};
let tokens = quote! {
#orignal
#[allow(unused)]
impl #app_token {
fn start() {
use clap::{App, SubCommand, Arg, AppSettings};
let app = #app;
let args = app.get_matches();
#matchy
}
}
};
tokens.into()
}