From 7bd90bb66320a460c71be2924736006108e9ff95 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Thu, 20 Mar 2025 23:08:44 +0800 Subject: [PATCH] Implement `Default` for `Options` of `mv` and `cp` (#7506) --- src/uu/cp/src/cp.rs | 83 +++++++++++++++---- src/uu/mv/src/mv.rs | 20 ++++- src/uucore/src/lib/features/backup_control.rs | 7 +- src/uucore/src/lib/features/update_control.rs | 3 +- 4 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index f8a8d66fe..0aab4b68a 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -110,15 +110,16 @@ impl UError for Error { pub type CopyResult = Result; /// Specifies how to overwrite files. -#[derive(Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] pub enum ClobberMode { Force, RemoveDestination, + #[default] Standard, } /// Specifies whether files should be overwritten. -#[derive(Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum OverwriteMode { /// [Default] Always overwrite existing files Clobber(ClobberMode), @@ -128,18 +129,39 @@ pub enum OverwriteMode { NoClobber, } +impl Default for OverwriteMode { + fn default() -> Self { + Self::Clobber(ClobberMode::default()) + } +} + /// Possible arguments for `--reflink`. -#[derive(Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum ReflinkMode { Always, Auto, Never, } +impl Default for ReflinkMode { + #[allow(clippy::derivable_impls)] + fn default() -> Self { + #[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))] + { + ReflinkMode::Auto + } + #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))] + { + ReflinkMode::Never + } + } +} + /// Possible arguments for `--sparse`. -#[derive(Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)] pub enum SparseMode { Always, + #[default] Auto, Never, } @@ -152,10 +174,11 @@ pub enum TargetType { } /// Copy action to perform -#[derive(PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Default)] pub enum CopyMode { Link, SymLink, + #[default] Copy, Update, AttrOnly, @@ -174,7 +197,7 @@ pub enum CopyMode { /// For full compatibility with GNU, these options should also combine. We /// currently only do a best effort imitation of that behavior, because it is /// difficult to achieve in clap, especially with `--no-preserve`. -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct Attributes { #[cfg(unix)] pub ownership: Preserve, @@ -185,6 +208,12 @@ pub struct Attributes { pub xattr: Preserve, } +impl Default for Attributes { + fn default() -> Self { + Self::NONE + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Preserve { // explicit means whether the --no-preserve flag is used or not to distinguish out the default value. @@ -224,6 +253,7 @@ impl Ord for Preserve { /// /// The fields are documented with the arguments that determine their value. #[allow(dead_code)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct Options { /// `--attributes-only` pub attributes_only: bool, @@ -287,6 +317,34 @@ pub struct Options { pub progress_bar: bool, } +impl Default for Options { + fn default() -> Self { + Self { + attributes_only: false, + backup: BackupMode::default(), + copy_contents: false, + cli_dereference: false, + copy_mode: CopyMode::default(), + dereference: false, + no_target_dir: false, + one_file_system: false, + overwrite: OverwriteMode::default(), + parents: false, + sparse_mode: SparseMode::default(), + strip_trailing_slashes: false, + reflink_mode: ReflinkMode::default(), + attributes: Attributes::default(), + recursive: false, + backup_suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(), + target_dir: None, + update: UpdateMode::default(), + debug: false, + verbose: false, + progress_bar: false, + } + } +} + /// Enum representing if a file has been skipped. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum PerformedAction { @@ -1091,18 +1149,7 @@ impl Options { } } } else { - #[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))] - { - ReflinkMode::Auto - } - #[cfg(not(any( - target_os = "linux", - target_os = "android", - target_os = "macos" - )))] - { - ReflinkMode::Never - } + ReflinkMode::default() } }, sparse_mode: { diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index e35c33097..7ad267cf8 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -88,14 +88,32 @@ pub struct Options { pub debug: bool, } +impl Default for Options { + fn default() -> Self { + Self { + overwrite: OverwriteMode::default(), + backup: BackupMode::default(), + suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(), + update: UpdateMode::default(), + target_dir: None, + no_target_dir: false, + verbose: false, + strip_slashes: false, + progress_bar: false, + debug: false, + } + } +} + /// specifies behavior of the overwrite flag -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Default)] pub enum OverwriteMode { /// '-n' '--no-clobber' do not overwrite NoClobber, /// '-i' '--interactive' prompt before overwrite Interactive, ///'-f' '--force' overwrite without prompt + #[default] Force, } diff --git a/src/uucore/src/lib/features/backup_control.rs b/src/uucore/src/lib/features/backup_control.rs index 03793a50b..4bf859df7 100644 --- a/src/uucore/src/lib/features/backup_control.rs +++ b/src/uucore/src/lib/features/backup_control.rs @@ -114,13 +114,16 @@ static VALID_ARGS_HELP: &str = "Valid arguments are: - 'existing', 'nil' - 'numbered', 't'"; +pub const DEFAULT_BACKUP_SUFFIX: &str = "~"; + /// Available backup modes. /// /// The mapping of the backup modes to the CLI arguments is annotated on the /// enum variants. -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] pub enum BackupMode { /// Argument 'none', 'off' + #[default] NoBackup, /// Argument 'simple', 'never' SimpleBackup, @@ -254,7 +257,7 @@ pub fn determine_backup_suffix(matches: &ArgMatches) -> String { if let Some(suffix) = supplied_suffix { String::from(suffix) } else { - env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| "~".to_owned()) + env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| DEFAULT_BACKUP_SUFFIX.to_owned()) } } diff --git a/src/uucore/src/lib/features/update_control.rs b/src/uucore/src/lib/features/update_control.rs index 34cb8478b..95b403aff 100644 --- a/src/uucore/src/lib/features/update_control.rs +++ b/src/uucore/src/lib/features/update_control.rs @@ -49,9 +49,10 @@ use clap::ArgMatches; /// Available update mode -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Default)] pub enum UpdateMode { /// --update=`all`, `` + #[default] ReplaceAll, /// --update=`none` ReplaceNone,