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.rs211
1 files changed, 170 insertions, 41 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 12a635f..c815155 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,8 +1,10 @@
1use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; 1use crate::{modrinth::{versions, extract_current_version, Version, projects, get_raw_versions, project}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}};
2 2
3//TODO DO IT 3//TODO DO IT
4pub struct ModVer { 4#[derive(Debug, Clone, PartialEq, Eq)]
5 5pub enum IDSelector {
6 ModificationID(String),
7 VersionID(String)
6} 8}
7 9
8pub async fn modification(config: Cfg, input: Input) -> MLE<()> { 10pub async fn modification(config: Cfg, input: Input) -> MLE<()> {
@@ -22,27 +24,148 @@ async fn add(config: Cfg, input: Input) -> MLE<()> {
22 24
23 Ok(()) 25 Ok(())
24} 26}
25//TODO impl specific version 27
26pub async fn mods_add(config: Cfg, mod_id: Vec<String>, list: List, direct_download: bool, set_version: bool) -> MLE<()> { 28#[derive(Debug, Clone)]
27 29pub struct ProjectInfo {
30 pub mod_id: String,
31 pub slug: String,
32 pub title: String,
33 pub current_version: Option<Version>,
34 pub applicable_versions: Vec<String>,
35 pub download_link: String,
36}
37
38//TODO impl specific version... Rewrite yay
39pub async fn mods_add(config: Cfg, ids: Vec<IDSelector>, list: List, direct_download: bool, set_version: bool) -> MLE<()> {
28 println!("Add mods to {}", list.id); 40 println!("Add mods to {}", list.id);
29 println!(" └Add mods:"); 41 println!(" └Add mods:");
30 let projects = if mod_id.len() == 1 { 42
31 vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] 43 let mut mod_ids: Vec<String> = Vec::new();
32 } else { 44 let mut ver_ids: Vec<String> = Vec::new();
33 projects(String::from(&config.apis.modrinth), mod_id).await 45
34 }; 46 //"Sort" project ids from version ids to be able to handle them differently but in a batch
47 for id in ids {
48 match id {
49 IDSelector::ModificationID(pid) => mod_ids.push(pid),
50 IDSelector::VersionID(vid) => ver_ids.push(vid),
51 }
52 }
35 53
36 let mut downloadstack: Vec<Version> = Vec::new(); 54 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
55 if !mod_ids.is_empty() { projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) };
56 if !ver_ids.is_empty() { projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) };
57
58 if projectinfo.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")) };
37 59
38 for project in projects { 60 let mut downloadstack: Vec<Version> = Vec::new();
61
62 //Adding each mod to the lists and downloadstack
63 for project in projectinfo {
64 let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { project.current_version.clone().unwrap().id };
65
66 match userlist_insert(config.clone(), &list.id, &project.mod_id, &current_version_id, project.clone().applicable_versions, &project.download_link, set_version) {
67 Err(e) => {
68 let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id);
69 if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) }
70 },
71 Ok(..) => { Ok(..) },
72 }?;
73
74 match mods_insert(config.clone(), &project.mod_id, &project.slug, &project.title) {
75 Err(e) => {
76 if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) }
77 },
78 Ok(..) => Ok(..),
79 }?;
80
81 if project.current_version.is_some() { downloadstack.push(project.current_version.unwrap()) };
82 }
83
84 //Download all the added mods
85 if direct_download {
86 download_versions(list.clone(), config.clone(), downloadstack).await?;
87 };
88
89
90 // let projects = if id.len() == 1 {
91 // vec![project(String::from(&config.apis.modrinth), &mod_id).await]
92 // } else {
93 // projects(String::from(&config.apis.modrinth), &mod_id).await
94 // };
95//
96// let mut downloadstack: Vec<Version> = Vec::new();
97//
98// for project in projects {
99// println!("\t└{}", project.title);
100// println!("\t └Get versions");
101// let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await;
102//
103// let mut available_versions_vec: Vec<String> = Vec::new();
104// let current_version: Option<Version>;
105// let current_version_id: String;
106// let file: String;
107// if !available_versions.is_empty() {
108// let current_id = extract_current_version(available_versions.clone())?;
109// println!("\t └Current version: {}", current_id);
110//
111// current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap());
112//
113// current_version_id = current_version.clone().unwrap().id;
114//
115// file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url;
116// for ver in available_versions {
117// available_versions_vec.push(ver.id);
118// };
119// } else {
120// println!("\t └There's currently no mod version for your specified target");
121// current_version = None;
122// current_version_id = String::from("NONE");
123// file = String::from("NONE");
124// available_versions_vec.push(String::from("NONE"));
125// }
126//
127// match userlist_insert(config.clone(), &list.id, &project.id, &current_version_id, available_versions_vec, &file, set_version) {
128// Err(e) => {
129// let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id);
130// if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) }
131// },
132// Ok(..) => { Ok(..) },
133// }?;
134//
135// match mods_insert(config.clone(), &project.id, &project.slug, &project.title) {
136// Err(e) => {
137// if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) }
138// },
139// Ok(..) => Ok(..),
140// }?;
141//
142// downloadstack.push(current_version.unwrap());
143// };
144//
145// //Download all the added mods
146// if direct_download {
147// download_versions(list.clone(), config.clone(), downloadstack).await?;
148// };
149//
150 Ok(())
151}
152
153async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec<ProjectInfo>> {
154 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
155
156 //Get required information from mod_ids
157 let m_projects = match mod_ids.len() {
158 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await],
159 2.. => projects(&config.apis.modrinth, mod_ids).await,
160 _ => panic!("PANIC"),
161 };
162 for project in m_projects {
39 println!("\t└{}", project.title); 163 println!("\t└{}", project.title);
40 println!("\t └Get versions"); 164 println!("\t └Get versions");
41 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; 165 let available_versions = versions(&config.apis.modrinth, String::from(&project.id), list.clone()).await;
42 166
43 let mut available_versions_vec: Vec<String> = Vec::new(); 167 let mut available_versions_vec: Vec<String> = Vec::new();
44 let current_version: Option<Version>; 168 let current_version: Option<Version>;
45 let current_version_id: String;
46 let file: String; 169 let file: String;
47 if !available_versions.is_empty() { 170 if !available_versions.is_empty() {
48 let current_id = extract_current_version(available_versions.clone())?; 171 let current_id = extract_current_version(available_versions.clone())?;
@@ -50,49 +173,55 @@ pub async fn mods_add(config: Cfg, mod_id: Vec<String>, list: List, direct_downl
50 173
51 current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); 174 current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap());
52 175
53 current_version_id = current_version.clone().unwrap().id;
54
55 file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; 176 file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url;
56 for ver in available_versions { 177 for ver in available_versions {
57 available_versions_vec.push(ver.id); 178 available_versions_vec.push(ver.id);
58 }; 179 };
180
181 projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version, applicable_versions: available_versions_vec, download_link: file })
182
59 } else { 183 } else {
60 println!("\t └There's currently no mod version for your specified target"); 184 println!("\t └There's currently no mod version for your specified target");
61 current_version = None; 185 current_version = None;
62 current_version_id = String::from("NONE");
63 file = String::from("NONE"); 186 file = String::from("NONE");
64 available_versions_vec.push(String::from("NONE")); 187 available_versions_vec.push(String::from("NONE"));
188 projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version, applicable_versions: available_versions_vec, download_link: file })
65 } 189 }
190 };
66 191
67 match userlist_insert(config.clone(), &list.id, &project.id, &current_version_id, available_versions_vec, &file, set_version) { 192 Ok(projectinfo)
68 Err(e) => { 193}
69 let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id);
70 if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) }
71 },
72 Ok(..) => { Ok(..) },
73 }?;
74
75 match mods_insert(config.clone(), &project.id, &project.slug, &project.title) {
76 Err(e) => {
77 if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) }
78 },
79 Ok(..) => Ok(..),
80 }?;
81 194
82 downloadstack.push(current_version.unwrap()); 195async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> {
196 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
197
198 //Get required information from ver_ids
199 let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await;
200 let mut v_mod_ids: Vec<String> = Vec::new();
201 for ver in v_versions.clone() {
202 v_mod_ids.push(ver.project_id);
83 }; 203 };
84 204 let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await;
85 //Download all the added mods 205 v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id));
86 if direct_download { 206 v_projects.sort_by(|a, b| a.id.cmp(&b.id));
87 download_versions(list.clone(), config.clone(), downloadstack).await?; 207
208 for (i, project) in v_projects.into_iter().enumerate() {
209 let version = &v_versions[i];
210 println!("\t└{}({})", project.title, version.id);
211 let file = version.clone().files.into_iter().find(|f| f.primary).unwrap().url;
212 projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version: Some(version.clone()), applicable_versions: vec![String::from(&version.id)], download_link: file })
88 }; 213 };
89 214 Ok(projectinfo)
90 Ok(())
91} 215}
92 216
93fn remove(config: Cfg, input: Input) -> MLE<()> { 217fn remove(config: Cfg, input: Input) -> MLE<()> {
94 218
95 let mod_id = mods_get_id(&config.data, input.mod_id.as_ref().unwrap())?; 219 let id = match input.clone().mod_id.unwrap() {
220 IDSelector::ModificationID(id) => id,
221 IDSelector::VersionID(..) => return Err(MLError::new(ErrorType::ArgumentError, "NO_MOD_ID")),
222 };
223
224 let mod_id = mods_get_id(&config.data, &id)?;
96 225
97 let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; 226 let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?;
98 227