From 194aa89d632fa87da9db57770a00671db245ee44 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Thu, 31 Jul 2025 09:49:27 +0200 Subject: [PATCH] rm: implement From for InteractiveMode --- src/uu/rm/locales/en-US.ftl | 1 - src/uu/rm/locales/fr-FR.ftl | 1 - src/uu/rm/src/rm.rs | 25 +++++++++++++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/uu/rm/locales/en-US.ftl b/src/uu/rm/locales/en-US.ftl index 5a1231806..de12feae4 100644 --- a/src/uu/rm/locales/en-US.ftl +++ b/src/uu/rm/locales/en-US.ftl @@ -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 diff --git a/src/uu/rm/locales/fr-FR.ftl b/src/uu/rm/locales/fr-FR.ftl index 80842e8bc..2bff4a66f 100644 --- a/src/uu/rm/locales/fr-FR.ftl +++ b/src/uu/rm/locales/fr-FR.ftl @@ -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 diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index 5cda4b03d..f7971aa80 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -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::(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::(OPT_INTERACTIVE).unwrap().as_str()) } else { InteractiveMode::PromptProtected }