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 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) (limited to 'src/commands/download.rs') 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 { -- cgit v1.2.3