From 8398b4003b72b3eefea11d8f71897ddb48f4b0ad Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sat, 13 May 2023 12:37:07 +0200 Subject: impl of set_version import --- src/commands/modification.rs | 69 +++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 20 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 216a06e..ab42468 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,4 +1,4 @@ -use std::io::Write; +use std::{io::Write, collections::HashMap}; use crate::{ config::Cfg, @@ -12,6 +12,12 @@ use crate::{ List, }; +#[derive(Debug, Clone)] +pub struct AddMod { + pub id: IDSelector, + pub set_version: bool +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum IDSelector { ModificationID(String), @@ -26,26 +32,27 @@ pub struct ProjectInfo { pub current_version: Option, pub applicable_versions: Vec, pub download_link: String, + pub set_version: bool, } +//TODO fix setversion to per mod pub async fn mod_add( config: Cfg, - ids: Vec, + mods: 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(); + let mut mod_ids: Vec<(String, bool)> = Vec::new(); + let mut ver_ids: Vec<(String, bool)> = Vec::new(); //"Sort" project ids from version ids to be able to handle them differently but in a batch - for id in ids { - match id { - IDSelector::ModificationID(pid) => mod_ids.push(pid), - IDSelector::VersionID(vid) => ver_ids.push(vid), + for m in mods { + match m.id { + IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), + IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), } } @@ -83,7 +90,7 @@ pub async fn mod_add( ¤t_version_id, project.clone().applicable_versions, &project.download_link, - set_version, + project.set_version, ) { Err(e) => { let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); @@ -128,13 +135,23 @@ pub async fn mod_add( Ok(()) } -async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE> { +async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE> { + + let mut setmap: HashMap = HashMap::new(); + + let mut ids = vec![]; + + for id in mod_ids { + setmap.insert(id.0.to_string(), id.1); + ids.push(id.0); + } + let mut projectinfo: Vec = Vec::new(); //Get required information from mod_ids - let m_projects = match mod_ids.len() { - 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await], - 2.. => projects(&config.apis.modrinth, mod_ids).await, + let m_projects = match ids.len() { + 1 => vec![project(&config.apis.modrinth, &ids[0]).await], + 2.. => projects(&config.apis.modrinth, ids).await, _ => panic!("PANIC"), }; for project in m_projects { @@ -180,12 +197,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE, list: List) -> MLE, list: List) -> MLE) -> MLE> { +async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE> { + + let mut setmap: HashMap = HashMap::new(); + + let mut ids = vec![]; + + for id in ver_ids { + setmap.insert(id.0.to_string(), id.1); + ids.push(id.0); + } 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_versions = get_raw_versions(&config.apis.modrinth, ids).await; let mut v_mod_ids: Vec = Vec::new(); for ver in v_versions.clone() { v_mod_ids.push(ver.project_id); @@ -230,12 +258,13 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE .unwrap() .url; projectinfo.push(ProjectInfo { - mod_id: project.id, + mod_id: String::from(&project.id), slug: project.slug, title: project.title, current_version: Some(version.clone()), applicable_versions: vec![String::from(&version.id)], download_link: file, + set_version: setmap.get(&project.id).unwrap().clone(), }) } Ok(projectinfo) @@ -281,7 +310,7 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { // Remove mod from main list if not used elsewhere let mut mod_used = false; for id in list_ids { - let mods = match userlist_get_all_ids(config.clone(), id) { + let mods = match userlist_get_all_ids(config.clone(), &id) { Ok(m) => m, Err(err) => { if err.to_string() == "Database: NO_MODS_USERLIST" { -- cgit v1.2.3 From 3181da20e001146653a09f0734e0340b043d9b4b Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 13 May 2023 12:59:38 +0200 Subject: removed todos, fixed get version set --- src/commands/io.rs | 2 -- src/commands/modification.rs | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/io.rs b/src/commands/io.rs index 2bab132..dd294bc 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -110,8 +110,6 @@ pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE for id in exportlist.versions { ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} ); } - //TODO impl set_version and good direct download - //TODO impl all at once, dafuck ?done? mod_add(config.clone(), ver_ids, list, direct_download).await?; } Ok(()) diff --git a/src/commands/modification.rs b/src/commands/modification.rs index ab42468..9a1a651 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -35,7 +35,6 @@ pub struct ProjectInfo { pub set_version: bool, } -//TODO fix setversion to per mod pub async fn mod_add( config: Cfg, mods: Vec, @@ -264,7 +263,7 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE