summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2022-11-29 22:59:19 +0100
committerFxQnLr <[email protected]>2022-11-29 22:59:19 +0100
commit575d2493e8e5747bf65321f7277e52211d73e387 (patch)
tree11b20e371316047110ffd36d1fe5bfefd2183c07 /src/commands/modification.rs
parentddde9204c72dd867f920f07f6483be03dda7cf68 (diff)
downloadmodlist-575d2493e8e5747bf65321f7277e52211d73e387.tar
modlist-575d2493e8e5747bf65321f7277e52211d73e387.tar.gz
modlist-575d2493e8e5747bf65321f7277e52211d73e387.zip
fixed mod without matching specific version
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r--src/commands/modification.rs38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 8e39d11..ac23970 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,6 +1,6 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions}; 3use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions};
4 4
5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
6 6
@@ -26,27 +26,41 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>
26 let project = project(String::from(&config.apis.modrinth), &args[0]).await; 26 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
27 27
28 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; 28 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await;
29
30 let current_id = extract_current_version(available_versions.clone())?;
31
32 let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap();
33 29
34 let file = current_version.clone().files.into_iter().find(|f| f.primary).unwrap().url;
35
36 let mut available_versions_vec: Vec<String> = Vec::new(); 30 let mut available_versions_vec: Vec<String> = Vec::new();
37 for ver in available_versions { 31 let current_version: Option<Version>;
38 available_versions_vec.push(ver.id); 32 let current_version_id: String;
33 let file: String;
34 if !available_versions.is_empty() {
35 let current_id = extract_current_version(available_versions.clone())?;
36
37 current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap());
38
39 current_version_id = current_version.clone().unwrap().id;
40
41 file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url;
42
43 for ver in available_versions {
44 available_versions_vec.push(ver.id);
45 };
46 } else {
47 println!("There's currently no mod version for your specified target");
48 current_version = None;
49 current_version_id = String::from("NONE");
50 file = String::from("NONE");
51 available_versions_vec.push(String::from("NONE"));
39 } 52 }
53
40 //add to current list and mod table 54 //add to current list and mod table
41 match userlist_get_all_ids(config.clone(), current_list.clone().id) { 55 match userlist_get_all_ids(config.clone(), current_list.clone().id) {
42 Ok(mods) => { 56 Ok(mods) => {
43 if mods.contains(&project.id) { 57 if mods.contains(&project.id) {
44 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } 58 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); }
45 else { 59 else {
46 userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version.id), available_versions_vec, file)?; 60 userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?;
47 } 61 }
48 }, 62 },
49 Err(..) => userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version.id), available_versions_vec, file)?, 63 Err(..) => userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?,
50 }; 64 };
51 65
52 match mods_get_all_ids(config.clone()) { 66 match mods_get_all_ids(config.clone()) {
@@ -62,7 +76,7 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>
62 }, 76 },
63 }; 77 };
64 78
65 if !input.disable_download { download_versions(current_list, vec![current_version]).await?; } 79 if !input.disable_download && current_version.is_some() { download_versions(current_list, vec![current_version.unwrap()]).await?; }
66 80
67 Ok(()) 81 Ok(())
68} 82}