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
//! The basic structure for the field's data, containing all the available
//! attributes in `#[conf(...)]` and some additional contents.
use crate::error::{Error, ErrorKind, Result};
use crate::opt::{Opt, OptArgData, OptBaseData, OptFileData, OptKind};
use darling::FromField;
use std::rc::Rc;
use syn::{spanned::Spanned, Field, Ident, Path, Type, TypePath};
#[derive(FromField)]
#[darling(attributes(conf))]
pub struct Attrs {
pub ident: Option<Ident>,
pub ty: Type,
#[darling(skip)]
pub is_option: bool,
#[darling(skip)]
pub takes_value: bool,
#[darling(default)]
pub default: Option<String>,
#[darling(default)]
pub no_long: bool,
#[darling(default)]
pub long: Option<String>,
#[darling(default)]
pub no_short: bool,
#[darling(default)]
pub short: Option<String>,
#[darling(default)]
pub help: Option<String>,
#[darling(default)]
pub negated_arg: bool,
#[darling(default)]
pub no_file: bool,
#[darling(default)]
pub file: Option<String>,
#[darling(default)]
pub section: Option<String>,
}
impl Attrs {
/// Method to initialize a `Attrs` completely, from the parsing
/// done by `darling`, and some extra checks for conflicts and for the
/// type. The `Attrs::from_field` method generated by `darling`
/// shouldn't be used by itself.
pub fn init(field: Field) -> Result<Attrs> {
let mut attrs = Attrs::from_field(&field)?;
attrs.apply_rules();
attrs.check_conflicts()?;
Ok(attrs)
}
/// Applies some rules to itself depending on its attributes.
fn apply_rules(&mut self) {
// Painfully obtaining the type `T` inside `Option<T>` to assign
// `is_optional`.
if let Type::Path(TypePath {
path: Path { segments, .. },
..
}) = &self.ty
{
if segments.len() == 1 && segments.first().unwrap().ident == "Option" {
let args = &segments.first().unwrap().arguments;
use syn::{
AngleBracketedGenericArguments as Brackets, GenericArgument::Type as InnerType,
PathArguments::AngleBracketed as PathAngles,
};
// Obtaining the type inside the `Option<T>`.
if let PathAngles(Brackets { args, .. }) = args {
if let InnerType(ty) = args.first().unwrap() {
self.ty = ty.clone();
self.is_option = true;
}
}
}
}
// Only boolean flags won't take value.
self.takes_value = true;
if let Type::Path(TypePath {
path: Path { segments, .. },
..
}) = &self.ty
{
if segments.len() == 1 && segments.first().unwrap().ident == "bool" {
self.takes_value = false;
}
}
}
fn check_conflicts(&self) -> Result<()> {
// Given an original expression and a list of other expressions it
// conflicts with, it returns an error in case both of them are true.
// The macro makes this a bit less repetitive.
macro_rules! check_conflicts {
($orig:expr, $others:expr) => {
let (orig, orig_name) = $orig;
if orig {
for (confl, confl_name) in $others.iter() {
if *confl {
return Err(Error {
span: self.ident.span(),
kind: ErrorKind::ConflictAttrs(
orig_name.to_string(),
confl_name.to_string(),
),
});
}
}
}
};
}
// Empty fields may not use any other attribute, except for `default`.
check_conflicts!(
(
self.no_short && self.no_long && self.no_file,
"no_short, no_long and no_file"
),
[
(self.long.is_some(), "long"),
(self.short.is_some(), "short"),
(self.help.is_some(), "help"),
(self.negated_arg, "negated_arg"),
(self.file.is_some(), "file"),
(self.section.is_some(), "section"),
]
);
check_conflicts!(
(self.no_short && self.no_long, "no_short and no_long"),
[
(self.negated_arg, "negated_arg"),
(self.help.is_some(), "help"),
]
);
check_conflicts!(
(self.no_short, "no_short"),
[(self.short.is_some(), "short"),]
);
check_conflicts!((self.no_long, "no_long"), [(self.long.is_some(), "long"),]);
check_conflicts!(
(self.negated_arg, "negated_arg"),
[
(self.takes_value, "field's type"),
(self.default.is_some(), "default"),
]
);
check_conflicts!(
(self.no_file, "no_file"),
[
(self.file.is_some(), "file"),
(self.section.is_some(), "section"),
]
);
Ok(())
}
pub fn get_file_data(&self) -> OptFileData {
OptFileData {
name: self
.file
.clone()
.unwrap_or_else(|| self.ident.as_ref().unwrap().to_string()),
section: self
.section
.clone()
.unwrap_or_else(|| "Defaults".to_string()),
}
}
pub fn get_arg_data(&self) -> Result<OptArgData> {
// The long or short values may be empty, meaning that the
// value should be converted from the field name.
let ident = self.ident.clone().unwrap().to_string();
let long = if self.no_long {
None
} else {
let long = self.long.clone().unwrap_or_else(|| ident.clone());
Some(long.replace("_", "-"))
};
let short = if self.no_short {
None
} else {
match &self.short {
Some(s) => {
// If the user provides the short name, this makes
// sure it's a single character.
let mut chars = s.chars();
let first = chars.next();
let second = chars.next();
match (first, second) {
(Some(ch), None) => Some(ch.to_string()),
_ => {
return Err(Error {
span: self.ident.span(),
kind: ErrorKind::Parse(
"short argument can't be longer than \
one character"
.to_string(),
),
})
}
}
}
None => {
// Otherwise, the short name is obtained from the
// identifier, which must be at least a character
// long, so `unwrap()` is used.
Some(ident.chars().next().unwrap().to_string())
}
}
};
Ok(OptArgData {
long,
short,
help: self.help.clone(),
negated: self.negated_arg,
})
}
/// Parses the attributes into an option, which may be empty, an argument,
/// a file, or both. Thus, two options are returned in case it's both.
pub fn parse_opt(self) -> Result<(Opt, Option<Opt>)> {
let base = Rc::new(OptBaseData {
is_option: self.is_option,
default: self.default.clone(),
id: self.ident.clone().unwrap(),
ty: self.ty.clone(),
});
let arg_kind = if self.takes_value {
OptKind::Arg
} else {
OptKind::Flag
};
let ret = if self.no_long && self.no_short && self.no_file {
(
Opt {
base,
kind: OptKind::Empty,
},
None,
)
} else if self.no_file {
(
Opt {
base,
kind: arg_kind(self.get_arg_data()?),
},
None,
)
} else if self.no_long && self.no_short {
(
Opt {
base,
kind: OptKind::File(self.get_file_data()),
},
None,
)
} else {
(
Opt {
base: Rc::clone(&base),
kind: arg_kind(self.get_arg_data()?),
},
Some(Opt {
base,
kind: OptKind::File(self.get_file_data()),
}),
)
};
Ok(ret)
}
}