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 be 0
30/// Arg is the thing that is a number or is in command_nicknames (case insensitive),
31/// command_nicknames is a array of alternative names for commands
32/// all_is_255 controlls wheater the arg "all"(case insensitive) should output 255
33/// # Examples
34///
35/// ```
36/// asserteq!(0,command_ifyer::what_to_execute(0,"fOO",&["Foo","Bar"],true).unwrap);
37/// asserteq!(1,command_ifyer::what_to_execute(0,"1",&["hi","HeLlo"],false).unwrap);
38/// asserteq!(255,command_ifyer::what_to_execute(0, "all", &[hi,HeLlo]),true.unwrap);
39/// ```
40/// ```
41/// let args: Vec<String> = std::env::args().collect();
42/// let mut thing_to_execute = 255;
43/// if args.len() > 1 {
44///     thing_to_execute = command_ifyer::what_to_execute(0, &args[1], &["Foo", "Bar"], true).unwrap();
45/// }
46/// if thing_to_execute == 0 || thing_to_execute == 255 {
47///    println("foo");
48/// }
49/// if thing_to_execute == 255 || thing_to_execute == 1 {
50///     println!("bar");
51/// }
52/// ```
53pub fn what_to_execute(version :u128, arg: &String, command_nicknames: &[&str], all_is_255:bool) -> Option<usize> {
54    if version != 0{
55        panic!("917421232 version not supported.");
56    }
57    for i in 0..command_nicknames.len() {
58        if arg.to_lowercase() == command_nicknames[i].to_lowercase() {
59            return Some(i);
60        } 
61    }
62    if arg.to_lowercase() == "all".to_lowercase() && all_is_255 {
63        return Some(255)
64    }
65//    let parsed_arg = arg.trim().parse();
66    if arg.trim().parse::<usize>().is_ok() {
67        let more_parsed_arg:usize = arg.trim().parse().expect("129394860");
68        return Some(more_parsed_arg);
69    }
70    None
71}