diff options
author | FxQnLr <[email protected]> | 2023-05-13 12:37:07 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-05-13 12:37:07 +0200 |
commit | 8398b4003b72b3eefea11d8f71897ddb48f4b0ad (patch) | |
tree | e389808b24fca06aed882177c9d70a5d9121c619 /src/commands/modification.rs | |
parent | bf8fa23bf00584e4efa3060cf8ca4932173061c9 (diff) | |
download | modlist-8398b4003b72b3eefea11d8f71897ddb48f4b0ad.tar modlist-8398b4003b72b3eefea11d8f71897ddb48f4b0ad.tar.gz modlist-8398b4003b72b3eefea11d8f71897ddb48f4b0ad.zip |
impl of set_version import
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 69 |
1 files changed, 49 insertions, 20 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 216a06e..ab42468 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,27 @@ 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 | ||
38 | //TODO fix setversion to per mod | ||
31 | pub async fn mod_add( | 39 | pub async fn mod_add( |
32 | config: Cfg, | 40 | config: Cfg, |
33 | ids: Vec<IDSelector>, | 41 | mods: Vec<AddMod>, |
34 | list: List, | 42 | list: List, |
35 | direct_download: bool, | 43 | direct_download: bool, |
36 | set_version: bool, | ||
37 | ) -> MLE<()> { | 44 | ) -> MLE<()> { |
38 | println!("Add mods to {}", list.id); | 45 | println!("Add mods to {}", list.id); |
39 | println!(" └Add mods:"); | 46 | println!(" └Add mods:"); |
40 | 47 | ||
41 | let mut mod_ids: Vec<String> = Vec::new(); | 48 | let mut mod_ids: Vec<(String, bool)> = Vec::new(); |
42 | let mut ver_ids: Vec<String> = Vec::new(); | 49 | let mut ver_ids: Vec<(String, bool)> = Vec::new(); |
43 | 50 | ||
44 | //"Sort" project ids from version ids to be able to handle them differently but in a batch | 51 | //"Sort" project ids from version ids to be able to handle them differently but in a batch |
45 | for id in ids { | 52 | for m in mods { |
46 | match id { | 53 | match m.id { |
47 | IDSelector::ModificationID(pid) => mod_ids.push(pid), | 54 | IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), |
48 | IDSelector::VersionID(vid) => ver_ids.push(vid), | 55 | IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), |
49 | } | 56 | } |
50 | } | 57 | } |
51 | 58 | ||
@@ -83,7 +90,7 @@ pub async fn mod_add( | |||
83 | ¤t_version_id, | 90 | ¤t_version_id, |
84 | project.clone().applicable_versions, | 91 | project.clone().applicable_versions, |
85 | &project.download_link, | 92 | &project.download_link, |
86 | set_version, | 93 | project.set_version, |
87 | ) { | 94 | ) { |
88 | Err(e) => { | 95 | Err(e) => { |
89 | let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); | 96 | let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); |
@@ -128,13 +135,23 @@ pub async fn mod_add( | |||
128 | Ok(()) | 135 | Ok(()) |
129 | } | 136 | } |
130 | 137 | ||
131 | async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec<ProjectInfo>> { | 138 | async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE<Vec<ProjectInfo>> { |
139 | |||
140 | let mut setmap: HashMap<String, bool> = HashMap::new(); | ||
141 | |||
142 | let mut ids = vec![]; | ||
143 | |||
144 | for id in mod_ids { | ||
145 | setmap.insert(id.0.to_string(), id.1); | ||
146 | ids.push(id.0); | ||
147 | } | ||
148 | |||
132 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); | 149 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); |
133 | 150 | ||
134 | //Get required information from mod_ids | 151 | //Get required information from mod_ids |
135 | let m_projects = match mod_ids.len() { | 152 | let m_projects = match ids.len() { |
136 | 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await], | 153 | 1 => vec![project(&config.apis.modrinth, &ids[0]).await], |
137 | 2.. => projects(&config.apis.modrinth, mod_ids).await, | 154 | 2.. => projects(&config.apis.modrinth, ids).await, |
138 | _ => panic!("PANIC"), | 155 | _ => panic!("PANIC"), |
139 | }; | 156 | }; |
140 | for project in m_projects { | 157 | for project in m_projects { |
@@ -180,12 +197,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec | |||
180 | } | 197 | } |
181 | 198 | ||
182 | projectinfo.push(ProjectInfo { | 199 | projectinfo.push(ProjectInfo { |
183 | mod_id: project.id, | 200 | mod_id: String::from(&project.id), |
184 | slug: project.slug, | 201 | slug: project.slug, |
185 | title: project.title, | 202 | title: project.title, |
186 | current_version, | 203 | current_version, |
187 | applicable_versions: available_versions_vec, | 204 | applicable_versions: available_versions_vec, |
188 | download_link: file, | 205 | download_link: file, |
206 | set_version: setmap.get(&project.id).unwrap().clone(), | ||
189 | }) | 207 | }) |
190 | } else { | 208 | } else { |
191 | println!("\t └There's currently no mod version for your specified target"); | 209 | println!("\t └There's currently no mod version for your specified target"); |
@@ -193,12 +211,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec | |||
193 | file = String::from("NONE"); | 211 | file = String::from("NONE"); |
194 | available_versions_vec.push(String::from("NONE")); | 212 | available_versions_vec.push(String::from("NONE")); |
195 | projectinfo.push(ProjectInfo { | 213 | projectinfo.push(ProjectInfo { |
196 | mod_id: project.id, | 214 | mod_id: String::from(&project.id), |
197 | slug: project.slug, | 215 | slug: project.slug, |
198 | title: project.title, | 216 | title: project.title, |
199 | current_version, | 217 | current_version, |
200 | applicable_versions: available_versions_vec, | 218 | applicable_versions: available_versions_vec, |
201 | download_link: file, | 219 | download_link: file, |
220 | set_version: setmap.get(&project.id).unwrap().clone(), | ||
202 | }) | 221 | }) |
203 | } | 222 | } |
204 | } | 223 | } |
@@ -206,11 +225,20 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec | |||
206 | Ok(projectinfo) | 225 | Ok(projectinfo) |
207 | } | 226 | } |
208 | 227 | ||
209 | async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> { | 228 | async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<ProjectInfo>> { |
229 | |||
230 | let mut setmap: HashMap<String, bool> = HashMap::new(); | ||
231 | |||
232 | let mut ids = vec![]; | ||
233 | |||
234 | for id in ver_ids { | ||
235 | setmap.insert(id.0.to_string(), id.1); | ||
236 | ids.push(id.0); | ||
237 | } | ||
210 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); | 238 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); |
211 | 239 | ||
212 | //Get required information from ver_ids | 240 | //Get required information from ver_ids |
213 | let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; | 241 | let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await; |
214 | let mut v_mod_ids: Vec<String> = Vec::new(); | 242 | let mut v_mod_ids: Vec<String> = Vec::new(); |
215 | for ver in v_versions.clone() { | 243 | for ver in v_versions.clone() { |
216 | v_mod_ids.push(ver.project_id); | 244 | v_mod_ids.push(ver.project_id); |
@@ -230,12 +258,13 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo> | |||
230 | .unwrap() | 258 | .unwrap() |
231 | .url; | 259 | .url; |
232 | projectinfo.push(ProjectInfo { | 260 | projectinfo.push(ProjectInfo { |
233 | mod_id: project.id, | 261 | mod_id: String::from(&project.id), |
234 | slug: project.slug, | 262 | slug: project.slug, |
235 | title: project.title, | 263 | title: project.title, |
236 | current_version: Some(version.clone()), | 264 | current_version: Some(version.clone()), |
237 | applicable_versions: vec![String::from(&version.id)], | 265 | applicable_versions: vec![String::from(&version.id)], |
238 | download_link: file, | 266 | download_link: file, |
267 | set_version: setmap.get(&project.id).unwrap().clone(), | ||
239 | }) | 268 | }) |
240 | } | 269 | } |
241 | Ok(projectinfo) | 270 | Ok(projectinfo) |
@@ -281,7 +310,7 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { | |||
281 | // Remove mod from main list if not used elsewhere | 310 | // Remove mod from main list if not used elsewhere |
282 | let mut mod_used = false; | 311 | let mut mod_used = false; |
283 | for id in list_ids { | 312 | for id in list_ids { |
284 | let mods = match userlist_get_all_ids(config.clone(), id) { | 313 | let mods = match userlist_get_all_ids(config.clone(), &id) { |
285 | Ok(m) => m, | 314 | Ok(m) => m, |
286 | Err(err) => { | 315 | Err(err) => { |
287 | if err.to_string() == "Database: NO_MODS_USERLIST" { | 316 | if err.to_string() == "Database: NO_MODS_USERLIST" { |