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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
use std::env;
use crate::{config::Cfg, list, modification, update, setup, download, error::{MLError, ErrorType, MLE}};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Input {
pub command: Cmd,
pub subcommand: Option<Subcmd>,
pub args: Option<Vec<String>>,
pub direct_download: bool,
pub all_lists: bool,
pub delete_old: bool,
pub clean: bool,
}
impl Input {
fn from(string: &str) -> MLE<Self> {
let mut split: Vec<&str> = string.split(' ').collect();
let mut direct_download = false;
let mut all_lists = false;
let mut delete_old = false;
let mut clean = false;
for (i, input) in split.clone().into_iter().enumerate() {
if input.starts_with("--") {
match input {
"--direct-download" => direct_download = true,
"--all-lists" => all_lists = true,
"--delete-old" => delete_old = true,
"--clean" => clean = true,
_ => continue,
}
split.remove(i);
}
}
let command = Cmd::from(split.remove(0))?;
let subcommand = match split.is_empty() {
false => Some(Subcmd::from(split.remove(0))?),
true => None
};
let args = match split.is_empty() {
true => None,
false => {
let mut strsplit: Vec<String> = Vec::new();
for s in split {
strsplit.push(String::from(s))
}
Some(strsplit)
}
};
Ok(Self { command, subcommand, args, direct_download, all_lists, delete_old, clean })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cmd {
Mod,
List,
Update,
Download,
Setup
}
impl Cmd {
fn from(string: &str) -> MLE<Self> {
let cmd = match string {
"mod" => Self::Mod,
"list" => Self::List,
"update" => Self::Update,
"download" => Self::Download,
"setup" => Self::Setup,
_ => return Err(MLError::new(ErrorType::ArgumentError, "Unknown command"))
};
Ok(cmd)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Subcmd {
Add,
Remove,
Change
}
impl Subcmd {
fn from(string: &str) -> MLE<Self> {
let cmd = match string {
"add" => Self::Add,
"remove" => Self::Remove,
"change" => Self::Change,
_ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND"))
};
Ok(cmd)
}
}
pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
let mut args: Vec<String> = env::args().collect();
args.reverse();
args.pop();
args.reverse();
let input = Input::from(&args.join(" "))?;
match input.command {
Cmd::Mod => {
modification(config, input).await
},
Cmd::List => {
list(config, input)
},
Cmd::Update => {
update(config, input).await
},
Cmd::Setup => {
setup(config).await
},
Cmd::Download => {
download(config, input).await
}
}
}
#[test]
fn input_from() {
let string = "list add test 1.19.2 fabric";
let input = Input{ command: Cmd::List, subcommand: Some(Subcmd::Add), args: Some(vec![String::from("test"), String::from("1.19.2"), String::from("fabric")]), direct_download: false, all_lists: false, clean: false, delete_old: false };
assert_eq!(Input::from(string).unwrap(), input);
}
|