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/files.rs | |
parent | 11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff) | |
download | modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.tar.gz modlist-ecc4743fdec43eb578e9c35bb008c68909f1517e.zip |
better error handlingrefactor
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/src/files.rs b/src/files.rs index 98785fd..2a6e949 100644 --- a/src/files.rs +++ b/src/files.rs | |||
@@ -10,7 +10,13 @@ use std::{ | |||
10 | use tokio::task::JoinSet; | 10 | use tokio::task::JoinSet; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | apis::modrinth::Version, cache::{copy_cached_version, get_cached_versions}, config::Cfg, data::list::List, db::{mods_get_info, userlist_add_disabled_versions}, error::{EType, MLErr, MLE}, PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER | 13 | apis::modrinth::Version, |
14 | cache::{copy_cached_version, get_cached_versions}, | ||
15 | config::Cfg, | ||
16 | data::list::List, | ||
17 | db::{mods_get_info, userlist_add_disabled_versions}, | ||
18 | errors::{ConversionError, Error, MLE}, | ||
19 | PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER, | ||
14 | }; | 20 | }; |
15 | 21 | ||
16 | /// # Errors | 22 | /// # Errors |
@@ -25,15 +31,14 @@ pub async fn download_versions( | |||
25 | 31 | ||
26 | let mut js = JoinSet::new(); | 32 | let mut js = JoinSet::new(); |
27 | 33 | ||
28 | let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?; | 34 | let style_spinner = ProgressStyle::with_template(STYLE_SPINNER)?; |
29 | 35 | ||
30 | let all = progress.insert_before( | 36 | let all = progress.insert_before( |
31 | progress_before, | 37 | progress_before, |
32 | ProgressBar::new(versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?), | 38 | ProgressBar::new(versions.len().try_into()?), |
33 | ); | 39 | ); |
34 | all.set_style( | 40 | all.set_style( |
35 | ProgressStyle::with_template(STYLE_BAR_POS) | 41 | ProgressStyle::with_template(STYLE_BAR_POS)? |
36 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
37 | .progress_chars(PROGRESS_CHARS), | 42 | .progress_chars(PROGRESS_CHARS), |
38 | ); | 43 | ); |
39 | all.set_message(format!("✓Downloading {}", list.id)); | 44 | all.set_message(format!("✓Downloading {}", list.id)); |
@@ -87,8 +92,8 @@ async fn download_version( | |||
87 | None => files[0].clone(), | 92 | None => files[0].clone(), |
88 | }; | 93 | }; |
89 | let mut splitname: Vec<&str> = file.filename.split('.').collect(); | 94 | let mut splitname: Vec<&str> = file.filename.split('.').collect(); |
90 | let Ok(extension) = splitname.pop().ok_or("") else { | 95 | let Some(extension) = splitname.pop() else { |
91 | return Err(MLErr::new(EType::Other, "NO_FILE_EXTENSION")) | 96 | return Err(Error::NoFileExtension); |
92 | }; | 97 | }; |
93 | let filename = format!( | 98 | let filename = format!( |
94 | "{}.mr.{}.{}.{}", | 99 | "{}.mr.{}.{}.{}", |
@@ -169,7 +174,12 @@ pub fn disable_version( | |||
169 | 174 | ||
170 | rename(file, disabled)?; | 175 | rename(file, disabled)?; |
171 | 176 | ||
172 | userlist_add_disabled_versions(config, ¤t_list.id, versionid, mod_id)?; | 177 | userlist_add_disabled_versions( |
178 | config, | ||
179 | ¤t_list.id, | ||
180 | versionid, | ||
181 | mod_id, | ||
182 | )?; | ||
173 | 183 | ||
174 | Ok(()) | 184 | Ok(()) |
175 | } | 185 | } |
@@ -189,8 +199,8 @@ pub fn get_file_path(list: &List, versionid: &str) -> MLE<String> { | |||
189 | for file in read_dir(&list.download_folder)? { | 199 | for file in read_dir(&list.download_folder)? { |
190 | let path = file?.path(); | 200 | let path = file?.path(); |
191 | if path.is_file() { | 201 | if path.is_file() { |
192 | let Ok(pathstr) = path.to_str().ok_or("") else { | 202 | let Some(pathstr) = path.to_str() else { |
193 | return Err(MLErr::new(EType::Other, "INVALID_PATH")) | 203 | return Err(Error::PathNotFound); |
194 | }; | 204 | }; |
195 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | 205 | let namesplit: Vec<&str> = pathstr.split('.').collect(); |
196 | let ver_id = namesplit[namesplit.len() - 2]; | 206 | let ver_id = namesplit[namesplit.len() - 2]; |
@@ -198,11 +208,8 @@ pub fn get_file_path(list: &List, versionid: &str) -> MLE<String> { | |||
198 | } | 208 | } |
199 | } | 209 | } |
200 | 210 | ||
201 | let Ok(filename) = names.get(versionid).ok_or("") else { | 211 | let Some(filename) = names.get(versionid) else { |
202 | return Err(MLErr::new( | 212 | return Err(Error::PathNotFound); |
203 | EType::ArgumentError, | ||
204 | "VERSION_NOT_FOUND_IN_FILES", | ||
205 | )) | ||
206 | }; | 213 | }; |
207 | 214 | ||
208 | Ok(filename.to_owned()) | 215 | Ok(filename.to_owned()) |
@@ -213,13 +220,16 @@ pub fn get_downloaded_versions(list: &List) -> MLE<HashMap<String, String>> { | |||
213 | let mut versions: HashMap<String, String> = HashMap::new(); | 220 | let mut versions: HashMap<String, String> = HashMap::new(); |
214 | for file in read_dir(&list.download_folder)? { | 221 | for file in read_dir(&list.download_folder)? { |
215 | let path = file?.path(); | 222 | let path = file?.path(); |
216 | if path.is_file() | 223 | if path.is_file() { |
217 | && path | 224 | let Some(extension) = path.extension() else { |
218 | .extension() | 225 | return Err(Error::NoFileExtension); |
219 | .ok_or(MLErr::new(EType::IoError, "extension"))? | 226 | }; |
220 | == "jar" | 227 | if extension != "jar" { |
221 | { | 228 | continue; |
222 | let pathstr = path.to_str().ok_or(MLErr::new(EType::IoError, "path_to_str"))?; | 229 | } |
230 | let Some(pathstr) = path.to_str() else { | ||
231 | return Err(ConversionError::InvalidPath)?; | ||
232 | }; | ||
223 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | 233 | let namesplit: Vec<&str> = pathstr.split('.').collect(); |
224 | versions.insert( | 234 | versions.insert( |
225 | String::from(namesplit[namesplit.len() - 3]), | 235 | String::from(namesplit[namesplit.len() - 3]), |