command_ifyer/
lib.rs

1/*
2
3Copyright 2025 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9       http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16 */
17/*
18
19Copyright (c) 2025 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣
20
21Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the " Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
22
23The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26*/
27
28/// Decides what to execute
29/// Version must == 0 or == 1
30/// Behaviour for version not being == 0 or == 1 is undefined
31/// 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 == the index that it is at
32/// Else if all_is_255 && arg == "all" than Some(255) is returned
33/// Else if arg is a decimal integer it returns arg as a integer
34/// Else it returns None if version == 0 or Some(245) if version == 1
35/// # Examples
36///
37/// ```
38/// asserteq!(Some(0),command_ifyer::what_to_execute(0,"fOO",&["Foo","Bar"],true));
39/// asserteq!(Some(1),command_ifyer::what_to_execute(0,"1",&["hi","HeLlo"],false));
40/// asserteq!(Some(255),command_ifyer::what_to_execute(0, "all", &["hi","HeLlo"]),true);
41/// asserteq!(Some(3),command_ifyer::what_to_execute(0, "all", &["hi","HeLlo","All"], true))
42/// asserteq!(None,command_ifyer::what_to_execute(0,"When is the Testificate Man full lenght movie going to come out?",&["Hi","HeLlo"],false))
43/// ```
44/// ```
45/// let args: Vec<String> = std::env::args().collect();
46/// let mut thing_to_execute = Some(255);
47/// if args.len() > 1 {
48///     thing_to_execute = command_ifyer::what_to_execute(0, &args[1], &["Foo", "Bar"], true);
49/// }
50/// if thing_to_execute == Some(0)|| thing_to_execute == Some(255) {
51///    println("foo");
52/// }
53/// if thing_to_execute == Some(255) || thing_to_execute == Some(1) {
54///     println!("bar");
55/// }
56/// ```
57pub fn what_to_execute(
58    version: u128,
59    arg: &String,
60    command_nicknames: &[&str],
61    all_is_255: bool,
62) -> Option<usize> {
63    if version != 0 {
64        panic!("917421232 version not supported.");
65    }
66    for i in 0..command_nicknames.len() {
67        if arg.to_lowercase() == command_nicknames[i].to_lowercase() {
68            return Some(i);
69        }
70    }
71    if arg.to_lowercase() == "all".to_lowercase() && all_is_255 {
72        return Some(255);
73    }
74    //    let parsed_arg = arg.trim().parse();
75    if arg.trim().parse::<usize>().is_ok() {
76        let more_parsed_arg: usize = arg.trim().parse().expect("129394860");
77        return Some(more_parsed_arg);
78    }
79    None
80}