From 29635b9e3833296b2c908914104ba7165d22d3d5 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 12:03:13 +0200 Subject: remove `# Panics` and fix clippy --- src/commands/download.rs | 48 +++++++++++++++++++-------------------- src/commands/io.rs | 3 +-- src/commands/modification.rs | 54 +++++++++++++++++++++++++++++--------------- src/commands/update.rs | 22 +++++++++++------- 4 files changed, 75 insertions(+), 52 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 7321832..7af1066 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -15,7 +15,6 @@ use crate::{ use crate::{PROGRESS_CHARS, STYLE_BAR_POS}; /// # Errors -/// # Panics pub async fn download( config: &Cfg, liststack: Vec, @@ -23,29 +22,31 @@ pub async fn download( delete_old: bool, ) -> MLE<()> { let mp = MultiProgress::new(); - let download_p = - mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); + let download_p = mp.add(ProgressBar::new( + liststack + .len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, + )); download_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); for current_list in liststack { download_p.set_message(format!("Download in {}", current_list.id)); - let downloaded_versions = - get_downloaded_versions(¤t_list)?; + 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(), - )), + Err(e) => { + Err(MLErr::new(EType::DBError, e.to_string().as_str())) + } }?; let mut to_download: Vec = vec![]; @@ -62,8 +63,7 @@ pub async fn download( to_download.push(current_version); } else { let downloaded_version = current_download - .ok_or("SOMETHING_HAS_REALLY_GONE_WRONG") - .unwrap(); + .ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; if ¤t_version != downloaded_version { to_disable.push(( mod_id.clone(), @@ -87,7 +87,7 @@ pub async fn download( download_versions( current_list.clone(), config.clone(), - get_raw_versions(&config.apis.modrinth, to_download).await, + get_raw_versions(&config.apis.modrinth, to_download).await?, &mp, &download_p, ) @@ -95,13 +95,18 @@ pub async fn download( } if !to_disable.is_empty() { - let d_p = mp.insert_before( - &download_p, - ProgressBar::new(to_disable.len().try_into().unwrap()), - ); + let d_p = + mp.insert_before( + &download_p, + ProgressBar::new(to_disable.len().try_into().map_err( + |_| MLErr::new(EType::Other, "ListStackLen"), + )?), + ); d_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| { + MLErr::new(EType::LibIndicatif, "template error") + })? .progress_chars(PROGRESS_CHARS), ); for ver in to_disable { @@ -112,12 +117,7 @@ pub async fn download( } else { d_p.set_message(format!("Disable version {}", ver.1)); d_p.inc(1); - disable_version( - config, - ¤t_list, - ver.1, - ver.0, - )?; + disable_version(config, ¤t_list, ver.1, ver.0)?; }; } diff --git a/src/commands/io.rs b/src/commands/io.rs index c9691c4..3e171f1 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -68,7 +68,6 @@ impl ExportList { } /// # Errors -/// # Panics pub fn export(config: &Cfg, list: Option) -> MLE<()> { let progress = ProgressBar::new_spinner(); progress.set_style( @@ -103,7 +102,7 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { .join("mlexport.toml") .into_os_string() .into_string() - .unwrap(); + .map_err(|_| MLErr::new(EType::IoError, "No String"))?; progress.set_message("Create file"); let mut file = File::create(&filestr)?; diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 6e6213f..aa1174a 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -44,7 +44,6 @@ pub struct ProjectInfo { } /// # Errors -/// # Panics pub async fn mod_add( config: &Cfg, mods: Vec, @@ -56,11 +55,14 @@ 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() + .map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?, + )); add_p.set_style( - ProgressStyle::with_template(STYLE_BAR_POS).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })? + ProgressStyle::with_template(STYLE_BAR_POS) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); add_p.set_message("Sort ids"); @@ -98,11 +100,16 @@ pub async fn mod_add( //Adding each mod to the lists and downloadstack let project_p = mp.insert_before( &add_p, - ProgressBar::new(projectinfo.len().try_into().unwrap()), + ProgressBar::new( + projectinfo + .len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "infolen"))?, + ), ); project_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); @@ -112,7 +119,11 @@ pub async fn mod_add( let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { - project.current_version.clone().unwrap().id + project + .current_version + .clone() + .ok_or(MLErr::new(EType::Other, "cur_ver"))? + .id }; match userlist_insert( @@ -158,7 +169,11 @@ pub async fn mod_add( }?; if project.current_version.is_some() { - downloadstack.push(project.current_version.unwrap()); + downloadstack.push( + project + .current_version + .ok_or(MLErr::new(EType::Other, "cur_ver"))?, + ); }; project_p.inc(1); @@ -202,8 +217,8 @@ async fn get_mod_infos( //Get required information from mod_ids let m_projects = match ids.len() { - 1 => vec![project(&config.apis.modrinth, &ids[0]).await], - 2.. => projects(&config.apis.modrinth, ids).await, + 1 => vec![project(&config.apis.modrinth, &ids[0]).await?], + 2.. => projects(&config.apis.modrinth, ids).await?, _ => panic!("PANIC"), }; for project in m_projects { @@ -212,7 +227,7 @@ async fn get_mod_infos( String::from(&project.id), list.clone(), ) - .await; + .await?; let mut available_versions_vec: Vec = Vec::new(); let current_version: Option; @@ -228,7 +243,9 @@ async fn get_mod_infos( current_version, applicable_versions: available_versions_vec, download_link: file, - set_version: *setmap.get(&project.id).unwrap(), + set_version: *setmap + .get(&project.id) + .ok_or(MLErr::new(EType::Other, "not in setmap"))?, }); } else { let current_id = @@ -285,12 +302,12 @@ async fn get_ver_info( let mut projectinfo: Vec = Vec::new(); //Get required information from ver_ids - let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await; + let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await?; let mut v_mod_ids: Vec = Vec::new(); for ver in v_versions.clone() { v_mod_ids.push(ver.project_id); } - let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await; + let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await?; v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id)); v_projects.sort_by(|a, b| a.id.cmp(&b.id)); @@ -328,9 +345,10 @@ 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) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); let mod_id = mods_get_id(&config.data, id)?; diff --git a/src/commands/update.rs b/src/commands/update.rs index d0b930d..c7965e3 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -18,7 +18,6 @@ use crate::{ }; /// # Errors -/// # Panics pub async fn update( config: &Cfg, liststack: Vec, @@ -44,19 +43,26 @@ 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).unwrap()); + list_p.set_style( + ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { + MLErr::new(EType::LibIndicatif, "template error") + })?, + ); 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().unwrap()), + ProgressBar::new( + mods.len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, + ), ); list_u_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); @@ -129,12 +135,12 @@ pub async fn update( let d_p = mp.insert_before( &list_p, ProgressBar::new( - current_versions.len().try_into().unwrap(), + current_versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, ), ); d_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); for ver in current_versions { @@ -175,7 +181,7 @@ async fn specific_update( progress: &ProgressBar, ) -> MLE { let applicable_versions = - versions(&config.apis.modrinth, String::from(id), list.clone()).await; + versions(&config.apis.modrinth, String::from(id), list.clone()).await?; let mut versions: Vec = vec![]; -- cgit v1.2.3