perforce-cli 0.1.0

A type-safe builder library for spawning Perforce (p4) commands, with compile-time option state isolation and multi-version support.
Documentation
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
//! Scratch verification of every documented `p4 clients` syntax form.
//!
//! This is a throwaway example: for each documented command line it walks
//! the legal selection orders through the type-state builder, asserting
//! that they all converge on the same arguments. The reference is the
//! current command reference:
//!
//! ```text
//! p4 [g-opts] clients [-t] [-u user [--user-case-insensitive] | --me] [[-e|-E] filter] [-m max] [-S stream] [-a | -s serverID]
//! p4 [g-opts] clients -U
//! ```
//!
//! (`--me` requires 2016.1+; `--user-case-insensitive` requires 2024.2+.)
//!
//! The three exclusive groups `-u user | --me`, `-e | -E`, and
//! `-a | -s serverID` are independent of each other and of the free
//! options `-t`, `-m max`, and `-S stream`: any selection order converges
//! on the documented injection order `-t -u/--me -e/-E -m -S -a/-s`.

use std::io;
use std::path::PathBuf;
use std::process::Child;

use perforce_cli::P4Cli;
use perforce_cli::cmd::SubCommand;
use perforce_cli::cmd::clients::Clients;
use perforce_cli::global::GlobalOpts;
use perforce_cli::spawn::ParameterizedSpawn;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Collects the arguments assembled on a [`std::process::Command`] as
/// strings for easy comparison.
fn args_of(command: &std::process::Command) -> Vec<String> {
    command
        .get_args()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect()
}

/// Asserts the arguments produced by `setup_command`.
fn expect_args(cmd: &impl SubCommand, expected: &[&str]) {
    assert_eq!(args_of(&cmd.setup_command("p4")), expected);
}

/// Asserts that one chained builder expression assembles `expected`.
///
/// ```ignore
/// expect_chain!(["clients", "-m", "5"], p4.clients().limit(5));
/// ```
macro_rules! expect_chain {
    ([$($e:literal),* $(,)?], $($chain:tt)*) => {{
        let cmd = $($chain)*;
        expect_args(&cmd, &[$($e),*]);
    }};
}

/// A tiny batch helper that just prints its arguments. `p4 clients` only
/// appends the subcommand name and flags; none of those contain spaces, so
/// `%*` captures the exact argv tail.
fn echo_bin() -> PathBuf {
    let path = std::env::temp_dir().join("p4cli_clients_echo_args.bat");
    std::fs::write(&path, "@echo %*\r\n").expect("write echo helper");
    path
}

/// Normalizes the echo helper output into logical arguments.
fn parse_echo(stdout: &[u8]) -> Vec<String> {
    String::from_utf8_lossy(stdout)
        .split_whitespace()
        .map(|s| s.trim_matches('"').to_string())
        .collect()
}

/// Actually spawns the echo helper and returns the argv tail it received.
/// `p4 clients` takes no trailing operand, so every form spawns with the
/// unit parameter.
fn spawned_args_unit<C>(cmd: &mut C) -> Vec<String>
where
    C: ParameterizedSpawn<(), Output = Child, Error = io::Error>,
{
    let output = cmd
        .spawn_with(())
        .expect("spawn echo helper")
        .wait_with_output()
        .expect("wait for echo helper");
    assert!(output.status.success(), "echo helper exited unsuccessfully");
    parse_echo(&output.stdout)
}

/// Starts a builder whose executable is the local echo helper.
fn echo_clients() -> Clients {
    Clients::new(echo_bin(), GlobalOpts::new())
}

// Form 1 -------------------------------------------------------------------
// `p4 clients`
//
// The bare command lists every client workspace known to the system; the
// `workspaces` alias assembles the identical command line.

fn form_1_bare() {
    let p4 = P4Cli::default();

    expect_chain!(["clients"], p4.clients());
    expect_chain!(["clients"], p4.workspaces());

    let mut cmd = echo_clients();
    assert_eq!(spawned_args_unit(&mut cmd), ["clients"]);
}

// Form 2 -------------------------------------------------------------------
// `p4 clients [-t] [-u user [--user-case-insensitive] | --me] [[-e|-E] filter] [-m max] [-S stream] [-a | -s serverID]`
//
// Every option on its own, entered straight from the bare command.

