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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
use std::marker::PhantomData;
use crate::parser::Argument;
use mingling_core::{EnumTag, Flag};
#[doc(hidden)]
pub mod builtin;
#[doc(hidden)]
pub mod bools;
/// A builder for extracting values from command-line arguments.
///
/// The `Picker` struct holds parsed arguments and provides a fluent interface
/// to extract values associated with specific flags.
pub struct Picker<G> {
/// The parsed command-line arguments.
pub args: Argument,
_phantom: PhantomData<G>,
}
impl<R> Picker<R> {
/// Creates a new `Picker` from a value that can be converted into `Argument`.
pub fn new(args: impl Into<Argument>) -> Picker<R> {
Picker {
args: args.into(),
_phantom: PhantomData,
}
}
/// Extracts a value for the given flag and returns a `Pick1` builder.
///
/// The extracted type `TNext` must implement `Pickable` and `Default`.
/// If the flag is not present, the default value for `TNext` is used.
pub fn pick<TNext>(mut self, val: impl Into<Flag>) -> Pick1<TNext, R>
where
TNext: Pickable<Output = TNext> + Default,
{
let v = TNext::pick(&mut self.args, val.into()).unwrap_or_default();
Pick1 {
args: self.args,
val_1: v,
route: None,
}
}
/// Extracts a value for the given flag, returning the provided default value if not present,
/// and returns a `Pick1` builder.
///
/// The extracted type `TNext` must implement `Pickable`.
/// If the flag is not present, the provided `or` value is used.
pub fn pick_or<TNext>(mut self, val: impl Into<Flag>, or: impl Into<TNext>) -> Pick1<TNext, R>
where
TNext: Pickable<Output = TNext>,
{
let v = TNext::pick(&mut self.args, val.into()).unwrap_or(or.into());
Pick1 {
args: self.args,
val_1: v,
route: None,
}
}
/// Extracts a value for the given flag, storing the provided route if the flag is not present,
/// and returns a `Pick1` builder.
///
/// The extracted type `TNext` must implement `Pickable` and `Default`.
/// If the flag is not present, the default value for `TNext` is used and the provided `route`
/// is stored in the returned builder for later error handling.
pub fn pick_or_route<TNext>(mut self, val: impl Into<Flag>, route: R) -> Pick1<TNext, R>
where
TNext: Pickable<Output = TNext> + Default,
{
let v = match TNext::pick(&mut self.args, val.into()) {
Some(value) => value,
None => {
return Pick1 {
args: self.args,
val_1: TNext::default(),
route: Some(route),
};
}
};
Pick1 {
args: self.args,
val_1: v,
route: None,
}
}
/// Extracts a value for the given flag, returning `None` if the flag is not present,
/// and returns an `Option<Pick1<TNext>>` builder.
///
/// The extracted type `TNext` must implement `Pickable`.
/// If the flag is not present, `None` is returned.
pub fn require<TNext>(mut self, val: impl Into<Flag>) -> Option<Pick1<TNext, R>>
where
TNext: Pickable<Output = TNext>,
{
let v = TNext::pick(&mut self.args, val.into());
match v {
Some(s) => Some(Pick1 {
args: self.args,
val_1: s,
route: None,
}),
None => None,
}
}
/// Applies an operation to the parsed arguments and returns the modified `Picker`.
///
/// Takes a closure that receives the current `Argument` and returns a new `Argument`.
/// The returned `Argument` replaces the original arguments in the builder.
/// This method can be used to modify or transform the parsed arguments before extracting values.
pub fn operate_args<F: FnOnce(Argument) -> Argument>(mut self, operation: F) -> Self {
self.args = operation(self.args);
self
}
}
impl<T: Into<Argument>, G> From<T> for Picker<G> {
fn from(value: T) -> Self {
Picker::new(value)
}
}
/// Extracts values from command-line arguments
///
/// The `Pickable` trait defines how to extract the value of a specific flag from parsed arguments
pub trait Pickable {
/// The output type produced by the extraction operation, must implement the `Default` trait
type Output: Default;
/// Extracts the value associated with the given flag from the provided arguments
///
/// If the flag exists and the value can be successfully extracted, returns `Some(Output)`;
/// otherwise returns `None`
fn pick(args: &mut Argument, flag: Flag) -> Option<Self::Output>;
}
/// Internal macro: generates the struct definition and common methods
/// (after, after_or_route, operate_args) for Pick2 through Pick12.
macro_rules! define_pick_struct {
($n:ident $final:ident $final_val:ident $($T:ident $val:ident),+) => {
#[doc(hidden)]
pub struct $n<$($T,)+ R>
where
$($T: Pickable,)+
{
#[allow(dead_code)]
args: Argument,
$(pub $val: $T,)+
route: Option<R>,
}
impl<$($T,)+ R> $n<$($T,)+ R>
where
$($T: Pickable,)+
{
/// Applies a transformation to the last extracted value.
///
/// Takes a closure that receives the last extracted value and returns a new value of the same type.
/// The transformed value replaces the original value in the builder.
/// This method can be used to modify or validate the extracted value before final unpacking.
pub fn after<F>(mut self, mut edit: F) -> Self
where
F: FnMut($final) -> $final,
{
self.$final_val = edit(self.$final_val);
self
}
/// Applies a transformation to the last extracted value, storing a route if the transformation fails.
///
/// Takes a closure that receives a reference to the last extracted value and returns a `Result`.
/// If the closure returns `Ok(new_value)`, the new value replaces the original value in the builder.
/// If the closure returns `Err(route)`, the provided `route` is stored in the builder for later error handling.
/// If a route was already stored from a previous `pick_or_route` call, the existing route is preserved.
pub fn after_or_route<F>(mut self, mut edit: F) -> Self
where
F: FnMut(&$final) -> Result<$final, R>,
{
let value = &self.$final_val;
match edit(value) {
Ok(new_value) => {
self.$final_val = new_value;
}
Err(err_route) => {
let new_route = match self.route {
Some(existing_route) => Some(existing_route),
None => Some(err_route),
};
self.route = new_route;
}
}
self
}
/// Applies an operation to the parsed arguments and returns the modified builder.
///
/// Takes a closure that receives the current `Argument` and returns a new `Argument`.
/// The returned `Argument` replaces the original arguments in the builder.
/// This method can be used to modify or transform the parsed arguments before extracting values.
pub fn operate_args<F: FnOnce(Argument) -> Argument>(mut self, operation: F) -> Self {
self.args = operation(self.args);
self
}
}
};
}
/// Internal macro: generates `From` impl for PickN into a tuple.
macro_rules! impl_pick_from_tuple {
($n:ident $($T:ident $val:ident),+) => {
impl<$($T,)+ R> From<$n<$($T,)+ R>> for ($($T,)+)
where
$($T: Pickable,)+
{
fn from(pick: $n<$($T,)+ R>) -> Self {
($(pick.$val,)+)
}
}
};
}
/// Internal macro: generates `unpack` and `unpack_directly` for PickN (N >= 2)
/// that return a tuple.
macro_rules! impl_pick_unpack_tuple {
($n:ident $($T:ident $val:ident),+) => {
impl<$($T,)+ R> $n<$($T,)+ R>
where
$($T: Pickable,)+
{
/// Unpacks the builder into a tuple of extracted values.
///
/// Returns `Ok((T1, T2, ...))` if all required flags were present.
/// Returns `Err(R)` if a required flag was missing and a route was provided via `pick_or_route`.
pub fn unpack(self) -> Result<($($T,)+), R> {
match self.route {
Some(route) => Err(route),
None => Ok(($(self.$val,)+)),
}
}
/// Unpacks the builder into a tuple of extracted values.
///
/// Returns the tuple of extracted values regardless of whether any required flags were missing.
/// If a required flag was missing and a route was provided via `pick_or_route`, the default value
/// for that type is included in the tuple.
pub fn unpack_directly(self) -> ($($T,)+) {
($(self.$val,)+)
}
}
};
}
define_pick_struct! { Pick1 T1 val_1 T1 val_1 }
impl<T1, R> From<Pick1<T1, R>> for (T1,)
where
T1: Pickable,
{
fn from(pick: Pick1<T1, R>) -> Self {
(pick.val_1,)
}
}
impl<T1, R> Pick1<T1, R>
where
T1: Pickable,
{
/// Unpacks the builder into a tuple of extracted values.
///
/// Returns `Ok((T1, T2, ...))` if all required flags were present.
/// Returns `Err(R)` if a required flag was missing and a route was provided via `pick_or_route`.
pub fn unpack(self) -> Result<T1, R> {
match self.route {
Some(route) => Err(route),
None => Ok(self.val_1),
}
}
/// Unpacks the builder into a tuple of extracted values.
///
/// Returns the tuple of extracted values regardless of whether any required flags were missing.
/// If a required flag was missing and a route was provided via `pick_or_route`, the default value
/// for that type is included in the tuple.
pub fn unpack_directly(self) -> T1 {
self.val_1
}
}
define_pick_struct! { Pick2 T2 val_2 T1 val_1, T2 val_2 }
impl_pick_from_tuple! { Pick2 T1 val_1, T2 val_2 }
impl_pick_unpack_tuple! { Pick2 T1 val_1, T2 val_2 }
define_pick_struct! { Pick3 T3 val_3 T1 val_1, T2 val_2, T3 val_3 }
impl_pick_from_tuple! { Pick3 T1 val_1, T2 val_2, T3 val_3 }
impl_pick_unpack_tuple! { Pick3 T1 val_1, T2 val_2, T3 val_3 }
define_pick_struct! { Pick4 T4 val_4 T1 val_1, T2 val_2, T3 val_3, T4 val_4 }
impl_pick_from_tuple! { Pick4 T1 val_1, T2 val_2, T3 val_3, T4 val_4 }
impl_pick_unpack_tuple! { Pick4 T1 val_1, T2 val_2, T3 val_3, T4 val_4 }
define_pick_struct! { Pick5 T5 val_5 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5 }
impl_pick_from_tuple! { Pick5 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5 }
impl_pick_unpack_tuple! { Pick5 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5 }
define_pick_struct! { Pick6 T6 val_6 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6 }
impl_pick_from_tuple! { Pick6 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6 }
impl_pick_unpack_tuple! { Pick6 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6 }
define_pick_struct! { Pick7 T7 val_7 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7 }
impl_pick_from_tuple! { Pick7 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7 }
impl_pick_unpack_tuple! { Pick7 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7 }
define_pick_struct! { Pick8 T8 val_8 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8 }
impl_pick_from_tuple! { Pick8 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8 }
impl_pick_unpack_tuple! { Pick8 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8 }
define_pick_struct! { Pick9 T9 val_9 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9 }
impl_pick_from_tuple! { Pick9 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9 }
impl_pick_unpack_tuple! { Pick9 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9 }
define_pick_struct! { Pick10 T10 val_10 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10 }
impl_pick_from_tuple! { Pick10 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10 }
impl_pick_unpack_tuple! { Pick10 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10 }
define_pick_struct! { Pick11 T11 val_11 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11 }
impl_pick_from_tuple! { Pick11 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11 }
impl_pick_unpack_tuple! { Pick11 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11 }
define_pick_struct! { Pick12 T12 val_12 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11, T12 val_12 }
impl_pick_from_tuple! { Pick12 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11, T12 val_12 }
impl_pick_unpack_tuple! { Pick12 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11, T12 val_12 }
#[doc(hidden)]
macro_rules! impl_pick_structs {
($n:ident $next:ident $next_val:ident $($T:ident $val:ident),+) => {
impl<$($T,)+ R> $n<$($T,)+ R>
where
$($T: Pickable,)+
{
/// Extracts a value for the given flag and returns a `PickN` builder.
pub fn pick<TNext>(mut self, val: impl Into<mingling_core::Flag>) -> $next<$($T,)+ TNext, R>
where
TNext: Pickable<Output = TNext> + Default,
{
let v = TNext::pick(&mut self.args, val.into()).unwrap_or_default();
$next {
args: self.args,
$($val: self.$val,)+
$next_val: v,
route: self.route,
}
}
/// Extracts a value for the given flag, returning the provided default value if not present,
/// and returns a `PickN` builder.
///
/// The extracted type `TNext` must implement `Pickable`.
/// If the flag is not present, the provided `or` value is used.
pub fn pick_or<TNext>(mut self, val: impl Into<mingling_core::Flag>, or: impl Into<TNext>) -> $next<$($T,)+ TNext, R>
where
TNext: Pickable<Output = TNext>,
{
let v = TNext::pick(&mut self.args, val.into()).unwrap_or(or.into());
$next {
args: self.args,
$($val: self.$val,)+
$next_val: v,
route: self.route,
}
}
/// Extracts a value for the given flag, storing the provided route if the flag is not present,
/// and returns a `PickN` builder.
///
/// The extracted type `TNext` must implement `Pickable` and `Default`.
/// If the flag is not present, the default value for `TNext` is used and the provided `route`
/// is stored in the returned builder for later error handling.
///
/// If a route was already stored from a previous `pick_or_route` call (i.e., `self.route` is `Some`),
/// the existing route is preserved and the new `route` parameter is ignored.
pub fn pick_or_route<TNext>(mut self, val: impl Into<mingling_core::Flag>, route: R) -> $next<$($T,)+ TNext, R>
where
TNext: Pickable<Output = TNext> + Default,
{
let v = match TNext::pick(&mut self.args, val.into()) {
Some(value) => value,
None => {
let new_route = match self.route {
Some(existing_route) => Some(existing_route),
None => Some(route),
};
return $next {
args: self.args,
$($val: self.$val,)+
$next_val: TNext::default(),
route: new_route,
};
}
};
$next {
args: self.args,
$($val: self.$val,)+
$next_val: v,
route: self.route,
}
}
/// Extracts a value for the given flag, returning `None` if the flag is not present,
/// and returns an `Option<PickN<TNext>>` builder.
///
/// The extracted type `TNext` must implement `Pickable`.
/// If the flag is not present, `None` is returned.
pub fn require<TNext>(mut self, val: impl Into<mingling_core::Flag>) -> Option<$next<$($T,)+ TNext, R>>
where
TNext: Pickable<Output = TNext>,
{
let v = TNext::pick(&mut self.args, val.into());
match v {
Some(s) => Some($next {
args: self.args,
$($val: self.$val,)+
$next_val: s,
route: self.route,
}),
None => None,
}
}
}
};
}
impl_pick_structs! { Pick1 Pick2 val_2 T1 val_1 }
impl_pick_structs! { Pick2 Pick3 val_3 T1 val_1, T2 val_2 }
impl_pick_structs! { Pick3 Pick4 val_4 T1 val_1, T2 val_2, T3 val_3 }
impl_pick_structs! { Pick4 Pick5 val_5 T1 val_1, T2 val_2, T3 val_3, T4 val_4 }
impl_pick_structs! { Pick5 Pick6 val_6 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5 }
impl_pick_structs! { Pick6 Pick7 val_7 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6 }
impl_pick_structs! { Pick7 Pick8 val_8 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7 }
impl_pick_structs! { Pick8 Pick9 val_9 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8 }
impl_pick_structs! { Pick9 Pick10 val_10 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9 }
impl_pick_structs! { Pick10 Pick11 val_11 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10 }
impl_pick_structs! { Pick11 Pick12 val_12 T1 val_1, T2 val_2, T3 val_3, T4 val_4, T5 val_5, T6 val_6, T7 val_7, T8 val_8, T9 val_9, T10 val_10, T11 val_11 }
pub trait PickableEnum: EnumTag + Default {}
impl<T> Pickable for T
where
T: PickableEnum,
{
type Output = T;
fn pick(args: &mut Argument, flag: Flag) -> Option<Self::Output> {
let name = args.pick_argument(flag)?;
T::build_enum(name)
}
}
|