forked from TeXitoi/structopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattrs.rs
308 lines (301 loc) · 11.1 KB
/
attrs.rs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::{env, mem};
use quote::Tokens;
use syn::{self, Attribute, MetaNameValue, MetaList, LitStr, TypePath};
use syn::Type::Path;
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Ty {
Bool,
Vec,
Option,
Other,
}
#[derive(Debug)]
pub struct Attrs {
name: String,
methods: Vec<Method>,
parser: (Parser, Tokens),
has_custom_parser: bool,
is_subcommand: bool,
ty: Ty,
}
#[derive(Debug)]
struct Method {
name: String,
args: Tokens,
}
#[derive(Debug, PartialEq)]
pub enum Parser {
FromStr,
TryFromStr,
FromOsStr,
TryFromOsStr,
FromOccurrences,
}
impl ::std::str::FromStr for Parser {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"from_str" => Ok(Parser::FromStr),
"try_from_str" => Ok(Parser::TryFromStr),
"from_os_str" => Ok(Parser::FromOsStr),
"try_from_os_str" => Ok(Parser::TryFromOsStr),
"from_occurrences" => Ok(Parser::FromOccurrences),
_ => Err(format!("unsupported parser {}", s))
}
}
}
impl Attrs {
fn new(name: String) -> Attrs {
Attrs {
name: name,
methods: vec![],
parser: (Parser::TryFromStr, quote!(::std::str::FromStr::from_str)),
has_custom_parser: false,
is_subcommand: false,
ty: Ty::Other,
}
}
fn push_str_method(&mut self, name: &str, arg: &str) {
match (name, arg) {
("about", "") | ("version", "") | ("author", "") => {
let methods = mem::replace(&mut self.methods, vec![]);
self.methods = methods
.into_iter()
.filter(|m| m.name != name)
.collect();
}
("name", new_name) => self.name = new_name.into(),
(name, arg) => self.methods.push(Method {
name: name.to_string(),
args: quote!(#arg),
}),
}
}
fn push_attrs(&mut self, attrs: &[Attribute]) {
use Meta::*;
use NestedMeta::*;
use Lit::*;
let iter = attrs.iter()
.filter_map(|attr| {
let path = &attr.path;
match quote!(#path) == quote!(structopt) {
true => Some(
attr.interpret_meta()
.expect(&format!("invalid structopt syntax: {}", quote!(attr)))
),
false => None,
}
}).
flat_map(|m| match m {
List(l) => l.nested,
tokens => panic!("unsupported syntax: {}", quote!(#tokens).to_string()),
})
.map(|m| match m {
Meta(m) => m,
ref tokens => panic!("unsupported syntax: {}", quote!(#tokens).to_string()),
});
for attr in iter {
match attr {
NameValue(MetaNameValue { ident, lit: Str(ref value), .. }) =>
self.push_str_method(ident.as_ref(), &value.value()),
NameValue(MetaNameValue { ident, lit, .. }) => {
self.methods.push(Method {
name: ident.to_string(),
args: quote!(#lit),
})
}
List(MetaList { ident, ref nested, .. }) if ident == "parse" => {
if nested.len() != 1 {
panic!("parse must have exactly one argument");
}
self.has_custom_parser = true;
self.parser = match nested[0] {
Meta(NameValue(MetaNameValue { ident, lit: Str(ref v), .. })) => {
let function: syn::Path = v.parse().expect("parser function path");
let parser = ident.as_ref().parse().unwrap();
(parser, quote!(#function))
}
Meta(Word(ref i)) => {
use Parser::*;
let parser = i.as_ref().parse().unwrap();
let function = match parser {
FromStr => quote!(::std::convert::From::from),
TryFromStr => quote!(::std::str::FromStr::from_str),
FromOsStr => quote!(::std::convert::From::from),
TryFromOsStr => panic!("cannot omit parser function name with `try_from_os_str`"),
FromOccurrences => quote!({|v| v as _}),
};
(parser, function)
}
ref l @ _ => panic!("unknown value parser specification: {}", quote!(#l)),
};
}
List(MetaList { ident, ref nested, .. }) if ident == "raw" => {
for method in nested {
match *method {
Meta(NameValue(MetaNameValue { ident, lit: Str(ref v), .. })) =>
self.push_raw_method(ident.as_ref(), v),
ref mi @ _ => panic!("unsupported raw entry: {}", quote!(#mi)),
}
}
}
Word(ref w) if w == "subcommand" => self.is_subcommand = true,
ref i @ List(..) | ref i @ Word(..) =>
panic!("unsupported option: {}", quote!(#i)),
}
}
}
fn push_raw_method(&mut self, name: &str, args: &LitStr) {
let ts: ::proc_macro2::TokenStream = args.value().parse()
.expect(&format!("bad parameter {} = {}: the parameter must be valid rust code", name, quote!(#args)));
self.methods.push(Method {
name: name.to_string(),
args: quote!(#(#ts)*),
})
}
fn push_doc_comment(&mut self, attrs: &[Attribute], name: &str) {
let doc_comments: Vec<_> = attrs.iter()
.filter_map(|attr| {
let path = &attr.path;
match quote!(#path) == quote!(doc) {
true => attr.interpret_meta(),
false => None,
}
})
.filter_map(|attr| {
use Meta::*;
use Lit::*;
if let NameValue(MetaNameValue { ident, lit: Str(s), .. }) = attr {
if ident != "doc" { return None; }
let value = s.value();
let text = value
.trim_left_matches("//!")
.trim_left_matches("///")
.trim_left_matches("/*!")
.trim_left_matches("/**")
.trim_right_matches("*/")
.trim();
if text.is_empty() {
Some("\n\n".to_string())
} else{
Some(text.to_string())
}
} else {
None
}
})
.collect();
if doc_comments.is_empty() { return; }
let arg = doc_comments
.join(" ")
.split('\n')
.map(|l| l.trim().to_string())
.collect::<Vec<_>>()
.join("\n");
self.methods.push(Method {
name: name.to_string(),
args: quote!(#arg),
});
}
pub fn from_struct(attrs: &[Attribute], name: String) -> Attrs {
let mut res = Self::new(name);
let attrs_with_env = [
("version", "CARGO_PKG_VERSION"),
("about", "CARGO_PKG_DESCRIPTION"),
("author", "CARGO_PKG_AUTHORS"),
];
attrs_with_env.iter()
.filter_map(|&(m, v)| env::var(v).ok().and_then(|arg| Some((m, arg))))
.filter(|&(_, ref arg)| !arg.is_empty())
.for_each(|(name, arg)| {
if arg == "author" { arg.replace(":", ", "); }
res.push_str_method(name, &arg);
});
res.push_doc_comment(attrs, "about");
res.push_attrs(attrs);
if res.has_custom_parser {
panic!("parse attribute is only allowed on fields");
}
if res.is_subcommand {
panic!("subcommand is only allowed on fields");
}
res
}
pub fn from_field(field: &syn::Field) -> Attrs {
let name = field.ident.as_ref().unwrap().as_ref().to_string();
let mut res = Self::new(name);
res.push_doc_comment(&field.attrs, "help");
res.push_attrs(&field.attrs);
if res.is_subcommand {
if res.has_custom_parser {
panic!("parse attribute is not allowed for subcommand");
}
if !res.methods.iter().all(|m| m.name == "help") {
panic!("methods in attributes is not allowed for subcommand");
}
}
if let Path(TypePath { path: syn::Path { ref segments, .. }, .. }) = field.ty {
res.ty = match segments.iter().last().unwrap().ident.as_ref() {
"bool" => Ty::Bool,
"Option" => Ty::Option,
"Vec" => Ty::Vec,
_ => Ty::Other,
};
}
if res.has_custom_parser {
match res.ty {
Ty::Option | Ty::Vec => (),
_ => res.ty = Ty::Other,
}
}
match res.ty {
Ty::Bool => {
if res.has_method("default_value") {
panic!("default_value is meaningless for bool")
}
if res.has_method("required") {
panic!("required is meaningless for bool")
}
},
Ty::Option => {
if res.has_method("default_value") {
panic!("default_value is meaningless for Option")
}
if res.has_method("required") {
panic!("required is meaningless for Option")
}
},
_ => (),
}
res
}
pub fn has_method(&self, method: &str) -> bool {
self.methods.iter().find(|m| m.name == method).is_some()
}
pub fn methods(&self) -> Tokens {
let methods = self.methods.iter().map(|&Method { ref name, ref args }| {
let name: ::syn::Ident = name.as_str().into();
quote!( .#name(#args) )
});
quote!( #(#methods)* )
}
pub fn name(&self) -> &str {
&self.name
}
pub fn parser(&self) -> &(Parser, Tokens) {
&self.parser
}
pub fn ty(&self) -> Ty {
self.ty
}
pub fn is_subcommand(&self) -> bool {
self.is_subcommand
}
}