From 942558c75200aaad0b4d8561a1f6999f88f843a4 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 12:31:02 +0200 Subject: remove allow too_many_lines and fix them --- src/commands/download.rs | 184 ++++++++++++++++++++++--------------------- src/commands/modification.rs | 114 +++++++++++++-------------- src/commands/update.rs | 84 ++++++++++---------- src/main.rs | 184 +++++++++++++++++++++++-------------------- 4 files changed, 293 insertions(+), 273 deletions(-) (limited to 'src') diff --git a/src/commands/download.rs b/src/commands/download.rs index 7af1066..7ea5c29 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,5 +1,3 @@ -#![allow(clippy::too_many_lines)] - use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{config::Cfg, List}; @@ -35,105 +33,113 @@ pub async fn download( ); for current_list in liststack { - 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 mut to_download: Vec = vec![]; - //(mod_id, version_id) - let mut to_disable: Vec<(String, String)> = vec![]; - - for version in current_version_ids { - let mod_id = version.0; - let current_version = version.1; - - let current_download = downloaded_versions.get(&mod_id); - - if current_download.is_none() || clean { - to_download.push(current_version); - } else { - let downloaded_version = current_download - .ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; - if ¤t_version != downloaded_version { - to_disable.push(( - mod_id.clone(), - String::from(downloaded_version), - )); - to_download.push(current_version); - } - } - } + download_list(config, mp.clone(), download_p.clone(), current_list, clean, delete_old).await?; + } - if clean { - clean_list_dir(¤t_list)?; - }; + download_p.finish_with_message("Downloaded all lists"); + + Ok(()) +} + +async fn download_list( + config: &Cfg, + mp: MultiProgress, + download_p: ProgressBar, + current_list: List, + clean: bool, + delete_old: bool, +) -> MLE<()> { + 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 mut to_download: Vec = vec![]; + //(mod_id, version_id) + let mut to_disable: Vec<(String, String)> = vec![]; + + for version in current_version_ids { + let mod_id = version.0; + let current_version = version.1; - if to_download.is_empty() { - download_p.println(format!( - "There are no new versions to download for {}", - current_list.id - )); + let current_download = downloaded_versions.get(&mod_id); + + if current_download.is_none() || clean { + to_download.push(current_version); } else { - download_versions( - current_list.clone(), - config.clone(), - get_raw_versions(&config.apis.modrinth, to_download).await?, - &mp, - &download_p, - ) - .await?; + let downloaded_version = + current_download.ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; + if ¤t_version != downloaded_version { + to_disable + .push((mod_id.clone(), String::from(downloaded_version))); + to_download.push(current_version); + } } + } - 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"), - )?), - ); - d_p.set_style( - ProgressStyle::with_template(STYLE_BAR_POS) - .map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })? - .progress_chars(PROGRESS_CHARS), - ); - for ver in to_disable { - if delete_old { - d_p.set_message(format!("Delete version {}", ver.1)); - d_p.inc(1); - delete_version(¤t_list, &ver.1)?; - } else { - d_p.set_message(format!("Disable version {}", ver.1)); - d_p.inc(1); - disable_version(config, ¤t_list, ver.1, ver.0)?; - }; - } + if clean { + clean_list_dir(¤t_list)?; + }; + + 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(), + get_raw_versions(&config.apis.modrinth, to_download).await?, + &mp, + &download_p, + ) + .await?; + } - let del_msg = if delete_old { - "Deleted all old versions" + 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"))?, + ), + ); + d_p.set_style( + ProgressStyle::with_template(STYLE_BAR_POS) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? + .progress_chars(PROGRESS_CHARS), + ); + for ver in to_disable { + if delete_old { + d_p.set_message(format!("Delete version {}", ver.1)); + d_p.inc(1); + delete_version(¤t_list, &ver.1)?; } else { - "Disabled all old versions" + d_p.set_message(format!("Disable version {}", ver.1)); + d_p.inc(1); + disable_version(config, ¤t_list, ver.1, ver.0)?; }; - - d_p.finish_with_message(del_msg); } - download_p.inc(1); + let del_msg = if delete_old { + "Deleted all old versions" + } else { + "Disabled all old versions" + }; + + d_p.finish_with_message(del_msg); } - download_p.finish_with_message("Downloaded all lists"); + download_p.inc(1); Ok(()) } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index aa1174a..8f115ee 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,5 +1,3 @@ -#![allow(clippy::too_many_lines)] - use std::collections::HashMap; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; @@ -114,60 +112,7 @@ pub async fn mod_add( ); for project in projectinfo { - project_p.set_message(format!("Add {}", project.title)); - - let current_version_id = if project.current_version.is_none() { - String::from("NONE") - } else { - project - .current_version - .clone() - .ok_or(MLErr::new(EType::Other, "cur_ver"))? - .id - }; - - match userlist_insert( - config, - &list.id, - &project.mod_id, - ¤t_version_id, - &project.applicable_versions, - &project.download_link, - project.set_version, - ) { - Err(e) => { - 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", - )) - } else { - Err(e) - } - } - Ok(..) => Ok(..), - }?; - - match mods_insert( - config, - &project.mod_id, - &project.slug, - &project.title, - ) { - Err(e) => { - if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { - Ok(..) - } else { - Err(e) - } - } - Ok(..) => Ok(..), - }?; - + add_project(config, &project_p, &project, &list)?; if project.current_version.is_some() { downloadstack.push( project @@ -175,8 +120,6 @@ pub async fn mod_add( .ok_or(MLErr::new(EType::Other, "cur_ver"))?, ); }; - - project_p.inc(1); } project_p.finish_with_message("Added all mods to the database"); @@ -199,6 +142,61 @@ pub async fn mod_add( Ok(()) } +fn add_project( + config: &Cfg, + project_p: &ProgressBar, + project: &ProjectInfo, + list: &List, +) -> MLE<()> { + project_p.set_message(format!("Add {}", project.title)); + + let current_version_id = if project.current_version.is_none() { + String::from("NONE") + } else { + project + .current_version + .clone() + .ok_or(MLErr::new(EType::Other, "cur_ver"))? + .id + }; + + match userlist_insert( + config, + &list.id, + &project.mod_id, + ¤t_version_id, + &project.applicable_versions, + &project.download_link, + project.set_version, + ) { + Err(e) => { + 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")) + } else { + Err(e) + } + } + Ok(..) => Ok(..), + }?; + + match mods_insert(config, &project.mod_id, &project.slug, &project.title) { + Err(e) => { + if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { + Ok(..) + } else { + Err(e) + } + } + Ok(..) => Ok(..), + }?; + + project_p.inc(1); + + Ok(()) +} + async fn get_mod_infos( config: &Cfg, mod_ids: Vec<(String, bool)>, diff --git a/src/commands/update.rs b/src/commands/update.rs index c7965e3..f83030d 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,5 +1,3 @@ -#![allow(clippy::too_many_lines)] - use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ @@ -70,44 +68,7 @@ pub async fn update( let mut updatestack: Vec = vec![]; for id in mods { - 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); - continue; - } - - //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)); - ver - } - Err(e) => { - if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { - } else { - return Err(e); - }; - list_u_p.inc(1); - continue; - } - }, - ); - list_u_p.inc(1); + update_mod(config, id, list_u_p.clone(), ¤t_list, &mut updatestack, &mut current_versions, clean).await?; } list_u_p.finish_with_message(format!( @@ -173,6 +134,49 @@ 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)); + + //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); + + Ok(()) +} + async fn specific_update( config: &Cfg, clean: bool, diff --git a/src/main.rs b/src/main.rs index a478ec7..f388a82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ -#![allow(clippy::too_many_lines)] - use clap::{Parser, Subcommand}; use modlist::{ config::Cfg, db::{config_get_current_list, lists_get, lists_get_all_ids}, - download, export, get_current_list, import, list_add, list_change, - list_lists, list_remove, list_version, mod_add, mod_remove, update, AddMod, - IDSelector, List, Modloader, VersionLevel, + download, + error::MLE, + export, get_current_list, import, list_add, list_change, list_lists, + list_remove, list_version, mod_add, mod_remove, update, AddMod, IDSelector, + List, Modloader, VersionLevel, }; #[derive(Parser)] @@ -163,90 +163,11 @@ async fn main() { let config = Cfg::init(cli.config).await.unwrap(); match cli.command { - Commands::Mod { command } => match command { - ModCommands::Add { - id, - version, - list, - download, - lock, - } => { - let listf = match list { - Some(list) => lists_get(&config, &list).unwrap(), - None => lists_get( - &config, - &config_get_current_list(&config).unwrap(), - ) - .unwrap(), - }; - - let marked_id = if version { - IDSelector::VersionID(id) - } else { - IDSelector::ModificationID(id) - }; - - let add_id = AddMod { - id: marked_id, - set_version: lock, - }; - - mod_add(&config, vec![add_id], listf, download).await - } - ModCommands::Remove { id, list } => { - let listf = match list { - Some(list) => lists_get(&config, &list).unwrap(), - None => lists_get( - &config, - &config_get_current_list(&config).unwrap(), - ) - .unwrap(), - }; - mod_remove(&config, &id, &listf) - } - }, + Commands::Mod { command } => handle_mod(config, command).await, Commands::List { command, force_gameupdate, - } => match command { - ListCommands::Add { - id, - directory, - modloader, - version, - } => { - let ml = match modloader { - Some(ml) => Modloader::from(&ml).unwrap(), - None => config.defaults.modloader.clone(), - }; - - let versions_path = &config.versions; - let ver = match version { - Some(ver) => VersionLevel::from(&ver) - .get(versions_path, force_gameupdate) - .await - .unwrap(), - None => config - .defaults - .version - .clone() - .get(versions_path, force_gameupdate) - .await - .unwrap(), - }; - - list_add(&config, &id, &ver, &ml, &directory) - } - ListCommands::Remove { id } => list_remove(&config, &id), - ListCommands::List => list_lists(&config), - ListCommands::Change { id } => list_change(&config, &id), - ListCommands::Version { - id, - version, - download, - remove, - } => list_version(&config, &id, version, download, remove).await, - }, + } => handle_list(config, command, force_gameupdate).await, Commands::Update { all, download, @@ -310,3 +231,94 @@ async fn main() { } .unwrap(); } + +async fn handle_mod(config: Cfg, command: ModCommands) -> MLE<()> { + match command { + ModCommands::Add { + id, + version, + list, + download, + lock, + } => { + let listf = match list { + Some(list) => lists_get(&config, &list).unwrap(), + None => lists_get( + &config, + &config_get_current_list(&config).unwrap(), + ) + .unwrap(), + }; + + let marked_id = if version { + IDSelector::VersionID(id) + } else { + IDSelector::ModificationID(id) + }; + + let add_id = AddMod { + id: marked_id, + set_version: lock, + }; + + mod_add(&config, vec![add_id], listf, download).await + } + ModCommands::Remove { id, list } => { + let listf = match list { + Some(list) => lists_get(&config, &list).unwrap(), + None => lists_get( + &config, + &config_get_current_list(&config).unwrap(), + ) + .unwrap(), + }; + mod_remove(&config, &id, &listf) + } + } +} + +async fn handle_list( + config: Cfg, + command: ListCommands, + force_gameupdate: bool, +) -> MLE<()> { + match command { + ListCommands::Add { + id, + directory, + modloader, + version, + } => { + let ml = match modloader { + Some(ml) => Modloader::from(&ml).unwrap(), + None => config.defaults.modloader.clone(), + }; + + let versions_path = &config.versions; + let ver = match version { + Some(ver) => VersionLevel::from(&ver) + .get(versions_path, force_gameupdate) + .await + .unwrap(), + None => config + .defaults + .version + .clone() + .get(versions_path, force_gameupdate) + .await + .unwrap(), + }; + + list_add(&config, &id, &ver, &ml, &directory) + } + ListCommands::Remove { id } => list_remove(&config, &id), + ListCommands::List => list_lists(&config), + ListCommands::Change { id } => list_change(&config, &id), + ListCommands::Version { + id, + version, + download, + remove, + } => list_version(&config, &id, version, download, remove).await, + } +} -- cgit v1.2.3