Skip to content

Commit 194aa89

Browse files
committed
rm: implement From for InteractiveMode
1 parent f332443 commit 194aa89

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/uu/rm/locales/en-US.ftl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ rm-help-verbose = explain what is being done
3232
# Error messages
3333
rm-error-missing-operand = missing operand
3434
Try '{$util_name} --help' for more information.
35-
rm-error-invalid-interactive-argument = Invalid argument to interactive ({$arg})
3635
rm-error-cannot-remove-no-such-file = cannot remove {$file}: No such file or directory
3736
rm-error-cannot-remove-permission-denied = cannot remove {$file}: Permission denied
3837
rm-error-cannot-remove-is-directory = cannot remove {$file}: Is a directory

src/uu/rm/locales/fr-FR.ftl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ rm-help-verbose = expliquer ce qui est fait
3232
# Messages d'erreur
3333
rm-error-missing-operand = opérande manquant
3434
Essayez '{$util_name} --help' pour plus d'informations.
35-
rm-error-invalid-interactive-argument = Argument invalide pour interactive ({$arg})
3635
rm-error-cannot-remove-no-such-file = impossible de supprimer {$file} : Aucun fichier ou répertoire de ce type
3736
rm-error-cannot-remove-permission-denied = impossible de supprimer {$file} : Permission refusée
3837
rm-error-cannot-remove-is-directory = impossible de supprimer {$file} : C'est un répertoire

src/uu/rm/src/rm.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ use uucore::{format_usage, os_str_as_bytes, prompt_yes, show_error};
2929
enum RmError {
3030
#[error("{}", translate!("rm-error-missing-operand", "util_name" => uucore::execution_phrase()))]
3131
MissingOperand,
32-
#[error("{}", translate!("rm-error-invalid-interactive-argument", "arg" => _0.clone()))]
33-
InvalidInteractiveArgument(String),
3432
#[error("{}", translate!("rm-error-cannot-remove-no-such-file", "file" => _0.quote()))]
3533
CannotRemoveNoSuchFile(String),
3634
#[error("{}", translate!("rm-error-cannot-remove-permission-denied", "file" => _0.quote()))]
@@ -61,6 +59,20 @@ pub enum InteractiveMode {
6159
PromptProtected,
6260
}
6361

62+
// We implement `From` instead of `TryFrom` because clap guarantees that we only receive valid values.
63+
//
64+
// The `PromptProtected` variant is not supposed to be created from a string.
65+
impl From<&str> for InteractiveMode {
66+
fn from(s: &str) -> Self {
67+
match s {
68+
"never" => Self::Never,
69+
"once" => Self::Once,
70+
"always" => Self::Always,
71+
_ => unreachable!("should be prevented by clap"),
72+
}
73+
}
74+
}
75+
6476
/// Options for the `rm` command
6577
///
6678
/// All options are public so that the options can be programmatically
@@ -167,14 +179,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
167179
} else if matches.get_flag(OPT_PROMPT_ONCE) {
168180
InteractiveMode::Once
169181
} else if matches.contains_id(OPT_INTERACTIVE) {
170-
match matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str() {
171-
"never" => InteractiveMode::Never,
172-
"once" => InteractiveMode::Once,
173-
"always" => InteractiveMode::Always,
174-
val => {
175-
return Err(RmError::InvalidInteractiveArgument(val.to_string()).into());
176-
}
177-
}
182+
InteractiveMode::from(matches.get_one::<String>(OPT_INTERACTIVE).unwrap().as_str())
178183
} else {
179184
InteractiveMode::PromptProtected
180185
}

0 commit comments

Comments
 (0)