diff options
Diffstat (limited to 'src/input.rs')
-rw-r--r-- | src/input.rs | 344 |
1 files changed, 0 insertions, 344 deletions
diff --git a/src/input.rs b/src/input.rs deleted file mode 100644 index 6c62ab7..0000000 --- a/src/input.rs +++ /dev/null | |||
@@ -1,344 +0,0 @@ | |||
1 | use crate::{error::{MLE, MLError, ErrorType}, Modloader, config::Cfg, db::lists_get, get_current_list, List, modrinth::{get_minecraft_version, MCVersionType}, IDSelector}; | ||
2 | |||
3 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
4 | pub struct Input { | ||
5 | pub command: Option<Cmd>, | ||
6 | pub mod_options: Option<ModOptions>, | ||
7 | pub mod_id: Option<IDSelector>, | ||
8 | pub set_version: bool, | ||
9 | pub all_lists: bool, | ||
10 | pub clean: bool, | ||
11 | pub direct_download: bool, | ||
12 | pub delete_old: bool, | ||
13 | pub list: Option<List>, | ||
14 | pub list_options: Option<ListOptions>, | ||
15 | pub list_id: Option<String>, | ||
16 | pub list_mcversion: Option<String>, | ||
17 | pub modloader: Option<Modloader>, | ||
18 | pub directory: Option<String>, | ||
19 | pub io_options: Option<IoOptions>, | ||
20 | pub file: Option<String>, | ||
21 | } | ||
22 | |||
23 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
24 | pub enum Cmd { | ||
25 | Mod, | ||
26 | List, | ||
27 | Update, | ||
28 | Download, | ||
29 | Io, | ||
30 | Version, | ||
31 | Setup, | ||
32 | } | ||
33 | |||
34 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
35 | pub enum ModOptions { | ||
36 | Add, | ||
37 | Remove | ||
38 | } | ||
39 | |||
40 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
41 | pub enum ListOptions { | ||
42 | Add, | ||
43 | Remove, | ||
44 | Change, | ||
45 | Version, | ||
46 | } | ||
47 | |||
48 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
49 | pub enum IoOptions { | ||
50 | Export, | ||
51 | Import | ||
52 | } | ||
53 | |||
54 | impl Input { | ||
55 | fn from(config: Cfg, input: Vec<String>) -> MLE<Self> { | ||
56 | let input_string = input.join(" "); | ||
57 | let mut args: Vec<&str> = input_string.split(" -").collect(); | ||
58 | args[0] = args[0].split_at(1).1; | ||
59 | |||
60 | let mut command: Option<Cmd> = None; | ||
61 | |||
62 | let mut mod_options: Option<ModOptions> = None; | ||
63 | let mut mod_id: Option<IDSelector> = None; | ||
64 | let mut set_version = false; | ||
65 | let mut all_lists = false; | ||
66 | let mut clean = false; | ||
67 | let mut direct_download = true; | ||
68 | let mut delete_old = false; | ||
69 | let mut list: Option<List> = None; | ||
70 | let mut list_options: Option<ListOptions> = None; | ||
71 | let mut list_id: Option<String> = None; | ||
72 | let mut list_mcversion: Option<String> = None; | ||
73 | let mut modloader: Option<Modloader> = None; | ||
74 | let mut directory: Option<String> = None; | ||
75 | let mut io_options: Option<IoOptions> = None; | ||
76 | let mut file: Option<String> = None; | ||
77 | |||
78 | for arg in args { | ||
79 | let arg_split: Vec<&str> = arg.trim().split(' ').collect(); | ||
80 | match arg_split[0] { | ||
81 | "v" | "version" => { | ||
82 | command = Some(Cmd::Version); | ||
83 | }, | ||
84 | "d" | "download" => { | ||
85 | command = Some(Cmd::Download); | ||
86 | }, | ||
87 | "u" | "update" => { | ||
88 | command = Some(Cmd::Update); | ||
89 | }, | ||
90 | "ma" => { | ||
91 | command = Some(Cmd::Mod); | ||
92 | mod_options = Some(ModOptions::Add); | ||
93 | if arg_split.len() == 2 { | ||
94 | mod_id = Some(IDSelector::ModificationID(String::from(arg_split[1]))); | ||
95 | } else { | ||
96 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a list mod slug or id")); | ||
97 | } | ||
98 | }, | ||
99 | //TODO impl this | ||
100 | "mv" => { | ||
101 | command = Some(Cmd::Mod); | ||
102 | mod_options = Some(ModOptions::Add); | ||
103 | if arg_split.len() == 2 { | ||
104 | mod_id = Some(IDSelector::VersionID(String::from(arg_split[1]))); | ||
105 | } else { | ||
106 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a version id")); | ||
107 | }; | ||
108 | }, | ||
109 | "mr" => { | ||
110 | command = Some(Cmd::Mod); | ||
111 | mod_options = Some(ModOptions::Remove); | ||
112 | if arg_split.len() == 2 { | ||
113 | mod_id = Some(IDSelector::ModificationID(String::from(arg_split[1]))); | ||
114 | } else { | ||
115 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a mod id")); | ||
116 | }; | ||
117 | }, | ||
118 | "set_version" => { | ||
119 | set_version = true; | ||
120 | }, | ||
121 | "all_lists" => { | ||
122 | all_lists = true; | ||
123 | }, | ||
124 | "clean" => { | ||
125 | clean = true; | ||
126 | }, | ||
127 | "no_download" => { | ||
128 | direct_download = false; | ||
129 | }, | ||
130 | "delete_old" => { | ||
131 | delete_old = true; | ||
132 | }, | ||
133 | "l" => { | ||
134 | if arg_split.len() == 2 { | ||
135 | list = Some(lists_get(config.clone(), String::from(arg_split[1]))?); | ||
136 | } else { | ||
137 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a list via it's id")); | ||
138 | } | ||
139 | } | ||
140 | "la" => { | ||
141 | command = Some(Cmd::List); | ||
142 | list_options = Some(ListOptions::Add); | ||
143 | if arg_split.len() == 2 { | ||
144 | list_id = Some(String::from(arg_split[1])); | ||
145 | } else { | ||
146 | return Err(MLError::new(ErrorType::ArgumentError, "Please give the new list an id")); | ||
147 | } | ||
148 | }, | ||
149 | "lr" => { | ||
150 | command = Some(Cmd::List); | ||
151 | list_options = Some(ListOptions::Remove); | ||
152 | }, | ||
153 | "lc" => { | ||
154 | command = Some(Cmd::List); | ||
155 | list_options = Some(ListOptions::Change); | ||
156 | }, | ||
157 | "lv" => { | ||
158 | command = Some(Cmd::List); | ||
159 | list_options = Some(ListOptions::Version); | ||
160 | if arg_split.len() == 2 { | ||
161 | list_mcversion = Some(String::from(arg_split[1])); | ||
162 | } else { | ||
163 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a minecraft version")); | ||
164 | } | ||
165 | }, | ||
166 | "mcv" => { | ||
167 | if arg_split.len() == 2 { | ||
168 | list_mcversion = Some(String::from(arg_split[1])); | ||
169 | } else { | ||
170 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a minecraft version")); | ||
171 | } | ||
172 | }, | ||
173 | "ml" => { | ||
174 | if arg_split.len() == 2 { | ||
175 | modloader = Some(Modloader::from(arg_split[1])?); | ||
176 | } else { | ||
177 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a modloader")); | ||
178 | } | ||
179 | }, | ||
180 | "dir" => { | ||
181 | if arg_split.len() == 2 { | ||
182 | directory = Some(String::from(arg_split[1])); | ||
183 | } else { | ||
184 | return Err(MLError::new(ErrorType::ArgumentError, "Please specify a directory")); | ||
185 | } | ||
186 | }, | ||
187 | "export" => { | ||
188 | command = Some(Cmd::Io); | ||
189 | io_options = Some(IoOptions::Export); | ||
190 | }, | ||
191 | "import" => { | ||
192 | command = Some(Cmd::Io); | ||
193 | io_options = Some(IoOptions::Import); | ||
194 | }, | ||
195 | "f" => { | ||
196 | file = Some(String::from(arg_split[1])); | ||
197 | }, | ||
198 | "setup" => { | ||
199 | command = Some(Cmd::Setup); | ||
200 | } | ||
201 | _ => return Err(MLError::new(ErrorType::ArgumentError, format!("Unknown Argument ({})", arg_split[0]).as_str())), | ||
202 | } | ||
203 | } | ||
204 | |||
205 | Ok(Self { | ||
206 | command, | ||
207 | mod_options, | ||
208 | mod_id, | ||
209 | set_version, | ||
210 | all_lists, | ||
211 | clean, | ||
212 | direct_download, | ||
213 | delete_old, | ||
214 | list, | ||
215 | list_options, | ||
216 | list_id, | ||
217 | list_mcversion, | ||
218 | modloader, | ||
219 | directory, | ||
220 | io_options, | ||
221 | file | ||
222 | }) | ||
223 | } | ||
224 | } | ||
225 | |||
226 | pub async fn get_input(config: Cfg, args: Vec<String>) -> MLE<Input> { | ||
227 | let input = Input::from(config.clone(), args)?; | ||
228 | |||
229 | if input.command.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "No command specified")); }; | ||
230 | |||
231 | match input.clone().command.unwrap() { | ||
232 | Cmd::Mod => check_mod(input, config), | ||
233 | Cmd::List => check_list(input, config).await, | ||
234 | _ => Ok(input), | ||
235 | } | ||
236 | } | ||
237 | |||
238 | fn check_mod(mut input: Input, config: Cfg) -> MLE<Input> { | ||
239 | if input.mod_options.is_none() { | ||
240 | return Err(MLError::new(ErrorType::ArgumentError, "No mod option")); | ||
241 | }; | ||
242 | match input.clone().mod_options.unwrap() { | ||
243 | ModOptions::Add => { | ||
244 | if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "No mod id/slug or version id")); }; | ||
245 | if input.list_id.is_none() { input.list = Some(get_current_list(config)?); }; | ||
246 | Ok(input) | ||
247 | }, | ||
248 | ModOptions::Remove => { | ||
249 | if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID")); }; | ||
250 | if input.list_id.is_none() { input.list = Some(get_current_list(config)?); }; | ||
251 | Ok(input) | ||
252 | }, | ||
253 | } | ||
254 | } | ||
255 | |||
256 | async fn check_list(mut input: Input, config: Cfg) -> MLE<Input> { | ||
257 | if input.list_options.is_none() { | ||
258 | return Err(MLError::new(ErrorType::ArgumentError, "NO_LIST_ARGUMENT")); | ||
259 | }; | ||
260 | match input.clone().list_options.unwrap() { | ||
261 | ListOptions::Add => { | ||
262 | if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "no list id specified")); }; | ||
263 | if input.list_mcversion.is_none() { | ||
264 | println!("No Minecraft Version specified, defaulting to latest release"); | ||
265 | input.list_mcversion = Some(get_minecraft_version(&config.apis.modrinth, MCVersionType::Release).await); | ||
266 | }; | ||
267 | if input.directory.is_none() { | ||
268 | let id = input.clone().list_id.unwrap(); | ||
269 | println!("No download directory specified, defaulting to ./downloads/{}", id); | ||
270 | input.directory = Some(format!("./downloads/{}", id)) | ||
271 | }; | ||
272 | if input.modloader.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "no modloader specified")); }; | ||
273 | Ok(input) | ||
274 | }, | ||
275 | ListOptions::Remove => { | ||
276 | if input.list.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "NO_LIST_SPECIFIED")); }; | ||
277 | Ok(input) | ||
278 | }, | ||
279 | ListOptions::Change => { | ||
280 | if input.list.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "NO_LIST_SPECIFIED")); }; | ||
281 | Ok(input) | ||
282 | }, | ||
283 | ListOptions::Version => { | ||
284 | if input.list.is_none() { | ||
285 | println!("No list specified, using default"); | ||
286 | input.list = Some(get_current_list(config)?); | ||
287 | }; | ||
288 | Ok(input) | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | |||
293 | #[test] | ||
294 | fn input_from() { | ||
295 | let config = Cfg::init("modlist.toml").unwrap(); | ||
296 | assert_eq!( | ||
297 | Input::from(config, vec![String::from("-la test -lv 1.19.3")]).unwrap(), | ||
298 | Input { | ||
299 | command: Some(Cmd::List), | ||
300 | mod_options: None, | ||
301 | mod_id: None, | ||
302 | set_version: false, | ||
303 | all_lists: false, | ||
304 | clean: false, | ||
305 | direct_download: false, | ||
306 | delete_old: false, | ||
307 | list: None, | ||
308 | list_options: Some(ListOptions::Add), | ||
309 | list_id: Some(String::from("test")), | ||
310 | list_mcversion: Some(String::from("1.19.3")), | ||
311 | modloader: None, | ||
312 | directory: None, | ||
313 | io_options: None, | ||
314 | file: None, | ||
315 | } | ||
316 | ); | ||
317 | |||
318 | } | ||
319 | |||
320 | #[tokio::test] | ||
321 | async fn get_input_test() { | ||
322 | let config = Cfg::init("modlist.toml").unwrap(); | ||
323 | assert_eq!( | ||
324 | get_input(config.clone(), vec![String::from("-ma test")]).await.unwrap(), | ||
325 | Input { | ||
326 | command: Some(Cmd::Mod), | ||
327 | mod_options: Some(ModOptions::Add), | ||
328 | mod_id: Some(IDSelector::ModificationID(String::from("test"))), | ||
329 | set_version: false, | ||
330 | all_lists: false, | ||
331 | clean: false, | ||
332 | direct_download: false, | ||
333 | delete_old: false, | ||
334 | list: Some(lists_get(config.clone(), String::from("one")).unwrap()), | ||
335 | list_options: None, | ||
336 | list_id: None, | ||
337 | list_mcversion: None, | ||
338 | modloader: None, | ||
339 | directory: None, | ||
340 | io_options: None, | ||
341 | file: None | ||
342 | } | ||
343 | ) | ||
344 | } | ||