From 8050cfcd70a16273cc2814fe29c8ee08320d85d3 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 20 Apr 2023 15:13:58 +0200 Subject: cargo fmt --- src/commands/update.rs | 116 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 30 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index e5751c0..3d9578b 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,14 +1,30 @@ -use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, userlist_get_current_version, userlist_get_set_version, mods_get_info}, List, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; - -pub async fn update(config: Cfg, liststack: Vec, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> { +use crate::{ + config::Cfg, + db::{ + mods_get_info, userlist_change_versions, userlist_get_all_ids, + userlist_get_applicable_versions, userlist_get_current_version, userlist_get_set_version, + }, + error::{ErrorType, MLError, MLE}, + files::{clean_list_dir, delete_version, disable_version, download_versions}, + modrinth::{extract_current_version, versions, Version}, + List, +}; + +pub async fn update( + config: Cfg, + liststack: Vec, + clean: bool, + direct_download: bool, + delete_old: bool, +) -> MLE<()> { for current_list in liststack { let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; - + let mut current_versions: Vec<(String, String)> = vec![]; - + println!(" └Update mods:"); let mut updatestack: Vec = vec![]; - + for id in mods { let info = mods_get_info(config.clone(), &id)?; println!("\t└{}", info.title); @@ -19,27 +35,39 @@ pub async fn update(config: Cfg, liststack: Vec, clean: bool, direct_downl } //Getting current installed version for disable or delete - let disable_version = userlist_get_current_version(config.clone(), ¤t_list.id, &id)?; + let disable_version = + userlist_get_current_version(config.clone(), ¤t_list.id, &id)?; updatestack.push( - match specific_update(config.clone(), clean, current_list.clone(), String::from(&id)).await { + match specific_update( + config.clone(), + clean, + current_list.clone(), + String::from(&id), + ) + .await + { Ok(ver) => { current_versions.push((disable_version, id)); ver - }, + } Err(e) => { if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { - println!("\t └No new version found for the specified minecraft version"); + println!( + "\t └No new version found for the specified minecraft version" + ); } else { return Err(e); }; continue; } - } + }, ) - }; + } - if clean { clean_list_dir(¤t_list)?; }; + if clean { + clean_list_dir(¤t_list)?; + }; if direct_download && !updatestack.is_empty() { download_versions(current_list.clone(), config.clone(), updatestack).await?; @@ -50,7 +78,7 @@ pub async fn update(config: Cfg, liststack: Vec, clean: bool, direct_downl if delete_old { println!("\t └Delete version {}", ver.0); delete_version(current_list.clone(), ver.0)?; - } else if ver.0 != "NONE" { + } else if ver.0 != "NONE" { println!("\t └Disable version {}", ver.0); disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; }; @@ -63,10 +91,11 @@ pub async fn update(config: Cfg, liststack: Vec, clean: bool, direct_downl } async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE { - let applicable_versions = versions(&config.apis.modrinth, String::from(&id), list.clone()).await; - + let applicable_versions = + versions(&config.apis.modrinth, String::from(&id), list.clone()).await; + let mut versions: Vec = vec![]; - + if !applicable_versions.is_empty() { for ver in &applicable_versions { versions.push(String::from(&ver.id)); @@ -77,8 +106,14 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML let mut current: Vec = vec![]; //TODO Split clean and no match - if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&id))?) { - + if clean + || (versions.join("|") + != userlist_get_applicable_versions( + config.clone(), + String::from(&list.id), + String::from(&id), + )?) + { let current_str = extract_current_version(applicable_versions.clone())?; if clean { @@ -89,35 +124,54 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML }; //get new versions - let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { + let current_ver = match applicable_versions + .into_iter() + .find(|ver| ver.id == current_str) + .ok_or("!no current version in applicable_versions") + { Ok(v) => Ok(v), Err(e) => Err(MLError::new(ErrorType::Other, e)), }?; current.push(current_ver.clone()); //TODO implement version selection if no primary - let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { + let link = match current_ver + .files + .into_iter() + .find(|f| f.primary) + .ok_or("!no primary in links") + { Ok(p) => Ok(p), Err(e) => Err(MLError::new(ErrorType::Other, e)), - }?.url; + }? + .url; userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; } - if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; - + if current.is_empty() { + return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")); + }; + //println!(" └✔️"); Ok(current[0].clone()) } #[tokio::test] async fn download_updates_test() { + use crate::{ + modrinth::{Hash, Version, VersionFile, VersionType}, + List, Modloader, + }; - use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; - let config = Cfg::init("modlist.toml").unwrap(); - let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Fabric, download_folder: String::from("./dev/tests/dl") }; - - let versions = vec![Version { + let current_list = List { + id: String::from("..."), + mc_version: String::from("..."), + modloader: Modloader::Fabric, + download_folder: String::from("./dev/tests/dl"), + }; + + let versions = vec![Version { id: "dEqtGnT9".to_string(), project_id: "kYuIpRLv".to_string(), author_id: "Qnt13hO8".to_string(), @@ -147,5 +201,7 @@ async fn download_updates_test() { "fabric".to_string() ] }]; - assert!(download_versions(current_list, config, versions).await.is_ok()) + assert!(download_versions(current_list, config, versions) + .await + .is_ok()) } -- cgit v1.2.3