aboutsummaryrefslogtreecommitdiff
path: root/mingling_picker/src/flag.rs
blob: 33c78292940e736792239d7ca51be59136a60cf1 (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
use crate::Pickable;
use std::marker::PhantomData;

/// Represents a constraint definition for a parameter selection.
///
/// This structure describes the constraints that a command-line parameter (Picker parameter item)
/// should satisfy, including its full name list (with aliases), short name form, and whether it is
/// positional.
///
/// # Field Descriptions
///
/// - `full`: Full name or alias list. For example, `["config", "cfg"]` means the parameter can be
///   matched with either `--config` or `--cfg`. Must contain at least one non-empty string.
///
/// - `short`: Short name (single character). For example, `Some('c')` means it can be passed using
///   the `-c` form. If set to `None`, the short name form is not supported.
///
/// - `positional`: Whether the parameter is positional (i.e., an argument without a flag).
///   - `true`: The parameter is positional; it is matched by its position in the command line rather
///     than by a `--name` or `-n` flag.
///   - `false`: The parameter is a named (flag-based) parameter.
///
/// - `_type`: PhantomData to hold the type parameter.
#[derive(Default, Clone, Copy)]
pub struct PickerFlag<'a, Type>
where
    Type: Pickable<'a>,
{
    /// Full name, may include variant names (aliases), e.g., `["config", "cfg"]`.
    pub full: &'a [&'a str],

    /// Short name, e.g., `'c'`.
    pub short: Option<char>,

    /// Whether the parameter is positional (no flag, matched by position).
    pub positional: bool,

    /// PhantomData to hold the type parameter.
    pub internal_type: PhantomData<Type>,
}

impl<'a, Type> PickerFlag<'a, Type>
where
    Type: Pickable<'a>,
{
    /// Creates a new `PickerFlag` with the provided parameters.
    pub fn new(full: &'a [&'a str], short: Option<char>, positional: bool) -> Self {
        Self {
            full,
            short,
            positional,
            internal_type: PhantomData,
        }
    }

    /// Returns the full name list (including aliases).
    pub fn full(&self) -> &'a [&'a str] {
        self.full
    }

    /// Returns the short name, if any.
    pub fn short(&self) -> Option<char> {
        self.short
    }

    /// Returns whether the parameter is positional.
    ///
    /// If `full` is empty or `short` is `None`, the parameter is considered positional
    /// regardless of the stored value.
    pub fn is_positional(&self) -> bool {
        if self.full.is_empty() && self.short.is_none() {
            true
        } else {
            self.positional
        }
    }

    /// Sets the full name list.
    pub fn set_full(&mut self, full: &'a [&'a str]) {
        self.full = full;
    }

    /// Sets the short name.
    pub fn set_short(&mut self, short: Option<char>) {
        self.short = short;
    }

    /// Sets whether the parameter is positional.
    pub fn set_positional(&mut self, positional: bool) {
        self.positional = positional;
    }

    /// Sets the full name list and returns self.
    pub fn with_full(mut self, full: &'a [&'a str]) -> Self {
        self.full = full;
        self
    }

    /// Clears the full name list (sets it to an empty slice) and returns self.
    pub fn without_full(mut self) -> Self {
        self.full = &[];
        self
    }

    /// Sets the short name to the given character and returns self.
    pub fn with_short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }

    /// Clears the short name (sets it to None) and returns self.
    pub fn without_short(mut self) -> Self {
        self.short = None;
        self
    }

    /// Sets whether the parameter is positional and returns self.
    pub fn with_positional(mut self, positional: bool) -> Self {
        self.positional = positional;
        self
    }
}

/// Describes the attribute (behavior) of a command-line parameter.
///
/// The ordering reflects parse priority (higher = parsed first):
/// `Positional < PositionalMulti < Flag < Single < Multi`
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PickerFlagAttr {
    /// Positional argument matched by its position (e.g., an input file).
    #[default]
    Positional,

    /// Positional argument that accepts multiple values (e.g., multiple input files).
    PositionalMulti,

    /// Boolean flag with no associated value (e.g., `--verbose`).
    Flag,

    /// Accepts a single value (e.g., `--name Alice`).
    Single,

    /// Accepts multiple values (e.g., `--file a.txt --file b.txt`).
    Multi,
}

