aboutsummaryrefslogtreecommitdiff
path: root/mingling_picker/src/picker/patterns.rs
blob: 20faadd77ff5d314e603114fac8a312a1f0509b6 (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
use mingling_picker_macros::internal_repeat;

use crate::{Pickable, Picker, PickerArgResult, PickerArgs, PickerFlag};

internal_repeat!(1..=32 => {
    #[doc(hidden)]
    pub struct PickerPattern$<'a, (T$,+), Route>
    where (T$: Pickable<'a>,+)
    {
        pub args: PickerArgs<'a>,
        pub error_route: Option<Route>,
        (
            pub flag_$: &'a PickerFlag<'a, T$>,
            pub result_$: PickerArgResult<T$>,
            pub default_$: Option<Box<dyn FnOnce() -> T$>>,
            pub route_$: Option<Box<dyn FnOnce() -> Route>>,
            pub post_$: Option<Box<dyn FnOnce(T$) -> T$>>,
        +)
    }
});

internal_repeat!(1..=32 => {
    impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route>
    where (T$: Pickable<'a>,+)
    {
        /// Sets a default value provider for this flag.
        ///
        /// If the flag is not provided by the user at runtime, the given closure will be
        /// called to produce a default value. The closure is expected to return `T$`.
        ///
        /// # Example
        ///
        /// ```ignore
        /// let pattern = picker
        ///     .pick(&my_flag)
        ///     .or(|| 42);
        /// ```
        #[allow(clippy::type_complexity)]
        pub fn or<F>(mut self, func: F) -> Self
        where
            F: FnMut() -> T$,
            F: 'static,
        {
            self.default_$ = Some(Box::new(func));
            self
        }

        /// Uses the default value for this flag's type if the flag is not provided.
        ///
        /// If the flag is not provided by the user at runtime, the default value for `T$`
        /// (as defined by the `Default` trait) will be used.
        ///
        /// # Example
        ///
        /// ```ignore
        /// let pattern = picker
        ///     .pick(&my_flag)
        ///     .or_default();
        /// ```
        #[allow(clippy::type_complexity)]
        pub fn or_default(mut self) -> Self
        where
            T$: Default,
        {
            self.default_$ = Some(Box::new(|| T$::default()));
            self
        }

        /// Sets a route for when the flag is not provided.
        ///
        /// If the flag is not provided by the user at runtime, the given closure will be
        /// called to produce a route value that will be returned early.
        ///
        /// # Example
        ///
        /// ```ignore
        /// let pattern = picker
        ///     .pick(&my_flag)
        ///     .or_route(|| Redirect::home());
        /// ```
        pub fn or_route<F>(mut self, func: F) -> Self
        where
            F: FnMut() -> Route,
            F: 'static,
        {
            self.route_$ = Some(Box::new(func));
            self
        }


        /// Resets the route for this picker pattern, allowing a different route type.
        ///
        /// This method converts the current `PickerPattern` into a new one with a different
        /// route type `NewRoute`. All existing flag configurations, defaults, and post-
        /// processing functions are preserved, but the `error_route` and individual
        /// `route_$` fields are cleared (set to `None`).
        ///
        /// This is useful when you want to change the error/redirect route type mid-chain,
        /// for example when composing patterns from different contexts that use different
        /// route enums.
        #[allow(clippy::type_complexity)]
        pub fn with_route<NewRoute>(self) -> PickerPattern$<'a, (T$,+), NewRoute> {
            PickerPattern$ {
                args: self.args,
                error_route: None,
                (
                    flag_$: self.flag_$,
                    result_$: self.result_$,
                    default_$: self.default_$,
                    route_$: None,
                    post_$: self.post_$,
                +)
            }
        }

        /// Attaches a post-processing function to this flag.
        ///
        /// After the flag's value is parsed (or defaulted), the given closure will be
        /// invoked with the parsed value and its return value will be used as the final
        /// result. This allows transforming or validating the parsed value.
        ///
        /// # Example
        ///
        /// ```ignore
        /// let pattern = picker
        ///     .pick(&my_flag)
        ///     .post(|val| val * 2);
        /// ```
        #[allow(clippy::type_complexity)]
        pub fn post<F>(mut self, func: F) -> Self
        where
            F: FnMut(T$) -> T$,
            F: 'static,
        {
            self.post_$ = Some(Box::new(func));
            self
        }
    }
});

internal_repeat!(1..32 => {
   impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route>
   where (T$: Pickable<'a>,+)
   {
       #[allow(clippy::type_complexity)]
       /// Adds a new flag to the picking chain, returning a new `PickerPattern` with one more type parameter.
       ///
       /// This method extends the current picking pattern by appending an additional flag.
       /// The previous flags and their results are preserved as part of the new pattern.
       /// The new flag's result is initially `Unparsed`.
       pub fn pick<N>(self, flag: &'a PickerFlag<'a, N>) -> PickerPattern$+<'a, (T$,+), N, Route>
       where
           N: Pickable<'a>,
       {
           PickerPattern$+ {
               // Args
               args: self.args,
               error_route: self.error_route,

               // Current
               flag_$+: flag,
               result_$+: PickerArgResult::Unparsed,
               default_$+: None,
               route_$+: None,
               post_$+: None,

               // Prev
               (
                   flag_$: self.flag_$,
                   result_$: self.result_$,
                   default_$: self.default_$,
                   route_$: self.route_$,
                   post_$: self.post_$,
                +)
           }
       }
   }
});

impl<'a, Route> Picker<'a, Route> {
    /// Creates a `PickerPattern1` from the given flag to start a picking chain.
    ///
    /// This method initiates a parameter picking chain with one flag.
    /// The result is initially `Unparsed`.
    pub fn pick<N>(self, flag: &'a PickerFlag<'a, N>) -> PickerPattern1<'a, N, Route>
    where
        N: Pickable<'a>,
    {
        PickerPattern1 {
            args: self.args,
            error_route: None::<Route>,
            flag_1: flag,
            result_1: PickerArgResult::Unparsed,
            route_1: None,
            default_1: None,
            post_1: None,
        }
    }
}