From ecc4743fdec43eb578e9c35bb008c68909f1517e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 17:32:19 +0200 Subject: better error handling --- src/files.rs | 54 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-) (limited to 'src/files.rs') 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::{ use tokio::task::JoinSet; use crate::{ - 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 + apis::modrinth::Version, + cache::{copy_cached_version, get_cached_versions}, + config::Cfg, + data::list::List, + db::{mods_get_info, userlist_add_disabled_versions}, + errors::{ConversionError, Error, MLE}, + PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER, }; /// # Errors @@ -25,15 +31,14 @@ pub async fn download_versions( let mut js = JoinSet::new(); - let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?; + let style_spinner = ProgressStyle::with_template(STYLE_SPINNER)?; let all = progress.insert_before( progress_before, - ProgressBar::new(versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?), + ProgressBar::new(versions.len().try_into()?), ); all.set_style( - ProgressStyle::with_template(STYLE_BAR_POS) - .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? + ProgressStyle::with_template(STYLE_BAR_POS)? .progress_chars(PROGRESS_CHARS), ); all.set_message(format!("âś“Downloading {}", list.id)); @@ -87,8 +92,8 @@ async fn download_version( None => files[0].clone(), }; let mut splitname: Vec<&str> = file.filename.split('.').collect(); - let Ok(extension) = splitname.pop().ok_or("") else { - return Err(MLErr::new(EType::Other, "NO_FILE_EXTENSION")) + let Some(extension) = splitname.pop() else { + return Err(Error::NoFileExtension); }; let filename = format!( "{}.mr.{}.{}.{}", @@ -169,7 +174,12 @@ pub fn disable_version( rename(file, disabled)?; - userlist_add_disabled_versions(config, ¤t_list.id, versionid, mod_id)?; + userlist_add_disabled_versions( + config, + ¤t_list.id, + versionid, + mod_id, + )?; Ok(()) } @@ -189,8 +199,8 @@ pub fn get_file_path(list: &List, versionid: &str) -> MLE { for file in read_dir(&list.download_folder)? { let path = file?.path(); if path.is_file() { - let Ok(pathstr) = path.to_str().ok_or("") else { - return Err(MLErr::new(EType::Other, "INVALID_PATH")) + let Some(pathstr) = path.to_str() else { + return Err(Error::PathNotFound); }; let namesplit: Vec<&str> = pathstr.split('.').collect(); let ver_id = namesplit[namesplit.len() - 2]; @@ -198,11 +208,8 @@ pub fn get_file_path(list: &List, versionid: &str) -> MLE { } } - let Ok(filename) = names.get(versionid).ok_or("") else { - return Err(MLErr::new( - EType::ArgumentError, - "VERSION_NOT_FOUND_IN_FILES", - )) + let Some(filename) = names.get(versionid) else { + return Err(Error::PathNotFound); }; Ok(filename.to_owned()) @@ -213,13 +220,16 @@ pub fn get_downloaded_versions(list: &List) -> MLE> { let mut versions: HashMap = HashMap::new(); for file in read_dir(&list.download_folder)? { let path = file?.path(); - if path.is_file() - && path - .extension() - .ok_or(MLErr::new(EType::IoError, "extension"))? - == "jar" - { - let pathstr = path.to_str().ok_or(MLErr::new(EType::IoError, "path_to_str"))?; + if path.is_file() { + let Some(extension) = path.extension() else { + return Err(Error::NoFileExtension); + }; + if extension != "jar" { + continue; + } + let Some(pathstr) = path.to_str() else { + return Err(ConversionError::InvalidPath)?; + }; let namesplit: Vec<&str> = pathstr.split('.').collect(); versions.insert( String::from(namesplit[namesplit.len() - 3]), -- cgit v1.2.3