summaryrefslogtreecommitdiff
path: root/src/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/input.rs')
-rw-r--r--src/input.rs363
1 files changed, 223 insertions, 140 deletions
diff --git a/src/input.rs b/src/input.rs
index 28a0b7b..cdd3938 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -1,85 +1,22 @@
1use std::env; 1use crate::{error::{MLE, MLError, ErrorType}, Modloader, config::Cfg, db::lists_get, get_current_list, List};
2use crate::{config::Cfg, list, modification, update, setup, download, io, error::{MLError, ErrorType, MLE}};
3 2
4#[derive(Debug, Clone, PartialEq, Eq)] 3#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Input { 4pub struct Input {
6 pub command: Cmd, 5 pub command: Option<Cmd>,
7 pub subcommand: Option<Subcmd>, 6 pub download: bool,
8 pub args: Option<Vec<String>>, 7 pub update: bool,
9 pub direct_download: bool, 8 pub mod_options: Option<ModOptions>,
10 pub all_lists: bool, 9 pub mod_id: Option<String>,
11 pub delete_old: bool, 10 pub mod_version: Option<String>,
12 pub clean: bool, 11 pub list: Option<List>,
13 pub disable_download: bool, 12 pub list_options: Option<ListOptions>,
14 pub version: bool, 13 pub list_id: Option<String>,
15} 14 pub list_mcversion: Option<String>,
16 15 pub modloader: Option<Modloader>,
17impl Input { 16 pub directory: Option<String>,
18 fn from(string: &str) -> MLE<Self> { 17 pub export: bool,
19 let mut split: Vec<&str> = string.split(' ').collect(); 18 pub import: bool,
20 19 pub file: Option<String>,
21 let mut direct_download = false;
22 let mut all_lists = false;
23 let mut delete_old = false;
24 let mut clean = false;
25 let mut disable_download = false;
26 let mut version = false;
27
28 let mut toremove: Vec<usize> = vec![];
29 for (i, input) in split.clone().into_iter().enumerate() {
30 if input.starts_with("--") {
31 match input {
32 "--direct-download" => direct_download = true,
33 "--all-lists" => all_lists = true,
34 "--delete-old" => delete_old = true,
35 "--clean" => clean = true,
36 "--disable-download" => disable_download = true,
37 "--version" => version = true,
38 _ => continue,
39 }
40 toremove.push(i)
41 }
42 }
43
44 for rem in toremove.into_iter().rev() {
45 split.remove(rem);
46 }
47
48 if version {
49 match std::env::var("DEV") {
50 Ok(dev) => {
51 let devint = dev.parse::<i32>().unwrap();
52 if devint >= 1 {
53 println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION"));
54 } else {
55 println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION"));
56 }
57 },
58 Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")),
59 }
60
61 std::process::exit(0);
62 }
63
64 let command = Cmd::from(split.remove(0))?;
65 let subcommand = match split.is_empty() {
66 false => Some(Subcmd::from(split.remove(0))?),
67 true => None
68 };
69
70 let args = match split.is_empty() {
71 true => None,
72 false => {
73 let mut strsplit: Vec<String> = Vec::new();
74 for s in split {
75 strsplit.push(String::from(s))
76 }
77 Some(strsplit)
78 }
79 };
80
81 Ok(Self { command, subcommand, args, direct_download, all_lists, delete_old, clean, disable_download, version })
82 }
83} 20}
84 21
85#[derive(Debug, Clone, PartialEq, Eq)] 22#[derive(Debug, Clone, PartialEq, Eq)]
@@ -88,90 +25,236 @@ pub enum Cmd {
88 List, 25 List,
89 Update, 26 Update,
90 Download, 27 Download,
91 Setup, 28 Io,
92 Io
93} 29}
94 30
95impl Cmd { 31#[derive(Debug, Clone, PartialEq, Eq)]
96 fn from(string: &str) -> MLE<Self> { 32pub enum ModOptions {
97 let cmd = match string { 33 Add,
98 "mod" => Self::Mod, 34 Remove
99 "list" => Self::List,
100 "update" => Self::Update,
101 "download" => Self::Download,
102 "setup" => Self::Setup,
103 "io" => Self::Io,
104 _ => return Err(MLError::new(ErrorType::ArgumentError, "Unknown command"))
105 };
106 Ok(cmd)
107 }
108} 35}
109 36
110#[derive(Debug, Clone, PartialEq, Eq)] 37#[derive(Debug, Clone, PartialEq, Eq)]
111pub enum Subcmd { 38pub enum ListOptions {
112 Add, 39 Add,
113 Remove, 40 Remove,
114 Change, 41 Change,
115 Version,
116 Export,
117 Import,
118} 42}
119 43
120impl Subcmd { 44impl Input {
121 fn from(string: &str) -> MLE<Self> { 45 fn from(config: Cfg, input: Vec<String>) -> MLE<Self> {
122 let cmd = match string { 46 let input_string = input.join(" ");
123 "add" => Self::Add, 47 let mut args: Vec<&str> = input_string.split('-').collect();
124 "remove" => Self::Remove, 48 args.reverse();
125 "change" => Self::Change, 49 args.pop();
126 "version" => Self::Version, 50 args.reverse();
127 "export" => Self::Export, 51
128 "import" => Self::Import, 52 let mut command: Option<Cmd> = None;
129 _ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND")) 53
130 }; 54 let mut download = false;
131 Ok(cmd) 55 let mut update = false;
56 let mut mod_options: Option<ModOptions> = None;
57 let mut mod_id: Option<String> = None;
58 let mut mod_version: Option<String> = None;
59 let mut list: Option<List> = None;
60 let mut list_options: Option<ListOptions> = None;
61 let mut list_id: Option<String> = None;
62 let mut list_mcversion: Option<String> = None;
63 let mut modloader: Option<Modloader> = None;
64 let mut directory: Option<String> = None;
65 let mut export = false;
66 let mut import = false;
67 let mut file: Option<String> = None;
68
69 for arg in args {
70 let arg_split: Vec<&str> = arg.trim().split(" ").collect();
71 match arg_split[0] {
72 "v" | "version" => { show_version(); },
73 "d" | "download" => {
74 command = Some(Cmd::Download);
75 },
76 "u" | "update" => {
77 command = Some(Cmd::Update);
78 }
79 "ma" => {
80 command = Some(Cmd::Mod);
81 mod_options = Some(ModOptions::Add);
82 mod_id = Some(String::from(arg_split[1]));
83 },
84 "l" => {
85 list = Some(lists_get(config.clone(), String::from(arg_split[1]))?);
86 }
87 "la" => {
88 command = Some(Cmd::List);
89 list_options = Some(ListOptions::Add);
90 list_id = Some(String::from(arg_split[1]));
91 },
92 "lr" => {
93 command = Some(Cmd::List);
94 list_options = Some(ListOptions::Remove);
95 list_id = Some(String::from(arg_split[1]));
96 },
97 "lc" => {
98 command = Some(Cmd::List);
99 list_options = Some(ListOptions::Change);
100 list_id = Some(String::from(arg_split[1]));
101 },
102 "lv" => {
103 list_mcversion = Some(String::from(arg_split[1]));
104 }
105 _ => return Err(MLError::new(ErrorType::ArgumentError, "UnknownArgument")),
106 }
107 }
108
109 Ok(Self {
110 command,
111 download,
112 update,
113 mod_options,
114 mod_id,
115 mod_version,
116 list,
117 list_options,
118 list_id,
119 list_mcversion,
120 modloader,
121 directory,
122 export,
123 import,
124 file
125 })
132 } 126 }
133} 127}
134 128
135pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 129fn show_version() {
136 let mut args: Vec<String> = env::args().collect(); 130 match std::env::var("DEV") {
137 args.reverse(); 131 Ok(dev) => {
138 args.pop(); 132 let devint = dev.parse::<i32>().unwrap();
139 args.reverse(); 133 if devint >= 1 {
140 134 println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION"));
141 let input = Input::from(&args.join(" "))?; 135 } else {
142 136 println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION"));
143 match input.command { 137 }
144 Cmd::Mod => {
145 modification(config, input).await
146 }, 138 },
147 Cmd::List => { 139 Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")),
148 list(config, input).await 140 }
141 std::process::exit(0);
142}
143
144pub async fn get_input(config: Cfg, args: Vec<String>) -> MLE<Input> {
145 let input = Input::from(config.clone(), args)?;
146
147 match input.clone().command.unwrap() {
148 Cmd::Mod => check_mod(input, config),
149 Cmd::List => check_list(input),
150 Cmd::Update => check_update(input),
151 Cmd::Download => check_download(input),
152 Cmd::Io => check_io(input),
153 }
154}
155
156fn check_mod(mut input: Input, config: Cfg) -> MLE<Input> {
157 print!("Checkmod");
158 if input.mod_options.is_none() {
159 return Err(MLError::new(ErrorType::ArgumentError, "NO_MOD_ARGUMENT"));
160 };
161 match input.clone().mod_options.unwrap() {
162 ModOptions::Add => {
163 print!("Checkadd");
164 if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID")); };
165 if input.list_id.is_none() { println!("NOLIST"); input.list = Some(get_current_list(config.clone())?); };
166 Ok(input)
149 }, 167 },
150 Cmd::Update => { 168 ModOptions::Remove => {
151 match update(config, input).await { 169 if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID")); };
152 Ok(..) => Ok(()), 170 Ok(input)
153 Err(..) => Err(Box::new(MLError::new(ErrorType::Other, "UPDATE_ERR")))
154 }
155 }, 171 },
156 Cmd::Setup => { 172 }
157 setup(config).await 173}
174
175fn check_list(mut input: Input) -> MLE<Input> {
176 if input.list_options.is_none() {
177 return Err(MLError::new(ErrorType::ArgumentError, "NO_LIST_ARGUMENT"));
178 };
179 match input.clone().list_options.unwrap() {
180 ListOptions::Add => {
181 if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); };
182 if input.list_mcversion.is_none() { /*TODO Get latest version */ input.list_mcversion = Some(String::from("1.19.3")) };
183 if input.modloader.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_MODLOADER")); };
184 if input.directory.is_none() { input.directory = Some(format!("./downloads/{}", input.clone().list_id.expect("earlier if failed"))) };
185 Ok(input)
158 }, 186 },
159 Cmd::Download => { 187 ListOptions::Remove => {
160 download(config, input).await 188 if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); };
189 Ok(input)
161 }, 190 },
162 Cmd::Io => { 191 ListOptions::Change => {
163 io(config, input).await 192 //TODO check if no change
193 if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); };
194 Ok(input)
164 } 195 }
165 } 196 }
166} 197}
167 198
199fn check_update(input: Input) -> MLE<Input> {
200 Ok(input)
201}
202
203fn check_download(input: Input) -> MLE<Input> {
204 Ok(input)
205}
206
207fn check_io(input: Input) -> MLE<Input> {
208 Ok(input)
209}
210
168#[test] 211#[test]
169fn input_from() { 212fn input_from() {
170 let string = "list add test 1.19.2 fabric"; 213 let config = Cfg::init("modlist.toml").unwrap();
171 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, disable_download: false, version: false }; 214 assert_eq!(
172 assert_eq!(Input::from(string).unwrap(), input); 215 Input::from(config.clone(), vec![String::from("-la test -lv 1.19.3")]).unwrap(),
216 Input {
217 command: Some(Cmd::List),
218 download: false,
219 update: false,
220 mod_options: None,
221 mod_id: None,
222 mod_version: None,
223 list: None,
224 list_options: Some(ListOptions::Add),
225 list_id: Some(String::from("test")),
226 list_mcversion: Some(String::from("1.19.3")),
227 modloader: None,
228 directory: None,
229 export: false,
230 import: false,
231 file: None
232 }
233 );
173 234
174 let string = "update --direct-download --delete-old"; 235}
175 let input = Input{ command: Cmd::Update, subcommand: None, args: None, direct_download: true, all_lists: false, clean: false, delete_old: true, disable_download: false, version: false }; 236
176 assert_eq!(Input::from(string).unwrap(), input); 237#[tokio::test]
238async fn get_input_test() {
239 let config = Cfg::init("modlist.toml").unwrap();
240 assert_eq!(
241 get_input(config.clone(), vec![String::from("-ma test")]).await.unwrap(),
242 Input {
243 command: Some(Cmd::Mod),
244 download: false,
245 update: false,
246 mod_options: Some(ModOptions::Add),
247 mod_id: Some(String::from("test")),
248 mod_version: None,
249 list: Some(lists_get(config.clone(), String::from("one")).unwrap()),
250 list_options: None,
251 list_id: None,
252 list_mcversion: None,
253 modloader: None,
254 directory: None,
255 export: false,
256 import: false,
257 file: None
258 }
259 )
177} 260}