summaryrefslogtreecommitdiff
path: root/src/input.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-17 21:20:09 +0100
committerfxqnlr <[email protected]>2022-11-17 21:20:09 +0100
commitfdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113 (patch)
treeec7c7c80434b339f9442882f1e2dce6f60cc9edd /src/input.rs
parent5145dd23f1777180d8003e76f59af57643796516 (diff)
downloadmodlist-fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113.tar
modlist-fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113.tar.gz
modlist-fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113.zip
added --clean for update && list downloadfolder
Diffstat (limited to 'src/input.rs')
-rw-r--r--src/input.rs140
1 files changed, 103 insertions, 37 deletions
diff --git a/src/input.rs b/src/input.rs
index c7e82d9..109fa0c 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -1,69 +1,135 @@
1use std::{io::{Error, ErrorKind}, env}; 1use std::env;
2use crate::{config::Cfg, list, modification, update, setup, download}; 2use crate::{config::Cfg, list, modification, update, setup, download, error::{MLError, ErrorType, MLE}};
3 3
4#[derive(Debug, PartialEq, Eq)] 4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Input { 5pub struct Input {
6 pub command: String, 6 pub command: Cmd,
7 pub subcommand: Option<Subcmd>,
7 pub args: Option<Vec<String>>, 8 pub args: Option<Vec<String>>,
9 pub direct_download: bool,
10 pub force_download: bool,
11 pub all_lists: bool,
12 pub delete_old: bool,
13 pub clean: bool,
8} 14}
9 15
10impl Input { 16impl Input {
11 pub fn from(string: String) -> Result<Self, Box<dyn std::error::Error>> { 17 pub fn from(string: &str) -> MLE<Self> {
12 let mut split: Vec<&str> = string.split(' ').collect(); 18 let mut split: Vec<&str> = string.split(' ').collect();
13 let command: String; 19
14 let mut args: Option<Vec<String>> = None; 20 let mut direct_download = false;
15 if split[0].is_empty() { split.remove(0); }; 21 let mut force_download = false;
16 match split.len() { 22 let mut all_lists = false;
17 0 => { Err(Box::new(Error::new(ErrorKind::InvalidInput, "NO_ARGS"))) } 23 let mut delete_old = false;
18 1 => Ok( Input { command: split[0].to_string(), args }), 24 let mut clean = false;
19 2.. => { 25
20 command = split[0].to_string(); 26 for (i, input) in split.clone().into_iter().enumerate() {
21 split.remove(0); 27 if input.starts_with("--") {
22 let mut str_args: Vec<String> = vec![]; 28 match input {
23 for e in split { 29 "--direct-download" => direct_download = true,
24 str_args.push(e.to_string()); 30 "--force-download" => force_download = true,
31 "--all_lists" => all_lists = true,
32 "--delete_old" => delete_old = true,
33 "--clean" => clean = true,
34 _ => continue,
25 } 35 }
26 args = Some(str_args); 36 split.remove(i);
27 Ok(Input { command, args }) 37 }
28 },
29 _ => { panic!("This should never happen") }
30 } 38 }
39
40 let command = Cmd::from(split.remove(0))?;
41 let subcommand = match split.is_empty() {
42 false => Some(Subcmd::from(split.remove(0))?),
43 true => None
44 };
45
46 let args = match split.is_empty() {
47 true => None,
48 false => {
49 let mut strsplit: Vec<String> = Vec::new();
50 for s in split {
51 strsplit.push(String::from(s))
52 }
53 Some(strsplit)
54 }
55 };
56
57 Ok(Self { command, subcommand, args, direct_download, force_download, all_lists, delete_old, clean })
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub enum Cmd {
63 Mod,
64 List,
65 Update,
66 Download,
67 Setup
68}
69
70impl Cmd {
71 pub fn from(string: &str) -> MLE<Self> {
72 let cmd = match string {
73 "mod" => Self::Mod,
74 "list" => Self::List,
75 "update" => Self::Update,
76 "download" => Self::Download,
77 "setup" => Self::Setup,
78 _ => return Err(MLError::new(ErrorType::ArgumentError, "Unknown command"))
79 };
80 Ok(cmd)
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub enum Subcmd {
86 Add,
87 Remove,
88 Change
89}
90
91impl Subcmd {
92 fn from(string: &str) -> MLE<Self> {
93 let cmd = match string {
94 "add" => Self::Add,
95 "remove" => Self::Remove,
96 "change" => Self::Change,
97 _ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND"))
98 };
99 Ok(cmd)
31 } 100 }
32} 101}
33 102
34pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 103pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
35 let mut args: Vec<String> = env::args().collect(); 104 let mut args: Vec<String> = env::args().collect();
36 dbg!(&args);
37 args.reverse(); 105 args.reverse();
38 args.pop(); 106 args.pop();
39 args.reverse(); 107 args.reverse();
40 dbg!(&args);
41 108
42 let input = Input::from(args.join(" "))?; 109 let input = Input::from(&args.join(" "))?;
43 110
44 match input.command.as_str() { 111 match input.command {
45 "mod" => { 112 Cmd::Mod => {
46 modification(config, input.args).await 113 modification(config, input).await
47 }, 114 },
48 "list" => { 115 Cmd::List => {
49 list(config, input.args) 116 list(config, input)
50 }, 117 },
51 "update" => { 118 Cmd::Update => {
52 update(config).await 119 update(config, input).await
53 }, 120 },
54 "setup" => { 121 Cmd::Setup => {
55 setup(config).await 122 setup(config).await
56 }, 123 },
57 "download" => { 124 Cmd::Download => {
58 download(config).await 125 download(config).await
59 }, 126 }
60 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_COMMAND"))),
61 } 127 }
62} 128}
63 129
64#[test] 130#[test]
65fn input_from() { 131fn input_from() {
66 let string = String::from("list add test 1.19.2 fabric"); 132 let string = "lis add test 1.19.2 fabric";
67 let input = Input { command: String::from("list"), args: Some(vec![String::from("add"), String::from("test"), String::from("1.19.2"), String::from("fabric")]) }; 133 let input = Input{ command: Cmd::List, subcommand: Some(Subcmd::Add), args: Some(vec![String::from("test"), String::from("1.19.2"), String::from("fabric")]), force_download: false, direct_download: false, all_lists: false };
68 assert_eq!(Input::from(string).unwrap(), input); 134 assert_eq!(Input::from(string).unwrap(), input);
69} 135}