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 describe;
29pub mod diff;
30pub mod diff2;
31pub mod edit;
32pub mod filelog;
33pub mod print;
34pub mod sync;
35pub mod r#where;
36
37pub use add::Add;
38pub use admin::AdminEntry;
39#[cfg(not(feature = "lt2016_1"))]
40pub use aliases::Aliases;
41pub use annotate::Annotate;
42pub use archive::Archive;
43pub use attribute::Attribute;
44#[cfg(not(feature = "lt2017_2"))]
45pub use bgtask::BackGroundTask;
46pub use branch::Branch;
47pub use branches::Branches;
48pub use cachepurge::CachePurge;
49pub use change::Change;
50pub use changes::Changes;
51pub use clean::Clean;
52pub use client::Client;
53pub use clients::Clients;
54#[cfg(not(feature = "lt2015_1"))]
55pub use clone::Clone;
56pub use configure::Configure;
57pub use copy::Copy;
58pub use counter::Counter;
59pub use counters::Counters;
60pub use cstat::CStat;
61pub use dbschema::DBSchema;
62pub use dbstat::DBStat;
63pub use dbverify::DBVerify;
64pub use describe::Describe;
65pub use diff::Diff;
66pub use diff::DisplayOptions;
67pub use diff2::Diff2;
68pub use edit::Edit;
69pub use filelog::FileLog;
70pub use print::Print;
71pub use sync::Sync;
72pub use r#where::Where;
73
74use std::{ffi::OsStr, process::Command};
75
76use crate::global::GlobalOpts;
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum LongOutput {
85 Default,
87 Truncated,
89}
90
91impl LongOutput {
92 pub fn as_str(&self) -> &'static str {
94 match self {
95 LongOutput::Default => "-l",
96 LongOutput::Truncated => "-L",
97 }
98 }
99}
100
101#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
107pub enum DiffFormat {
108 #[default]
110 Default,
111 Context(Option<u32>),
114 Rcs,
117 Summary,
120 Unified(Option<u32>),
123}
124
125#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
134pub enum WhitespaceHandling {
135 #[default]
137 None,
138 IgnoreLineEndings,
140 IgnoreChangesWithinWhitespace,
142 IgnoreAllWhitespace,
144}
145
146#[derive(Debug, Clone, PartialEq, Eq)]
175pub enum DiffOptions {
176 Typed {
179 format: DiffFormat,
180 whitespace: WhitespaceHandling,
181 },
182 Raw(String),
185}
186
187impl Default for DiffOptions {
188 fn default() -> Self {
189 DiffOptions::Typed {
190 format: DiffFormat::Default,
191 whitespace: WhitespaceHandling::None,
192 }
193 }
194}
195
196impl DiffOptions {
197 pub fn format(&self) -> Option<DiffFormat> {
200 match self {
201 DiffOptions::Typed { format, .. } => Some(*format),
202 DiffOptions::Raw(_) => None,
203 }
204 }
205
206 pub fn whitespace_handling(&self) -> Option<WhitespaceHandling> {
209 match self {
210 DiffOptions::Typed { whitespace, .. } => Some(*whitespace),
211 DiffOptions::Raw(_) => None,
212 }
213 }
214
215 pub fn raw_str(&self) -> Option<&str> {
218 match self {
219 DiffOptions::Typed { .. } => None,
220 DiffOptions::Raw(s) => Some(s),
221 }
222 }
223
224 pub fn inject_arg(&self, command: &mut Command) {
229 use DiffFormat::*;
230 use WhitespaceHandling::*;
231
232 let (format, whitespace) = match self {
233 DiffOptions::Typed { format, whitespace } => (*format, *whitespace),
234 DiffOptions::Raw(s) => {
235 command.arg(format!("-d{s}"));
236 return;
237 }
238 };
239
240 if matches!(format, Default) && matches!(whitespace, None) {
241 return;
242 }
243
244 let mut s = String::from("-d");
245
246 match format {
247 Default => {}
248 Context(num) => {
249 s.push('c');
250 if let Some(n) = num {
251 s.push_str(&n.to_string());
252 }
253 }
254 Rcs => s.push('n'),
255 Summary => s.push('s'),
256 Unified(num) => {
257 s.push('u');
258 if let Some(n) = num {
259 s.push_str(&n.to_string());
260 }
261 }
262 }
263
264 match whitespace {
265 None => {}
266 IgnoreLineEndings => s.push('l'),
267 IgnoreChangesWithinWhitespace => s.push('b'),
268 IgnoreAllWhitespace => s.push('w'),
269 }
270
271 command.arg(s);
272 }
273}
274
275impl From<String> for DiffOptions {
276 fn from(s: String) -> Self {
277 DiffOptions::Raw(s)
278 }
279}
280
281#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
285pub struct DiffOptionsTypedMode {
286 format: DiffFormat,
287 whitespace: WhitespaceHandling,
288}
289
290#[derive(Debug, Clone, PartialEq, Eq)]
293pub struct DiffOptionsRawMode {
294 raw: String,
295}
296
297#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
330pub struct DiffOptionsBuilder<M> {
331 mode: M,
332}
333
334impl DiffOptionsBuilder<DiffOptionsTypedMode> {
335 pub fn new() -> Self {
339 Self {
340 mode: DiffOptionsTypedMode::default(),
341 }
342 }
343
344 pub fn context(num: Option<u32>) -> Self {
346 Self {
347 mode: DiffOptionsTypedMode {
348 format: DiffFormat::Context(num),
349 whitespace: WhitespaceHandling::None,
350 },
351 }
352 }
353
354 pub fn rcs() -> Self {
356 Self {
357 mode: DiffOptionsTypedMode {
358 format: DiffFormat::Rcs,
359 whitespace: WhitespaceHandling::None,
360 },
361 }
362 }
363
364 pub fn summary() -> Self {
366 Self {
367 mode: DiffOptionsTypedMode {
368 format: DiffFormat::Summary,
369 whitespace: WhitespaceHandling::None,
370 },
371 }
372 }
373
374 pub fn unified(num: Option<u32>) -> Self {
376 Self {
377 mode: DiffOptionsTypedMode {
378 format: DiffFormat::Unified(num),
379 whitespace: WhitespaceHandling::None,
380 },
381 }
382 }
383
384 pub fn ignore_line_endings(mut self) -> Self {
386 self.mode.whitespace = WhitespaceHandling::IgnoreLineEndings;
387 self
388 }
389
390 pub fn ignore_changes_within_whitespace(mut self) -> Self {
392 self.mode.whitespace = WhitespaceHandling::IgnoreChangesWithinWhitespace;
393 self
394 }
395
396 pub fn ignore_all_whitespace(mut self) -> Self {
398 self.mode.whitespace = WhitespaceHandling::IgnoreAllWhitespace;
399 self
400 }
401}
402
403impl DiffOptionsBuilder<DiffOptionsRawMode> {
404 pub fn raw(s: impl Into<String>) -> Self {
410 Self {
411 mode: DiffOptionsRawMode { raw: s.into() },
412 }
413 }
414}
415
416impl From<DiffOptionsBuilder<DiffOptionsTypedMode>> for DiffOptions {
417 fn from(builder: DiffOptionsBuilder<DiffOptionsTypedMode>) -> Self {
418 DiffOptions::Typed {
419 format: builder.mode.format,
420 whitespace: builder.mode.whitespace,
421 }
422 }
423}
424
425impl From<DiffOptionsBuilder<DiffOptionsRawMode>> for DiffOptions {
426 fn from(builder: DiffOptionsBuilder<DiffOptionsRawMode>) -> Self {
427 DiffOptions::Raw(builder.mode.raw)
428 }
429}
430
431#[derive(Debug, Clone, Copy, Default)]
438pub struct Unselected;
439
440impl ExclusiveOption for Unselected {
441 fn inject_args(&self, _: &mut Command) {}
442}
443
444pub trait ExclusiveOption {
457 #[allow(unused_variables)]
458 fn inject_args(&self, command: &mut Command) {}
461}
462
463pub trait SubCommand {
464 fn name(&self) -> &str;
465
466 fn inject_local_args(&self, command: &mut Command);
467
468 fn global_opts(&self) -> Option<&GlobalOpts> {
469 None
470 }
471
472 fn inject_args(&self, command: &mut Command) {
473 if let Some(global_opts) = self.global_opts() {
475 global_opts.setup_args(command);
476 };
477 self.inject_local_args(command.arg(self.name()));
479 }
480
481 fn setup_command<S: AsRef<OsStr>>(&self, bin: S) -> Command {
482 let mut cmd = Command::new(bin);
483
484 self.inject_args(&mut cmd);
485 cmd
486 }
487}
488
489#[cfg(test)]
492pub(crate) fn args_of(command: &Command) -> Vec<String> {
493 command
494 .get_args()
495 .map(|arg| arg.to_string_lossy().into_owned())
496 .collect()
497}
498
499#[cfg(test)]
500mod diff_options_tests {
501 use super::*;
502
503 fn injected(opts: impl Into<DiffOptions>) -> Vec<String> {
504 let mut cmd = Command::new("p4");
505 opts.into().inject_arg(&mut cmd);
506 args_of(&cmd)
507 }
508
509 #[test]
510 fn default_emits_nothing() {
511 assert!(injected(DiffOptionsBuilder::new()).is_empty());
512 assert!(injected(DiffOptions::default()).is_empty());
513 }
514
515 #[test]
516 fn unified_format() {
517 assert_eq!(injected(DiffOptionsBuilder::unified(None)), ["-du"]);
518 }
519
520 #[test]
521 fn unified_format_with_context() {
522 assert_eq!(injected(DiffOptionsBuilder::unified(Some(3))), ["-du3"]);
523 }
524
525 #[test]
526 fn context_format() {
527 assert_eq!(injected(DiffOptionsBuilder::context(None)), ["-dc"]);
528 assert_eq!(injected(DiffOptionsBuilder::context(Some(5))), ["-dc5"]);
529 }
530
531 #[test]
532 fn rcs_format() {
533 assert_eq!(injected(DiffOptionsBuilder::rcs()), ["-dn"]);
534 }
535
536 #[test]
537 fn summary_format() {
538 assert_eq!(injected(DiffOptionsBuilder::summary()), ["-ds"]);
539 }
540
541 #[test]
542 fn whitespace_only() {
543 assert_eq!(
544 injected(DiffOptionsBuilder::new().ignore_line_endings()),
545 ["-dl"]
546 );
547 }
548
549 #[test]
550 fn unified_with_ignore_changes_within_whitespace() {
551 assert_eq!(
552 injected(DiffOptionsBuilder::unified(None).ignore_changes_within_whitespace()),
553 ["-dub"]
554 );
555 }
556
557 #[test]
558 fn context_with_ignore_all_whitespace() {
559 assert_eq!(
560 injected(DiffOptionsBuilder::context(Some(2)).ignore_all_whitespace()),
561 ["-dc2w"]
562 );
563 }
564
565 #[test]
566 fn raw_passthrough() {
567 assert_eq!(injected(DiffOptionsBuilder::raw("-C 25")), ["-d-C 25"]);
568 assert_eq!(injected(DiffOptionsBuilder::raw("--brief")), ["-d--brief"]);
569 }
570
571 #[test]
572 fn getters_reflect_variant() {
573 let typed = DiffOptions::from(DiffOptionsBuilder::unified(Some(3)).ignore_line_endings());
574 assert_eq!(typed.format(), Some(DiffFormat::Unified(Some(3))));
575 assert_eq!(
576 typed.whitespace_handling(),
577 Some(WhitespaceHandling::IgnoreLineEndings)
578 );
579 assert_eq!(typed.raw_str(), None);
580
581 let raw = DiffOptions::from(DiffOptionsBuilder::raw("abc"));
582 assert_eq!(raw.format(), None);
583 assert_eq!(raw.whitespace_handling(), None);
584 assert_eq!(raw.raw_str(), Some("abc"));
585 }
586}