Skip to main content

what_to_execute

Function what_to_execute 

Source
pub fn what_to_execute(
    version: u128,
    arg: &str,
    command_nicknames: &[&str],
    all_is_255: bool,
) -> Option<usize>
Expand description

what_to_execute decides what to execute
version must == 0 or == 1.
When it is executed it returns a number that is determined by comparing arg to command_nicknames (capital insensitive) and if arg is in command_nicknames it returns Some(i) where i is the index of arg in command_nicknames.
Else if all_is_255 && arg == "all" than Some(255) is returned
Else if arg is a decimal integer it returns arg as a integer
Else it returns None if version == 0 or Some(245) if version == 1
I recommend you read the code.
version being equal to 1 is deprecated (see #1).
I reccommend setting constants for each of the command nicknames to make the code more readable

ยงExamples

use command_ifyer::what_to_execute;
assert_eq!(Some(0),what_to_execute(0,&("fOO".to_string()),&["Foo","Bar"],true));
assert_eq!(Some(1),what_to_execute(0,"1",&["hi","HeLlo"],false));
assert_eq!(Some(255),what_to_execute(0, &("all".to_string()), &["hi","HeLlo"], true));
assert_eq!(Some(2),what_to_execute(0, &("all".to_string()), &["hi","HeLlo","All"], true));
assert_eq!(None,what_to_execute(0,"When is the Testificate Man full lenght movie going to come out?",&["Hi","HeLlo"],false));
const FOO: Option<usize> = Some(0);
const BAR: Option<usize> = Some(1);
const ALL: Option<usize> = Some(255);
let args: Vec<String> = std::env::args().collect();
let mut thing_to_execute = Some(255);
if args.len() > 1 {
    thing_to_execute = command_ifyer::what_to_execute(0, &args[1], &["Foo", "Bar"], true);
}
if thing_to_execute == FOO || thing_to_execute == ALL {
   println!("foo");
}
if thing_to_execute == BAR || thing_to_execute == ALL {
    println!("bar");
}