aboutsummaryrefslogtreecommitdiff
path: root/docs/pages/3-parsing-complex-arguments.md
blob: 285e17d87b7ef1c572dcb57f2588a9a7421af818 (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
<h1 align="center">Parsing Complex Args</h1>
<p align="center">
    Use Mingling Picker to parse complex user input
</p>

## Intro

  In the prev. example, we built a CLI app with a `"greet"` subcommand that outputs the user's first arg.

  You may have noticed the approach used was almost direct string manipulation—not very semantic, and hard to maintain long-term.

```rust
let name = args.first().cloned().unwrap_or_else(|| "World".to_string());
```

  This chapter introduces a new **Mingling** feature: `Picker`. It provides a lightweight parsing solution that meshes well with **Mingling**'s typed routing.

  To enable `Picker`, edit `Cargo.toml` ✏️

```toml
[dependencies]
mingling = { 
    version = "...", 
    features = ["parser"] 
}
```

  Enough talk, let's get coding and rewrite the parsing logic from the prev. section ✏️

```rust
#[chain]
fn handle_greet_entry(prev: GreetEntry) -> NextProcess {
    // Prev. approach:
    // let args = prev.inner;
    // let name = args.first().cloned().unwrap_or_else(|| "World".to_string());

    // New approach with Picker
    let name = prev.pick_or((), "World").unpack();

    ResultGreetSomeone::new(name)
}
```

  `Picker` implements `pick`, `pick_or`, and `pick_or_route` for anything `Into<Vec<String>>`. These functions let you semantically **pick** args from a string list and convert them into structured data.

  In the code above:

```rust
prev.pick_or((), "World").unpack();
```

  Its meaning:

```rust
   prev.pick_or((), "World").unpack();
// ~~~~ ~~~~~~~ ~~  ~~~~~~~  ~~~~~~~~
// |    |       |   |        |_ unpack to String
// |    |       |   |__________ default value is "World"
// |    |       |______________ pick the first positional arg (no flag)
// |    |______________________ pick or use default
// |___________________________ from the prev. input                
```

## Parsing Flag Args

  If your app needs to parse flag args (e.g., `greet --name Alice`), do:

```rust
prev.pick_or(["--name", "-n"], "World").unpack();
```

  Its meaning:

```rust
   prev.pick_or(["--name", "-n"], "World").unpack();
// ~~~~ ~~~~~~~ ~~~~~~~~~~~~~~~~  ~~~~~~~  ~~~~~~~~
// |    |       |                 |        |_ unpack to String
// |    |       |                 |__________ default value is "World"
// |    |       |____________________________ pick the value after "--name" or "-n"
// |    |____________________________________ pick or use default
// |_________________________________________ from the prev. input                
```

## About `.unpack()` 💡

  You may have noticed `Picker` calls `.unpack()` at the end of parsing. It converts the parsed result into structured info.

  For a single pick, `.unpack()` returns a single value. For multiple picks, `Picker` returns a tuple:

```rust
let name_single: String = prev.clone().pick_or((), "World").unpack();
let (name, age, id) = prev
    .pick::<String>(["--name", "-n"])
    .pick::<u8>(["--age", "-a"])
    .pick::<u32>(["--id", "-I"])
    .unpack();

// Parses: --name Alice --age 21 --id 0711251
```

> [!IMPORTANT]
> `Picker` is very order-sensitive, esp. with positional args: it parses sequentially.
>
> If you need to parse positional args, make sure to pick & consume all **flag args** first.

## Using `pick_or_route` for Edge Cases

  Ha, as the old saying goes: "Never trust your users." Missing required args, type mismatches, enabling mutually exclusive options—these are all headache-inducing edge cases.

  `pick_or_route` handles these by routing the chain to a dedicated error-handling type, giving you fine-grained error control.

  Let's write a simple example showing basic usage:

```rust
dispatcher!("greet", GreetCommand => GreetEntry);

pack!(ResultGreetSomeone = String);
pack!(ErrorGreetNoNameProvided = ());

#[chain]
fn handle_greet_entry(prev: GreetEntry) -> NextProcess {
    // Use `pick_or_route` to extract the `--name` arg
    // If missing or parse fails, route to ErrorGreetNoNameProvided
    let pick_result = prev
        .pick_or_route(
            ["--name", "-n"],
            ErrorGreetNoNameProvided::default().to_render(),
        )
        // After using any routable method, `unpack` returns `Result<Value, Route>`
        .unpack();

    // Use the `route!` macro to expand `pick_result`,
    // If it's `Err`, the chain returns here, routing to the specified type
    let name = route!(pick_result);
    ResultGreetSomeone::new(name).to_chain()
}

// Handles rendering for `ErrorGreetNoNameProvided`
#[renderer]
fn render_err_greet_no_name_provided(_prev: ErrorGreetNoNameProvided) {
    r_println!("Error: No name provided.")
}

#[renderer]
fn render_greet_someone(prev: ResultGreetSomeone) {
    r_println!("Hello, {}!", *prev);
}
```

  Using `pick_or_route` makes the code a bit more complex: `.unpack()` no longer returns the value directly, but `Result<Value, Route>`.

  However, **Mingling** provides the `route!` macro to simplify expansion. It's not complex—just cuts some boilerplate:

```rust
let name = route!(pick_result);

// Expands to
let name = match pick_result {
    Ok(r) => r,
    Err(e) => return e,
};
```

## Post-Processing Extracted Values

  After using `pick` to extract user input, you can use `after` or `after_or_route` to process the arg immediately ✏️

```rust
#[chain]
fn handle_greet_entry(prev: GreetEntry) -> NextProcess {
    let name = prev
        .pick_or(["--name", "-n"], "World")
        // After extracting `--name`, format it immediately
        .after(|name: String| {
            name.replace(['-', '_', '.'], " ")
                .to_lowercase()
                .trim()
                .to_string();
            name
        })
        .unpack();

    ResultGreetSomeone::new(name) // name is now formatted
}
```

  Similarly, use `after_or_route` to handle format errors in input args ✏️

```rust
dispatcher!("greet", GreetCommand => GreetEntry);

pack!(ResultGreetSomeone = String);
pack!(ErrorGreetNameTooLong = usize);

#[chain]
fn handle_greet_entry(prev: GreetEntry) -> NextProcess {
    let pick_result = prev
        .pick_or(["--name", "-n"], "World")
        // Unlike `after`, this borrows &String
        .after_or_route(|name: &String| {
            name.replace(['-', '_', '.'], " ")
                .to_lowercase()
                .trim()
                .to_string();

            // Check name length, route to error type if too long
            let len = name.len();
            if len < 32 {
                Ok(name.clone())
            } else {
                Err(ErrorGreetNameTooLong::new(len).to_render())
            }
        })
        .unpack();
    let name = route!(pick_result);

    ResultGreetSomeone::new(name).to_chain()
}

#[renderer]
fn render_error_greet_name_too_long(prev: ErrorGreetNameTooLong) {
    let len = *prev;
    r_println!("Error: name too long (length: {} > 32)", len);
}

#[renderer]
fn render_greet_someone(prev: ResultGreetSomeone) {
    r_println!("Hello, {}!", *prev);
}
```

## Parsing Booleans

  `Picker` can parse **bool** types too, but with both explicit and implicit modes:

  |Mode|Format|
  |-|-|
  |Explicit|`--confirm true` or `--confirm yes`|
  |Implicit|`--confirmed`|

  - Using `.pick` on `bool` uses implicit parsing: flag present → `true`
  - Using `.pick` on `mingling::parser::Yes` or `mingling::parser::True` uses explicit parsing; the value must be `true` / `yes` to be recognized as `true`

  Generally, implicit parsing is enough, but for positional args or important confirmations, explicit logic might be more semantic.

```rust
#[chain]
fn handle_some_entry(prev: SomeEntry) -> NextProcess {
    let confirmed: bool = prev.pick::<Yes>(()).unpack().is_yes();
    let confirm: bool = prev.pick::<bool>(["--confirm", "-C"]).unpack();

    // other logic
}
```

## Special Use: `usize` Parsing

  **Mingling** has a special use for `usize`: parsing strings like `25G`, `32mb`, etc. ✏️

```rust
#[test]
fn parse_size() {
    let vec = vec!["--size".to_string(), "25mib".to_string()];
    let size: usize = vec.pick(["--size", "-S"]).unpack();
    assert_eq!(size, 25 * 1024 * 1024);
}
```

## Custom Parsable Types

  Use the `Pickable` trait to make your types parsable by `Picker`. This is where `Picker`'s extensibility comes from ✏️

```rust
// Must implement Default: parse failures record the default directly
#[derive(Default)]
pub struct Address {
    ip: String,
    port: u16,
}

impl Pickable for Address {
    type Output = Self;
    fn pick(args: &mut Argument, flag: Flag) -> Option<Self::Output> {
        // Extract raw string from Argument using Flag
        let raw = args.pick_argument(flag)?;

        // Parse raw string into structured data
        let parts: Vec<&str> = raw.split(':').collect();
        let ip = parts.first()?.to_string();
        let port: u16 = parts.get(1)?.parse().ok()?;

        Some(Address { ip, port })
    }
}
```

  With `Pickable` implemented for `Address`, we can now use `ip:port` format for input ✏️

```rust
dispatcher!("connect", ConnectCommand => ConnectEntry);

pack!(ResultConnected = Address);

#[chain]
fn handle_connect_entry(prev: ConnectEntry) -> NextProcess {
    let address: Address = prev.pick("--addr").unpack();
    ResultConnected::new(address)
}

#[renderer]
fn render_connected(prev: ResultConnected) {
    let addr = prev.inner;
    r_println!("Connected: IP: {} PORT: {}", addr.ip, addr.port);
}
```

  Running it:

```bash
~> your-bin connect --addr 127.0.0.1:8080
Connected: IP: 127.0.0.1 PORT: 8080
```

## Auto-Implementing Pickable for Enums

  No need to manually implement `Pickable` for enums: `Picker` auto-implements it for any type that implements `PickableEnum`, as long as it also implements `EnumTag` ✏️

```rust
// Debug  : for rendering
// Default: for Picker parsing
// EnumTag: for implementing PickableEnum
#[derive(Debug, Default, EnumTag)]
pub enum Fruits {
    #[default]
    Apple,
    Banana,
    Orange,
}

// Implement PickableEnum for Fruits
impl PickableEnum for Fruits {}
```

  Now you can directly use `Picker` to parse this type ✏️

```rust
pack!(ResultFruit = Fruits);

#[chain]
fn handle_eat_fruit_entry(prev: EatFruitEntry) -> NextProcess {
    let fruit: Fruits = prev.pick("--fruit").unpack();
    ResultFruit::new(fruit)
}

#[renderer]
fn render_ate_fruit(prev: ResultFruit) {
    r_println!("Picked fruit: {:?}", *prev);
}
```

  That's all for `Picker`'s usage. In the next chapter, I'll introduce how to implement help docs for commands in **Mingling**.

<p align="center" style="font-size: 0.85em; color: gray;">
    Written by @Weicao-CatilGrass
</p>