mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 14:07:46 +00:00
backup_control: Rework function interfaces
Change all relevant functions to return `UResult`s from `BackupError` instead of error strings. Make `determine_backup_mode/suffix` accept `clap::ArgMatches` as input argument to parse for the arguments themselves, using the arguments with are defined in the `arguments` submodule. This way the user only needs to include the pre-defined arguments from the `arguments` module and passes a reference to the applications `ArgMatches` into the respective functions here. The functions then take care of handling the arguments. It is recommended to use the arguments provided in the `arguments` module over custom-defined ones.
This commit is contained in:
parent
54086ef4c5
commit
e132fd49d9
1 changed files with 16 additions and 27 deletions
|
@ -200,7 +200,8 @@ pub mod arguments {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn determine_backup_suffix(supplied_suffix: Option<&str>) -> String {
|
pub fn determine_backup_suffix(matches: &ArgMatches) -> String {
|
||||||
|
let supplied_suffix = matches.value_of(arguments::OPT_SUFFIX);
|
||||||
if let Some(suffix) = supplied_suffix {
|
if let Some(suffix) = supplied_suffix {
|
||||||
String::from(suffix)
|
String::from(suffix)
|
||||||
} else {
|
} else {
|
||||||
|
@ -305,17 +306,13 @@ pub fn determine_backup_suffix(supplied_suffix: Option<&str>) -> String {
|
||||||
/// };
|
/// };
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn determine_backup_mode(
|
pub fn determine_backup_mode(matches: &ArgMatches) -> UResult<BackupMode> {
|
||||||
short_opt_present: bool,
|
if matches.is_present(arguments::OPT_BACKUP) {
|
||||||
long_opt_present: bool,
|
|
||||||
long_opt_value: Option<&str>,
|
|
||||||
) -> Result<BackupMode, String> {
|
|
||||||
if long_opt_present {
|
|
||||||
// Use method to determine the type of backups to make. When this option
|
// Use method to determine the type of backups to make. When this option
|
||||||
// is used but method is not specified, then the value of the
|
// is used but method is not specified, then the value of the
|
||||||
// VERSION_CONTROL environment variable is used. And if VERSION_CONTROL
|
// VERSION_CONTROL environment variable is used. And if VERSION_CONTROL
|
||||||
// is not set, the default backup type is ‘existing’.
|
// is not set, the default backup type is ‘existing’.
|
||||||
if let Some(method) = long_opt_value {
|
if let Some(method) = matches.value_of(arguments::OPT_BACKUP) {
|
||||||
// Second argument is for the error string that is returned.
|
// Second argument is for the error string that is returned.
|
||||||
match_method(method, "backup type")
|
match_method(method, "backup type")
|
||||||
} else if let Ok(method) = env::var("VERSION_CONTROL") {
|
} else if let Ok(method) = env::var("VERSION_CONTROL") {
|
||||||
|
@ -324,7 +321,7 @@ pub fn determine_backup_mode(
|
||||||
} else {
|
} else {
|
||||||
Ok(BackupMode::ExistingBackup)
|
Ok(BackupMode::ExistingBackup)
|
||||||
}
|
}
|
||||||
} else if short_opt_present {
|
} else if matches.is_present(arguments::OPT_BACKUP_NO_ARG) {
|
||||||
// the short form of this option, -b does not accept any argument.
|
// the short form of this option, -b does not accept any argument.
|
||||||
// Using -b is equivalent to using --backup=existing.
|
// Using -b is equivalent to using --backup=existing.
|
||||||
Ok(BackupMode::ExistingBackup)
|
Ok(BackupMode::ExistingBackup)
|
||||||
|
@ -347,10 +344,13 @@ pub fn determine_backup_mode(
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// If `method` is ambiguous (i.e. may resolve to multiple backup modes) or
|
/// If `method` is invalid or ambiguous (i.e. may resolve to multiple backup
|
||||||
/// invalid, an error is returned. The error contains the formatted error string
|
/// modes), an [`InvalidArgument`][10] or [`AmbiguousArgument`][11] error is
|
||||||
/// which may then be passed to the [`show_usage_error`] macro.
|
/// returned, respectively.
|
||||||
fn match_method(method: &str, origin: &str) -> Result<BackupMode, String> {
|
///
|
||||||
|
/// [10]: BackupError::InvalidArgument
|
||||||
|
/// [11]: BackupError::AmbiguousArgument
|
||||||
|
fn match_method(method: &str, origin: &str) -> UResult<BackupMode> {
|
||||||
let matches: Vec<&&str> = BACKUP_CONTROL_VALUES
|
let matches: Vec<&&str> = BACKUP_CONTROL_VALUES
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|val| val.starts_with(method))
|
.filter(|val| val.starts_with(method))
|
||||||
|
@ -364,21 +364,10 @@ fn match_method(method: &str, origin: &str) -> Result<BackupMode, String> {
|
||||||
_ => unreachable!(), // cannot happen as we must have exactly one match
|
_ => unreachable!(), // cannot happen as we must have exactly one match
|
||||||
// from the list above.
|
// from the list above.
|
||||||
}
|
}
|
||||||
|
} else if matches.is_empty() {
|
||||||
|
Err(BackupError::InvalidArgument(method.to_string(), origin.to_string()).into())
|
||||||
} else {
|
} else {
|
||||||
let error_type = if matches.is_empty() {
|
Err(BackupError::AmbiguousArgument(method.to_string(), origin.to_string()).into())
|
||||||
"invalid"
|
|
||||||
} else {
|
|
||||||
"ambiguous"
|
|
||||||
};
|
|
||||||
Err(format!(
|
|
||||||
"{0} argument ‘{1}’ for ‘{2}’
|
|
||||||
Valid arguments are:
|
|
||||||
- ‘none’, ‘off’
|
|
||||||
- ‘simple’, ‘never’
|
|
||||||
- ‘existing’, ‘nil’
|
|
||||||
- ‘numbered’, ‘t’",
|
|
||||||
error_type, method, origin
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue