summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r--src/commands/modification.rs68
1 files changed, 48 insertions, 20 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 216a06e..9a1a651 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,4 +1,4 @@
1use std::io::Write; 1use std::{io::Write, collections::HashMap};
2 2
3use crate::{ 3use crate::{
4 config::Cfg, 4 config::Cfg,
@@ -12,6 +12,12 @@ use crate::{
12 List, 12 List,
13}; 13};
14 14
15#[derive(Debug, Clone)]
16pub struct AddMod {
17 pub id: IDSelector,
18 pub set_version: bool
19}
20
15#[derive(Debug, Clone, PartialEq, Eq)] 21#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum IDSelector { 22pub enum IDSelector {
17 ModificationID(String), 23 ModificationID(String),
@@ -26,26 +32,26 @@ pub struct ProjectInfo {
26 pub current_version: Option<Version>, 32 pub current_version: Option<Version>,
27 pub applicable_versions: Vec<String>, 33 pub applicable_versions: Vec<String>,
28 pub download_link: String, 34 pub download_link: String,
35 pub set_version: bool,
29} 36}
30 37
31pub async fn mod_add( 38pub async fn mod_add(
32 config: Cfg, 39 config: Cfg,
33 ids: Vec<IDSelector>, 40 mods: Vec<AddMod>,
34 list: List, 41 list: List,
35 direct_download: bool, 42 direct_download: bool,
36 set_version: bool,
37) -> MLE<()> { 43) -> MLE<()> {
38 println!("Add mods to {}", list.id); 44 println!("Add mods to {}", list.id);
39 println!(" └Add mods:"); 45 println!(" └Add mods:");
40 46
41 let mut mod_ids: Vec<String> = Vec::new(); 47 let mut mod_ids: Vec<(String, bool)> = Vec::new();
42 let mut ver_ids: Vec<String> = Vec::new(); 48 let mut ver_ids: Vec<(String, bool)> = Vec::new();
43 49
44 //"Sort" project ids from version ids to be able to handle them differently but in a batch 50 //"Sort" project ids from version ids to be able to handle them differently but in a batch
45 for id in ids { 51 for m in mods {
46 match id { 52 match m.id {
47 IDSelector::ModificationID(pid) => mod_ids.push(pid), 53 IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)),
48 IDSelector::VersionID(vid) => ver_ids.push(vid), 54 IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)),
49 } 55 }
50 } 56 }
51 57
@@ -83,7 +89,7 @@ pub async fn mod_add(
83 &current_version_id, 89 &current_version_id,
84 project.clone().applicable_versions, 90 project.clone().applicable_versions,
85 &project.download_link, 91 &project.download_link,
86 set_version, 92 project.set_version,
87 ) { 93 ) {
88 Err(e) => { 94 Err(e) => {
89 let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); 95 let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id);
@@ -128,13 +134,23 @@ pub async fn mod_add(
128 Ok(()) 134 Ok(())
129} 135}
130 136
131async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec<ProjectInfo>> { 137async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE<Vec<ProjectInfo>> {
138
139 let mut setmap: HashMap<String, bool> = HashMap::new();
140
141 let mut ids = vec![];
142
143 for id in mod_ids {
144 setmap.insert(id.0.to_string(), id.1);
145 ids.push(id.0);
146 }
147
132 let mut projectinfo: Vec<ProjectInfo> = Vec::new(); 148 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
133 149
134 //Get required information from mod_ids 150 //Get required information from mod_ids
135 let m_projects = match mod_ids.len() { 151 let m_projects = match ids.len() {
136 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await], 152 1 => vec![project(&config.apis.modrinth, &ids[0]).await],
137 2.. => projects(&config.apis.modrinth, mod_ids).await, 153 2.. => projects(&config.apis.modrinth, ids).await,
138 _ => panic!("PANIC"), 154 _ => panic!("PANIC"),
139 }; 155 };
140 for project in m_projects { 156 for project in m_projects {
@@ -180,12 +196,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec
180 } 196 }
181 197
182 projectinfo.push(ProjectInfo { 198 projectinfo.push(ProjectInfo {
183 mod_id: project.id, 199 mod_id: String::from(&project.id),
184 slug: project.slug, 200 slug: project.slug,
185 title: project.title, 201 title: project.title,
186 current_version, 202 current_version,
187 applicable_versions: available_versions_vec, 203 applicable_versions: available_versions_vec,
188 download_link: file, 204 download_link: file,
205 set_version: setmap.get(&project.id).unwrap().clone(),
189 }) 206 })
190 } else { 207 } else {
191 println!("\t └There's currently no mod version for your specified target"); 208 println!("\t └There's currently no mod version for your specified target");
@@ -193,12 +210,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec
193 file = String::from("NONE"); 210 file = String::from("NONE");
194 available_versions_vec.push(String::from("NONE")); 211 available_versions_vec.push(String::from("NONE"));
195 projectinfo.push(ProjectInfo { 212 projectinfo.push(ProjectInfo {
196 mod_id: project.id, 213 mod_id: String::from(&project.id),
197 slug: project.slug, 214 slug: project.slug,
198 title: project.title, 215 title: project.title,
199 current_version, 216 current_version,
200 applicable_versions: available_versions_vec, 217 applicable_versions: available_versions_vec,
201 download_link: file, 218 download_link: file,
219 set_version: setmap.get(&project.id).unwrap().clone(),
202 }) 220 })
203 } 221 }
204 } 222 }
@@ -206,11 +224,20 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec
206 Ok(projectinfo) 224 Ok(projectinfo)
207} 225}
208 226
209async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> { 227async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<ProjectInfo>> {
228
229 let mut setmap: HashMap<String, bool> = HashMap::new();
230
231 let mut ids = vec![];
232
233 for id in ver_ids {
234 setmap.insert(id.0.to_string(), id.1);
235 ids.push(id.0);
236 }
210 let mut projectinfo: Vec<ProjectInfo> = Vec::new(); 237 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
211 238
212 //Get required information from ver_ids 239 //Get required information from ver_ids
213 let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; 240 let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await;
214 let mut v_mod_ids: Vec<String> = Vec::new(); 241 let mut v_mod_ids: Vec<String> = Vec::new();
215 for ver in v_versions.clone() { 242 for ver in v_versions.clone() {
216 v_mod_ids.push(ver.project_id); 243 v_mod_ids.push(ver.project_id);
@@ -230,12 +257,13 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>
230 .unwrap() 257 .unwrap()
231 .url; 258 .url;
232 projectinfo.push(ProjectInfo { 259 projectinfo.push(ProjectInfo {
233 mod_id: project.id, 260 mod_id: String::from(&project.id),
234 slug: project.slug, 261 slug: project.slug,
235 title: project.title, 262 title: project.title,
236 current_version: Some(version.clone()), 263 current_version: Some(version.clone()),
237 applicable_versions: vec![String::from(&version.id)], 264 applicable_versions: vec![String::from(&version.id)],
238 download_link: file, 265 download_link: file,
266 set_version: setmap.get(&version.id).unwrap().clone(),
239 }) 267 })
240 } 268 }
241 Ok(projectinfo) 269 Ok(projectinfo)
@@ -281,7 +309,7 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> {
281 // Remove mod from main list if not used elsewhere 309 // Remove mod from main list if not used elsewhere
282 let mut mod_used = false; 310 let mut mod_used = false;
283 for id in list_ids { 311 for id in list_ids {
284 let mods = match userlist_get_all_ids(config.clone(), id) { 312 let mods = match userlist_get_all_ids(config.clone(), &id) {
285 Ok(m) => m, 313 Ok(m) => m,
286 Err(err) => { 314 Err(err) => {
287 if err.to_string() == "Database: NO_MODS_USERLIST" { 315 if err.to_string() == "Database: NO_MODS_USERLIST" {