rm: implement From for InteractiveMode

This commit is contained in:
Daniel Hofstetter
2025-07-31 09:49:27 +02:00
parent f33244321a
commit 194aa89d63
3 changed files with 15 additions and 12 deletions
-1
View File
@@ -32,7 +32,6 @@ rm-help-verbose = explain what is being done
# Error messages
rm-error-missing-operand = missing operand
Try '{$util_name} --help' for more information.
rm-error-invalid-interactive-argument = Invalid argument to interactive ({$arg})
rm-error-cannot-remove-no-such-file = cannot remove {$file}: No such file or directory
rm-error-cannot-remove-permission-denied = cannot remove {$file}: Permission denied
rm-error-cannot-remove-is-directory = cannot remove {$file}: Is a directory
-1
View File
@@ -32,7 +32,6 @@ rm-help-verbose = expliquer ce qui est fait
# Messages d'erreur
rm-error-missing-operand = opérande manquant
Essayez '{$util_name} --help' pour plus d'informations.
rm-error-invalid-interactive-argument = Argument invalide pour interactive ({$arg})
rm-error-cannot-remove-no-such-file = impossible de supprimer {$file} : Aucun fichier ou répertoire de ce type
rm-error-cannot-remove-permission-denied = impossible de supprimer {$file} : Permission refusée
rm-error-cannot-remove-is-directory = impossible de supprimer {$file} : C'est un répertoire
+15 -10
View File
@@ -29,8 +29,6 @@ use uucore::{format_usage, os_str_as_bytes, prompt_yes, show_error};
enum RmError {
#[error("{}", translate!("rm-error-missing-operand", "util_name" => uucore::execution_phrase()))]
MissingOperand,
#[error("{}", translate!("rm-error-invalid-interactive-argument", "arg" => _0.clone()))]
InvalidInteractiveArgument(String),
#[error("{}", translate!("rm-error-cannot-remove-no-such-file", "file" => _0.quote()))]
CannotRemoveNoSuchFile(String),
#[error("{}", translate!("rm-error-cannot-remove-permission-denied", "file" => _0.quote()))]
@@ -61,6 +59,20 @@ pub enum InteractiveMode {
PromptProtected,
}
// We implement `From` instead of `TryFrom` because clap guarantees that we only receive valid values.
//
// The `PromptProtected` variant is not supposed to be created from a string.
impl From<&str> for InteractiveMode {
fn from(s: &str) -> Self {
match s {
"never" => Self::Never,
"once" => Self::Once,
"always" => Self::Always,
_ => unreachable!("should be prevented by clap"),
}
}
}
/// Options for the `rm` command
///
/// All options are public so that the options can be programmatically
@@ -167,14 +179,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else if matches.get_flag(OPT_PROMPT_ONCE) {
InteractiveMode::Once
} else if matches.contains_id(OPT_INTERACTIVE) {
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
"never" => InteractiveMode::Never,
"once" => InteractiveMode::Once,
"always" => InteractiveMode::Always,
val => {
return Err(RmError::InvalidInteractiveArgument(val.to_string()).into());
}
}
InteractiveMode::from(matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str())
} else {
InteractiveMode::PromptProtected
}