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/update.rs | 160 ++++++++++++++++++++++--------------------------- 1 file changed, 73 insertions(+), 87 deletions(-) (limited to 'src/commands/update.rs') 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