summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2023-01-15 16:58:45 +0100
committerfxqnlr <[email protected]>2023-01-15 16:58:45 +0100
commit30c1ffcecb541d9b27982df6af26948514cbadaa (patch)
tree11a898fc729bc813059fc8b66b7a27a3e9ec532e /src
parentfad32c31b59001bed46fa860fbc3a994d278e700 (diff)
downloadmodlist-30c1ffcecb541d9b27982df6af26948514cbadaa.tar
modlist-30c1ffcecb541d9b27982df6af26948514cbadaa.tar.gz
modlist-30c1ffcecb541d9b27982df6af26948514cbadaa.zip
started implementation of new input system
Diffstat (limited to 'src')
-rw-r--r--src/commands/list.rs16
-rw-r--r--src/commands/mod.rs20
-rw-r--r--src/input.rs363
-rw-r--r--src/lib.rs6
-rw-r--r--src/main.rs9
5 files changed, 253 insertions, 161 deletions
diff --git a/src/commands/list.rs b/src/commands/list.rs
index 3998cce..2fec1c7 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,6 +1,6 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}, modrinth::MCVersionType}; 3use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, ListOptions}, /*cmd_update,*/ error::{MLE, ErrorType, MLError}, /*modrinth::MCVersionType*/};
4 4
5#[derive(Debug, Clone, PartialEq, Eq)] 5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct List { 6pub struct List {
@@ -9,20 +9,20 @@ pub struct List {
9 pub modloader: Modloader, 9 pub modloader: Modloader,
10 pub download_folder: String, 10 pub download_folder: String,
11} 11}
12 12/*
13pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 13pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
14 14
15 match input.subcommand.ok_or("")? { 15 match input.list_options.ok_or("")? {
16 Subcmd::Add => { 16 ListOptions::Add => {
17 match add(config, input.args.ok_or("")?) { 17 match add(config, input.args.ok_or("")?) {
18 Ok(..) => Ok(()), 18 Ok(..) => Ok(()),
19 Err(e) => Err(Box::new(e)) 19 Err(e) => Err(Box::new(e))
20 } 20 }
21 }, 21 },
22 Subcmd::Change => { 22 ListOptions::Change => {
23 change(config, input.args) 23 change(config, input.args)
24 }, 24 },
25 Subcmd::Remove => { 25 ListOptions::Remove => {
26 match remove(config, input.args.ok_or("")?) { 26 match remove(config, input.args.ok_or("")?) {
27 Ok(..) => Ok(()), 27 Ok(..) => Ok(()),
28 Err(e) => Err(Box::new(e)) 28 Err(e) => Err(Box::new(e))
@@ -39,7 +39,7 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::E
39 } 39 }
40 } 40 }
41} 41}
42 42*/
43pub fn get_current_list(config: Cfg) -> MLE<List> { 43pub fn get_current_list(config: Cfg) -> MLE<List> {
44 let id = config_get_current_list(config.clone())?; 44 let id = config_get_current_list(config.clone())?;
45 lists_get(config, id) 45 lists_get(config, id)
@@ -83,6 +83,7 @@ fn remove(config: Cfg, args: Vec<String>) -> MLE<()> {
83 } 83 }
84} 84}
85 85
86/*
86///Changing the current lists version and updating it 87///Changing the current lists version and updating it
87/// #Arguments 88/// #Arguments
88/// 89///
@@ -95,3 +96,4 @@ async fn version(config: Cfg, args: Option<Vec<String>>, version_type: Option<MC
95 //update the list & with -- args 96 //update the list & with -- args
96 cmd_update(config, vec![current_list], true, true, false).await 97 cmd_update(config, vec![current_list], true, true, false).await
97} 98}
99*/
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 0d5bd00..29fc600 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -1,13 +1,13 @@
1pub mod modification; 1//pub mod modification;
2pub mod list; 2pub mod list;
3pub mod update; 3//pub mod update;
4pub mod setup; 4//pub mod setup;
5pub mod download; 5//pub mod download;
6pub mod io; 6//pub mod io;
7 7
8pub use modification::*; 8//pub use modification::*;
9pub use list::*; 9pub use list::*;
10pub use update::*; 10//pub use update::*;
11pub use setup::*; 11//pub use setup::*;
12pub use download::*; 12//pub use download::*;
13pub use io::*; 13//pub use io::*;
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}
diff --git a/src/lib.rs b/src/lib.rs
index eb845d8..ef97a53 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,14 +1,14 @@
1pub mod apis; 1//pub mod apis;
2pub mod config; 2pub mod config;
3pub mod commands; 3pub mod commands;
4pub mod input; 4pub mod input;
5pub mod db; 5pub mod db;
6pub mod error; 6pub mod error;
7pub mod files; 7//pub mod files;
8 8
9use std::path::Path; 9use std::path::Path;
10 10
11pub use apis::*; 11//pub use apis::*;
12pub use commands::*; 12pub use commands::*;
13use error::{MLE, ErrorType, MLError}; 13use error::{MLE, ErrorType, MLError};
14 14
diff --git a/src/main.rs b/src/main.rs
index 59d41c5..39e46e8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,10 +1,17 @@
1use std::env;
2
1use modlist::{config::Cfg, input::get_input}; 3use modlist::{config::Cfg, input::get_input};
2 4
3#[tokio::main] 5#[tokio::main]
4async fn main() { 6async fn main() {
5 let config = Cfg::init("modlist.toml").unwrap(); 7 let config = Cfg::init("modlist.toml").unwrap();
6 8
7 match get_input(config).await { 9 let mut args: Vec<String> = env::args().collect();
10 args.reverse();
11 args.pop();
12 args.reverse();
13
14 match get_input(config, args).await {
8 Ok(..) => (), 15 Ok(..) => (),
9 Err(e) => { 16 Err(e) => {
10 println!("{}", e); 17 println!("{}", e);