aboutsummaryrefslogtreecommitdiff
path: root/mingling_picker/src/infos.rs
blob: c0309e02dd87d1116a643f2a517aae2a227072d7 (plain) (blame)
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
use crate::{Pickable, PickerFlag};

/// Represents the result of parsing or looking up a value.
///
/// This enum is generic over the type being parsed. It models three possible outcomes:
/// - [`Unparsed`](PickerArgResult::Unparsed): The value has not yet been parsed (default).
/// - [`Parsed`](PickerArgResult::Parsed): The value was successfully parsed into `Type`.
/// - [`NotFound`](PickerArgResult::NotFound): The requested value could not be found.
#[derive(Default)]
pub enum PickerArgResult<Type> {
    /// The value has not yet been parsed (default).
    #[default]
    Unparsed,

    /// The value was successfully parsed into `Type`.
    Parsed(Type),

    /// The requested value could not be found.
    NotFound,
}

impl<Type, E> From<Result<Type, E>> for PickerArgResult<Type> {
    /// Converts a `Result<Type, E>` into a `PickerArgResult<Type>`.
    ///
    /// - `Ok(value)` maps to [`Parsed(value)`](PickerArgResult::Parsed).
    /// - `Err(_)` maps to [`NotFound`](PickerArgResult::NotFound).
    fn from(result: Result<Type, E>) -> Self {
        match result {
            Ok(value) => PickerArgResult::Parsed(value),
            Err(_) => PickerArgResult::NotFound,
        }
    }
}

impl<Type> From<Option<Type>> for PickerArgResult<Type> {
    /// Converts an `Option<Type>` into a `PickerArgResult<Type>`.
    ///
    /// - `Some(value)` maps to [`Parsed(value)`](PickerArgResult::Parsed).
    /// - `None` maps to [`NotFound`](PickerArgResult::NotFound).
    fn from(option: Option<Type>) -> Self {
        match option {
            Some(value) => PickerArgResult::Parsed(value),
            None => PickerArgResult::NotFound,
        }
    }
}

impl<Type> PickerArgResult<Type> {
    /// Returns `true` if the result is [`Parsed`](PickerArgResult::Parsed).
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert!(result.is_parsed());
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert!(!result.is_parsed());
    /// ```
    pub fn is_parsed(&self) -> bool {
        matches!(self, PickerArgResult::Parsed(_))
    }

    /// Returns `true` if the result is [`Parsed`](PickerArgResult::Parsed) or [`NotFound`](PickerArgResult::NotFound).
    /// i.e., the value exists (was either found or not yet parsed).
    /// Typically indicates the value was "found" in some sense.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert!(result.is_found());
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert!(result.is_found());
    /// ```
    pub fn is_found(&self) -> bool {
        matches!(self, PickerArgResult::Parsed(_) | PickerArgResult::NotFound)
    }

    /// Returns `true` if the result is [`Unparsed`](PickerArgResult::Unparsed) or [`NotFound`](PickerArgResult::NotFound).
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Unparsed;
    /// assert!(result.is_err());
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(10);
    /// assert!(!result.is_err());
    /// ```
    pub fn is_err(&self) -> bool {
        !matches!(self, PickerArgResult::Parsed(_))
    }

    /// Returns `Some(&Type)` if [`Parsed`](PickerArgResult::Parsed), otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert_eq!(result.parsed(), Some(&42));
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert_eq!(result.parsed(), None);
    /// ```
    pub fn parsed(&self) -> Option<&Type> {
        if let PickerArgResult::Parsed(value) = self {
            Some(value)
        } else {
            None
        }
    }

    /// Returns the contained [`Parsed`](PickerArgResult::Parsed) value or panics with a given message.
    ///
    /// # Panics
    /// Panics if the value is not [`Parsed`](PickerArgResult::Parsed), with a message including the provided `msg`.
    ///
    /// # Examples
    ///
    /// ```should_panic
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// result.expect("expected a parsed value");
    /// ```
    pub fn expect(self, msg: &str) -> Type {
        match self {
            PickerArgResult::Parsed(value) => value,
            _ => panic!("{}", msg),
        }
    }