fn form_2_single_options() {
    let p4 = P4Cli::default();

    expect_chain!(["clients", "-t"], p4.clients().display_time(true));
    // `display_time(false)` enters the regular form but injects nothing.
    expect_chain!(["clients"], p4.clients().display_time(false));
    expect_chain!(["clients", "-m", "5"], p4.clients().limit(5));
    expect_chain!(
        ["clients", "-S", "//stream/main"],
        p4.clients().stream("//stream/main")
    );
    expect_chain!(
        ["clients", "-e", "p4vBeta*"],
        p4.clients().filter("p4vBeta*", false)
    );
    expect_chain!(
        ["clients", "-E", "*Us*rs"],
        p4.clients().filter("*Us*rs", true)
    );
    expect_chain!(["clients", "-u", "O*hara"], p4.clients().user("O*hara"));
    expect_chain!(["clients", "-a"], p4.clients().all());
    expect_chain!(
        ["clients", "-s", "edge.1667"],
        p4.clients().server_id("edge.1667")
    );

    #[cfg(not(feature = "lt2016_1"))]
    expect_chain!(["clients", "--me"], p4.clients().filter_me());

    #[cfg(not(feature = "lt2024_2"))]
    expect_chain!(
        ["clients", "-u", "O*hara", "--user-case-insensitive"],
        p4.clients().user("O*hara").case_insensitive()
    );

    // Each single-option form spawns.
    let mut cmd = echo_clients().limit(5);
    assert_eq!(spawned_args_unit(&mut cmd), ["clients", "-m", "5"]);
}

// Form 3 -------------------------------------------------------------------
// The regular form's option groups are orthogonal: the three exclusive
// groups `-u user | --me`, `-e | -E`, and `-a | -s serverID` plus the free
// options `-t`, `-m max`, and `-S stream` may be selected in ANY order;
// every path converges on the documented injection order.

fn form_3_selection_orders() {
    let p4 = P4Cli::default();

    // `-u` and `-E` are independent groups: both selection orders converge.
    expect_chain!(
        ["clients", "-u", "O*hara", "-E", "*Us*rs"],
        p4.clients().user("O*hara").filter("*Us*rs", true)
    );
    expect_chain!(
        ["clients", "-u", "O*hara", "-E", "*Us*rs"],
        p4.clients().filter("*Us*rs", true).user("O*hara")
    );

    // `--me` and `-e` are independent groups as well: both selection
    // orders are legal and converge (`--me` belongs to the OWNER group,
    // not to the name-filter group).
    #[cfg(not(feature = "lt2016_1"))]
    {
        expect_chain!(
            ["clients", "--me", "-e", "p4vBeta*"],
            p4.clients().filter_me().filter("p4vBeta*", false)
        );
        expect_chain!(
            ["clients", "--me", "-e", "p4vBeta*"],
            p4.clients().filter("p4vBeta*", false).filter_me()
        );
    }

    // `--user-case-insensitive` chains anywhere after `-u` without
    // blocking the remaining groups.
    #[cfg(not(feature = "lt2024_2"))]
    {
        expect_chain!(
            [
                "clients",
                "-u",
                "O*hara",
                "--user-case-insensitive",
                "-s",
                "edge.1667"
            ],
            p4.clients()
                .user("O*hara")
                .case_insensitive()
                .server_id("edge.1667")
        );
        expect_chain!(
            [
                "clients",
                "-u",
                "O*hara",
                "--user-case-insensitive",
                "-s",
                "edge.1667"
            ],
            p4.clients()
                .user("O*hara")
                .server_id("edge.1667")
                .case_insensitive()
        );
        expect_chain!(
            [
                "clients",
                "-u",
                "O*hara",
                "--user-case-insensitive",
                "-s",
                "edge.1667"
            ],
            p4.clients()
                .server_id("edge.1667")
                .user("O*hara")
                .case_insensitive()
        );
    }

    // The full option set in several selection orders converges
    // identically.
    let full = [
        "clients",
        "-t",
        "-u",
        "O*hara",
        "-E",
        "*Us*rs",
        "-m",
        "5",
        "-S",
        "//stream/main",
        "-s",
        "edge.1667",
    ];
    expect_chain!(
        [
            "clients",
            "-t",
            "-u",
            "O*hara",
            "-E",
            "*Us*rs",
            "-m",
            "5",
            "-S",
            "//stream/main",
            "-s",
            "edge.1667",
        ],
        p4.clients()
            .display_time(true)
            .user("O*hara")
            .filter("*Us*rs", true)
            .limit(5)
            .stream("//stream/main")
            .server_id("edge.1667")
    );
    expect_chain!(
        [
            "clients",
            "-t",
            "-u",
            "O*hara",
            "-E",
            "*Us*rs",
            "-m",
            "5",
            "-S",
            "//stream/main",
            "-s",
            "edge.1667",
        ],
        p4.clients()
            .server_id("edge.1667")
            .stream("//stream/main")
            .limit(5)
            .filter("*Us*rs", true)
            .user("O*hara")
            .display_time(true)
    );
    expect_chain!(
        [
            "clients",
            "-t",
            "-u",
            "O*hara",
            "-E",
            "*Us*rs",
            "-m",
            "5",
            "-S",
            "//stream/main",
            "-s",
            "edge.1667",
        ],
        p4.clients()
            .filter("*Us*rs", true)
            .server_id("edge.1667")
            .stream("//stream/main")
            .user("O*hara")
            .limit(5)
            .display_time(true)
    );

    // The same full set with `--me` and `-a`.
    #[cfg(not(feature = "lt2016_1"))]
    {
        expect_chain!(
            [
                "clients",
                "-t",
                "--me",
                "-e",
                "p4vBeta*",
                "-m",
                "5",
                "-S",
                "//stream/main",
                "-a",
            ],
            p4.clients()
                .all()
                .filter_me()
                .display_time(true)
                .filter("p4vBeta*", false)
                .stream("//stream/main")
                .limit(5)
        );
        expect_chain!(
            [
                "clients",
                "-t",
                "--me",
                "-e",
                "p4vBeta*",
                "-m",
                "5",
                "-S",
                "//stream/main",
                "-a",
            ],
            p4.clients()
                .filter("p4vBeta*", false)
                .filter_me()
                .all()
                .limit(5)
                .stream("//stream/main")
                .display_time(true)
        );
    }

    // The `set_*` variants mutate an already-entered regular form.
    let mut cmd = p4.clients().user("O*hara");
    cmd.set_limit(10)
        .set_display_time(true)
        .set_stream("//stream/dev");
    assert_eq!(cmd.get_limit(), Some(10));
    assert!(cmd.get_display_time());
    assert_eq!(cmd.get_stream(), Some(&"//stream/dev".to_string()));
    expect_args(
        &cmd,
        &[
            "clients",
            "-t",
            "-u",
            "O*hara",
            "-m",
            "10",
            "-S",
            "//stream/dev",
        ],
    );

    // The full house spawns with the converged argument order.
    let mut cmd = echo_clients()
        .display_time(true)
        .user("O*hara")
        .filter("*Us*rs", true)
        .limit(5)
        .stream("//stream/main")
        .server_id("edge.1667");
    assert_eq!(spawned_args_unit(&mut cmd), full);
}

