From 6a91d0a864f9edd9d9fe50ca89ccbce4fc98e043 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 11:12:04 +0200 Subject: do nearly anything to shut clippy up --- src/commands/download.rs | 30 +++++++++++++---------- src/commands/io.rs | 24 +++++++++++++++---- src/commands/list.rs | 33 +++++++++++++++++++------ src/commands/modification.rs | 57 +++++++++++++++++++++++++------------------- src/commands/update.rs | 39 ++++++++++++++++-------------- 5 files changed, 116 insertions(+), 67 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 3e50c87..7321832 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,9 +1,11 @@ +#![allow(clippy::too_many_lines)] + use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{config::Cfg, List}; use crate::{ db::userlist_get_all_current_versions_with_mods, - error::{ErrorType, MLError, MLE}, + error::{EType, MLErr, MLE}, files::{ clean_list_dir, delete_version, disable_version, download_versions, get_downloaded_versions, @@ -12,6 +14,8 @@ use crate::{ }; use crate::{PROGRESS_CHARS, STYLE_BAR_POS}; +/// # Errors +/// # Panics pub async fn download( config: &Cfg, liststack: Vec, @@ -31,15 +35,15 @@ pub async fn download( download_p.set_message(format!("Download in {}", current_list.id)); let downloaded_versions = - get_downloaded_versions(current_list.clone())?; + get_downloaded_versions(¤t_list)?; let current_version_ids = match userlist_get_all_current_versions_with_mods( config, - String::from(¤t_list.id), + ¤t_list.id, ) { Ok(i) => Ok(i), - Err(e) => Err(MLError::new( - ErrorType::DBError, + Err(e) => Err(MLErr::new( + EType::DBError, e.to_string().as_str(), )), }?; @@ -74,7 +78,12 @@ pub async fn download( clean_list_dir(¤t_list)?; }; - if !to_download.is_empty() { + if to_download.is_empty() { + download_p.println(format!( + "There are no new versions to download for {}", + current_list.id + )); + } else { download_versions( current_list.clone(), config.clone(), @@ -83,11 +92,6 @@ pub async fn download( &download_p, ) .await?; - } else { - download_p.println(format!( - "There are no new versions to download for {}", - current_list.id - )); } if !to_disable.is_empty() { @@ -104,13 +108,13 @@ pub async fn download( if delete_old { d_p.set_message(format!("Delete version {}", ver.1)); d_p.inc(1); - delete_version(¤t_list, ver.1)?; + delete_version(¤t_list, &ver.1)?; } else { d_p.set_message(format!("Disable version {}", ver.1)); d_p.inc(1); disable_version( config, - current_list.clone(), + ¤t_list, ver.1, ver.0, )?; diff --git a/src/commands/io.rs b/src/commands/io.rs index 1d17f7c..c9691c4 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -9,7 +9,7 @@ use crate::{ lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, userlist_get_current_version, userlist_get_set_version, }, - error::MLE, + error::{EType, MLErr, MLE}, mod_add, AddMod, IDSelector, List, Modloader, STYLE_OPERATION, }; @@ -67,15 +67,26 @@ impl ExportList { } } +/// # Errors +/// # Panics pub fn export(config: &Cfg, list: Option) -> MLE<()> { let progress = ProgressBar::new_spinner(); - progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + progress.set_style( + ProgressStyle::with_template(STYLE_OPERATION) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); 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.unwrap())?.id); + list_ids.push( + lists_get( + config, + &list.ok_or(MLErr::new(EType::Other, "nolist"))?, + )? + .id, + ); } let mut lists: Vec = vec![]; @@ -88,7 +99,7 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { let toml = toml::to_string(&Export { lists })?; let filestr = dirs::home_dir() - .unwrap() + .ok_or(MLErr::new(EType::Other, "no home"))? .join("mlexport.toml") .into_os_string() .into_string() @@ -102,6 +113,7 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { Ok(()) } +/// # Errors pub async fn import( config: &Cfg, file_str: &str, @@ -117,7 +129,9 @@ pub async fn import( id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, - download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(), + download_folder: exportlist + .download_folder + .ok_or(MLErr::new(EType::Other, "NO_DL"))?, }; lists_insert( config, diff --git a/src/commands/list.rs b/src/commands/list.rs index 63105cf..47c1dc6 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_name_repetitions)] use indicatif::{ProgressBar, ProgressStyle}; use crate::{ @@ -6,7 +7,7 @@ use crate::{ config_change_current_list, config_get_current_list, lists_get, lists_get_all_ids, lists_insert, lists_remove, lists_version, }, - error::{ErrorType, MLError, MLE}, + error::{EType, MLErr, MLE}, update, Modloader, STYLE_OPERATION, }; @@ -18,11 +19,13 @@ pub struct List { pub download_folder: String, } +/// # Errors pub fn get_current_list(config: &Cfg) -> MLE { let id = config_get_current_list(config)?; lists_get(config, &id) } +/// # Errors pub fn list_add( config: &Cfg, id: &str, @@ -31,20 +34,27 @@ pub fn list_add( directory: &str, ) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_style( + ProgressStyle::with_template(STYLE_OPERATION) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); p.set_message(format!("Create {id}")); lists_insert(config, id, mc_version, modloader, directory)?; p.finish_with_message(format!("Created {id}")); Ok(()) } +/// # Errors pub fn list_change(config: &Cfg, id: &str) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_style( + ProgressStyle::with_template(STYLE_OPERATION) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); p.set_message(format!("Change default list to {id}")); if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { - return Err(MLError::new(ErrorType::ArgumentError, "List not found")); + return Err(MLErr::new(EType::ArgumentError, "List not found")); }; config_change_current_list(config, id)?; @@ -52,9 +62,13 @@ pub fn list_change(config: &Cfg, id: &str) -> MLE<()> { Ok(()) } +/// # Errors pub fn list_remove(config: &Cfg, id: &str) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_style( + ProgressStyle::with_template(STYLE_OPERATION) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); p.set_message(format!("Remove {id}")); lists_remove(config, id)?; p.finish_with_message(format!("Removed {id}")); @@ -67,6 +81,7 @@ pub fn list_remove(config: &Cfg, id: &str) -> MLE<()> { /// /// * `config` - The current config /// * `args` - All args, to extract the new version +/// # Errors pub async fn list_version( config: &Cfg, id: &str, @@ -75,7 +90,10 @@ pub async fn list_version( delete: bool, ) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + p.set_style( + ProgressStyle::with_template(STYLE_OPERATION) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); p.set_message(format!( "Change version for list {id} to minecraft version: {mc_version}" )); @@ -90,7 +108,8 @@ pub async fn list_version( update(config, vec![list], true, download, delete).await } -pub fn list_list(config: &Cfg) -> MLE<()> { +/// # Errors +pub fn list_lists(config: &Cfg) -> MLE<()> { let lists = lists_get_all_ids(config)?; for list in lists { let l = lists_get(config, &list)?; diff --git a/src/commands/modification.rs b/src/commands/modification.rs index e0e54b2..6e6213f 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,3 +1,5 @@ +#![allow(clippy::too_many_lines)] + use std::collections::HashMap; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; @@ -9,7 +11,7 @@ use crate::{ mods_remove, userlist_get_all_ids, userlist_get_current_version, userlist_insert, userlist_remove, }, - error::{ErrorType, MLError, MLE}, + error::{EType, MLErr, MLE}, files::{delete_version, download_versions}, modrinth::{ extract_current_version, get_raw_versions, project, projects, versions, @@ -41,6 +43,8 @@ pub struct ProjectInfo { pub set_version: bool, } +/// # Errors +/// # Panics pub async fn mod_add( config: &Cfg, mods: Vec, @@ -52,10 +56,11 @@ 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().unwrap())); + 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) - .unwrap() + ProgressStyle::with_template(STYLE_BAR_POS).map_err(|_| { + MLErr::new(EType::LibIndicatif, "template error") + })? .progress_chars(PROGRESS_CHARS), ); add_p.set_message("Sort ids"); @@ -83,7 +88,7 @@ pub async fn mod_add( }; if projectinfo.is_empty() { - return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); + return Err(MLErr::new(EType::ArgumentError, "NO_IDS?")); }; add_p.set_message("Add mods to database"); @@ -115,7 +120,7 @@ pub async fn mod_add( &list.id, &project.mod_id, ¤t_version_id, - project.clone().applicable_versions, + &project.applicable_versions, &project.download_link, project.set_version, ) { @@ -125,8 +130,8 @@ pub async fn mod_add( list.id ); if e.to_string() == expected_err { - Err(MLError::new( - ErrorType::ModError, + Err(MLErr::new( + EType::ModError, "MOD_ALREADY_ON_SELECTED_LIST", )) } else { @@ -212,7 +217,20 @@ async fn get_mod_infos( let mut available_versions_vec: Vec = Vec::new(); let current_version: Option; let file: String; - if !available_versions.is_empty() { + if available_versions.is_empty() { + current_version = None; + file = String::from("NONE"); + available_versions_vec.push(String::from("NONE")); + projectinfo.push(ProjectInfo { + mod_id: String::from(&project.id), + slug: project.slug, + title: project.title, + current_version, + applicable_versions: available_versions_vec, + download_link: file, + set_version: *setmap.get(&project.id).unwrap(), + }); + } else { let current_id = extract_current_version(available_versions.clone())?; @@ -246,19 +264,6 @@ async fn get_mod_infos( download_link: file, set_version: *setmap.get(&project.slug).unwrap(), }); - } else { - current_version = None; - file = String::from("NONE"); - available_versions_vec.push(String::from("NONE")); - projectinfo.push(ProjectInfo { - mod_id: String::from(&project.id), - slug: project.slug, - title: project.title, - current_version, - applicable_versions: available_versions_vec, - download_link: file, - set_version: *setmap.get(&project.id).unwrap(), - }); } } @@ -319,9 +324,13 @@ async fn get_ver_info( /// * `config` - config struct /// * `id` - name, slug or id of the mod /// * `list` - List struct +/// +/// # 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).unwrap()); + 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)?; @@ -334,7 +343,7 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { userlist_remove(config, &list.id, &mod_id)?; progress.set_message("Delete file"); - match delete_version(list, version) { + match delete_version(list, &version) { Ok(()) => (), Err(err) => { if err.to_string() diff --git a/src/commands/update.rs b/src/commands/update.rs index c19c02c..d0b930d 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,3 +1,5 @@ +#![allow(clippy::too_many_lines)] + use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ @@ -7,7 +9,7 @@ use crate::{ userlist_get_applicable_versions, userlist_get_current_version, userlist_get_set_version, }, - error::{ErrorType, MLError, MLE}, + error::{EType, MLErr, MLE}, files::{ clean_list_dir, delete_version, disable_version, download_versions, }, @@ -15,6 +17,8 @@ use crate::{ List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; +/// # Errors +/// # Panics pub async fn update( config: &Cfg, liststack: Vec, @@ -24,11 +28,15 @@ pub async fn update( ) -> MLE<()> { let mp = MultiProgress::new(); - let update_p = - mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); + let update_p = mp.add(ProgressBar::new( + liststack + .len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, + )); update_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); @@ -133,16 +141,11 @@ pub async fn update( if delete_old { d_p.set_message(format!("Delete version {}", ver.0)); d_p.inc(1); - delete_version(¤t_list, ver.0)?; + delete_version(¤t_list, &ver.0)?; } else if ver.0 != "NONE" { d_p.set_message(format!("Disable version {}", ver.0)); d_p.inc(1); - disable_version( - config, - current_list.clone(), - ver.0, - ver.1, - )?; + disable_version(config, ¤t_list, ver.0, ver.1)?; }; } @@ -176,12 +179,12 @@ async fn specific_update( let mut versions: Vec = vec![]; - if !applicable_versions.is_empty() { + if applicable_versions.is_empty() { + versions.push(String::from("NONE")); + } else { for ver in &applicable_versions { versions.push(String::from(&ver.id)); } - } else { - versions.push(String::from("NONE")); } let mut current: Vec = vec![]; @@ -189,7 +192,7 @@ async fn specific_update( || (versions.join("|") != userlist_get_applicable_versions( config, - String::from(&list.id), + &list.id, String::from(id), )?) { @@ -209,7 +212,7 @@ async fn specific_update( .ok_or("!no current version in applicable_versions") { Ok(v) => Ok(v), - Err(e) => Err(MLError::new(ErrorType::Other, e)), + Err(e) => Err(MLErr::new(EType::Other, e)), }?; current.push(current_ver.clone()); @@ -223,7 +226,7 @@ async fn specific_update( userlist_change_versions( config, - list.id, + &list.id, current_str, versions.join("|"), link, @@ -232,7 +235,7 @@ async fn specific_update( } if current.is_empty() { - return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")); + return Err(MLErr::new(EType::ModError, "NO_UPDATE_AVAILABLE")); }; Ok(current[0].clone()) -- cgit v1.2.3