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/commands/download.rs | 44 +++++------- src/commands/io.rs | 29 ++++---- src/commands/list.rs | 38 ++++------ src/commands/modification.rs | 63 ++++++++--------- src/commands/update.rs | 160 ++++++++++++++++++++----------------------- 5 files changed, 144 insertions(+), 190 deletions(-) (limited to 'src/commands') 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 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::apis::modrinth::get_raw_versions; +use crate::errors::Error; use crate::{config::Cfg, List}; use crate::{ db::userlist_get_all_current_versions_with_mods, - error::{EType, MLErr, MLE}, + errors::MLE, files::{ clean_list_dir, delete_version, disable_version, download_versions, get_downloaded_versions, @@ -20,20 +21,22 @@ pub async fn download( delete_old: bool, ) -> MLE<()> { let mp = MultiProgress::new(); - let download_p = mp.add(ProgressBar::new( - liststack - .len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, - )); + let download_p = mp.add(ProgressBar::new(liststack.len().try_into()?)); download_p.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), ); for current_list in liststack { - download_list(config, mp.clone(), download_p.clone(), current_list, clean, delete_old).await?; + download_list( + config, + mp.clone(), + download_p.clone(), + current_list, + clean, + delete_old, + ) + .await?; } download_p.finish_with_message("Downloaded all lists"); @@ -52,13 +55,8 @@ async fn download_list( download_p.set_message(format!("Download in {}", current_list.id)); let downloaded_versions = get_downloaded_versions(¤t_list)?; - let current_version_ids = match userlist_get_all_current_versions_with_mods( - config, - ¤t_list.id, - ) { - Ok(i) => Ok(i), - Err(e) => Err(MLErr::new(EType::DBError, e.to_string().as_str())), - }?; + let current_version_ids = + userlist_get_all_current_versions_with_mods(config, ¤t_list.id)?; let mut to_download: Vec = vec![]; //(mod_id, version_id) @@ -74,7 +72,7 @@ async fn download_list( to_download.push(current_version); } else { let downloaded_version = - current_download.ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; + current_download.ok_or(Error::NoDownload)?; if ¤t_version != downloaded_version { to_disable .push((mod_id.clone(), String::from(downloaded_version))); @@ -106,16 +104,10 @@ async fn download_list( if !to_disable.is_empty() { let d_p = mp.insert_before( &download_p, - ProgressBar::new( - to_disable - .len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, - ), + ProgressBar::new(to_disable.len().try_into()?), ); d_p.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), ); 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; use std::io::prelude::*; use crate::{ - config::Cfg, data::modification::{AddMod, IDSelector}, db::{ + config::Cfg, + data::modification::{AddMod, IDSelector}, + db::{ lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, userlist_get_current_version, userlist_get_set_version, - }, error::{EType, MLErr, MLE}, mod_add, List, Modloader, STYLE_OPERATION + }, + errors::{ConversionError, Error, MLE}, + mod_add, List, Modloader, STYLE_OPERATION, }; #[derive(Debug, Serialize, Deserialize)] @@ -67,22 +71,13 @@ impl ExportList { /// # Errors pub fn export(config: &Cfg, list: Option) -> MLE<()> { let progress = ProgressBar::new_spinner(); - progress.set_style( - ProgressStyle::with_template(STYLE_OPERATION) - .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, - ); + progress.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); let mut list_ids: Vec = vec![]; if list.is_none() { list_ids = lists_get_all_ids(config)?; } else { - list_ids.push( - lists_get( - config, - &list.ok_or(MLErr::new(EType::Other, "nolist"))?, - )? - .id, - ); + list_ids.push(lists_get(config, &list.ok_or(Error::ListNotFound)?)?.id); } let mut lists: Vec = vec![]; @@ -95,11 +90,11 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { let toml = toml::to_string(&Export { lists })?; let filestr = dirs::home_dir() - .ok_or(MLErr::new(EType::Other, "no home"))? + .ok_or(Error::SysDirNotFound("home".to_string()))? .join("mlexport.toml") .into_os_string() .into_string() - .map_err(|_| MLErr::new(EType::IoError, "No String"))?; + .map_err(|_| ConversionError::InvalidPath)?; progress.set_message("Create file"); let mut file = File::create(&filestr)?; @@ -124,10 +119,10 @@ pub async fn import( let list = List { id: exportlist.id, mc_version: exportlist.mc_version, - modloader: Modloader::from(&exportlist.launcher)?, + modloader: Modloader::try_from(exportlist.launcher.as_str())?, download_folder: exportlist .download_folder - .ok_or(MLErr::new(EType::Other, "NO_DL"))?, + .ok_or(Error::NoDownloadFolder)?, }; lists_insert( 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 @@ use indicatif::{ProgressBar, ProgressStyle}; use crate::{ - config::Cfg, data::modloader::Modloader, db::{ - config_change_current_list, lists_get, - lists_get_all_ids, lists_insert, lists_remove, lists_version, - }, error::{EType, MLErr, MLE}, update, STYLE_OPERATION + config::Cfg, + data::modloader::Modloader, + db::{ + config_change_current_list, lists_get, lists_get_all_ids, lists_insert, + lists_remove, lists_version, + }, + errors::{Error, MLE}, + update, STYLE_OPERATION, }; /// # Errors @@ -16,11 +20,7 @@ pub fn add( directory: &str, ) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!("Create {id}")); lists_insert(config, id, mc_version, modloader, directory)?; p.finish_with_message(format!("Created {id}")); @@ -30,15 +30,11 @@ pub fn add( /// # Errors pub fn change(config: &Cfg, id: &str) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!("Change default list to {id}")); if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { - return Err(MLErr::new(EType::ArgumentError, "List not found")); + return Err(Error::ListNotFound); }; config_change_current_list(config, id)?; @@ -49,11 +45,7 @@ pub fn change(config: &Cfg, id: &str) -> MLE<()> { /// # Errors pub fn remove(config: &Cfg, id: &str) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!("Remove {id}")); lists_remove(config, id)?; p.finish_with_message(format!("Removed {id}")); @@ -75,11 +67,7 @@ pub async fn version( delete: bool, ) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!( "Change version for list {id} to minecraft version: {mc_version}" )); 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; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ - apis::modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, config::Cfg, data::{modification::{AddMod, IDSelector}, project::ProjectInfo}, db::{ + apis::modrinth::{ + extract_current_version, get_raw_versions, project, projects, versions, + Version, + }, + config::Cfg, + data::{ + modification::{AddMod, IDSelector}, + projectinfo::ProjectInfo, + }, + db::{ lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, mods_remove, userlist_get_all_ids, userlist_get_current_version, userlist_insert, userlist_remove, - }, error::{EType, MLErr, MLE}, files::{delete_version, download_versions}, List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION + }, + errors::{Error, MLE}, + files::{delete_version, download_versions}, + List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; /// # Errors @@ -22,14 +34,9 @@ pub async fn mod_add( let mut mod_ids: Vec<(String, bool)> = Vec::new(); let mut ver_ids: Vec<(String, bool)> = Vec::new(); - let add_p = mp.add(ProgressBar::new( - mods.len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?, - )); + let add_p = mp.add(ProgressBar::new(mods.len().try_into()?)); add_p.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), ); add_p.set_message("Sort ids"); @@ -57,7 +64,7 @@ pub async fn mod_add( }; if projectinfo.is_empty() { - return Err(MLErr::new(EType::ArgumentError, "NO_IDS?")); + return Err(Error::NoProjectInfo); }; add_p.set_message("Add mods to database"); @@ -65,29 +72,18 @@ pub async fn mod_add( let mut downloadstack: Vec = Vec::new(); //Adding each mod to the lists and downloadstack - let project_p = mp.insert_before( - &add_p, - ProgressBar::new( - projectinfo - .len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "infolen"))?, - ), - ); + let project_p = mp + .insert_before(&add_p, ProgressBar::new(projectinfo.len().try_into()?)); project_p.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), ); for project in projectinfo { add_project(config, &project_p, &project, &list)?; if project.current_version.is_some() { - downloadstack.push( - project - .current_version - .ok_or(MLErr::new(EType::Other, "cur_ver"))?, - ); + downloadstack + .push(project.current_version.ok_or(Error::NoCurrentVersion)?); }; } @@ -125,7 +121,7 @@ fn add_project( project .current_version .clone() - .ok_or(MLErr::new(EType::Other, "cur_ver"))? + .ok_or(Error::NoCurrentVersion)? .id }; @@ -142,9 +138,9 @@ fn add_project( let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); if e.to_string() == expected_err { - Err(MLErr::new(EType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) + Err(Error::ModAlreadyOnList) } else { - Err(e) + Err(e)? } } Ok(..) => Ok(..), @@ -212,7 +208,7 @@ async fn get_mod_infos( download_link: file, set_version: *setmap .get(&project.id) - .ok_or(MLErr::new(EType::Other, "not in setmap"))?, + .ok_or(Error::VersionSetNotSet)?, }); } else { let current_id = @@ -312,10 +308,7 @@ async fn get_ver_info( /// # Errors pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { let progress = ProgressBar::new_spinner(); - progress.set_style( - ProgressStyle::with_template(STYLE_OPERATION) - .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, - ); + progress.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); let mod_id = mods_get_id(&config.data, id)?; @@ -351,7 +344,7 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { if err.to_string() == "Database: NO_MODS_USERLIST" { return Ok(()); }; - return Err(err); + return Err(err)?; } }; 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 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ - apis::modrinth::{extract_current_version, versions, Version}, config::Cfg, data::list::List, db::{ + apis::modrinth::{extract_current_version, versions, Version}, + config::Cfg, + data::list::List, + db::{ mods_get_info, userlist_change_versions, userlist_get_all_ids, userlist_get_applicable_versions, userlist_get_current_version, userlist_get_set_version, - }, error::{EType, MLErr, MLE}, files::{ + }, + errors::{Error, MLE}, + files::{ clean_list_dir, delete_version, disable_version, download_versions, - }, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION + }, + PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; /// # Errors @@ -20,15 +26,9 @@ pub async fn update( ) -> MLE<()> { let mp = MultiProgress::new(); - let update_p = mp.add(ProgressBar::new( - liststack - .len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, - )); + let update_p = mp.add(ProgressBar::new(liststack.len().try_into()?)); update_p.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), ); @@ -36,26 +36,15 @@ pub async fn update( update_p.set_message(format!("Update {}", current_list.id)); let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); - list_p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + list_p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); list_p.set_message("Update mods"); let mods = userlist_get_all_ids(config, ¤t_list.id)?; - let list_u_p = mp.insert_before( - &list_p, - ProgressBar::new( - mods.len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, - ), - ); + let list_u_p = + mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into()?)); list_u_p.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), ); @@ -63,7 +52,16 @@ pub async fn update( let mut updatestack: Vec = vec![]; for id in mods { - update_mod(config, id, list_u_p.clone(), ¤t_list, &mut updatestack, &mut current_versions, clean).await?; + update_mod( + config, + id, + list_u_p.clone(), + ¤t_list, + &mut updatestack, + &mut current_versions, + clean, + ) + .await?; } list_u_p.finish_with_message(format!( @@ -91,12 +89,11 @@ pub async fn update( let d_p = mp.insert_before( &list_p, ProgressBar::new( - current_versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, + current_versions.len().try_into()?, ), ); d_p.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), ); for ver in current_versions { @@ -129,45 +126,40 @@ pub async fn update( Ok(()) } -async fn update_mod(config: &Cfg, id: String, list_u_p: ProgressBar, current_list: &List, updatestack: &mut Vec, current_versions: &mut Vec<(String, String)>, clean: bool) -> MLE<()> { - let info = mods_get_info(config, &id)?; - list_u_p.set_message(format!("Update {}", info.title)); +async fn update_mod( + config: &Cfg, + id: String, + list_u_p: ProgressBar, + current_list: &List, + updatestack: &mut Vec, + current_versions: &mut Vec<(String, String)>, + clean: bool, +) -> MLE<()> { + let info = mods_get_info(config, &id)?; + list_u_p.set_message(format!("Update {}", info.title)); - //Skip check if version is set - if userlist_get_set_version(config, ¤t_list.id, &id)? { - list_u_p.inc(1); - return Ok(()); - } + //Skip check if version is set + if userlist_get_set_version(config, ¤t_list.id, &id)? { + list_u_p.inc(1); + return Ok(()); + } - //Getting current installed version for disable or delete - let disable_version = - userlist_get_current_version(config, ¤t_list.id, &id)?; - - updatestack.push( - match specific_update( - config, - clean, - current_list.clone(), - &id, - &list_u_p, - ) - .await - { - Ok(ver) => { - current_versions.push((disable_version, id.to_string())); - ver - } - Err(e) => { - if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { - } else { - return Err(e); - }; - list_u_p.inc(1); - return Ok(()); - } - }, - ); - list_u_p.inc(1); + //Getting current installed version for disable or delete + let disable_version = + userlist_get_current_version(config, ¤t_list.id, &id)?; + + let version = specific_update( + config, + clean, + current_list.clone(), + &id, + &list_u_p, + ).await?; + if let Some(v) = version { + updatestack.push(v); + current_versions.push((disable_version, id.to_string())); + } + list_u_p.inc(1); Ok(()) } @@ -178,7 +170,7 @@ async fn specific_update( list: List, id: &str, progress: &ProgressBar, -) -> MLE { +) -> MLE> { let applicable_versions = versions(&config.apis.modrinth, String::from(id), list.clone()).await?; @@ -192,14 +184,12 @@ async fn specific_update( } } - let mut current: Vec = vec![]; - if clean - || (versions.join("|") - != userlist_get_applicable_versions( + let mut current: Option = None; + if clean || (versions.join("|") != userlist_get_applicable_versions( config, &list.id, String::from(id), - )?) + )?) { let current_str = extract_current_version(applicable_versions.clone())?; @@ -211,15 +201,10 @@ async fn specific_update( } //get new versions - let current_ver = match applicable_versions - .into_iter() - .find(|ver| ver.id == current_str) - .ok_or("!no current version in applicable_versions") - { - Ok(v) => Ok(v), - Err(e) => Err(MLErr::new(EType::Other, e)), - }?; - current.push(current_ver.clone()); + let Some(current_ver) = applicable_versions.into_iter().find(|ver| ver.id == current_str) else { + return Err(Error::NoCurrentVersion); + }; + current = Some(current_ver.clone()); let files = ¤t_ver.files; @@ -237,11 +222,12 @@ async fn specific_update( link, id.to_string(), )?; - } - - if current.is_empty() { - return Err(MLErr::new(EType::ModError, "NO_UPDATE_AVAILABLE")); }; - Ok(current[0].clone()) + if current.is_none() { + // No Update Available + Ok(None) + } else { + Ok(current) + } } -- cgit v1.2.3