1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
use std::{io::{Error, ErrorKind}, env};
use crate::{config::Cfg, list, modification, update, setup, download};
#[derive(Debug, PartialEq, Eq)]
pub struct Input {
pub command: String,
pub args: Option<Vec<String>>,
}
impl Input {
pub fn from(string: String) -> Result<Self, Box<dyn std::error::Error>> {
let mut split: Vec<&str> = string.split(' ').collect();
let command: String;
let mut args: Option<Vec<String>> = None;
if split[0].is_empty() { split.remove(0); };
match split.len() {
0 => { Err(Box::new(Error::new(ErrorKind::InvalidInput, "NO_ARGS"))) }
1 => Ok( Input { command: split[0].to_string(), args }),
2.. => {
command = split[0].to_string();
split.remove(0);
let mut str_args: Vec<String> = vec![];
for e in split {
str_args.push(e.to_string());
}
args = Some(str_args);
Ok(Input { command, args })
},
_ => { panic!("This should never happen") }
}
}
}
pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
let mut args: Vec<String> = env::args().collect();
dbg!(&args);
args.reverse();
args.pop();
args.reverse();
dbg!(&args);
let input = Input::from(args.join(" "))?;
match input.command.as_str() {
"mod" => {
modification(config, input.args).await
},
"list" => {
list(config, input.args)
},
"update" => {
update(config).await
},
"setup" => {
setup(config).await
},
"download" => {
download(config).await
},
_ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_COMMAND"))),
}
}
#[test]
fn input_from() {
let string = String::from("list add test 1.19.2 fabric");
let input = Input { command: String::from("list"), args: Some(vec![String::from("add"), String::from("test"), String::from("1.19.2"), String::from("fabric")]) };
assert_eq!(Input::from(string).unwrap(), input);
}
|