diff options
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 181 |
1 files changed, 141 insertions, 40 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 454e148..ffc4e10 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,9 +1,19 @@ | |||
1 | use 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}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; | 1 | use crate::{ |
2 | config::Cfg, | ||
3 | db::{ | ||
4 | lists_get_all_ids, mods_get_id, mods_insert, mods_remove, userlist_get_all_ids, | ||
5 | userlist_get_current_version, userlist_insert, userlist_remove, | ||
6 | }, | ||
7 | error::{ErrorType, MLError, MLE}, | ||
8 | files::{delete_version, download_versions}, | ||
9 | modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, | ||
10 | List, | ||
11 | }; | ||
2 | 12 | ||
3 | #[derive(Debug, Clone, PartialEq, Eq)] | 13 | #[derive(Debug, Clone, PartialEq, Eq)] |
4 | pub enum IDSelector { | 14 | pub enum IDSelector { |
5 | ModificationID(String), | 15 | ModificationID(String), |
6 | VersionID(String) | 16 | VersionID(String), |
7 | } | 17 | } |
8 | 18 | ||
9 | #[derive(Debug, Clone)] | 19 | #[derive(Debug, Clone)] |
@@ -16,10 +26,16 @@ pub struct ProjectInfo { | |||
16 | pub download_link: String, | 26 | pub download_link: String, |
17 | } | 27 | } |
18 | 28 | ||
19 | pub async fn mod_add(config: Cfg, ids: Vec<IDSelector>, list: List, direct_download: bool, set_version: bool) -> MLE<()> { | 29 | pub async fn mod_add( |
30 | config: Cfg, | ||
31 | ids: Vec<IDSelector>, | ||
32 | list: List, | ||
33 | direct_download: bool, | ||
34 | set_version: bool, | ||
35 | ) -> MLE<()> { | ||
20 | println!("Add mods to {}", list.id); | 36 | println!("Add mods to {}", list.id); |
21 | println!(" └Add mods:"); | 37 | println!(" └Add mods:"); |
22 | 38 | ||
23 | let mut mod_ids: Vec<String> = Vec::new(); | 39 | let mut mod_ids: Vec<String> = Vec::new(); |
24 | let mut ver_ids: Vec<String> = Vec::new(); | 40 | let mut ver_ids: Vec<String> = Vec::new(); |
25 | 41 | ||
@@ -32,11 +48,17 @@ pub async fn mod_add(config: Cfg, ids: Vec<IDSelector>, list: List, direct_downl | |||
32 | } | 48 | } |
33 | 49 | ||
34 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); | 50 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); |
35 | if !mod_ids.is_empty() { projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) }; | 51 | if !mod_ids.is_empty() { |
36 | if !ver_ids.is_empty() { projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) }; | 52 | projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) |
53 | }; | ||
54 | if !ver_ids.is_empty() { | ||
55 | projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) | ||
56 | }; | ||
57 | |||
58 | if projectinfo.is_empty() { | ||
59 | return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); | ||
60 | }; | ||
37 | 61 | ||
38 | if projectinfo.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")) }; | ||
39 | |||
40 | let mut downloadstack: Vec<Version> = Vec::new(); | 62 | let mut downloadstack: Vec<Version> = Vec::new(); |
41 | 63 | ||
42 | //Adding each mod to the lists and downloadstack | 64 | //Adding each mod to the lists and downloadstack |
@@ -45,29 +67,59 @@ pub async fn mod_add(config: Cfg, ids: Vec<IDSelector>, list: List, direct_downl | |||
45 | } else { | 67 | } else { |
46 | println!(" └Insert mods in list {} and save infos", list.id); | 68 | println!(" └Insert mods in list {} and save infos", list.id); |
47 | } | 69 | } |
48 | 70 | ||
49 | for project in projectinfo { | 71 | for project in projectinfo { |
50 | let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { project.current_version.clone().unwrap().id }; | 72 | let current_version_id = if project.current_version.is_none() { |
51 | match userlist_insert(config.clone(), &list.id, &project.mod_id, ¤t_version_id, project.clone().applicable_versions, &project.download_link, set_version) { | 73 | String::from("NONE") |
74 | } else { | ||
75 | project.current_version.clone().unwrap().id | ||
76 | }; | ||
77 | match userlist_insert( | ||
78 | config.clone(), | ||
79 | &list.id, | ||
80 | &project.mod_id, | ||
81 | ¤t_version_id, | ||
82 | project.clone().applicable_versions, | ||
83 | &project.download_link, | ||
84 | set_version, | ||
85 | ) { | ||
52 | Err(e) => { | 86 | Err(e) => { |
53 | let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); | 87 | let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); |
54 | if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } | 88 | if e.to_string() == expected_err { |
55 | }, | 89 | Err(MLError::new( |
56 | Ok(..) => { Ok(..) }, | 90 | ErrorType::ModError, |
91 | "MOD_ALREADY_ON_SELECTED_LIST", | ||
92 | )) | ||
93 | } else { | ||
94 | Err(e) | ||
95 | } | ||
96 | } | ||
97 | Ok(..) => Ok(..), | ||
57 | }?; | 98 | }?; |
58 | 99 | ||
59 | match mods_insert(config.clone(), &project.mod_id, &project.slug, &project.title) { | 100 | match mods_insert( |
101 | config.clone(), | ||
102 | &project.mod_id, | ||
103 | &project.slug, | ||
104 | &project.title, | ||
105 | ) { | ||
60 | Err(e) => { | 106 | Err(e) => { |
61 | if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } | 107 | if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { |
62 | }, | 108 | Ok(..) |
109 | } else { | ||
110 | Err(e) | ||
111 | } | ||
112 | } | ||
63 | Ok(..) => Ok(..), | 113 | Ok(..) => Ok(..), |
64 | }?; | 114 | }?; |
65 | 115 | ||
66 | if project.current_version.is_some() { downloadstack.push(project.current_version.unwrap()) }; | 116 | if project.current_version.is_some() { |
117 | downloadstack.push(project.current_version.unwrap()) | ||
118 | }; | ||
67 | } | 119 | } |
68 | 120 | ||
69 | //Download all the added mods | 121 | //Download all the added mods |
70 | if direct_download { | 122 | if direct_download { |
71 | download_versions(list.clone(), config.clone(), downloadstack).await?; | 123 | download_versions(list.clone(), config.clone(), downloadstack).await?; |
72 | }; | 124 | }; |
73 | 125 | ||
@@ -86,7 +138,12 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec | |||
86 | for project in m_projects { | 138 | for project in m_projects { |
87 | println!("\t└{}", project.title); | 139 | println!("\t└{}", project.title); |
88 | println!("\t └Get versions"); | 140 | println!("\t └Get versions"); |
89 | let available_versions = versions(&config.apis.modrinth, String::from(&project.id), list.clone()).await; | 141 | let available_versions = versions( |
142 | &config.apis.modrinth, | ||
143 | String::from(&project.id), | ||
144 | list.clone(), | ||
145 | ) | ||
146 | .await; | ||
90 | 147 | ||
91 | let mut available_versions_vec: Vec<String> = Vec::new(); | 148 | let mut available_versions_vec: Vec<String> = Vec::new(); |
92 | let current_version: Option<Version>; | 149 | let current_version: Option<Version>; |
@@ -95,36 +152,63 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec | |||
95 | let current_id = extract_current_version(available_versions.clone())?; | 152 | let current_id = extract_current_version(available_versions.clone())?; |
96 | println!("\t └Current version: {}", current_id); | 153 | println!("\t └Current version: {}", current_id); |
97 | 154 | ||
98 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | 155 | current_version = Some( |
156 | available_versions | ||
157 | .clone() | ||
158 | .into_iter() | ||
159 | .find(|v| v.id == current_id) | ||
160 | .unwrap(), | ||
161 | ); | ||
99 | 162 | ||
100 | file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; | 163 | file = current_version |
164 | .clone() | ||
165 | .ok_or("") | ||
166 | .unwrap() | ||
167 | .files | ||
168 | .into_iter() | ||
169 | .find(|f| f.primary) | ||
170 | .unwrap() | ||
171 | .url; | ||
101 | for ver in available_versions { | 172 | for ver in available_versions { |
102 | available_versions_vec.push(ver.id); | 173 | available_versions_vec.push(ver.id); |
103 | }; | 174 | } |
104 | |||
105 | projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version, applicable_versions: available_versions_vec, download_link: file }) | ||
106 | 175 | ||
176 | projectinfo.push(ProjectInfo { | ||
177 | mod_id: project.id, | ||
178 | slug: project.slug, | ||
179 | title: project.title, | ||
180 | current_version, | ||
181 | applicable_versions: available_versions_vec, | ||
182 | download_link: file, | ||
183 | }) | ||
107 | } else { | 184 | } else { |
108 | println!("\t └There's currently no mod version for your specified target"); | 185 | println!("\t └There's currently no mod version for your specified target"); |
109 | current_version = None; | 186 | current_version = None; |
110 | file = String::from("NONE"); | 187 | file = String::from("NONE"); |
111 | available_versions_vec.push(String::from("NONE")); | 188 | available_versions_vec.push(String::from("NONE")); |
112 | projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version, applicable_versions: available_versions_vec, download_link: file }) | 189 | projectinfo.push(ProjectInfo { |
190 | mod_id: project.id, | ||
191 | slug: project.slug, | ||
192 | title: project.title, | ||
193 | current_version, | ||
194 | applicable_versions: available_versions_vec, | ||
195 | download_link: file, | ||
196 | }) | ||
113 | } | 197 | } |
114 | }; | 198 | } |
115 | 199 | ||
116 | Ok(projectinfo) | 200 | Ok(projectinfo) |
117 | } | 201 | } |
118 | 202 | ||
119 | async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> { | 203 | async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> { |
120 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); | 204 | let mut projectinfo: Vec<ProjectInfo> = Vec::new(); |
121 | 205 | ||
122 | //Get required information from ver_ids | 206 | //Get required information from ver_ids |
123 | let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; | 207 | let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; |
124 | let mut v_mod_ids: Vec<String> = Vec::new(); | 208 | let mut v_mod_ids: Vec<String> = Vec::new(); |
125 | for ver in v_versions.clone() { | 209 | for ver in v_versions.clone() { |
126 | v_mod_ids.push(ver.project_id); | 210 | v_mod_ids.push(ver.project_id); |
127 | }; | 211 | } |
128 | let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await; | 212 | let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await; |
129 | v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id)); | 213 | v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id)); |
130 | v_projects.sort_by(|a, b| a.id.cmp(&b.id)); | 214 | v_projects.sort_by(|a, b| a.id.cmp(&b.id)); |
@@ -132,9 +216,22 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo> | |||
132 | for (i, project) in v_projects.into_iter().enumerate() { | 216 | for (i, project) in v_projects.into_iter().enumerate() { |
133 | let version = &v_versions[i]; | 217 | let version = &v_versions[i]; |
134 | println!("\t└{}({})", project.title, version.id); | 218 | println!("\t└{}({})", project.title, version.id); |
135 | let file = version.clone().files.into_iter().find(|f| f.primary).unwrap().url; | 219 | let file = version |
136 | 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 }) | 220 | .clone() |
137 | }; | 221 | .files |
222 | .into_iter() | ||
223 | .find(|f| f.primary) | ||
224 | .unwrap() | ||
225 | .url; | ||
226 | projectinfo.push(ProjectInfo { | ||
227 | mod_id: project.id, | ||
228 | slug: project.slug, | ||
229 | title: project.title, | ||
230 | current_version: Some(version.clone()), | ||
231 | applicable_versions: vec![String::from(&version.id)], | ||
232 | download_link: file, | ||
233 | }) | ||
234 | } | ||
138 | Ok(projectinfo) | 235 | Ok(projectinfo) |
139 | } | 236 | } |
140 | 237 | ||
@@ -145,24 +242,28 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo> | |||
145 | /// * `id` - name, slug or id of the mod | 242 | /// * `id` - name, slug or id of the mod |
146 | /// * `list` - List struct | 243 | /// * `list` - List struct |
147 | pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { | 244 | pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { |
148 | |||
149 | let mod_id = mods_get_id(&config.data, id)?; | 245 | let mod_id = mods_get_id(&config.data, id)?; |
150 | 246 | ||
151 | let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; | 247 | let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; |
152 | 248 | ||
153 | userlist_remove(config.clone(), &list.id, &mod_id)?; | 249 | userlist_remove(config.clone(), &list.id, &mod_id)?; |
154 | delete_version(list, version)?; | 250 | delete_version(list, version)?; |
155 | 251 | ||
156 | let list_ids = lists_get_all_ids(config.clone())?; | 252 | let list_ids = lists_get_all_ids(config.clone())?; |
157 | 253 | ||
158 | // Remove mod from main list if not used elsewhere | 254 | // Remove mod from main list if not used elsewhere |
159 | let mut mod_used = false; | 255 | let mut mod_used = false; |
160 | for id in list_ids { | 256 | for id in list_ids { |
161 | let mods = userlist_get_all_ids(config.clone(), id)?; | 257 | let mods = userlist_get_all_ids(config.clone(), id)?; |
162 | if mods.contains(&mod_id) { mod_used = true; break; }; | 258 | if mods.contains(&mod_id) { |
163 | }; | 259 | mod_used = true; |
260 | break; | ||
261 | }; | ||
262 | } | ||
164 | 263 | ||
165 | if !mod_used { mods_remove(config, mod_id)?; }; | 264 | if !mod_used { |
265 | mods_remove(config, mod_id)?; | ||
266 | }; | ||
166 | 267 | ||
167 | Ok(()) | 268 | Ok(()) |
168 | } | 269 | } |