diff options
author | fx <[email protected]> | 2023-05-13 17:28:00 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-05-13 17:28:00 +0200 |
commit | 5a2ea0755b29a8811aeeec1c73679c5783082628 (patch) | |
tree | db2aaedda9678465763993eeec15bd20673394e8 /src/commands/modification.rs | |
parent | 9063a041f6b2e72f6e4a861c77ac16065dd5378b (diff) | |
parent | 3b9d717ecd61bd2b5c32ec117f38c7d67a109748 (diff) | |
download | modlist-5a2ea0755b29a8811aeeec1c73679c5783082628.tar modlist-5a2ea0755b29a8811aeeec1c73679c5783082628.tar.gz modlist-5a2ea0755b29a8811aeeec1c73679c5783082628.zip |
Merge pull request 'todos' (#5) from todos into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/5
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 68 |
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 @@ | |||
1 | use std::io::Write; | 1 | use std::{io::Write, collections::HashMap}; |
2 | 2 | ||
3 | use crate::{ | 3 | use 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)] | ||
16 | pub 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)] |
16 | pub enum IDSelector { | 22 | pub 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 | ||
31 | pub async fn mod_add( | 38 | pub 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 | ¤t_version_id, | 89 | ¤t_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 | ||
131 | async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec<ProjectInfo>> { | 137 | async 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 | ||
209 | async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> { | 227 | async 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" { |