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.rs57
1 files changed, 33 insertions, 24 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index e0e54b2..6e6213f 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,3 +1,5 @@
1#![allow(clippy::too_many_lines)]
2
1use std::collections::HashMap; 3use std::collections::HashMap;
2 4
3use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 5use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
@@ -9,7 +11,7 @@ use crate::{
9 mods_remove, userlist_get_all_ids, userlist_get_current_version, 11 mods_remove, userlist_get_all_ids, userlist_get_current_version,
10 userlist_insert, userlist_remove, 12 userlist_insert, userlist_remove,
11 }, 13 },
12 error::{ErrorType, MLError, MLE}, 14 error::{EType, MLErr, MLE},
13 files::{delete_version, download_versions}, 15 files::{delete_version, download_versions},
14 modrinth::{ 16 modrinth::{
15 extract_current_version, get_raw_versions, project, projects, versions, 17 extract_current_version, get_raw_versions, project, projects, versions,
@@ -41,6 +43,8 @@ pub struct ProjectInfo {
41 pub set_version: bool, 43 pub set_version: bool,
42} 44}
43 45
46/// # Errors
47/// # Panics
44pub async fn mod_add( 48pub async fn mod_add(
45 config: &Cfg, 49 config: &Cfg,
46 mods: Vec<AddMod>, 50 mods: Vec<AddMod>,
@@ -52,10 +56,11 @@ pub async fn mod_add(
52 let mut mod_ids: Vec<(String, bool)> = Vec::new(); 56 let mut mod_ids: Vec<(String, bool)> = Vec::new();
53 let mut ver_ids: Vec<(String, bool)> = Vec::new(); 57 let mut ver_ids: Vec<(String, bool)> = Vec::new();
54 58
55 let add_p = mp.add(ProgressBar::new(mods.len().try_into().unwrap())); 59 let add_p = mp.add(ProgressBar::new(mods.len().try_into().map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?));
56 add_p.set_style( 60 add_p.set_style(
57 ProgressStyle::with_template(STYLE_BAR_POS) 61 ProgressStyle::with_template(STYLE_BAR_POS).map_err(|_| {
58 .unwrap() 62 MLErr::new(EType::LibIndicatif, "template error")
63 })?
59 .progress_chars(PROGRESS_CHARS), 64 .progress_chars(PROGRESS_CHARS),
60 ); 65 );
61 add_p.set_message("Sort ids"); 66 add_p.set_message("Sort ids");
@@ -83,7 +88,7 @@ pub async fn mod_add(
83 }; 88 };
84 89
85 if projectinfo.is_empty() { 90 if projectinfo.is_empty() {
86 return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); 91 return Err(MLErr::new(EType::ArgumentError, "NO_IDS?"));
87 }; 92 };
88 93
89 add_p.set_message("Add mods to database"); 94 add_p.set_message("Add mods to database");
@@ -115,7 +120,7 @@ pub async fn mod_add(
115 &list.id, 120 &list.id,
116 &project.mod_id, 121 &project.mod_id,
117 &current_version_id, 122 &current_version_id,
118 project.clone().applicable_versions, 123 &project.applicable_versions,
119 &project.download_link, 124 &project.download_link,
120 project.set_version, 125 project.set_version,
121 ) { 126 ) {
@@ -125,8 +130,8 @@ pub async fn mod_add(
125 list.id 130 list.id
126 ); 131 );
127 if e.to_string() == expected_err { 132 if e.to_string() == expected_err {
128 Err(MLError::new( 133 Err(MLErr::new(
129 ErrorType::ModError, 134 EType::ModError,
130 "MOD_ALREADY_ON_SELECTED_LIST", 135 "MOD_ALREADY_ON_SELECTED_LIST",
131 )) 136 ))
132 } else { 137 } else {
@@ -212,7 +217,20 @@ async fn get_mod_infos(
212 let mut available_versions_vec: Vec<String> = Vec::new(); 217 let mut available_versions_vec: Vec<String> = Vec::new();
213 let current_version: Option<Version>; 218 let current_version: Option<Version>;
214 let file: String; 219 let file: String;
215 if !available_versions.is_empty() { 220 if available_versions.is_empty() {
221 current_version = None;
222 file = String::from("NONE");
223 available_versions_vec.push(String::from("NONE"));
224 projectinfo.push(ProjectInfo {
225 mod_id: String::from(&project.id),
226 slug: project.slug,
227 title: project.title,
228 current_version,
229 applicable_versions: available_versions_vec,
230 download_link: file,
231 set_version: *setmap.get(&project.id).unwrap(),
232 });
233 } else {
216 let current_id = 234 let current_id =
217 extract_current_version(available_versions.clone())?; 235 extract_current_version(available_versions.clone())?;
218 236
@@ -246,19 +264,6 @@ async fn get_mod_infos(
246 download_link: file, 264 download_link: file,
247 set_version: *setmap.get(&project.slug).unwrap(), 265 set_version: *setmap.get(&project.slug).unwrap(),
248 }); 266 });
249 } else {
250 current_version = None;
251 file = String::from("NONE");
252 available_versions_vec.push(String::from("NONE"));
253 projectinfo.push(ProjectInfo {
254 mod_id: String::from(&project.id),
255 slug: project.slug,
256 title: project.title,
257 current_version,
258 applicable_versions: available_versions_vec,
259 download_link: file,
260 set_version: *setmap.get(&project.id).unwrap(),
261 });
262 } 267 }
263 } 268 }
264 269
@@ -319,9 +324,13 @@ async fn get_ver_info(
319/// * `config` - config struct 324/// * `config` - config struct
320/// * `id` - name, slug or id of the mod 325/// * `id` - name, slug or id of the mod
321/// * `list` - List struct 326/// * `list` - List struct
327///
328/// # Errors
322pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { 329pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> {
323 let progress = ProgressBar::new_spinner(); 330 let progress = ProgressBar::new_spinner();
324 progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); 331 progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| {
332 MLErr::new(EType::LibIndicatif, "template error")
333 })?);
325 334
326 let mod_id = mods_get_id(&config.data, id)?; 335 let mod_id = mods_get_id(&config.data, id)?;
327 336
@@ -334,7 +343,7 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> {
334 userlist_remove(config, &list.id, &mod_id)?; 343 userlist_remove(config, &list.id, &mod_id)?;
335 344
336 progress.set_message("Delete file"); 345 progress.set_message("Delete file");
337 match delete_version(list, version) { 346 match delete_version(list, &version) {
338 Ok(()) => (), 347 Ok(()) => (),
339 Err(err) => { 348 Err(err) => {
340 if err.to_string() 349 if err.to_string()