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
|
// --------------------------------------------------------------------------------------------
// I have to say, the code generated by this `internal_repeat!` macro is really UGLY.
//
// But I have to admit, this is a **trade-off**. To achieve the syntax of `pick().pick().pick()`
// while ensuring type safety, this is the best approach I could think of.
//
// P.S. If there's a better way, please let me know. Thanks!
// --------------------------------------------------------------------------------------------
//
// Then, I must disable `clippy::type_complexity` — this guy is way too noisy.
#![allow(clippy::type_complexity)]
use crate::{Pickable, PickerArgAttr, PickerArgInfo, PickerArgResult, PickerArgs, TagPhaseContext};
use arg_picker_macros::internal_repeat;
internal_repeat!(1..=32 => {
use crate::PickerPattern$;
use crate::PickerResult$;
});
internal_repeat!(1..=32 => {
impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route>
where (T$: Pickable<'a>,+) {
/// Unwraps the result, panicking if a route was selected or a required
/// value is missing.
///
/// # Panics
///
/// Panics if a route was selected, or if a required argument was not
/// provided by the user.
pub fn unwrap(self) -> ((T$,+)) {
let p = self.parse();
((p.v$.expect(concat!("missing required argument at position ", $)),+))
}
/// Returns the parsed values, using the default values for any missing
/// required arguments, or panicking if a route was selected.
///
/// # Panics
///
/// Panics if a route was selected.
///
/// # Type Constraints
///
/// All types in the tuple must implement [`Default`].
pub fn unwrap_or_default(self) -> ((T$,+))
where
(
T$: Default,
+) {
let p = self.parse();
((p.v$.unwrap_or_default(),+))
}
/// Returns the parsed values, using the provided closure to generate
/// default values for any missing required arguments, or panicking if
/// a route was selected.
///
/// # Panics
///
/// Panics if a route was selected.
pub fn unwrap_or_else<F>(self, op: F) -> ((T$,+))
where
F: FnOnce(Route) -> ((T$,+)),
{
let r = self.to_result();
r.unwrap_or_else(op)
}
/// Returns the parsed values, or panics with the given message if
/// a route was selected.
///
/// # Panics
///
/// Panics if a route was selected, with the provided message.
///
/// # Type Constraints
///
/// `Route` must implement [`std::fmt::Debug`] so that the error
/// message can include the route value.
pub fn expect(self, msg: &str) -> ((T$,+)) where Route: std::fmt::Debug {
self.to_result().expect(msg)
}
/// Returns the individual option values without checking the route.
pub fn unpack(self) -> ((Option<T$>,+)) {
let p = self.parse();
((p.v$,+))
}
/// Converts to a `Result`, returning `Err(route)` if a route was selected,
/// or `Ok(values)` otherwise.
pub fn to_result(self) -> Result<((T$,+)), Route> {
let p = self.parse();
if let Some(r) = p.route {
return Err(r);
}
Ok(p.unwrap())
}
/// Converts to an `Option`, returning `None` if a route was selected,
/// or `Some(values)` otherwise.
pub fn to_option(self) -> Option<((T$,+))> {
let p = self.parse();
if p.route.is_some() {
return None;
}
Some(p.unwrap())
}
}
});
internal_repeat!(1..=32 => {
impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route>
where (T$: Pickable<'a>,+)
{
pub fn parse(mut self) -> PickerResult$<(T$,+), Route> {
// ArgInfos
let arg_infos: [PickerArgInfo; $] = [
(
PickerArgInfo::from(self.arg_$),
+)
];
let mut bundle: [
(
// Arg Attr
PickerArgAttr,
// Tag Func
Box<dyn FnOnce(&PickerArgs<'a>, &[u8]) -> Vec<usize>>,
// Pick Func
Box<dyn FnOnce(&[&str], &mut Option<Route>)>,
// Index
usize
)
; $] = [
(
(
// Arg Attr
T$::get_attr(self.arg_$),
// Tag Func
Box::new(|args, mask| {
let ctx = TagPhaseContext {
arg_info: &arg_infos[$-],
args,
mask
};
T$::tag(ctx)
}),
// Pick Func
Box::new(|args, error_route| {
self.result_$ = match T$::pick(args) {
PickerArgResult::Parsed(mut value) => {
// Postprocess
if let Some(post) = self.post_$ {
value = post(value);
}
PickerArgResult::Parsed(value)
},
other => {
if let Some(get_default) = self.default_$ {
let mut value = get_default();
// Postprocess
if let Some(post) = self.post_$ {
value = post(value);
}
PickerArgResult::Parsed(value)
} else {
if error_route.is_none() {
if let Some(get_route) = self.route_$ {
*error_route = Some(get_route().into());
}
}
other
}
},
}
}),
// Index
$
),
+)
];
// Sort by Bundle Ord (descending)
bundle.sort_by(|a, b| b.0.cmp(&a.0));
// Mask — size = number of args (not args), so use args length
let mut mask: Vec<u8> = vec![0u8; self.args.len()];
// Parsing
for (_, tag_func, pick_func, _idx) in bundle {
// Tag phase
let tagged = tag_func(&self.args, mask.as_slice());
let mut args_to_pick: Vec<&str> = vec![];
for i in tagged {
mask[i] = 1;
// Update args to pick
args_to_pick.push(self.args.get(i).unwrap_or_default());
}
// Pick phase
pick_func(args_to_pick.as_slice(), &mut self.error_route);
}
// Combine Result
let result: PickerResult$<(T$,+), Route> = PickerResult$ {
route: self.error_route,
(
v$: self.result_$.to_option(),
+)
};
result
}
}
});
|