Skip to content

Commit 7bd90bb

Browse files
authored
Implement Default for Options of mv and cp (#7506)
1 parent 187d3e5 commit 7bd90bb

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed

src/uu/cp/src/cp.rs

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,16 @@ impl UError for Error {
110110
pub type CopyResult<T> = Result<T, Error>;
111111

112112
/// Specifies how to overwrite files.
113-
#[derive(Clone, Copy, Eq, PartialEq)]
113+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
114114
pub enum ClobberMode {
115115
Force,
116116
RemoveDestination,
117+
#[default]
117118
Standard,
118119
}
119120

120121
/// Specifies whether files should be overwritten.
121-
#[derive(Clone, Copy, Eq, PartialEq)]
122+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
122123
pub enum OverwriteMode {
123124
/// [Default] Always overwrite existing files
124125
Clobber(ClobberMode),
@@ -128,18 +129,39 @@ pub enum OverwriteMode {
128129
NoClobber,
129130
}
130131

132+
impl Default for OverwriteMode {
133+
fn default() -> Self {
134+
Self::Clobber(ClobberMode::default())
135+
}
136+
}
137+
131138
/// Possible arguments for `--reflink`.
132-
#[derive(Copy, Clone, Eq, PartialEq)]
139+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
133140
pub enum ReflinkMode {
134141
Always,
135142
Auto,
136143
Never,
137144
}
138145

146+
impl Default for ReflinkMode {
147+
#[allow(clippy::derivable_impls)]
148+
fn default() -> Self {
149+
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
150+
{
151+
ReflinkMode::Auto
152+
}
153+
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))]
154+
{
155+
ReflinkMode::Never
156+
}
157+
}
158+
}
159+
139160
/// Possible arguments for `--sparse`.
140-
#[derive(Copy, Clone, Eq, PartialEq)]
161+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
141162
pub enum SparseMode {
142163
Always,
164+
#[default]
143165
Auto,
144166
Never,
145167
}
@@ -152,10 +174,11 @@ pub enum TargetType {
152174
}
153175

154176
/// Copy action to perform
155-
#[derive(PartialEq)]
177+
#[derive(Debug, Clone, Eq, PartialEq, Default)]
156178
pub enum CopyMode {
157179
Link,
158180
SymLink,
181+
#[default]
159182
Copy,
160183
Update,
161184
AttrOnly,
@@ -174,7 +197,7 @@ pub enum CopyMode {
174197
/// For full compatibility with GNU, these options should also combine. We
175198
/// currently only do a best effort imitation of that behavior, because it is
176199
/// difficult to achieve in clap, especially with `--no-preserve`.
177-
#[derive(Debug, PartialEq, Clone, Copy)]
200+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
178201
pub struct Attributes {
179202
#[cfg(unix)]
180203
pub ownership: Preserve,
@@ -185,6 +208,12 @@ pub struct Attributes {
185208
pub xattr: Preserve,
186209
}
187210

211+
impl Default for Attributes {
212+
fn default() -> Self {
213+
Self::NONE
214+
}
215+
}
216+
188217
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
189218
pub enum Preserve {
190219
// 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 {
224253
///
225254
/// The fields are documented with the arguments that determine their value.
226255
#[allow(dead_code)]
256+
#[derive(Debug, Clone, Eq, PartialEq)]
227257
pub struct Options {
228258
/// `--attributes-only`
229259
pub attributes_only: bool,
@@ -287,6 +317,34 @@ pub struct Options {
287317
pub progress_bar: bool,
288318
}
289319

320+
impl Default for Options {
321+
fn default() -> Self {
322+
Self {
323+
attributes_only: false,
324+
backup: BackupMode::default(),
325+
copy_contents: false,
326+
cli_dereference: false,
327+
copy_mode: CopyMode::default(),
328+
dereference: false,
329+
no_target_dir: false,
330+
one_file_system: false,
331+
overwrite: OverwriteMode::default(),
332+
parents: false,
333+
sparse_mode: SparseMode::default(),
334+
strip_trailing_slashes: false,
335+
reflink_mode: ReflinkMode::default(),
336+
attributes: Attributes::default(),
337+
recursive: false,
338+
backup_suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(),
339+
target_dir: None,
340+
update: UpdateMode::default(),
341+
debug: false,
342+
verbose: false,
343+
progress_bar: false,
344+
}
345+
}
346+
}
347+
290348
/// Enum representing if a file has been skipped.
291349
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
292350
enum PerformedAction {
@@ -1091,18 +1149,7 @@ impl Options {
10911149
}
10921150
}
10931151
} else {
1094-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
1095-
{
1096-
ReflinkMode::Auto
1097-
}
1098-
#[cfg(not(any(
1099-
target_os = "linux",
1100-
target_os = "android",
1101-
target_os = "macos"
1102-
)))]
1103-
{
1104-
ReflinkMode::Never
1105-
}
1152+
ReflinkMode::default()
11061153
}
11071154
},
11081155
sparse_mode: {

src/uu/mv/src/mv.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,32 @@ pub struct Options {
8888
pub debug: bool,
8989
}
9090

91+
impl Default for Options {
92+
fn default() -> Self {
93+
Self {
94+
overwrite: OverwriteMode::default(),
95+
backup: BackupMode::default(),
96+
suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(),
97+
update: UpdateMode::default(),
98+
target_dir: None,
99+
no_target_dir: false,
100+
verbose: false,
101+
strip_slashes: false,
102+
progress_bar: false,
103+
debug: false,
104+
}
105+
}
106+
}
107+
91108
/// specifies behavior of the overwrite flag
92-
#[derive(Clone, Debug, Eq, PartialEq)]
109+
#[derive(Clone, Debug, Eq, PartialEq, Default)]
93110
pub enum OverwriteMode {
94111
/// '-n' '--no-clobber' do not overwrite
95112
NoClobber,
96113
/// '-i' '--interactive' prompt before overwrite
97114
Interactive,
98115
///'-f' '--force' overwrite without prompt
116+
#[default]
99117
Force,
100118
}
101119

src/uucore/src/lib/features/backup_control.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,16 @@ static VALID_ARGS_HELP: &str = "Valid arguments are:
114114
- 'existing', 'nil'
115115
- 'numbered', 't'";
116116

117+
pub const DEFAULT_BACKUP_SUFFIX: &str = "~";
118+
117119
/// Available backup modes.
118120
///
119121
/// The mapping of the backup modes to the CLI arguments is annotated on the
120122
/// enum variants.
121-
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
123+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
122124
pub enum BackupMode {
123125
/// Argument 'none', 'off'
126+
#[default]
124127
NoBackup,
125128
/// Argument 'simple', 'never'
126129
SimpleBackup,
@@ -254,7 +257,7 @@ pub fn determine_backup_suffix(matches: &ArgMatches) -> String {
254257
if let Some(suffix) = supplied_suffix {
255258
String::from(suffix)
256259
} else {
257-
env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| "~".to_owned())
260+
env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| DEFAULT_BACKUP_SUFFIX.to_owned())
258261
}
259262
}
260263

src/uucore/src/lib/features/update_control.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@
4949
use clap::ArgMatches;
5050

5151
/// Available update mode
52-
#[derive(Clone, Debug, Eq, PartialEq)]
52+
#[derive(Clone, Debug, Eq, PartialEq, Default)]
5353
pub enum UpdateMode {
5454
/// --update=`all`, ``
55+
#[default]
5556
ReplaceAll,
5657
/// --update=`none`
5758
ReplaceNone,

0 commit comments

Comments
 (0)