// Form 4 -------------------------------------------------------------------
// `p4 clients -U`
//
// A standalone form listing unloaded workspaces; it accepts no other
// option and is reachable only from the bare command.

fn form_4_unloaded() {
    let p4 = P4Cli::default();

    expect_chain!(["clients", "-U"], p4.clients().unloaded());
    expect_chain!(["clients", "-U"], p4.workspaces().unloaded());

    let mut cmd = echo_clients().unloaded();
    assert_eq!(spawned_args_unit(&mut cmd), ["clients", "-U"]);
}

// Rejected combinations -----------------------------------------------------
//
// The chains below contradict the documented forms and must fail to
// compile; they are kept as executable documentation of the type-state
// rules. Uncomment any line to verify it is rejected.
//
// // `-u` and `--me` share one syntax group (`[-u user ... | --me]`):
// // setting either removes the other from the type state.
// let _ = P4Cli::default().clients().user("x").filter_me();
// let _ = P4Cli::default().clients().filter_me().user("x");
//
// // The name filter is set at most once (`filter` consumes the F state).
// let _ = P4Cli::default().clients().filter("a", false).filter("b", true);
//
// // `-a` and `-s` are mutually exclusive.
// let _ = P4Cli::default().clients().all().server_id("x");
// let _ = P4Cli::default().clients().server_id("x").all();
//
// // `--user-case-insensitive` exists only on the `-u user` state.
// let _ = P4Cli::default().clients().filter_me().case_insensitive();
// let _ = P4Cli::default().clients().filter("a", false).case_insensitive();
//
// // `-U` is a standalone form: reachable only from the bare command, and
// // offering no further options.
// let _ = P4Cli::default().clients().limit(5).unloaded();
// let _ = P4Cli::default().clients().unloaded().limit(5);

fn main() -> io::Result<()> {
    form_1_bare();
    form_2_single_options();
    form_3_selection_orders();
    form_4_unloaded();
    println!("all p4 clients syntax combinations verified");
    Ok(())
}