    /// Returns the contained [`Parsed`](PickerArgResult::Parsed) value or panics.
    ///
    /// # Panics
    /// Panics if the value is not [`Parsed`](PickerArgResult::Parsed).
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert_eq!(result.unwrap(), 42);
    /// ```
    ///
    /// ```should_panic
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// result.unwrap();
    /// ```
    pub fn unwrap(self) -> Type {
        match self {
            PickerArgResult::Parsed(value) => value,
            PickerArgResult::Unparsed => {
                panic!("called `PickerArgResult::unwrap()` on an `Unparsed` value")
            }
            PickerArgResult::NotFound => {
                panic!("called `PickerArgResult::unwrap()` on a `NotFound` value")
            }
        }
    }

    /// Returns the contained [`Parsed`](PickerArgResult::Parsed) value or a provided `default`.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert_eq!(result.unwrap_or(0), 42);
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert_eq!(result.unwrap_or(0), 0);
    /// ```
    pub fn unwrap_or(self, default: Type) -> Type {
        match self {
            PickerArgResult::Parsed(value) => value,
            _ => default,
        }
    }

    /// Returns the contained [`Parsed`](PickerArgResult::Parsed) value or computes it from a closure.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert_eq!(result.unwrap_or_else(|| 0), 42);
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert_eq!(result.unwrap_or_else(|| 0), 0);
    /// ```
    pub fn unwrap_or_else<F: FnOnce() -> Type>(self, f: F) -> Type {
        match self {
            PickerArgResult::Parsed(value) => value,
            _ => f(),
        }
    }

    /// Returns the contained [`Parsed`](PickerArgResult::Parsed) value or the default value of `Type`.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert_eq!(result.unwrap_or_default(), 42);
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert_eq!(result.unwrap_or_default(), 0);
    /// ```
    pub fn unwrap_or_default(self) -> Type
    where
        Type: Default,
    {
        match self {
            PickerArgResult::Parsed(value) => value,
            _ => Type::default(),
        }
    }

    /// Converts `PickerArgResult<Type>` into `Option<Type>`.
    ///
    /// Returns `Some(Type)` if [`Parsed`](PickerArgResult::Parsed), otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::PickerArgResult;
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Parsed(42);
    /// assert_eq!(result.to_option(), Some(42));
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::NotFound;
    /// assert_eq!(result.to_option(), None);
    ///
    /// let result: PickerArgResult<i32> = PickerArgResult::Unparsed;
    /// assert_eq!(result.to_option(), None);
    /// ```
    pub fn to_option(self) -> Option<Type> {
        match self {
            PickerArgResult::Parsed(value) => Some(value),
            _ => None,
        }
    }
}

/// Represents metadata about a command-line argument or flag.
///
/// This struct stores all relevant information about a tag/argument that can be used
/// for parsing command-line inputs. It includes the short form (e.g., `-n`), long form
/// (e.g., `--name`), aliases, and various flags that control parsing behavior.
pub struct PickerArgInfo<'a> {
    /// The short form of the tag, e.g. `'n'` for `-n`.
    pub short: Option<char>,
    /// The long form of the tag, e.g. `"name"` for `--name`.
    pub long: Option<&'a str>,
    /// Alternative names for the tag, e.g. `["-N", "--nickname"]`.
    pub alias: Option<Vec<&'a str>>,
    /// Whether this tag is a positional argument (no `-` or `--` prefix).
    pub positional: bool,
    /// Whether this tag is optional or required.
    pub optional: bool,
    /// Whether this tag can accept multiple values.
    pub multi: bool,
    /// Whether this tag participates in parsing after a `--` separator.
    pub is_flag: bool,
}