impl PickerFlagAttr {
    /// Determines if the given `PickerFlag` represents a positional parameter.
    ///
    /// If the flag is positional (determined by `flag.is_positional()`), returns
    /// `PickerFlagAttr::Positional`. Otherwise, invokes the `other` closure to
    /// produce and return a `PickerFlagAttr`.
    ///
    /// # Parameters
    ///
    /// - `flag`: A reference to the [`PickerFlag`] to evaluate.
    /// - `other`: A closure that returns a [`PickerFlagAttr`] when the flag is
    ///   **not** positional.
    #[inline(always)]
    pub fn positional_or_else<'a, T>(
        flag: &PickerFlag<'a, T>,
        other: fn() -> PickerFlagAttr,
    ) -> PickerFlagAttr
    where
        T: Pickable<'a>,
    {
        if flag.is_positional() {
            PickerFlagAttr::Positional
        } else {
            other()
        }
    }

    /// Determines if the given `PickerFlag` represents a positional parameter and returns
    /// `PickerFlagAttr::Positional` if so. Otherwise, returns the provided `default` attribute.
    ///
    /// # Parameters
    ///
    /// - `flag`: A reference to the [`PickerFlag`] to evaluate.
    /// - `default`: The [`PickerFlagAttr`] to return if the flag is not positional.
    #[inline(always)]
    pub fn positional_or<'a, T>(flag: &PickerFlag<'a, T>, default: PickerFlagAttr) -> PickerFlagAttr
    where
        T: Pickable<'a>,
    {
        if flag.is_positional() {
            PickerFlagAttr::Positional
        } else {
            default
        }
    }

    /// Determines if the given `PickerFlag` represents a positional parameter and returns
    /// `PickerFlagAttr::Positional` if so. Otherwise, returns `PickerFlagAttr::Single`.
    ///
    /// # Parameters
    ///
    /// - `flag`: A reference to the [`PickerFlag`] to evaluate.
    #[inline(always)]
    pub fn positional_or_single<'a, T>(flag: &PickerFlag<'a, T>) -> PickerFlagAttr
    where
        T: Pickable<'a>,
    {
        if flag.is_positional() {
            PickerFlagAttr::Positional
        } else {
            PickerFlagAttr::Single
        }
    }

    /// Determines if the given `PickerFlag` represents a positional parameter and returns
    /// `PickerFlagAttr::PositionalMulti` if so. Otherwise, returns `PickerFlagAttr::Multi`.
    ///
    /// # Parameters
    ///
    /// - `flag`: A reference to the [`PickerFlag`] to evaluate.
    #[inline(always)]
    pub fn positional_or_multi<'a, T>(flag: &PickerFlag<'a, T>) -> PickerFlagAttr
    where
        T: Pickable<'a>,
    {
        if flag.is_positional() {
            PickerFlagAttr::PositionalMulti
        } else {
            PickerFlagAttr::Multi
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_picker_flag_attr_ordering() {
        // Multi > Single > Flag > PositionalMulti > Positional
        assert!(PickerFlagAttr::Multi > PickerFlagAttr::Single);
        assert!(PickerFlagAttr::Multi > PickerFlagAttr::Flag);
        assert!(PickerFlagAttr::Multi > PickerFlagAttr::PositionalMulti);
        assert!(PickerFlagAttr::Multi > PickerFlagAttr::Positional);

        assert!(PickerFlagAttr::Single > PickerFlagAttr::Flag);
        assert!(PickerFlagAttr::Single > PickerFlagAttr::PositionalMulti);
        assert!(PickerFlagAttr::Single > PickerFlagAttr::Positional);

        assert!(PickerFlagAttr::Flag > PickerFlagAttr::PositionalMulti);
        assert!(PickerFlagAttr::Flag > PickerFlagAttr::Positional);

        assert!(PickerFlagAttr::PositionalMulti > PickerFlagAttr::Positional);

        // PartialOrd
        assert!(PickerFlagAttr::Multi >= PickerFlagAttr::Single);
        assert!(PickerFlagAttr::Single >= PickerFlagAttr::Flag);
        assert!(PickerFlagAttr::Flag >= PickerFlagAttr::PositionalMulti);
        assert!(PickerFlagAttr::PositionalMulti >= PickerFlagAttr::Positional);

        assert!(PickerFlagAttr::Positional < PickerFlagAttr::PositionalMulti);
        assert!(PickerFlagAttr::PositionalMulti < PickerFlagAttr::Flag);
        assert!(PickerFlagAttr::Flag < PickerFlagAttr::Single);
        assert!(PickerFlagAttr::Single < PickerFlagAttr::Multi);
    }

    #[test]
    fn test_picker_flag_attr_sorting() {
        // Sort
        let mut values = vec![
            PickerFlagAttr::Flag,
            PickerFlagAttr::Single,
            PickerFlagAttr::Positional,
            PickerFlagAttr::Multi,
            PickerFlagAttr::PositionalMulti,
        ];
        values.sort();
        assert_eq!(
            values,
            vec![
                PickerFlagAttr::Positional,
                PickerFlagAttr::PositionalMulti,
                PickerFlagAttr::Flag,
                PickerFlagAttr::Single,
                PickerFlagAttr::Multi,
            ]
        );
    }
}