summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-01-29 14:14:43 +0100
committerGitHub <[email protected]>2023-01-29 14:14:43 +0100
commit35d9e091b9b6f68e51a79c1a10e0a95cd2ae974e (patch)
tree68a63f39a5bf6241e4ca9499d03ea148ec9737c4 /src/commands/modification.rs
parent8f3c77986b36d7653fd44e16ef986f0ad284e0c4 (diff)
parentd7d0c904bff665ab5c8355f2381a0628ebbf7a30 (diff)
downloadmodlist-35d9e091b9b6f68e51a79c1a10e0a95cd2ae974e.tar
modlist-35d9e091b9b6f68e51a79c1a10e0a95cd2ae974e.tar.gz
modlist-35d9e091b9b6f68e51a79c1a10e0a95cd2ae974e.zip
Merge pull request #3 from FxQnLr/new_input
New input, fuck it
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r--src/commands/modification.rs49
1 files changed, 20 insertions, 29 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 7d4be8d..c82d6b5 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,34 +1,26 @@
1use std::io::{Error, ErrorKind}; 1use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}};
2 2
3use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; 3pub async fn modification(config: Cfg, input: Input) -> MLE<()> {
4 4 match input.clone().mod_options.ok_or("").unwrap() {
5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5 ModOptions::Add => {
6 match input.subcommand.as_ref().ok_or("")? {
7 Subcmd::Add => {
8 add(config, input).await 6 add(config, input).await
9 }, 7 },
10 Subcmd::Remove => { 8 ModOptions::Remove => {
11 remove(config, input.args.ok_or("")?) 9 remove(config, input)
12 }, 10 },
13 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE")))
14 } 11 }
15} 12}
16 13
17async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 14async fn add(config: Cfg, input: Input) -> MLE<()> {
18
19 let args = input.args.ok_or("")?;
20 15
21 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; 16 mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?;
22
23 let current_list = get_current_list(config.clone())?;
24
25 mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?;
26 17
27 Ok(()) 18 Ok(())
28} 19}
29 20
30pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { 21pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, direct_download: bool) -> MLE<()> {
31 22
23 //Fix printing (its horrible)
32 println!("Adding mod(s) {:?}", mod_id); 24 println!("Adding mod(s) {:?}", mod_id);
33 let projects = if mod_id.len() == 1 { 25 let projects = if mod_id.len() == 1 {
34 vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] 26 vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await]
@@ -50,7 +42,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl
50 42
51 current_version_id = current_version.clone().unwrap().id; 43 current_version_id = current_version.clone().unwrap().id;
52 44
53 file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; 45 file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url;
54 46
55 for ver in available_versions { 47 for ver in available_versions {
56 available_versions_vec.push(ver.id); 48 available_versions_vec.push(ver.id);
@@ -67,7 +59,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl
67 match userlist_get_all_ids(config.clone(), list.clone().id) { 59 match userlist_get_all_ids(config.clone(), list.clone().id) {
68 Ok(mods) => { 60 Ok(mods) => {
69 if mods.contains(&project.id) { 61 if mods.contains(&project.id) {
70 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } 62 return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); }
71 else { 63 else {
72 userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?; 64 userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?;
73 } 65 }
@@ -88,24 +80,23 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl
88 }, 80 },
89 }; 81 };
90 82
91 if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; 83 if direct_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; };
92 84
93 } 85 }
94 86
95 Ok(()) 87 Ok(())
96} 88}
97 89
98fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 90fn remove(config: Cfg, input: Input) -> MLE<()> {
99 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; 91
100 92 //TODO inplement deletion by slug or title
101 let current_list = get_current_list(config.clone())?; 93 let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?;
102 let mod_id = mods_get_id(config.clone(), String::from(&args[0]))?;
103 94
104 let version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&mod_id))?; 95 let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?;
105 96
106 //TODO implement remove from modlist if not in any other lists && config clean is true 97 //TODO implement remove from modlist if not in any other lists && config clean is true
107 userlist_remove(config.clone(), String::from(&current_list.id), String::from(&mod_id))?; 98 userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?;
108 delete_version(current_list, version)?; 99 delete_version(input.list.unwrap(), version)?;
109 100
110 let list_ids = lists_get_all_ids(config.clone())?; 101 let list_ids = lists_get_all_ids(config.clone())?;
111 102