impl<'a, T> From<PickerFlag<'a, T>> for PickerArgInfo<'a>
where
    T: Pickable<'a>,
{
    fn from(value: PickerFlag<'a, T>) -> Self {
        let (long, alias) = match value.full.len() {
            0 => (None, None),
            _ => {
                let long = Some(value.full[0]);
                let alias = if value.full.len() > 1 {
                    Some(value.full[1..].to_vec())
                } else {
                    None
                };
                (long, alias)
            }
        };

        Self {
            short: value.short,
            long,
            alias,
            positional: value.positional,
            optional: false,
            multi: false,
            is_flag: false,
        }
    }
}

impl<'a, T: Pickable<'a>> From<&'a PickerFlag<'a, T>> for PickerArgInfo<'a> {
    fn from(value: &'a PickerFlag<'a, T>) -> Self {
        let (long, alias) = match value.full.len() {
            0 => (None, None),
            _ => {
                let long = Some(value.full[0]);
                let alias = if value.full.len() > 1 {
                    Some(value.full[1..].to_vec())
                } else {
                    None
                };
                (long, alias)
            }
        };

        Self {
            short: value.short,
            long,
            alias,
            positional: value.positional,
            optional: false,
            multi: false,
            is_flag: false,
        }
    }
}

impl<'a> PickerArgInfo<'a> {
    /// Create a new `PickerTag` with default values.
    pub fn new() -> Self {
        Self {
            short: None,
            long: None,
            alias: None,
            positional: false,
            optional: false,
            multi: false,
            is_flag: false,
        }
    }

    /// Set the short flag (e.g., `'n'` for `-n`).
    pub fn with_short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }

    /// Set the long flag (e.g., `"name"` for `--name`).
    pub fn with_long(mut self, long: &'a str) -> Self {
        self.long = Some(long);
        self
    }

    /// Set aliases for the tag.
    pub fn with_alias(mut self, alias: Vec<&'a str>) -> Self {
        self.alias = Some(alias);
        self
    }

    /// Mark the tag as positional.
    pub fn with_positional(mut self, positional: bool) -> Self {
        self.positional = positional;
        self
    }

    /// Mark the tag as optional.
    pub fn with_optional(mut self, optional: bool) -> Self {
        self.optional = optional;
        self
    }

    /// Mark the tag as multi-value.
    pub fn with_multi(mut self, multi: bool) -> Self {
        self.multi = multi;
        self
    }

    /// Mark the tag as a flag that participates in parsing after `--`.
    pub fn with_is_flag(mut self, is_flag: bool) -> Self {
        self.is_flag = is_flag;
        self
    }

    /// Set the short flag (e.g., `'n'` for `-n`).
    pub fn set_short(&mut self, short: char) -> &mut Self {
        self.short = Some(short);
        self
    }

    /// Set the long flag (e.g., `"name"` for `--name`).
    pub fn set_long(&mut self, long: &'a str) -> &mut Self {
        self.long = Some(long);
        self
    }

    /// Set aliases for the tag.
    pub fn set_alias(&mut self, alias: Vec<&'a str>) -> &mut Self {
        self.alias = Some(alias);
        self
    }

    /// Set whether this tag is positional.
    pub fn set_positional(&mut self, positional: bool) -> &mut Self {
        self.positional = positional;
        self
    }

    /// Set whether this tag is optional.
    pub fn set_optional(&mut self, optional: bool) -> &mut Self {
        self.optional = optional;
        self
    }

    /// Set whether this tag accepts multiple values.
    pub fn set_multi(&mut self, multi: bool) -> &mut Self {
        self.multi = multi;
        self
    }

    /// Set whether this tag participates in parsing after a `--` separator.
    pub fn set_is_flag(&mut self, is_flag: bool) -> &mut Self {
        self.is_flag = is_flag;
        self
    }
}

impl<'a> Default for PickerArgInfo<'a> {
    fn default() -> Self {
        Self::new()
    }
}