1pub mod add;
2pub mod admin;
3#[cfg(not(feature = "lt2016_1"))]
4pub mod aliases;
5pub mod annotate;
6pub mod archive;
7pub mod attribute;
8#[cfg(not(feature = "lt2017_2"))]
9pub mod bgtask;
10pub mod branch;
11pub mod branches;
12pub mod cachepurge;
13pub mod change;
14pub mod changes;
15pub mod clean;
16pub mod client;
17pub mod clients;
18#[cfg(not(feature = "lt2015_1"))]
19pub mod clone;
20pub mod configure;
21pub mod copy;
22pub mod counter;
23pub mod counters;
24pub mod cstat;
25pub mod dbschema;
26pub mod dbstat;
27pub mod dbverify;
28pub mod delete;
29pub mod depot;
30pub mod depots;
31pub mod describe;
32#[cfg(not(feature = "lt2024_2"))]
33pub mod diagnostics;
34pub mod diff;
35pub mod diff2;
36pub mod dirs;
37pub mod diskspace;
38pub mod edit;
39pub mod export;
40#[cfg(not(feature = "lt2019_1"))]
41pub mod extension;
42pub mod filelog;
43pub mod print;
44pub mod sync;
45pub mod r#where;
46
47pub use add::Add;
48pub use admin::AdminEntry;
49#[cfg(not(feature = "lt2016_1"))]
50pub use aliases::Aliases;
51pub use annotate::Annotate;
52pub use archive::Archive;
53pub use attribute::Attribute;
54#[cfg(not(feature = "lt2017_2"))]
55pub use bgtask::BackGroundTask;
56pub use branch::Branch;
57pub use branches::Branches;
58pub use cachepurge::CachePurge;
59pub use change::Change;
60pub use changes::Changes;
61pub use clean::Clean;
62pub use client::Client;
63pub use clients::Clients;
64#[cfg(not(feature = "lt2015_1"))]
65pub use clone::Clone;
66pub use configure::Configure;
67pub use copy::Copy;
68pub use counter::Counter;
69pub use counters::Counters;
70pub use cstat::CStat;
71pub use dbschema::DBSchema;
72pub use dbstat::DBStat;
73pub use dbverify::DBVerify;
74pub use delete::Delete;
75pub use depot::Depot;
76#[cfg(not(feature = "lt2015_2"))]
77pub use depot::DepotType;
78pub use depots::Depots;
79pub use describe::Describe;
80#[cfg(not(feature = "lt2024_2"))]
81pub use diagnostics::Diagnostics;
82pub use diff::Diff;
83pub use diff::DisplayOptions;
84pub use diff2::Diff2;
85pub use dirs::Dirs;
86#[cfg(not(feature = "lt2022_2"))]
87pub use dirs::StreamView;
88pub use dirs::WorkspaceView;
89pub use diskspace::DiskSpace;
90pub use diskspace::P4Journal;
91pub use diskspace::P4Log;
92pub use diskspace::P4Root;
93pub use diskspace::Temp;
94pub use edit::Edit;
95pub use export::CheckPoint;
96pub use export::Export;
97pub use export::Format;
98pub use export::Journal;
99pub use export::RawJournal;
100#[cfg(not(feature = "lt2019_1"))]
101pub use extension::ExtensionEntry;
102pub use filelog::FileLog;
103pub use print::Print;
104pub use sync::Sync;
105pub use r#where::Where;
106
107use std::{ffi::OsStr, process::Command};
108
109use crate::global::GlobalOpts;
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq)]
117pub enum LongOutput {
118 Default,
120 Truncated,
122}
123
124impl LongOutput {
125 pub fn as_str(&self) -> &'static str {
127 match self {
128 LongOutput::Default => "-l",
129 LongOutput::Truncated => "-L",
130 }
131 }
132}
133
134#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
140pub enum DiffFormat {
141 #[default]
143 Default,
144 Context(Option<u32>),
147 Rcs,
150 Summary,
153 Unified(Option<u32>),
156}
157
158#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
167pub enum WhitespaceHandling {
168 #[default]
170 None,
171 IgnoreLineEndings,
173 IgnoreChangesWithinWhitespace,
175 IgnoreAllWhitespace,
177}
178
179#[derive(Debug, Clone, PartialEq, Eq)]
208pub enum DiffOptions {
209 Typed {
212 format: DiffFormat,
213 whitespace: WhitespaceHandling,
214 },
215 Raw(String),
218}
219
220impl Default for DiffOptions {
221 fn default() -> Self {
222 DiffOptions::Typed {
223 format: DiffFormat::Default,
224 whitespace: WhitespaceHandling::None,
225 }
226 }
227}
228
229impl DiffOptions {
230 pub fn format(&self) -> Option<DiffFormat> {
233 match self {
234 DiffOptions::Typed { format, .. } => Some(*format),
235 DiffOptions::Raw(_) => None,
236 }
237 }
238
239 pub fn whitespace_handling(&self) -> Option<WhitespaceHandling> {
242 match self {
243 DiffOptions::Typed { whitespace, .. } => Some(*whitespace),
244 DiffOptions::Raw(_) => None,
245 }
246 }
247
248 pub fn raw_str(&self) -> Option<&str> {
251 match self {
252 DiffOptions::Typed { .. } => None,
253 DiffOptions::Raw(s) => Some(s),
254 }
255 }
256
257 pub fn inject_arg(&self, command: &mut Command) {
262 use DiffFormat::*;
263 use WhitespaceHandling::*;
264
265 let (format, whitespace) = match self {
266 DiffOptions::Typed { format, whitespace } => (*format, *whitespace),
267 DiffOptions::Raw(s) => {
268 command.arg(format!("-d{s}"));
269 return;
270 }
271 };
272
273 if matches!(format, Default) && matches!(whitespace, None) {
274 return;
275 }
276
277 let mut s = String::from("-d");
278
279 match format {
280 Default => {}
281 Context(num) => {
282 s.push('c');
283 if let Some(n) = num {
284 s.push_str(&n.to_string());
285 }
286 }
287 Rcs => s.push('n'),
288 Summary => s.push('s'),
289 Unified(num) => {
290 s.push('u');
291 if let Some(n) = num {
292 s.push_str(&n.to_string());
293 }
294 }
295 }
296
297 match whitespace {
298 None => {}
299 IgnoreLineEndings => s.push('l'),
300 IgnoreChangesWithinWhitespace => s.push('b'),
301 IgnoreAllWhitespace => s.push('w'),
302 }
303
304 command.arg(s);
305 }
306}
307
308impl From<String> for DiffOptions {
309 fn from(s: String) -> Self {
310 DiffOptions::Raw(s)
311 }
312}
313
314#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
318pub struct DiffOptionsTypedMode {
319 format: DiffFormat,
320 whitespace: WhitespaceHandling,
321}
322
323#[derive(Debug, Clone, PartialEq, Eq)]
326pub struct DiffOptionsRawMode {
327 raw: String,
328}
329
330#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
363pub struct DiffOptionsBuilder<M> {
364 mode: M,
365}
366
367impl DiffOptionsBuilder<DiffOptionsTypedMode> {
368 pub fn new() -> Self {
372 Self {
373 mode: DiffOptionsTypedMode::default(),
374 }
375 }
376
377 pub fn context(num: Option<u32>) -> Self {
379 Self {
380 mode: DiffOptionsTypedMode {
381 format: DiffFormat::Context(num),
382 whitespace: WhitespaceHandling::None,
383 },
384 }
385 }
386
387 pub fn rcs() -> Self {
389 Self {
390 mode: DiffOptionsTypedMode {
391 format: DiffFormat::Rcs,
392 whitespace: WhitespaceHandling::None,
393 },
394 }
395 }
396
397 pub fn summary() -> Self {
399 Self {
400 mode: DiffOptionsTypedMode {
401 format: DiffFormat::Summary,
402 whitespace: WhitespaceHandling::None,
403 },
404 }
405 }
406
407 pub fn unified(num: Option<u32>) -> Self {
409 Self {
410 mode: DiffOptionsTypedMode {
411 format: DiffFormat::Unified(num),
412 whitespace: WhitespaceHandling::None,
413 },
414 }
415 }
416
417 pub fn ignore_line_endings(mut self) -> Self {
419 self.mode.whitespace = WhitespaceHandling::IgnoreLineEndings;
420 self
421 }
422
423 pub fn ignore_changes_within_whitespace(mut self) -> Self {
425 self.mode.whitespace = WhitespaceHandling::IgnoreChangesWithinWhitespace;
426 self
427 }
428
429 pub fn ignore_all_whitespace(mut self) -> Self {
431 self.mode.whitespace = WhitespaceHandling::IgnoreAllWhitespace;
432 self
433 }
434}
435
436impl DiffOptionsBuilder<DiffOptionsRawMode> {
437 pub fn raw(s: impl Into<String>) -> Self {
443 Self {
444 mode: DiffOptionsRawMode { raw: s.into() },
445 }
446 }
447}
448
449impl From<DiffOptionsBuilder<DiffOptionsTypedMode>> for DiffOptions {
450 fn from(builder: DiffOptionsBuilder<DiffOptionsTypedMode>) -> Self {
451 DiffOptions::Typed {
452 format: builder.mode.format,
453 whitespace: builder.mode.whitespace,
454 }
455 }
456}
457
458impl From<DiffOptionsBuilder<DiffOptionsRawMode>> for DiffOptions {
459 fn from(builder: DiffOptionsBuilder<DiffOptionsRawMode>) -> Self {
460 DiffOptions::Raw(builder.mode.raw)
461 }
462}
463
464#[derive(Debug, Clone, Copy, Default)]
471pub struct Unselected;
472
473impl ExclusiveOption for Unselected {
474 fn inject_args(&self, _: &mut Command) {}
475}
476
477pub trait ExclusiveOption {
490 #[allow(unused_variables)]
491 fn inject_args(&self, command: &mut Command) {}
494}
495
496pub trait SubCommand {
497 fn name(&self) -> &str;
498
499 fn inject_local_args(&self, command: &mut Command);
500
501 fn global_opts(&self) -> Option<&GlobalOpts> {
502 None
503 }
504
505 fn inject_args(&self, command: &mut Command) {
506 if let Some(global_opts) = self.global_opts() {
508 global_opts.setup_args(command);
509 };
510 self.inject_local_args(command.arg(self.name()));
512 }
513
514 fn setup_command<S: AsRef<OsStr>>(&self, bin: S) -> Command {
515 let mut cmd = Command::new(bin);
516
517 self.inject_args(&mut cmd);
518 cmd
519 }
520}
521
522#[cfg(test)]
525pub(crate) fn args_of(command: &Command) -> Vec<String> {
526 command
527 .get_args()
528 .map(|arg| arg.to_string_lossy().into_owned())
529 .collect()
530}
531
532#[cfg(test)]
533mod diff_options_tests {
534 use super::*;
535
536 fn injected(opts: impl Into<DiffOptions>) -> Vec<String> {
537 let mut cmd = Command::new("p4");
538 opts.into().inject_arg(&mut cmd);
539 args_of(&cmd)
540 }
541
542 #[test]
543 fn default_emits_nothing() {
544 assert!(injected(DiffOptionsBuilder::new()).is_empty());
545 assert!(injected(DiffOptions::default()).is_empty());
546 }
547
548 #[test]
549 fn unified_format() {
550 assert_eq!(injected(DiffOptionsBuilder::unified(None)), ["-du"]);
551 }
552
553 #[test]
554 fn unified_format_with_context() {
555 assert_eq!(injected(DiffOptionsBuilder::unified(Some(3))), ["-du3"]);
556 }
557
558 #[test]
559 fn context_format() {
560 assert_eq!(injected(DiffOptionsBuilder::context(None)), ["-dc"]);
561 assert_eq!(injected(DiffOptionsBuilder::context(Some(5))), ["-dc5"]);
562 }
563
564 #[test]
565 fn rcs_format() {
566 assert_eq!(injected(DiffOptionsBuilder::rcs()), ["-dn"]);
567 }
568
569 #[test]
570 fn summary_format() {
571 assert_eq!(injected(DiffOptionsBuilder::summary()), ["-ds"]);
572 }
573
574 #[test]
575 fn whitespace_only() {
576 assert_eq!(
577 injected(DiffOptionsBuilder::new().ignore_line_endings()),
578 ["-dl"]
579 );
580 }
581
582 #[test]
583 fn unified_with_ignore_changes_within_whitespace() {
584 assert_eq!(
585 injected(DiffOptionsBuilder::unified(None).ignore_changes_within_whitespace()),
586 ["-dub"]
587 );
588 }
589
590 #[test]
591 fn context_with_ignore_all_whitespace() {
592 assert_eq!(
593 injected(DiffOptionsBuilder::context(Some(2)).ignore_all_whitespace()),
594 ["-dc2w"]
595 );
596 }
597
598 #[test]
599 fn raw_passthrough() {
600 assert_eq!(injected(DiffOptionsBuilder::raw("-C 25")), ["-d-C 25"]);
601 assert_eq!(injected(DiffOptionsBuilder::raw("--brief")), ["-d--brief"]);
602 }
603
604 #[test]
605 fn getters_reflect_variant() {
606 let typed = DiffOptions::from(DiffOptionsBuilder::unified(Some(3)).ignore_line_endings());
607 assert_eq!(typed.format(), Some(DiffFormat::Unified(Some(3))));
608 assert_eq!(
609 typed.whitespace_handling(),
610 Some(WhitespaceHandling::IgnoreLineEndings)
611 );
612 assert_eq!(typed.raw_str(), None);
613
614 let raw = DiffOptions::from(DiffOptionsBuilder::raw("abc"));
615 assert_eq!(raw.format(), None);
616 assert_eq!(raw.whitespace_handling(), None);
617 assert_eq!(raw.raw_str(), Some("abc"));
618 }
619}