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
|
use std::io::{stdin, Error, ErrorKind};
use crate::{config::Cfg, list, modification, update};
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 user_input = String::new();
stdin()
.read_line(&mut user_input)
.expect("ERROR");
let input = Input::from(user_input.trim().to_string())?;
match input.command.as_str() {
"mod" => {
modification(config, input.args).await
},
"list" => {
list(config, input.args)
},
"update" => {
update(config).await
},
_ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_COMMAND"))),
}
}
|