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