command_ifyer/lib.rs
1// Lints are configured in Cargo.toml, to maintain lower MSRV.
2/*
3
4Copyright 2025,2026 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 */
18/*
19
20Copyright (c) 2025,2026 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣
21
22Permission 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:
23
24The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
25
26THE 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.
27*/
28
29// 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣 may use this under the CC0 license
30
31/// `what_to_execute` decides what to execute <br/>
32/// `version` must == `0` or == `1`.<br/>
33/// 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`.<br/>
34/// Else if `all_is_255` && `arg` == `"all"` than `Some(255)` is returned<br/>
35/// Else if `arg` is a decimal integer it returns `arg` as a integer<br/>
36/// Else it returns `None` if `version` == 0 or `Some(245)` if `version` == 1<br/>
37/// I recommend you read [the code](https://codeberg.org/Velocifyer/command-ifyer/src/branch/main/src/lib.rs).<br/>
38/// `version` being equal to 1 is deprecated (see [#1](https://codeberg.org/Velocifyer/command-ifyer/issues/1)).<br/>
39/// I reccommend setting constants for each of the command nicknames to make the code more readable
40/// # Examples
41///
42/// ```
43/// use command_ifyer::what_to_execute;
44/// assert_eq!(Some(0),what_to_execute(0,&("fOO".to_string()),&["Foo","Bar"],true));
45/// assert_eq!(Some(1),what_to_execute(0,"1",&["hi","HeLlo"],false));
46/// assert_eq!(Some(255),what_to_execute(0, &("all".to_string()), &["hi","HeLlo"], true));
47/// assert_eq!(Some(2),what_to_execute(0, &("all".to_string()), &["hi","HeLlo","All"], true));
48/// assert_eq!(None,what_to_execute(0,"When is the Testificate Man full lenght movie going to come out?",&["Hi","HeLlo"],false));
49/// ```
50/// ```
51/// const FOO: Option<usize> = Some(0);
52/// const BAR: Option<usize> = Some(1);
53/// const ALL: Option<usize> = Some(255);
54/// let args: Vec<String> = std::env::args().collect();
55/// let mut thing_to_execute = Some(255);
56/// if args.len() > 1 {
57/// thing_to_execute = command_ifyer::what_to_execute(0, &args[1], &["Foo", "Bar"], true);
58/// }
59/// if thing_to_execute == FOO || thing_to_execute == ALL {
60/// println!("foo");
61/// }
62/// if thing_to_execute == BAR || thing_to_execute == ALL {
63/// println!("bar");
64/// }
65/// ```
66pub fn what_to_execute(version: u128, arg: &str, command_nicknames: &[&str], all_is_255: bool,) -> Option<usize> {
67 let arg = arg.to_lowercase(); //make everythitg lowercase to make comparisons easier
68 assert!(version == 0 || version == 1, "917421232 This is not supported");
69 // handle arg just being the command name
70 for i in 0..command_nicknames.len() {
71 if arg == command_nicknames[i].to_lowercase() {
72 return Some(i);
73 }
74 }
75
76 // Handle arg being the index for the command name
77 if arg.trim().parse::<usize>().is_ok() {
78 let more_parsed_arg: usize = arg.trim().parse().expect("129394860");
79 return Some(more_parsed_arg);
80 }
81
82 if all_is_255 {
83 if arg == "all" {
84 return ALL;
85 }
86 }
87
88 // This is deprecated and will be removed in the next breaking version. (see [#1](https://codeberg.org/Velocifyer/command-ifyer/issues/1))
89 if version == 1 {
90 return Some(254)
91 }
92 return None;
93}
94
95/// A constant that will always be equal to 255, for better readability of the output of `what_to_execute` when `all_is_255` is enabled.
96pub const ALL: Option<usize> = Some(255);
97
98#[cfg(test)]
99mod tests {
100 use crate::what_to_execute;
101
102 #[test]
103 fn the_right_stuff_is_executed() {
104 assert_eq!(Some(0),what_to_execute(0,&("fOO".to_string()),&["Foo","Bar"],true));
105 assert_eq!(Some(1),what_to_execute(0,"1",&["hi","HeLlo"],false));
106 assert_eq!(Some(255),what_to_execute(0, &("all".to_string()), &["hi","HeLlo"], true));
107 assert_eq!(Some(2),what_to_execute(0, &("all".to_string()), &["hi","HeLlo","All"], true));
108 assert_eq!(None,what_to_execute(0,"When is the Testificate Man full lenght movie going to come out?",&["Hi","HeLlo"],false));
109 }
110}