summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
committerfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
commitecc4743fdec43eb578e9c35bb008c68909f1517e (patch)
tree73916114bc2bff8c72f759f5aae11a95d4dede22 /src/commands/modification.rs
parent11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff)
downloadmodlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar
modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar.gz
modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.zip
better error handlingrefactor
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r--src/commands/modification.rs63
1 files changed, 28 insertions, 35 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index d20f575..27ba098 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -3,11 +3,23 @@ use std::collections::HashMap;
3use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 3use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
4 4
5use crate::{ 5use crate::{
6 apis::modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, config::Cfg, data::{modification::{AddMod, IDSelector}, project::ProjectInfo}, db::{ 6 apis::modrinth::{
7 extract_current_version, get_raw_versions, project, projects, versions,
8 Version,
9 },
10 config::Cfg,
11 data::{
12 modification::{AddMod, IDSelector},
13 projectinfo::ProjectInfo,
14 },
15 db::{
7 lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, 16 lists_get_all_ids, mods_get_id, mods_get_info, mods_insert,
8 mods_remove, userlist_get_all_ids, userlist_get_current_version, 17 mods_remove, userlist_get_all_ids, userlist_get_current_version,
9 userlist_insert, userlist_remove, 18 userlist_insert, userlist_remove,
10 }, error::{EType, MLErr, MLE}, files::{delete_version, download_versions}, List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION 19 },
20 errors::{Error, MLE},
21 files::{delete_version, download_versions},
22 List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION,
11}; 23};
12 24
13/// # Errors 25/// # Errors
@@ -22,14 +34,9 @@ pub async fn mod_add(
22 let mut mod_ids: Vec<(String, bool)> = Vec::new(); 34 let mut mod_ids: Vec<(String, bool)> = Vec::new();
23 let mut ver_ids: Vec<(String, bool)> = Vec::new(); 35 let mut ver_ids: Vec<(String, bool)> = Vec::new();
24 36
25 let add_p = mp.add(ProgressBar::new( 37 let add_p = mp.add(ProgressBar::new(mods.len().try_into()?));
26 mods.len()
27 .try_into()
28 .map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?,
29 ));
30 add_p.set_style( 38 add_p.set_style(
31 ProgressStyle::with_template(STYLE_BAR_POS) 39 ProgressStyle::with_template(STYLE_BAR_POS)?
32 .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
33 .progress_chars(PROGRESS_CHARS), 40 .progress_chars(PROGRESS_CHARS),
34 ); 41 );
35 add_p.set_message("Sort ids"); 42 add_p.set_message("Sort ids");
@@ -57,7 +64,7 @@ pub async fn mod_add(
57 }; 64 };
58 65
59 if projectinfo.is_empty() { 66 if projectinfo.is_empty() {
60 return Err(MLErr::new(EType::ArgumentError, "NO_IDS?")); 67 return Err(Error::NoProjectInfo);
61 }; 68 };
62 69
63 add_p.set_message("Add mods to database"); 70 add_p.set_message("Add mods to database");
@@ -65,29 +72,18 @@ pub async fn mod_add(
65 let mut downloadstack: Vec<Version> = Vec::new(); 72 let mut downloadstack: Vec<Version> = Vec::new();
66 73
67 //Adding each mod to the lists and downloadstack 74 //Adding each mod to the lists and downloadstack
68 let project_p = mp.insert_before( 75 let project_p = mp
69 &add_p, 76 .insert_before(&add_p, ProgressBar::new(projectinfo.len().try_into()?));
70 ProgressBar::new(
71 projectinfo
72 .len()
73 .try_into()
74 .map_err(|_| MLErr::new(EType::Other, "infolen"))?,
75 ),
76 );
77 project_p.set_style( 77 project_p.set_style(
78 ProgressStyle::with_template(STYLE_BAR_POS) 78 ProgressStyle::with_template(STYLE_BAR_POS)?
79 .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
80 .progress_chars(PROGRESS_CHARS), 79 .progress_chars(PROGRESS_CHARS),
81 ); 80 );
82 81
83 for project in projectinfo { 82 for project in projectinfo {
84 add_project(config, &project_p, &project, &list)?; 83 add_project(config, &project_p, &project, &list)?;
85 if project.current_version.is_some() { 84 if project.current_version.is_some() {
86 downloadstack.push( 85 downloadstack
87 project 86 .push(project.current_version.ok_or(Error::NoCurrentVersion)?);
88 .current_version
89 .ok_or(MLErr::new(EType::Other, "cur_ver"))?,
90 );
91 }; 87 };
92 } 88 }
93 89
@@ -125,7 +121,7 @@ fn add_project(
125 project 121 project
126 .current_version 122 .current_version
127 .clone() 123 .clone()
128 .ok_or(MLErr::new(EType::Other, "cur_ver"))? 124 .ok_or(Error::NoCurrentVersion)?
129 .id 125 .id
130 }; 126 };
131 127
@@ -142,9 +138,9 @@ fn add_project(
142 let expected_err = 138 let expected_err =
143 format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); 139 format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id);
144 if e.to_string() == expected_err { 140 if e.to_string() == expected_err {
145 Err(MLErr::new(EType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) 141 Err(Error::ModAlreadyOnList)
146 } else { 142 } else {
147 Err(e) 143 Err(e)?
148 } 144 }
149 } 145 }
150 Ok(..) => Ok(..), 146 Ok(..) => Ok(..),
@@ -212,7 +208,7 @@ async fn get_mod_infos(
212 download_link: file, 208 download_link: file,
213 set_version: *setmap 209 set_version: *setmap
214 .get(&project.id) 210 .get(&project.id)
215 .ok_or(MLErr::new(EType::Other, "not in setmap"))?, 211 .ok_or(Error::VersionSetNotSet)?,
216 }); 212 });
217 } else { 213 } else {
218 let current_id = 214 let current_id =
@@ -312,10 +308,7 @@ async fn get_ver_info(
312/// # Errors 308/// # Errors
313pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { 309pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> {
314 let progress = ProgressBar::new_spinner(); 310 let progress = ProgressBar::new_spinner();
315 progress.set_style( 311 progress.set_style(ProgressStyle::with_template(STYLE_OPERATION)?);
316 ProgressStyle::with_template(STYLE_OPERATION)
317 .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?,
318 );
319 312
320 let mod_id = mods_get_id(&config.data, id)?; 313 let mod_id = mods_get_id(&config.data, id)?;
321 314
@@ -351,7 +344,7 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> {
351 if err.to_string() == "Database: NO_MODS_USERLIST" { 344 if err.to_string() == "Database: NO_MODS_USERLIST" {
352 return Ok(()); 345 return Ok(());
353 }; 346 };
354 return Err(err); 347 return Err(err)?;
355 } 348 }
356 }; 349 };
357 if mods.contains(&mod_id) { 350 if mods.contains(&mod_id) {