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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::{HashMap};
use std::convert::TryInto;
use syn::{Path, LitChar, LitInt, LitFloat};
use quote::ToTokens;
use crate::{Error, ValueClass, ArgValue};
#[derive(Clone)]
pub struct AttrReq {
pub arg_req: HashMap<String, ArgValueReq>,
pub path_req: ListReq<Path>,
pub char_req: ListReq<LitChar>,
pub integer_req: ListReq<LitInt>,
pub float_req: ListReq<LitFloat>,
pub string_req: ValueReq,
pub bytes_req: ValueReq,
pub bool_req: ValueReq,
}
impl AttrReq {
pub fn with(args: HashMap<&str, ArgValueReq>) -> AttrReq {
let args = args
.into_iter()
.map(|(name, req)| (name.to_owned(), req))
.collect();
AttrReq {
arg_req: args,
path_req: ListReq::Deny,
char_req: ListReq::Deny,
integer_req: ListReq::Deny,
float_req: ListReq::Deny,
string_req: ValueReq::Prohibited,
bytes_req: ValueReq::Prohibited,
bool_req: ValueReq::Prohibited,
}
}
}
#[derive(Clone)]
pub enum ArgValueReq {
Required {
default: Option<ArgValue>,
class: ValueClass,
},
Optional(ValueClass),
Prohibited,
}
impl ArgValueReq {
pub fn with_default(default: impl Into<ArgValue>) -> ArgValueReq {
let value = default.into();
ArgValueReq::Required {
class: value
.value_class()
.expect("Default argument value must not be `ArgValue::None`"),
default: Some(value),
}
}
pub fn required(class: ValueClass) -> ArgValueReq {
ArgValueReq::Required {
default: None,
class,
}
}
pub fn value_class(&self) -> Option<ValueClass> {
match self {
ArgValueReq::Required { class, .. } | ArgValueReq::Optional(class) => Some(*class),
ArgValueReq::Prohibited => None,
}
}
pub fn default_value(&self) -> ArgValue {
match self {
ArgValueReq::Required {
default: Some(d), ..
} => d.clone(),
_ => ArgValue::None,
}
}
pub fn is_required(&self) -> bool {
#[allow(clippy::match_like_matches_macro)]
match self {
ArgValueReq::Required { default: None, .. } => true,
_ => false,
}
}
pub fn check(
&self,
value: &mut ArgValue,
attr: impl ToString,
arg: impl ToString,
) -> Result<(), Error> {
let value = match (value, self) {
(ref val, ArgValueReq::Required { default: None, .. }) if val.is_none() => {
return Err(Error::ArgValueRequired {
attr: attr.to_string(),
arg: arg.to_string(),
})
}
(ref val, ArgValueReq::Prohibited) if val.is_some() => {
return Err(Error::ArgMustNotHaveValue {
attr: attr.to_string(),
arg: arg.to_string(),
})
}
(
ref val,
ArgValueReq::Required {
default: Some(d), ..
},
) if val.value_class() != d.value_class() && val.value_class().is_some() => {
panic!(
"Default value class {:?} does not match argument value class {:?} for attribute {}, argument {}",
d.value_class(),
val.value_class(),
attr.to_string(),
arg.to_string()
);
}
(val, req) => {
if val.is_none() {
if let ArgValueReq::Required {
default: Some(d), ..
} = req
{
*val = d.clone();
}
}
val
}
};
if let Some(value_class) = self.value_class() {
value_class.check(value, attr, arg)?;
}
Ok(())
}
}
#[derive(Clone)]
pub enum ValueReq {
Required,
Default(ArgValue),
Optional,
Prohibited,
}
impl ValueReq {
#[inline]
pub fn is_required(&self) -> bool {
#[allow(clippy::match_like_matches_macro)]
match self {
ValueReq::Required => true,
_ => false,
}
}
pub fn check<T>(
&self,
value: &mut T,
attr: impl ToString,
arg: impl ToString,
) -> Result<(), Error>
where
T: Clone,
ArgValue: From<T> + TryInto<T>,
Error: From<<ArgValue as TryInto<T>>::Error>,
{
let attr = attr.to_string();
let arg_value = ArgValue::from(value.clone());
match (self, value) {
(ValueReq::Required, _) if arg_value.is_none() => Err(Error::ArgValueRequired {
attr,
arg: arg.to_string(),
}),
(ValueReq::Prohibited, _) if arg_value.is_some() => Err(Error::ArgMustNotHaveValue {
attr,
arg: arg.to_string(),
}),
(ValueReq::Default(ref val), ref mut v) if arg_value.is_none() => {
**v = val.clone().try_into()?;
Ok(())
}
_ => Ok(()),
}
}
}
#[derive(Clone)]
pub enum ListReq<T>
where
T: Clone,
{
Single {
whitelist: Option<Vec<T>>,
default: Option<T>,
},
Many {
whitelist: Option<Vec<T>>,
required: bool,
max_no: Option<u8>,
},
Predefined {
whitelist: Option<Vec<T>>,
default: Vec<T>,
},
Deny,
}
impl<T> ListReq<T>
where
T: Clone + ToTokens,
{
pub fn check(
&self,
value: &mut Vec<T>,
attr: impl ToString,
arg: impl ToString,
) -> Result<(), Error> {
match (self, value.len()) {
(ListReq::Deny, x) if x > 0 => {
return Err(Error::ArgTypeProhibited {
attr: attr.to_string(),
arg: arg.to_string(),
})
}
(ListReq::Many { required: true, .. }, 0)
| (ListReq::Single { default: None, .. }, 0) => {
return Err(Error::ArgRequired {
attr: attr.to_string(),
arg: arg.to_string(),
})
}
(
ListReq::Many {
max_no: Some(max_no),
..
},
no,
) if no > *max_no as usize => {
return Err(Error::ArgNumberExceedsMax {
attr: attr.to_string(),
type_name: arg.to_string(),
no,
max_no: *max_no,
})
}
(
ListReq::Many {
whitelist: Some(whitelist),
..
},
len,
)
| (
ListReq::Predefined {
whitelist: Some(whitelist),
..
},
len,
)
| (
ListReq::Single {
whitelist: Some(whitelist),
..
},
len,
) if len > 0 => {
for item in value {
if !whitelist.iter().any(|i| {
i.to_token_stream().to_string() == item.to_token_stream().to_string()
}) {
return Err(Error::AttributeUnknownArgument {
attr: attr.to_string(),
arg: arg.to_string(),
});
}
}
}
(
ListReq::Single {
default: Some(d), ..
},
0,
) => value.push(d.clone()),
(ListReq::Predefined { default, .. }, 0) => *value = default.clone(),
_ => {}
}
Ok(())
}
}