1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

Implement Default for Options of mv and cp (#7506)

This commit is contained in:
jmjoy 2025-03-20 23:08:44 +08:00 committed by GitHub
parent 187d3e58b5
commit 7bd90bb663
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 91 additions and 22 deletions

View file

@ -110,15 +110,16 @@ impl UError for Error {
pub type CopyResult<T> = Result<T, Error>;
/// 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: {

View file

@ -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,
}

View file

@ -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())
}
}

View file

@ -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,