diff options
author | fxqnlr <[email protected]> | 2024-09-04 17:32:19 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-04 17:32:19 +0200 |
commit | ecc4743fdec43eb578e9c35bb008c68909f1517e (patch) | |
tree | 73916114bc2bff8c72f759f5aae11a95d4dede22 /src/commands | |
parent | 11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff) | |
download | modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar.gz modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.zip |
better error handlingrefactor
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/download.rs | 44 | ||||
-rw-r--r-- | src/commands/io.rs | 29 | ||||
-rw-r--r-- | src/commands/list.rs | 38 | ||||
-rw-r--r-- | src/commands/modification.rs | 63 | ||||
-rw-r--r-- | src/commands/update.rs | 160 |
5 files changed, 144 insertions, 190 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs index 269d5d3..bb946b0 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 1 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
2 | 2 | ||
3 | use crate::apis::modrinth::get_raw_versions; | 3 | use crate::apis::modrinth::get_raw_versions; |
4 | use crate::errors::Error; | ||
4 | use crate::{config::Cfg, List}; | 5 | use crate::{config::Cfg, List}; |
5 | use crate::{ | 6 | use crate::{ |
6 | db::userlist_get_all_current_versions_with_mods, | 7 | db::userlist_get_all_current_versions_with_mods, |
7 | error::{EType, MLErr, MLE}, | 8 | errors::MLE, |
8 | files::{ | 9 | files::{ |
9 | clean_list_dir, delete_version, disable_version, download_versions, | 10 | clean_list_dir, delete_version, disable_version, download_versions, |
10 | get_downloaded_versions, | 11 | get_downloaded_versions, |
@@ -20,20 +21,22 @@ pub async fn download( | |||
20 | delete_old: bool, | 21 | delete_old: bool, |
21 | ) -> MLE<()> { | 22 | ) -> MLE<()> { |
22 | let mp = MultiProgress::new(); | 23 | let mp = MultiProgress::new(); |
23 | let download_p = mp.add(ProgressBar::new( | 24 | let download_p = mp.add(ProgressBar::new(liststack.len().try_into()?)); |
24 | liststack | ||
25 | .len() | ||
26 | .try_into() | ||
27 | .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, | ||
28 | )); | ||
29 | download_p.set_style( | 25 | download_p.set_style( |
30 | ProgressStyle::with_template(STYLE_BAR_POS) | 26 | ProgressStyle::with_template(STYLE_BAR_POS)? |
31 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
32 | .progress_chars(PROGRESS_CHARS), | 27 | .progress_chars(PROGRESS_CHARS), |
33 | ); | 28 | ); |
34 | 29 | ||
35 | for current_list in liststack { | 30 | for current_list in liststack { |
36 | download_list(config, mp.clone(), download_p.clone(), current_list, clean, delete_old).await?; | 31 | download_list( |
32 | config, | ||
33 | mp.clone(), | ||
34 | download_p.clone(), | ||
35 | current_list, | ||
36 | clean, | ||
37 | delete_old, | ||
38 | ) | ||
39 | .await?; | ||
37 | } | 40 | } |
38 | 41 | ||
39 | download_p.finish_with_message("Downloaded all lists"); | 42 | download_p.finish_with_message("Downloaded all lists"); |
@@ -52,13 +55,8 @@ async fn download_list( | |||
52 | download_p.set_message(format!("Download in {}", current_list.id)); | 55 | download_p.set_message(format!("Download in {}", current_list.id)); |
53 | 56 | ||
54 | let downloaded_versions = get_downloaded_versions(¤t_list)?; | 57 | let downloaded_versions = get_downloaded_versions(¤t_list)?; |
55 | let current_version_ids = match userlist_get_all_current_versions_with_mods( | 58 | let current_version_ids = |
56 | config, | 59 | userlist_get_all_current_versions_with_mods(config, ¤t_list.id)?; |
57 | ¤t_list.id, | ||
58 | ) { | ||
59 | Ok(i) => Ok(i), | ||
60 | Err(e) => Err(MLErr::new(EType::DBError, e.to_string().as_str())), | ||
61 | }?; | ||
62 | 60 | ||
63 | let mut to_download: Vec<String> = vec![]; | 61 | let mut to_download: Vec<String> = vec![]; |
64 | //(mod_id, version_id) | 62 | //(mod_id, version_id) |
@@ -74,7 +72,7 @@ async fn download_list( | |||
74 | to_download.push(current_version); | 72 | to_download.push(current_version); |
75 | } else { | 73 | } else { |
76 | let downloaded_version = | 74 | let downloaded_version = |
77 | current_download.ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; | 75 | current_download.ok_or(Error::NoDownload)?; |
78 | if ¤t_version != downloaded_version { | 76 | if ¤t_version != downloaded_version { |
79 | to_disable | 77 | to_disable |
80 | .push((mod_id.clone(), String::from(downloaded_version))); | 78 | .push((mod_id.clone(), String::from(downloaded_version))); |
@@ -106,16 +104,10 @@ async fn download_list( | |||
106 | if !to_disable.is_empty() { | 104 | if !to_disable.is_empty() { |
107 | let d_p = mp.insert_before( | 105 | let d_p = mp.insert_before( |
108 | &download_p, | 106 | &download_p, |
109 | ProgressBar::new( | 107 | ProgressBar::new(to_disable.len().try_into()?), |
110 | to_disable | ||
111 | .len() | ||
112 | .try_into() | ||
113 | .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, | ||
114 | ), | ||
115 | ); | 108 | ); |
116 | d_p.set_style( | 109 | d_p.set_style( |
117 | ProgressStyle::with_template(STYLE_BAR_POS) | 110 | ProgressStyle::with_template(STYLE_BAR_POS)? |
118 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
119 | .progress_chars(PROGRESS_CHARS), | 111 | .progress_chars(PROGRESS_CHARS), |
120 | ); | 112 | ); |
121 | for ver in to_disable { | 113 | for ver in to_disable { |
diff --git a/src/commands/io.rs b/src/commands/io.rs index dea0d84..80bc7d6 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs | |||
@@ -4,10 +4,14 @@ use std::fs::File; | |||
4 | use std::io::prelude::*; | 4 | use std::io::prelude::*; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | config::Cfg, data::modification::{AddMod, IDSelector}, db::{ | 7 | config::Cfg, |
8 | data::modification::{AddMod, IDSelector}, | ||
9 | db::{ | ||
8 | lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, | 10 | lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, |
9 | userlist_get_current_version, userlist_get_set_version, | 11 | userlist_get_current_version, userlist_get_set_version, |
10 | }, error::{EType, MLErr, MLE}, mod_add, List, Modloader, STYLE_OPERATION | 12 | }, |
13 | errors::{ConversionError, Error, MLE}, | ||
14 | mod_add, List, Modloader, STYLE_OPERATION, | ||
11 | }; | 15 | }; |
12 | 16 | ||
13 | #[derive(Debug, Serialize, Deserialize)] | 17 | #[derive(Debug, Serialize, Deserialize)] |
@@ -67,22 +71,13 @@ impl ExportList { | |||
67 | /// # Errors | 71 | /// # Errors |
68 | pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { | 72 | pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { |
69 | let progress = ProgressBar::new_spinner(); | 73 | let progress = ProgressBar::new_spinner(); |
70 | progress.set_style( | 74 | progress.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
71 | ProgressStyle::with_template(STYLE_OPERATION) | ||
72 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, | ||
73 | ); | ||
74 | 75 | ||
75 | let mut list_ids: Vec<String> = vec![]; | 76 | let mut list_ids: Vec<String> = vec![]; |
76 | if list.is_none() { | 77 | if list.is_none() { |
77 | list_ids = lists_get_all_ids(config)?; | 78 | list_ids = lists_get_all_ids(config)?; |
78 | } else { | 79 | } else { |
79 | list_ids.push( | 80 | list_ids.push(lists_get(config, &list.ok_or(Error::ListNotFound)?)?.id); |
80 | lists_get( | ||
81 | config, | ||
82 | &list.ok_or(MLErr::new(EType::Other, "nolist"))?, | ||
83 | )? | ||
84 | .id, | ||
85 | ); | ||
86 | } | 81 | } |
87 | 82 | ||
88 | let mut lists: Vec<ExportList> = vec![]; | 83 | let mut lists: Vec<ExportList> = vec![]; |
@@ -95,11 +90,11 @@ pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { | |||
95 | let toml = toml::to_string(&Export { lists })?; | 90 | let toml = toml::to_string(&Export { lists })?; |
96 | 91 | ||
97 | let filestr = dirs::home_dir() | 92 | let filestr = dirs::home_dir() |
98 | .ok_or(MLErr::new(EType::Other, "no home"))? | 93 | .ok_or(Error::SysDirNotFound("home".to_string()))? |
99 | .join("mlexport.toml") | 94 | .join("mlexport.toml") |
100 | .into_os_string() | 95 | .into_os_string() |
101 | .into_string() | 96 | .into_string() |
102 | .map_err(|_| MLErr::new(EType::IoError, "No String"))?; | 97 | .map_err(|_| ConversionError::InvalidPath)?; |
103 | 98 | ||
104 | progress.set_message("Create file"); | 99 | progress.set_message("Create file"); |
105 | let mut file = File::create(&filestr)?; | 100 | let mut file = File::create(&filestr)?; |
@@ -124,10 +119,10 @@ pub async fn import( | |||
124 | let list = List { | 119 | let list = List { |
125 | id: exportlist.id, | 120 | id: exportlist.id, |
126 | mc_version: exportlist.mc_version, | 121 | mc_version: exportlist.mc_version, |
127 | modloader: Modloader::from(&exportlist.launcher)?, | 122 | modloader: Modloader::try_from(exportlist.launcher.as_str())?, |
128 | download_folder: exportlist | 123 | download_folder: exportlist |
129 | .download_folder | 124 | .download_folder |
130 | .ok_or(MLErr::new(EType::Other, "NO_DL"))?, | 125 | .ok_or(Error::NoDownloadFolder)?, |
131 | }; | 126 | }; |
132 | lists_insert( | 127 | lists_insert( |
133 | config, | 128 | config, |
diff --git a/src/commands/list.rs b/src/commands/list.rs index 23a9f0f..db8a831 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs | |||
@@ -1,10 +1,14 @@ | |||
1 | use indicatif::{ProgressBar, ProgressStyle}; | 1 | use indicatif::{ProgressBar, ProgressStyle}; |
2 | 2 | ||
3 | use crate::{ | 3 | use crate::{ |
4 | config::Cfg, data::modloader::Modloader, db::{ | 4 | config::Cfg, |
5 | config_change_current_list, lists_get, | 5 | data::modloader::Modloader, |
6 | lists_get_all_ids, lists_insert, lists_remove, lists_version, | 6 | db::{ |
7 | }, error::{EType, MLErr, MLE}, update, STYLE_OPERATION | 7 | config_change_current_list, lists_get, lists_get_all_ids, lists_insert, |
8 | lists_remove, lists_version, | ||
9 | }, | ||
10 | errors::{Error, MLE}, | ||
11 | update, STYLE_OPERATION, | ||
8 | }; | 12 | }; |
9 | 13 | ||
10 | /// # Errors | 14 | /// # Errors |
@@ -16,11 +20,7 @@ pub fn add( | |||
16 | directory: &str, | 20 | directory: &str, |
17 | ) -> MLE<()> { | 21 | ) -> MLE<()> { |
18 | let p = ProgressBar::new_spinner(); | 22 | let p = ProgressBar::new_spinner(); |
19 | p.set_style( | 23 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
20 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
21 | MLErr::new(EType::LibIndicatif, "template error") | ||
22 | })?, | ||
23 | ); | ||
24 | p.set_message(format!("Create {id}")); | 24 | p.set_message(format!("Create {id}")); |
25 | lists_insert(config, id, mc_version, modloader, directory)?; | 25 | lists_insert(config, id, mc_version, modloader, directory)?; |
26 | p.finish_with_message(format!("Created {id}")); | 26 | p.finish_with_message(format!("Created {id}")); |
@@ -30,15 +30,11 @@ pub fn add( | |||
30 | /// # Errors | 30 | /// # Errors |
31 | pub fn change(config: &Cfg, id: &str) -> MLE<()> { | 31 | pub fn change(config: &Cfg, id: &str) -> MLE<()> { |
32 | let p = ProgressBar::new_spinner(); | 32 | let p = ProgressBar::new_spinner(); |
33 | p.set_style( | 33 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
34 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
35 | MLErr::new(EType::LibIndicatif, "template error") | ||
36 | })?, | ||
37 | ); | ||
38 | p.set_message(format!("Change default list to {id}")); | 34 | p.set_message(format!("Change default list to {id}")); |
39 | 35 | ||
40 | if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { | 36 | if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { |
41 | return Err(MLErr::new(EType::ArgumentError, "List not found")); | 37 | return Err(Error::ListNotFound); |
42 | }; | 38 | }; |
43 | config_change_current_list(config, id)?; | 39 | config_change_current_list(config, id)?; |
44 | 40 | ||
@@ -49,11 +45,7 @@ pub fn change(config: &Cfg, id: &str) -> MLE<()> { | |||
49 | /// # Errors | 45 | /// # Errors |
50 | pub fn remove(config: &Cfg, id: &str) -> MLE<()> { | 46 | pub fn remove(config: &Cfg, id: &str) -> MLE<()> { |
51 | let p = ProgressBar::new_spinner(); | 47 | let p = ProgressBar::new_spinner(); |
52 | p.set_style( | 48 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
53 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
54 | MLErr::new(EType::LibIndicatif, "template error") | ||
55 | })?, | ||
56 | ); | ||
57 | p.set_message(format!("Remove {id}")); | 49 | p.set_message(format!("Remove {id}")); |
58 | lists_remove(config, id)?; | 50 | lists_remove(config, id)?; |
59 | p.finish_with_message(format!("Removed {id}")); | 51 | p.finish_with_message(format!("Removed {id}")); |
@@ -75,11 +67,7 @@ pub async fn version( | |||
75 | delete: bool, | 67 | delete: bool, |
76 | ) -> MLE<()> { | 68 | ) -> MLE<()> { |
77 | let p = ProgressBar::new_spinner(); | 69 | let p = ProgressBar::new_spinner(); |
78 | p.set_style( | 70 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
79 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
80 | MLErr::new(EType::LibIndicatif, "template error") | ||
81 | })?, | ||
82 | ); | ||
83 | p.set_message(format!( | 71 | p.set_message(format!( |
84 | "Change version for list {id} to minecraft version: {mc_version}" | 72 | "Change version for list {id} to minecraft version: {mc_version}" |
85 | )); | 73 | )); |
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; | |||
3 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 3 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
4 | 4 | ||
5 | use crate::{ | 5 | use 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 |
313 | pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { | 309 | pub 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) { |
diff --git a/src/commands/update.rs b/src/commands/update.rs index 721ced5..92ea9d6 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -1,13 +1,19 @@ | |||
1 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 1 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
2 | 2 | ||
3 | use crate::{ | 3 | use crate::{ |
4 | apis::modrinth::{extract_current_version, versions, Version}, config::Cfg, data::list::List, db::{ | 4 | apis::modrinth::{extract_current_version, versions, Version}, |
5 | config::Cfg, | ||
6 | data::list::List, | ||
7 | db::{ | ||
5 | mods_get_info, userlist_change_versions, userlist_get_all_ids, | 8 | mods_get_info, userlist_change_versions, userlist_get_all_ids, |
6 | userlist_get_applicable_versions, userlist_get_current_version, | 9 | userlist_get_applicable_versions, userlist_get_current_version, |
7 | userlist_get_set_version, | 10 | userlist_get_set_version, |
8 | }, error::{EType, MLErr, MLE}, files::{ | 11 | }, |
12 | errors::{Error, MLE}, | ||
13 | files::{ | ||
9 | clean_list_dir, delete_version, disable_version, download_versions, | 14 | clean_list_dir, delete_version, disable_version, download_versions, |
10 | }, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION | 15 | }, |
16 | PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, | ||
11 | }; | 17 | }; |
12 | 18 | ||
13 | /// # Errors | 19 | /// # Errors |
@@ -20,15 +26,9 @@ pub async fn update( | |||
20 | ) -> MLE<()> { | 26 | ) -> MLE<()> { |
21 | let mp = MultiProgress::new(); | 27 | let mp = MultiProgress::new(); |
22 | 28 | ||
23 | let update_p = mp.add(ProgressBar::new( | 29 | let update_p = mp.add(ProgressBar::new(liststack.len().try_into()?)); |
24 | liststack | ||
25 | .len() | ||
26 | .try_into() | ||
27 | .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, | ||
28 | )); | ||
29 | update_p.set_style( | 30 | update_p.set_style( |
30 | ProgressStyle::with_template(STYLE_BAR_POS) | 31 | ProgressStyle::with_template(STYLE_BAR_POS)? |
31 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
32 | .progress_chars(PROGRESS_CHARS), | 32 | .progress_chars(PROGRESS_CHARS), |
33 | ); | 33 | ); |
34 | 34 | ||
@@ -36,26 +36,15 @@ pub async fn update( | |||
36 | update_p.set_message(format!("Update {}", current_list.id)); | 36 | update_p.set_message(format!("Update {}", current_list.id)); |
37 | 37 | ||
38 | let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); | 38 | let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); |
39 | list_p.set_style( | 39 | list_p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
40 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
41 | MLErr::new(EType::LibIndicatif, "template error") | ||
42 | })?, | ||
43 | ); | ||
44 | list_p.set_message("Update mods"); | 40 | list_p.set_message("Update mods"); |
45 | 41 | ||
46 | let mods = userlist_get_all_ids(config, ¤t_list.id)?; | 42 | let mods = userlist_get_all_ids(config, ¤t_list.id)?; |
47 | 43 | ||
48 | let list_u_p = mp.insert_before( | 44 | let list_u_p = |
49 | &list_p, | 45 | mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into()?)); |
50 | ProgressBar::new( | ||
51 | mods.len() | ||
52 | .try_into() | ||
53 | .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, | ||
54 | ), | ||
55 | ); | ||
56 | list_u_p.set_style( | 46 | list_u_p.set_style( |
57 | ProgressStyle::with_template(STYLE_BAR_POS) | 47 | ProgressStyle::with_template(STYLE_BAR_POS)? |
58 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
59 | .progress_chars(PROGRESS_CHARS), | 48 | .progress_chars(PROGRESS_CHARS), |
60 | ); | 49 | ); |
61 | 50 | ||
@@ -63,7 +52,16 @@ pub async fn update( | |||
63 | let mut updatestack: Vec<Version> = vec![]; | 52 | let mut updatestack: Vec<Version> = vec![]; |
64 | 53 | ||
65 | for id in mods { | 54 | for id in mods { |
66 | update_mod(config, id, list_u_p.clone(), ¤t_list, &mut updatestack, &mut current_versions, clean).await?; | 55 | update_mod( |
56 | config, | ||
57 | id, | ||
58 | list_u_p.clone(), | ||
59 | ¤t_list, | ||
60 | &mut updatestack, | ||
61 | &mut current_versions, | ||
62 | clean, | ||
63 | ) | ||
64 | .await?; | ||
67 | } | 65 | } |
68 | 66 | ||
69 | list_u_p.finish_with_message(format!( | 67 | list_u_p.finish_with_message(format!( |
@@ -91,12 +89,11 @@ pub async fn update( | |||
91 | let d_p = mp.insert_before( | 89 | let d_p = mp.insert_before( |
92 | &list_p, | 90 | &list_p, |
93 | ProgressBar::new( | 91 | ProgressBar::new( |
94 | current_versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, | 92 | current_versions.len().try_into()?, |
95 | ), | 93 | ), |
96 | ); | 94 | ); |
97 | d_p.set_style( | 95 | d_p.set_style( |
98 | ProgressStyle::with_template(STYLE_BAR_POS) | 96 | ProgressStyle::with_template(STYLE_BAR_POS)? |
99 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
100 | .progress_chars(PROGRESS_CHARS), | 97 | .progress_chars(PROGRESS_CHARS), |
101 | ); | 98 | ); |
102 | for ver in current_versions { | 99 | for ver in current_versions { |
@@ -129,45 +126,40 @@ pub async fn update( | |||
129 | Ok(()) | 126 | Ok(()) |
130 | } | 127 | } |
131 | 128 | ||
132 | async fn update_mod(config: &Cfg, id: String, list_u_p: ProgressBar, current_list: &List, updatestack: &mut Vec<Version>, current_versions: &mut Vec<(String, String)>, clean: bool) -> MLE<()> { | 129 | async fn update_mod( |
133 | let info = mods_get_info(config, &id)?; | 130 | config: &Cfg, |
134 | list_u_p.set_message(format!("Update {}", info.title)); | 131 | id: String, |
132 | list_u_p: ProgressBar, | ||
133 | current_list: &List, | ||
134 | updatestack: &mut Vec<Version>, | ||
135 | current_versions: &mut Vec<(String, String)>, | ||
136 | clean: bool, | ||
137 | ) -> MLE<()> { | ||
138 | let info = mods_get_info(config, &id)?; | ||
139 | list_u_p.set_message(format!("Update {}", info.title)); | ||
135 | 140 | ||
136 | //Skip check if version is set | 141 | //Skip check if version is set |
137 | if userlist_get_set_version(config, ¤t_list.id, &id)? { | 142 | if userlist_get_set_version(config, ¤t_list.id, &id)? { |
138 | list_u_p.inc(1); | 143 | list_u_p.inc(1); |
139 | return Ok(()); | 144 | return Ok(()); |
140 | } | 145 | } |
141 | 146 | ||
142 | //Getting current installed version for disable or delete | 147 | //Getting current installed version for disable or delete |
143 | let disable_version = | 148 | let disable_version = |
144 | userlist_get_current_version(config, ¤t_list.id, &id)?; | 149 | userlist_get_current_version(config, ¤t_list.id, &id)?; |
145 | 150 | ||
146 | updatestack.push( | 151 | let version = specific_update( |
147 | match specific_update( | 152 | config, |
148 | config, | 153 | clean, |
149 | clean, | 154 | current_list.clone(), |
150 | current_list.clone(), | 155 | &id, |
151 | &id, | 156 | &list_u_p, |
152 | &list_u_p, | 157 | ).await?; |
153 | ) | 158 | if let Some(v) = version { |
154 | .await | 159 | updatestack.push(v); |
155 | { | 160 | current_versions.push((disable_version, id.to_string())); |
156 | Ok(ver) => { | 161 | } |
157 | current_versions.push((disable_version, id.to_string())); | 162 | list_u_p.inc(1); |
158 | ver | ||
159 | } | ||
160 | Err(e) => { | ||
161 | if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { | ||
162 | } else { | ||
163 | return Err(e); | ||
164 | }; | ||
165 | list_u_p.inc(1); | ||
166 | return Ok(()); | ||
167 | } | ||
168 | }, | ||
169 | ); | ||
170 | list_u_p.inc(1); | ||
171 | 163 | ||
172 | Ok(()) | 164 | Ok(()) |
173 | } | 165 | } |
@@ -178,7 +170,7 @@ async fn specific_update( | |||
178 | list: List, | 170 | list: List, |
179 | id: &str, | 171 | id: &str, |
180 | progress: &ProgressBar, | 172 | progress: &ProgressBar, |
181 | ) -> MLE<Version> { | 173 | ) -> MLE<Option<Version>> { |
182 | let applicable_versions = | 174 | let applicable_versions = |
183 | versions(&config.apis.modrinth, String::from(id), list.clone()).await?; | 175 | versions(&config.apis.modrinth, String::from(id), list.clone()).await?; |
184 | 176 | ||
@@ -192,14 +184,12 @@ async fn specific_update( | |||
192 | } | 184 | } |
193 | } | 185 | } |
194 | 186 | ||
195 | let mut current: Vec<Version> = vec![]; | 187 | let mut current: Option<Version> = None; |
196 | if clean | 188 | if clean || (versions.join("|") != userlist_get_applicable_versions( |
197 | || (versions.join("|") | ||
198 | != userlist_get_applicable_versions( | ||
199 | config, | 189 | config, |
200 | &list.id, | 190 | &list.id, |
201 | String::from(id), | 191 | String::from(id), |
202 | )?) | 192 | )?) |
203 | { | 193 | { |
204 | let current_str = extract_current_version(applicable_versions.clone())?; | 194 | let current_str = extract_current_version(applicable_versions.clone())?; |
205 | 195 | ||
@@ -211,15 +201,10 @@ async fn specific_update( | |||
211 | } | 201 | } |
212 | 202 | ||
213 | //get new versions | 203 | //get new versions |
214 | let current_ver = match applicable_versions | 204 | let Some(current_ver) = applicable_versions.into_iter().find(|ver| ver.id == current_str) else { |
215 | .into_iter() | 205 | return Err(Error::NoCurrentVersion); |
216 | .find(|ver| ver.id == current_str) | 206 | }; |
217 | .ok_or("!no current version in applicable_versions") | 207 | current = Some(current_ver.clone()); |
218 | { | ||
219 | Ok(v) => Ok(v), | ||
220 | Err(e) => Err(MLErr::new(EType::Other, e)), | ||
221 | }?; | ||
222 | current.push(current_ver.clone()); | ||
223 | 208 | ||
224 | let files = ¤t_ver.files; | 209 | let files = ¤t_ver.files; |
225 | 210 | ||
@@ -237,11 +222,12 @@ async fn specific_update( | |||
237 | link, | 222 | link, |
238 | id.to_string(), | 223 | id.to_string(), |
239 | )?; | 224 | )?; |
240 | } | ||
241 | |||
242 | if current.is_empty() { | ||
243 | return Err(MLErr::new(EType::ModError, "NO_UPDATE_AVAILABLE")); | ||
244 | }; | 225 | }; |
245 | 226 | ||
246 | Ok(current[0].clone()) | 227 | if current.is_none() { |
228 | // No Update Available | ||
229 | Ok(None) | ||
230 | } else { | ||
231 | Ok(current) | ||
232 | } | ||
247 | } | 233 | } |