diff options
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index f66ce28..7d4be8d 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,9 +1,8 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use std::io::{Error, ErrorKind}; |
2 | 2 | ||
3 | use 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, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; | 3 | use 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}; |
4 | 4 | ||
5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | |||
7 | match input.subcommand.as_ref().ok_or("")? { | 6 | match input.subcommand.as_ref().ok_or("")? { |
8 | Subcmd::Add => { | 7 | Subcmd::Add => { |
9 | add(config, input).await | 8 | add(config, input).await |
@@ -23,69 +22,75 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error> | |||
23 | 22 | ||
24 | let current_list = get_current_list(config.clone())?; | 23 | let current_list = get_current_list(config.clone())?; |
25 | 24 | ||
26 | mod_add(config, &args[0], current_list, input.disable_download).await?; | 25 | mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?; |
27 | 26 | ||
28 | Ok(()) | 27 | Ok(()) |
29 | } | 28 | } |
30 | 29 | ||
31 | pub async fn mod_add(config: Cfg, mod_id: &str, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { | 30 | pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { |
32 | 31 | ||
33 | println!("Adding mod {}", mod_id); | 32 | println!("Adding mod(s) {:?}", mod_id); |
34 | 33 | let projects = if mod_id.len() == 1 { | |
35 | let project = project(String::from(&config.apis.modrinth), &mod_id).await; | 34 | vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] |
36 | |||
37 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | ||
38 | |||
39 | let mut available_versions_vec: Vec<String> = Vec::new(); | ||
40 | let current_version: Option<Version>; | ||
41 | let current_version_id: String; | ||
42 | let file: String; | ||
43 | if !available_versions.is_empty() { | ||
44 | let current_id = extract_current_version(available_versions.clone())?; | ||
45 | |||
46 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | ||
47 | |||
48 | current_version_id = current_version.clone().unwrap().id; | ||
49 | |||
50 | file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; | ||
51 | |||
52 | for ver in available_versions { | ||
53 | available_versions_vec.push(ver.id); | ||
54 | }; | ||
55 | } else { | 35 | } else { |
56 | println!("There's currently no mod version for your specified target"); | 36 | projects(String::from(&config.apis.modrinth), mod_id).await |
57 | current_version = None; | ||
58 | current_version_id = String::from("NONE"); | ||
59 | file = String::from("NONE"); | ||
60 | available_versions_vec.push(String::from("NONE")); | ||
61 | } | ||
62 | |||
63 | //add to current list and mod table | ||
64 | match userlist_get_all_ids(config.clone(), list.clone().id) { | ||
65 | Ok(mods) => { | ||
66 | if mods.contains(&project.id) { | ||
67 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | ||
68 | else { | ||
69 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; | ||
70 | } | ||
71 | }, | ||
72 | Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, | ||
73 | }; | 37 | }; |
74 | 38 | ||
75 | match mods_get_all_ids(config.clone()) { | 39 | for project in projects { |
76 | Ok(mods) => { | 40 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
77 | if mods.contains(&project.id) { | 41 | |
78 | //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) | 42 | let mut available_versions_vec: Vec<String> = Vec::new(); |
79 | } else { | 43 | let current_version: Option<Version>; |
44 | let current_version_id: String; | ||
45 | let file: String; | ||
46 | if !available_versions.is_empty() { | ||
47 | let current_id = extract_current_version(available_versions.clone())?; | ||
48 | |||
49 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | ||
50 | |||
51 | current_version_id = current_version.clone().unwrap().id; | ||
52 | |||
53 | file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; | ||
54 | |||
55 | for ver in available_versions { | ||
56 | available_versions_vec.push(ver.id); | ||
57 | }; | ||
58 | } else { | ||
59 | println!("There's currently no mod version for your specified target"); | ||
60 | current_version = None; | ||
61 | current_version_id = String::from("NONE"); | ||
62 | file = String::from("NONE"); | ||
63 | available_versions_vec.push(String::from("NONE")); | ||
64 | } | ||
65 | |||
66 | //add to current list and mod table | ||
67 | match userlist_get_all_ids(config.clone(), list.clone().id) { | ||
68 | Ok(mods) => { | ||
69 | if mods.contains(&project.id) { | ||
70 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | ||
71 | else { | ||
72 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; | ||
73 | } | ||
74 | }, | ||
75 | Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, | ||
76 | }; | ||
77 | |||
78 | match mods_get_all_ids(config.clone()) { | ||
79 | Ok(mods) => { | ||
80 | if mods.contains(&project.id) { | ||
81 | //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) | ||
82 | } else { | ||
83 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
84 | } | ||
85 | }, | ||
86 | Err(..) => { | ||
80 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | 87 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; |
81 | } | 88 | }, |
82 | }, | 89 | }; |
83 | Err(..) => { | ||
84 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
85 | }, | ||
86 | }; | ||
87 | 90 | ||
88 | if !disable_download && current_version.is_some() { download_versions(list, vec![current_version.unwrap()]).await?; }; | 91 | if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; |
92 | |||
93 | } | ||
89 | 94 | ||
90 | Ok(()) | 95 | Ok(()) |
91 | } | 96 | } |