@@ -29,8 +29,6 @@ use uucore::{format_usage, os_str_as_bytes, prompt_yes, show_error};
2929enum 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