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/modification.rs | 181 +++++++++++++++++++++++++++++++++---------- 1 file changed, 141 insertions(+), 40 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 454e148..ffc4e10 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,9 +1,19 @@ -use crate::{modrinth::{versions, extract_current_version, Version, projects, get_raw_versions, project}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; +use crate::{ + config::Cfg, + db::{ + lists_get_all_ids, mods_get_id, mods_insert, mods_remove, userlist_get_all_ids, + userlist_get_current_version, userlist_insert, userlist_remove, + }, + error::{ErrorType, MLError, MLE}, + files::{delete_version, download_versions}, + modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, + List, +}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum IDSelector { ModificationID(String), - VersionID(String) + VersionID(String), } #[derive(Debug, Clone)] @@ -16,10 +26,16 @@ pub struct ProjectInfo { pub download_link: String, } -pub async fn mod_add(config: Cfg, ids: Vec, list: List, direct_download: bool, set_version: bool) -> MLE<()> { +pub async fn mod_add( + config: Cfg, + ids: Vec, + list: List, + direct_download: bool, + set_version: bool, +) -> MLE<()> { println!("Add mods to {}", list.id); println!(" └Add mods:"); - + let mut mod_ids: Vec = Vec::new(); let mut ver_ids: Vec = Vec::new(); @@ -32,11 +48,17 @@ pub async fn mod_add(config: Cfg, ids: Vec, list: List, direct_downl } let mut projectinfo: Vec = Vec::new(); - if !mod_ids.is_empty() { projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) }; - if !ver_ids.is_empty() { projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) }; + if !mod_ids.is_empty() { + projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) + }; + if !ver_ids.is_empty() { + projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) + }; + + if projectinfo.is_empty() { + return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")); + }; - if projectinfo.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")) }; - let mut downloadstack: Vec = Vec::new(); //Adding each mod to the lists and downloadstack @@ -45,29 +67,59 @@ pub async fn mod_add(config: Cfg, ids: Vec, list: List, direct_downl } else { println!(" └Insert mods in list {} and save infos", list.id); } - + for project in projectinfo { - let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { project.current_version.clone().unwrap().id }; - match userlist_insert(config.clone(), &list.id, &project.mod_id, ¤t_version_id, project.clone().applicable_versions, &project.download_link, set_version) { + let current_version_id = if project.current_version.is_none() { + String::from("NONE") + } else { + project.current_version.clone().unwrap().id + }; + match userlist_insert( + config.clone(), + &list.id, + &project.mod_id, + ¤t_version_id, + project.clone().applicable_versions, + &project.download_link, + set_version, + ) { Err(e) => { let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); - if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } - }, - Ok(..) => { Ok(..) }, + if e.to_string() == expected_err { + Err(MLError::new( + ErrorType::ModError, + "MOD_ALREADY_ON_SELECTED_LIST", + )) + } else { + Err(e) + } + } + Ok(..) => Ok(..), }?; - - match mods_insert(config.clone(), &project.mod_id, &project.slug, &project.title) { + + match mods_insert( + config.clone(), + &project.mod_id, + &project.slug, + &project.title, + ) { Err(e) => { - if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } - }, + if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { + Ok(..) + } else { + Err(e) + } + } Ok(..) => Ok(..), }?; - if project.current_version.is_some() { downloadstack.push(project.current_version.unwrap()) }; + if project.current_version.is_some() { + downloadstack.push(project.current_version.unwrap()) + }; } //Download all the added mods - if direct_download { + if direct_download { download_versions(list.clone(), config.clone(), downloadstack).await?; }; @@ -86,7 +138,12 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE = Vec::new(); let current_version: Option; @@ -95,36 +152,63 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE) -> MLE> { let mut projectinfo: Vec = Vec::new(); - + //Get required information from ver_ids let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_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; v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id)); v_projects.sort_by(|a, b| a.id.cmp(&b.id)); @@ -132,9 +216,22 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE for (i, project) in v_projects.into_iter().enumerate() { let version = &v_versions[i]; println!("\t└{}({})", project.title, version.id); - let file = version.clone().files.into_iter().find(|f| f.primary).unwrap().url; - projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version: Some(version.clone()), applicable_versions: vec![String::from(&version.id)], download_link: file }) - }; + let file = version + .clone() + .files + .into_iter() + .find(|f| f.primary) + .unwrap() + .url; + projectinfo.push(ProjectInfo { + mod_id: project.id, + slug: project.slug, + title: project.title, + current_version: Some(version.clone()), + applicable_versions: vec![String::from(&version.id)], + download_link: file, + }) + } Ok(projectinfo) } @@ -145,24 +242,28 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE /// * `id` - name, slug or id of the mod /// * `list` - List struct pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { - let mod_id = mods_get_id(&config.data, id)?; - + let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; userlist_remove(config.clone(), &list.id, &mod_id)?; delete_version(list, version)?; - + let list_ids = lists_get_all_ids(config.clone())?; - + // Remove mod from main list if not used elsewhere let mut mod_used = false; for id in list_ids { let mods = userlist_get_all_ids(config.clone(), id)?; - if mods.contains(&mod_id) { mod_used = true; break; }; - }; + if mods.contains(&mod_id) { + mod_used = true; + break; + }; + } - if !mod_used { mods_remove(config, mod_id)?; }; + if !mod_used { + mods_remove(config, mod_id)?; + }; Ok(()) } -- cgit v1.2.3